From 77393d86d5ce24d32b71f0d43eb2cc7240844575 Mon Sep 17 00:00:00 2001 From: samuelmbabhazi Date: Fri, 11 Oct 2024 14:28:58 +0200 Subject: [PATCH] [Fix] Edit team functionality not working --- .../teams-mutation.component.ts | 11 +++++-- .../organization-project.service.ts | 4 +-- .../organization-team-employee.service.ts | 32 +++++++++++-------- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/apps/gauzy/src/app/pages/teams/teams-mutation/teams-mutation.component.ts b/apps/gauzy/src/app/pages/teams/teams-mutation/teams-mutation.component.ts index 518a18fc9bc..22ae6d9c93e 100644 --- a/apps/gauzy/src/app/pages/teams/teams-mutation/teams-mutation.component.ts +++ b/apps/gauzy/src/app/pages/teams/teams-mutation/teams-mutation.component.ts @@ -78,8 +78,15 @@ export class TeamsMutationComponent implements OnInit { // Check if there is a valid team if (this.team) { // Extract employee and manager IDs from the team - const selectedEmployees = this.team.members.map((member) => member.id); - const selectedManagers = this.team.managers.map((manager) => manager.id); + const selectedManagers = [...new Set(this.team.managers.map((manager) => manager.id))]; + + const selectedEmployees = [ + ...new Set( + this.team.members + .filter((member) => !selectedManagers.includes(member.id)) + .map((member) => member.id) + ) + ]; // Patch form values with team information this.form.patchValue({ diff --git a/packages/core/src/organization-project/organization-project.service.ts b/packages/core/src/organization-project/organization-project.service.ts index 43856d0df9f..eda018760a6 100644 --- a/packages/core/src/organization-project/organization-project.service.ts +++ b/packages/core/src/organization-project/organization-project.service.ts @@ -266,10 +266,10 @@ export class OrganizationProjectService extends TenantAwareCrudService { const tenantId = RequestContext.currentTenantId(); - const membersToUpdate = [...managerIds, ...memberIds]; + const membersToUpdate = new Set([...managerIds, ...memberIds].filter(Boolean)); // Fetch existing team members with their roles const teamMembers = await this.typeOrmRepository.find({ @@ -58,25 +58,29 @@ export class OrganizationTeamEmployeeService extends TenantAwareCrudService [member.employeeId, member])); // Separate members to remove and to update - const removedMembers = teamMembers.filter((member) => !membersToUpdate.includes(member.employeeId)); - const updatedMembers = teamMembers.filter((member) => membersToUpdate.includes(member.employeeId)); + const removedMembers = teamMembers.filter((member) => !membersToUpdate.has(member.employeeId)); + const updatedMembers = teamMembers.filter((member) => membersToUpdate.has(member.employeeId)); // 1. Remove members who are no longer in the team if (removedMembers.length > 0) { - const removedMemberIds = removedMembers.map((member) => member.id); - await this.deleteMemberByIds(removedMemberIds); + await this.deleteMemberByIds(removedMembers.map((member) => member.id)); } // 2. Update role for existing members - for (const member of updatedMembers) { - const isManager = managerIds.includes(member.employeeId); - const newRole = isManager ? role : member.role?.id === role.id ? member.role : null; - - // Only update if the role is different - if (newRole && newRole.id !== member.roleId) { - await this.typeOrmRepository.update(member.id, { role: newRole }); - } - } + await Promise.all( + updatedMembers.map(async (member) => { + const isManager = managerIds.includes(member.employeeId); + const newRole = isManager ? role : null; + + // Only update if the role has changed + if (newRole?.id !== member.roleId) { + await this.typeOrmRepository.update(member.id, { + role: newRole, + isManager + }); + } + }) + ); // 3. Add new members to the team const newMembers = employees.filter((employee) => !existingMemberMap.has(employee.id));