From c9dd17ae14352c753ea8342ea625877209aad961 Mon Sep 17 00:00:00 2001 From: Chris Jean-Marie Date: Sat, 1 Mar 2025 00:27:25 +0000 Subject: [PATCH] Move user to person --- .../20241221174355_add-person-level.down.sql | 5 ++++ .../20241221174355_add-person-level.up.sql | 26 +++++++++++++++++++ backend/src/routes.rs | 19 ++++++++++++++ backend/templates/user.html | 18 +++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 backend/migrations/20241221174355_add-person-level.down.sql create mode 100644 backend/migrations/20241221174355_add-person-level.up.sql diff --git a/backend/migrations/20241221174355_add-person-level.down.sql b/backend/migrations/20241221174355_add-person-level.down.sql new file mode 100644 index 0000000..743e506 --- /dev/null +++ b/backend/migrations/20241221174355_add-person-level.down.sql @@ -0,0 +1,5 @@ +-- Remove person level tables +ALTER TABLE if exists users + drop column if exists person_id; + +drop table if exists people; \ No newline at end of file diff --git a/backend/migrations/20241221174355_add-person-level.up.sql b/backend/migrations/20241221174355_add-person-level.up.sql new file mode 100644 index 0000000..da90155 --- /dev/null +++ b/backend/migrations/20241221174355_add-person-level.up.sql @@ -0,0 +1,26 @@ +-- Add login table +create table if not exists people ( + id uuid NOT NULL DEFAULT gen_random_uuid (), + created_at timestamp without time zone NOT NULL DEFAULT now(), + created_by uuid NOT NULL, + updated_at timestamp without time zone NOT NULL DEFAULT now(), + updated_by uuid NOT NULL, + email text NOT NULL, + name text NOT NULL, + family_name text NOT NULL, + given_name text NOT NULL +); + +ALTER TABLE + people +ADD + CONSTRAINT people_pkey PRIMARY KEY (id); + +ALTER TABLE if exists users + ADD COLUMN person_id uuid REFERENCES people (id) ON DELETE SET NULL; + +-- Copy data +insert into people (created_by, updated_by, email, name, family_name, given_name) +select created_by, updated_by, email, name, family_name, given_name from users; + +update users u set person_id = p.id from people p where p.email = u.email; \ No newline at end of file diff --git a/backend/src/routes.rs b/backend/src/routes.rs index 61455d4..9e3e74f 100644 --- a/backend/src/routes.rs +++ b/backend/src/routes.rs @@ -28,6 +28,7 @@ struct UserProfileTemplate { user: UserData, user_roles: Vec, profile: UserData, + profile_accounts: Vec, profile_roles: Vec, non_profile_roles: Vec, } @@ -181,6 +182,23 @@ pub async fn user_profile( // Get logged in user roles let user_roles = get_user_roles_display(userid, &db_pool.clone()).await; + // Get user accounts + let profile_accounts = sqlx::query_as( r#"SELECT + id, + created_at, + created_by, + updated_at, + updated_by, + email, + name, + family_name, + given_name + FROM users WHERE person_id = $1"#) + .bind(user_id) + .fetch_all(&db_pool) + .await + .unwrap(); + // Get user roles let profile_roles = get_user_roles_display(user_id, &db_pool.clone()).await; @@ -193,6 +211,7 @@ pub async fn user_profile( user, user_roles, profile, + profile_accounts, profile_roles, non_profile_roles, }; diff --git a/backend/templates/user.html b/backend/templates/user.html index c6ae1fc..3085b3d 100644 --- a/backend/templates/user.html +++ b/backend/templates/user.html @@ -7,6 +7,24 @@ Given name: {{ profile.given_name }}
Family name: {{ profile.family_name }}
Your email address: {{ profile.email }}

+

Accounts

+ + + + + + + + + + {% for account in profile_accounts %} + + + + + {% endfor %} + +
NameAction
{{ account.name }}Delete

User Roles