diff --git a/src/modules/users/users.service.ts b/src/modules/users/users.service.ts index 376cd6d..0ae8852 100644 --- a/src/modules/users/users.service.ts +++ b/src/modules/users/users.service.ts @@ -54,15 +54,15 @@ export class UsersService implements OnModuleInit { const { users } = await lastValueFrom( this.usersClientService.UserFindAllByCustomerId({ customerId }), ); - this.adjustUsersPayload(users); - const dintinctDepartments = [...new Set(users.map((u) => u.department))] + const usersAdj = this.adjustUsersPayload(users); + const dintinctDepartments = [...new Set(usersAdj.map((u) => u.department))] .filter((value) => value != null) .sort(); - const dintinctJobTitles = [...new Set(users.map((u) => u.jobTitle))] + const dintinctJobTitles = [...new Set(usersAdj.map((u) => u.jobTitle))] .filter((value) => value != null) .sort(); return { - users, + users: usersAdj, jobTitles: dintinctJobTitles, departments: dintinctDepartments, }; @@ -72,8 +72,7 @@ export class UsersService implements OnModuleInit { const { user } = await lastValueFrom( this.usersClientService.UserFindOneById({ id }), ); - this.adjustUsersPayload([user]); - return { user }; + return { user: this.adjustUsersPayload([user])[0] }; } async createUser(req: CreateUserReq) { @@ -89,8 +88,7 @@ export class UsersService implements OnModuleInit { roleNames, }), ); - this.adjustUsersPayload([user]); - return { user }; + return { user: this.adjustUsersPayload([user])[0] }; } async updateUser(req: UpdateUserReq, id: string, customerId: string) { @@ -108,8 +106,7 @@ export class UsersService implements OnModuleInit { jobTitle, }), ); - this.adjustUsersPayload([user]); - return { user }; + return { user: this.adjustUsersPayload([user])[0] }; } async setRoles(body: SetUserRolesReq, customerId: string) { @@ -124,8 +121,8 @@ export class UsersService implements OnModuleInit { customerId, }), ); - this.adjustUsersPayload([user]); - return { user }; + + return { user: this.adjustUsersPayload([user])[0] }; } async batchCreateUser({ users }: BatchCreateUserReq) { @@ -145,10 +142,9 @@ export class UsersService implements OnModuleInit { users: usersToCreate, }), ); - this.adjustUsersPayload(usersCreated); return { - usersCreated, + usersCreated: this.adjustUsersPayload(usersCreated), // mapping error code to message errorUsers: errorUsers.map(({ user, error }) => ({ user, @@ -193,14 +189,14 @@ export class UsersService implements OnModuleInit { ); } - adjustUsersPayload(users: UserByCustomer[]) { - users?.forEach((u) => { - if (!u) return; - const roles: any[] = this.rolesService.getRolesPermissionsName(u.roles); - u.roles = roles; - u.hierarchy = - HIERARCHIES[u.hierarchy]?.name[this.language] || u.hierarchy; + private adjustUsersPayload(users: UserByCustomer[]) { + return users.map((u) => { + if (!u) return undefined; + return { + ...u, + roles: this.rolesService.getRolesPermissionsName(u.roles), + hierarchy: HIERARCHIES[u.hierarchy]?.name[this.language] || u.hierarchy, + }; }); - return users; } }