Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 16x 16x 16x 1x 1x 99x 99x 99x 99x 99x 99x 97x 97x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 2x 2x 2x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 99x 99x 8x 8x 2x 2x 2x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 4x 4x 4x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 4x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 7x 7x 7x 7x 7x 7x 7x 14x 14x 7x 7x 7x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 7x 7x 7x 8x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 8x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 7x 7x 7x 2x 2x 7x 2x 2x 7x 7x 7x 1x | import { isEmpty, set } from 'lodash-es';
import { type Company, type Team, type User } from '@amalia/core/models';
import { emptyAssignmentList, type AssignmentListScope } from '@amalia/core/types';
import { type TeamAssignmentService } from '@amalia/tenants/assignments/teams/core';
import { TeamRole } from '@amalia/tenants/assignments/teams/types';
import { type TeamsService } from '@amalia/tenants/teams/core';
import { AssignmentType, MatchScope, type TeamScope } from '@amalia/tenants/teams/types';
/**
* Build assignment scope for a given user.
*/
export class AssignmentScopeBuilder {
public constructor(
private readonly teamService: TeamsService,
private readonly teamAssignmentService: TeamAssignmentService,
) {}
public async build(
company: Company,
user: User,
mainTeamId: string | null,
scopesToFetch: TeamScope[],
): Promise<AssignmentListScope | undefined> {
if (isEmpty(scopesToFetch)) {
return undefined;
}
const allUserTeamsAndSubordinatesEver = await this.teamService.findAllWhereUserAssignedOrManager(
company,
user.id,
undefined,
null,
true,
);
const allUserTeamsEver = allUserTeamsAndSubordinatesEver.filter((t) =>
(t.teamAssignments ?? []).some((ta) => ta.userId === user.id),
);
// Those are all the team assignments I've ever had in all those teams.
// We need this in any cases because my own team assignments may be used to filter records
// based on my own assignments dates.
const allUserTeamAssignments = await Promise.all(
allUserTeamsEver
.flatMap((team) => (team.teamAssignments ?? []).filter((assignment) => assignment.userId === user.id))
.map(async (a) => {
let hierarchy: string[] = [];
if (a.teamRole === TeamRole.TEAM_MANAGER) {
const team = allUserTeamsEver.find((t) => t.id === a.teamId);
hierarchy = await this.teamService.populateTeamChildren(team ? [team] : []);
}
return {
...a,
user,
hierarchy,
};
}),
);
const assignmentList = emptyAssignmentList;
// fill "mine" scope with all user assignments
set(assignmentList, [AssignmentType.MINE, MatchScope.ALL, 0], allUserTeamAssignments);
// Actually mainTeam can be set in planAssignment
const mainTeam = allUserTeamsAndSubordinatesEver.find((team) => team.id === mainTeamId);
const teamsToFetch = mainTeam ? [mainTeam] : allUserTeamsEver;
for (const type of Object.values(AssignmentType)) {
await this.populateAssignmentListScope(company, user, assignmentList, type, teamsToFetch, scopesToFetch);
}
return assignmentList;
}
private async populateAssignmentListScope(
company: Company,
user: User,
assignmentList: AssignmentListScope,
type: AssignmentType,
teamsToFetch: Team[],
scopesToFetch: TeamScope[],
) {
// if type is not needed in scope, do not populate it
if (type === AssignmentType.MINE || !scopesToFetch.map((s) => s.role).includes(type)) {
return;
}
for (const scope of Object.values(MatchScope)) {
await this.populateAssignmentTypeScopeInList(
company,
user,
assignmentList,
type,
scope,
teamsToFetch,
scopesToFetch,
);
}
}
private async populateAssignmentTypeScopeInList(
company: Company,
user: User,
assignmentList: AssignmentListScope,
type: AssignmentType,
scope: MatchScope,
teamsToFetch: Team[],
scopesToFetch: TeamScope[],
) {
const filteredScopes = scopesToFetch.filter((s) => s.role === type).map((s) => s.scope);
// if scope is not needed, do not populate it
if (!filteredScopes.includes(scope)) {
return;
}
const maxDepth = this.computeAssignmentTypeHierarchyDepth(type, scopesToFetch, scope);
const userTeams = teamsToFetch.filter((team) =>
(team.teamAssignments ?? []).some((assignment) => assignment.userId === user.id),
);
const managerTeams = userTeams.filter((team) =>
(team.teamAssignments ?? []).some(
(assignment) => assignment.userId === user.id && assignment.teamRole === TeamRole.TEAM_MANAGER,
),
);
const teamScope = scope === MatchScope.SUB && userTeams.length > 0 ? managerTeams : teamsToFetch;
const role = this.getRoleFromAssignment(type);
const assignmentsWithHierarchy = await this.teamAssignmentService.getHierarchyAssignments(
company,
user,
teamScope,
scope,
role,
);
set(assignmentList[type][scope as keyof (typeof assignmentList)[AssignmentType]], '0', assignmentsWithHierarchy);
if (maxDepth > 0) {
const assignments = await this.teamAssignmentService.getHierarchyAssignmentsWithDepth(
company,
user,
teamScope,
maxDepth,
1,
{},
scope,
role,
);
Object.entries(assignments).forEach(([depth, as]) => {
set(assignmentList[type][scope as keyof (typeof assignmentList)[AssignmentType]], depth, as);
});
}
}
private computeAssignmentTypeHierarchyDepth(
type: AssignmentType,
scopesToFetch: TeamScope[],
scope: MatchScope,
): number {
const maxDepth = Math.max(
...scopesToFetch
.filter((s) => s.role === type)
.filter((s) => s.scope === scope)
.map((s) => s.depth),
);
return Number.isNaN(maxDepth) ? 1 : maxDepth;
}
private getRoleFromAssignment(type: AssignmentType): TeamRole | undefined {
let role: TeamRole | undefined;
if (type === AssignmentType.EMPLOYEE) {
role = TeamRole.TEAM_EMPLOYEE;
}
if (type === AssignmentType.MANAGER) {
role = TeamRole.TEAM_MANAGER;
}
return role;
}
}
|