diff --git a/src/users/getUser.ts b/src/users/getUser.ts index c0c05e8..fd02ec4 100644 --- a/src/users/getUser.ts +++ b/src/users/getUser.ts @@ -85,6 +85,14 @@ export async function getUser(userId: number) { .where('person.id', '=', userId) .executeTakeFirst(); + const roles = await db + .selectFrom('role') + .innerJoin('person_role', 'role.id', 'person_role.role_id') + .innerJoin('person', 'person.id', 'person_role.person_id') + .select(['role.label']) + .where('person_role.person_id', '=', userId) + .execute(); + if (!res) { throw new Error('User not found'); } @@ -121,5 +129,6 @@ export async function getUser(userId: number) { id: res.specialization_id, label: res.specialization_label, }, + roles: roles.map((role) => role.label), }; } diff --git a/src/users/getUsers.ts b/src/users/getUsers.ts index e457064..f3173a6 100644 --- a/src/users/getUsers.ts +++ b/src/users/getUsers.ts @@ -47,21 +47,31 @@ export async function getAll(personQuery: IPersonQuery) { 'person.pref_name', 'person.email', 'person.account_updated', - 'person.member_since', ]) .limit(personQuery.limit || 10) .offset(personQuery.offset || 0) .execute(); - return res.map((user) => { - return { - id: user.id, - first_name: user.first_name, - last_name: user.last_name, - pref_name: user.pref_name, - email: user.email || '', - account_updated: user.account_updated, - member_since: user.member_since, - }; - }); + const usersWithRoles = await Promise.all( + res.map(async (user) => { + const roles = await db + .selectFrom('role') + .innerJoin('person_role', 'role.id', 'person_role.role_id') + .innerJoin('person', 'person.id', 'person_role.person_id') + .select(['role.label']) + .where('person_role.person_id', '=', user.id) + .execute(); + + return { + id: user.id, + first_name: user.first_name, + last_name: user.last_name, + pref_name: user.pref_name, + email: user.email || '', + account_updated: user.account_updated, + roles: roles.map((role) => role.label), + }; + }) + ); + return usersWithRoles; }