Skip to content

Commit

Permalink
Do not allow to delete teamRoles if it's parent and all children are …
Browse files Browse the repository at this point in the history
…not selected
  • Loading branch information
anagperal committed Nov 7, 2024
1 parent d58492b commit 7a907d7
Showing 1 changed file with 51 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,15 @@ export function useIMTeamBuilder(diseaseOutbreakEventId: Id): State {
}
);

const selectedItemsUsernames = incidentManagementTeamItemsSelected?.map(
({ username }) => username
);

const hasSomeParentReporting = !!incidentManagementTeam?.teamHierarchy.some(
teamMember =>
teamMember.teamRoles?.some(teamRole =>
selectedItemsUsernames?.includes(teamRole.reportsToUsername || "")
)
const areAllParentsAndAllChildrenSelected = checkIfParentsAndAllChildrenSelected(
newSelection,
incidentManagementTeam?.teamHierarchy
);

setSelectedHierarchyItemIds(newSelection);
setDisableDeletion(isIncidentManagerRoleSelected || hasSomeParentReporting);
setDisableDeletion(
isIncidentManagerRoleSelected || !areAllParentsAndAllChildrenSelected
);
},
[
incidentManagementTeam?.teamHierarchy,
Expand Down Expand Up @@ -198,3 +194,48 @@ export function useIMTeamBuilder(diseaseOutbreakEventId: Id): State {
constactTableRows,
};
}

function checkIfParentsAndAllChildrenSelected(
teamRoleSelection: Id[],
incidentManagementTeamHierarchy: Maybe<TeamMember[]>
): boolean {
if (!incidentManagementTeamHierarchy) return true;

const selectedItemsInTeamHierarchy = incidentManagementTeamHierarchy
.map(teamMember => ({
...teamMember,
teamRoles: teamMember.teamRoles?.filter(teamRole =>
teamRoleSelection.includes(teamRole.id)
),
}))
.filter(teamMember => teamMember.teamRoles?.length);

const allTeamRoleChildrenIdsByParentId = getAllTeamRoleChildrenIdsByParentTeamRoleId(
incidentManagementTeamHierarchy
);

return selectedItemsInTeamHierarchy.every(teamMember => {
const teamRoleChildrenIds = allTeamRoleChildrenIdsByParentId.get(teamMember.username);
return (
!teamRoleChildrenIds ||
teamRoleChildrenIds.every(childId => {
return teamRoleSelection.includes(childId);
})
);
});
}

function getAllTeamRoleChildrenIdsByParentTeamRoleId(teamMembers: TeamMember[]): Map<string, Id[]> {
return teamMembers.reduce((acc, teamMember) => {
return (
teamMember.teamRoles?.reduce((innerAcc, teamRole) => {
const parentTeamRoleId = teamRole.reportsToUsername;
if (parentTeamRoleId) {
const existingChildren = innerAcc.get(parentTeamRoleId) || [];
innerAcc.set(parentTeamRoleId, [...existingChildren, teamRole.id]);
}
return innerAcc;
}, acc) || acc
);
}, new Map<string, Id[]>());
}

0 comments on commit 7a907d7

Please sign in to comment.