generated from EyeSeeTea/dhis2-app-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not allow child be in the team hierarchy with itself as parent
- Loading branch information
Showing
11 changed files
with
154 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 62 additions & 1 deletion
63
src/domain/entities/incident-management-team/IncidentManagementTeam.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,71 @@ | ||
import { Maybe } from "../../../utils/ts-utils"; | ||
import { TEAM_ROLE_FIELD_ID } from "../../../webapp/pages/form-page/incident-management-team-member-assignment/mapIncidentManagementTeamMemberToInitialFormState"; | ||
import { Struct } from "../generic/Struct"; | ||
import { ValidationError } from "../ValidationError"; | ||
import { TeamMember } from "./TeamMember"; | ||
|
||
interface IncidentManagementTeamAttrs { | ||
lastUpdated: Maybe<Date>; | ||
teamHierarchy: TeamMember[]; | ||
} | ||
|
||
export class IncidentManagementTeam extends Struct<IncidentManagementTeamAttrs>() {} | ||
export class IncidentManagementTeam extends Struct<IncidentManagementTeamAttrs>() { | ||
static validateNotCyclicalDependency( | ||
teamMember: string | undefined, | ||
reportsToUsername: string | undefined, | ||
currentIncidentManagementTeamHierarchy: TeamMember[], | ||
property: string | ||
): ValidationError[] { | ||
const descendantsUsernamesByParentUsername = getAllDescendantsUsernamesByParentUsername( | ||
currentIncidentManagementTeamHierarchy | ||
); | ||
|
||
const descendantsFromTeamMember = | ||
descendantsUsernamesByParentUsername.get(teamMember ?? "") ?? []; | ||
|
||
const isCyclicalDependency = descendantsFromTeamMember.includes(reportsToUsername ?? ""); | ||
|
||
return property === TEAM_ROLE_FIELD_ID || !isCyclicalDependency | ||
? [] | ||
: [ | ||
{ | ||
property: property, | ||
value: "", | ||
errors: ["cannot_create_cyclycal_dependency"], | ||
}, | ||
]; | ||
} | ||
} | ||
|
||
function getAllDescendantsUsernamesByParentUsername( | ||
teamMembers: TeamMember[] | ||
): Map<string, string[]> { | ||
const initialMap = teamMembers.reduce((acc, member) => { | ||
const entries = (member.teamRoles ?? []).map(role => ({ | ||
parentUsername: role.reportsToUsername ?? "PARENT_ROOT", | ||
username: member.username, | ||
})); | ||
|
||
return entries.reduce((mapAcc, { parentUsername, username }) => { | ||
return mapAcc.set(parentUsername, [...(mapAcc.get(parentUsername) ?? []), username]); | ||
}, acc); | ||
}, new Map<string, string[]>()); | ||
|
||
const getDescendantUsernames = ( | ||
parent: string, | ||
processedParents = new Set<string>() | ||
): string[] => { | ||
if (processedParents.has(parent)) return []; | ||
processedParents.add(parent); | ||
|
||
return (initialMap.get(parent) ?? []).flatMap(username => [ | ||
username, | ||
...getDescendantUsernames(username, processedParents), | ||
]); | ||
}; | ||
|
||
return Array.from(initialMap.keys()).reduce((descendantsMap, parent) => { | ||
descendantsMap.set(parent, getDescendantUsernames(parent)); | ||
return descendantsMap; | ||
}, new Map<string, string[]>()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
...reak-event/utils/applyRulesInFormState.ts → .../form-page/utils/applyRulesInFormState.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters