Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Edit team functionality not working #8391

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,10 @@ export class OrganizationProjectService extends TenantAwareCrudService<Organizat
const newRole = isManager ? managerRole : null;

// Only update if the role has changed
if (newRole && newRole.id !== member.roleId) {
if (newRole?.id !== member.roleId) {
await this.typeOrmOrganizationProjectEmployeeRepository.update(member.id, {
role: newRole,
isManager: true
isManager
});
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class OrganizationTeamEmployeeService extends TenantAwareCrudService<Orga
memberIds: ID[]
): Promise<void> {
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({
Expand All @@ -58,25 +58,29 @@ export class OrganizationTeamEmployeeService extends TenantAwareCrudService<Orga
const existingMemberMap = new Map(teamMembers.map((member) => [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));
Expand Down
Loading