From cf40c0f1ffcef69c3ea978395e8d1a26a5036128 Mon Sep 17 00:00:00 2001 From: JoaoSaIvador Date: Wed, 11 Jan 2023 17:14:36 +0000 Subject: [PATCH 1/2] fix: team page member sort --- .../teams/repositories/team.repository.ts | 13 +----------- .../teams/services/get.team.service.ts | 20 +++++++++++++++++-- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/backend/src/modules/teams/repositories/team.repository.ts b/backend/src/modules/teams/repositories/team.repository.ts index 015e60bb2..e96b24df7 100644 --- a/backend/src/modules/teams/repositories/team.repository.ts +++ b/backend/src/modules/teams/repositories/team.repository.ts @@ -15,18 +15,7 @@ export class TeamRepository } getTeam(teamId: string): Promise { - return this.findOneById( - teamId, - { _id: 1, name: 1 }, - { - path: 'users', - select: 'user role isNewJoiner', - populate: { - path: 'user', - select: '_id firstName lastName email joinedAt providerAccountCreatedAt' - } - } - ); + return this.findOneById(teamId, { _id: 1, name: 1 }); } getTeamsWithUsers(teamIds: string[]): Promise { diff --git a/backend/src/modules/teams/services/get.team.service.ts b/backend/src/modules/teams/services/get.team.service.ts index bfd232a45..0b08eb89f 100644 --- a/backend/src/modules/teams/services/get.team.service.ts +++ b/backend/src/modules/teams/services/get.team.service.ts @@ -23,8 +23,24 @@ export default class GetTeamService implements GetTeamServiceInterface { return this.teamRepository.countDocuments(); } - getTeam(teamId: string) { - return this.teamRepository.getTeam(teamId); + async getTeam(teamId: string) { + const team = await this.teamRepository.getTeam(teamId); + const users = await this.teamUserRepository.getUsersOfTeam(teamId); + + users.sort((a, b) => { + const fullNameA = `${a.user.firstName.toLowerCase()} ${a.user.lastName.toLowerCase()}`; + const fullNameB = `${b.user.firstName.toLowerCase()} ${b.user.lastName.toLowerCase()}`; + + return fullNameA < fullNameB ? -1 : 1; + }); + + const teamWithUsers = { + _id: team._id, + name: team.name, + users + }; + + return teamWithUsers; } async getTeamsOfUser(userId: string) { From d66ee950192b84bde7d99e2e5c377f5252e6c0a7 Mon Sep 17 00:00:00 2001 From: JoaoSaIvador Date: Thu, 12 Jan 2023 10:36:47 +0000 Subject: [PATCH 2/2] fix: sort team request after populate --- .../teams/repositories/team.repository.ts | 13 ++++++++++++- .../modules/teams/services/get.team.service.ts | 18 +++++++----------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/backend/src/modules/teams/repositories/team.repository.ts b/backend/src/modules/teams/repositories/team.repository.ts index e96b24df7..015e60bb2 100644 --- a/backend/src/modules/teams/repositories/team.repository.ts +++ b/backend/src/modules/teams/repositories/team.repository.ts @@ -15,7 +15,18 @@ export class TeamRepository } getTeam(teamId: string): Promise { - return this.findOneById(teamId, { _id: 1, name: 1 }); + return this.findOneById( + teamId, + { _id: 1, name: 1 }, + { + path: 'users', + select: 'user role isNewJoiner', + populate: { + path: 'user', + select: '_id firstName lastName email joinedAt providerAccountCreatedAt' + } + } + ); } getTeamsWithUsers(teamIds: string[]): Promise { diff --git a/backend/src/modules/teams/services/get.team.service.ts b/backend/src/modules/teams/services/get.team.service.ts index 0b08eb89f..c5384713b 100644 --- a/backend/src/modules/teams/services/get.team.service.ts +++ b/backend/src/modules/teams/services/get.team.service.ts @@ -25,22 +25,18 @@ export default class GetTeamService implements GetTeamServiceInterface { async getTeam(teamId: string) { const team = await this.teamRepository.getTeam(teamId); - const users = await this.teamUserRepository.getUsersOfTeam(teamId); - users.sort((a, b) => { - const fullNameA = `${a.user.firstName.toLowerCase()} ${a.user.lastName.toLowerCase()}`; - const fullNameB = `${b.user.firstName.toLowerCase()} ${b.user.lastName.toLowerCase()}`; + team.users.sort((a, b) => { + const userA = a.user as User; + const userB = b.user as User; + + const fullNameA = `${userA.firstName.toLowerCase()} ${userA.lastName.toLowerCase()}`; + const fullNameB = `${userB.firstName.toLowerCase()} ${userB.lastName.toLowerCase()}`; return fullNameA < fullNameB ? -1 : 1; }); - const teamWithUsers = { - _id: team._id, - name: team.name, - users - }; - - return teamWithUsers; + return team; } async getTeamsOfUser(userId: string) {