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 | 1x 1x 1x 1x 1x 1x 1x 102x 102x 102x 102x 102x 102x 102x 102x 162x 162x 324x 324x 324x 162x 162x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 102x 162x 162x 162x 162x 162x 162x 162x 162x 162x 162x 162x 162x 162x 162x 102x 102x 102x 102x 102x 2x 2x 102x 102x 102x 102x 102x 1x | import { type Period, type PlanAssignment, type Team, type TeamAssignment } from '@amalia/core/models';
import { dateIsInAssignmentRange, startOfMonthTimestamp } from '@amalia/ext/dates';
import { putOnTop } from '@amalia/ext/lodash';
import { TeamRole } from '@amalia/tenants/assignments/teams/types';
export class TeamScopeBuilder {
public build(
teams: Team[],
planAssignment: PlanAssignment,
period: Period,
): { teamsWithKeywords?: Team[]; myTeamAssignment?: TeamAssignment } {
const { user } = planAssignment;
// Filter team assignment using the start of the month as effectiveAsOf in order to compute the statements for the users assigned with a effectiveAsOf in a middle of the month.
const filteredTeamsWithAssignmentInPeriodRange = teams.map((team) => ({
...team,
teamAssignments: (team.teamAssignments ?? []).filter((ta) =>
dateIsInAssignmentRange(period.startDate, {
effectiveAsOf: ta.effectiveAsOf ? startOfMonthTimestamp({ timestamp: ta.effectiveAsOf }) : ta.effectiveAsOf,
effectiveUntil: ta.effectiveUntil,
}),
),
}));
// TeamIds where I actually appear is the teams from where I have a valid planAssignment for the current period.
const teamIdsWhereIAppear = new Set(
filteredTeamsWithAssignmentInPeriodRange
.filter((t) => t.teamAssignments.some((ta) => ta.userId === user.id))
.map((t) => t.id),
);
// Build members, employees and managers for each team from assignments.
let teamsWithKeywords = filteredTeamsWithAssignmentInPeriodRange
.filter((team) => teamIdsWhereIAppear.has(team.id))
.map((t) => {
const employees = t.teamAssignments
.filter((ta) => ta.teamRole === TeamRole.TEAM_EMPLOYEE)
.map((ta: TeamAssignment) => ta.user);
const managers = t.teamAssignments
.filter((tm) => tm.teamRole === TeamRole.TEAM_MANAGER)
.map((tm: TeamAssignment) => tm.user);
return {
...t,
employees,
managers,
// Build members from assignments and managers
members: t.teamAssignments.map((ta: TeamAssignment) => ta.user),
};
});
// If we have a main team in the plan assignment, put it on top, so it'll be
// used in the `team` keyword.
if (planAssignment.mainTeamId) {
teamsWithKeywords = putOnTop(teamsWithKeywords, (team) => team.id === planAssignment.mainTeamId);
}
const myTeamAssignment = teamsWithKeywords[0]?.teamAssignments?.find((ta) => ta.userId === user.id);
return { teamsWithKeywords, myTeamAssignment };
}
}
|