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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 14x 14x 14x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 3x 3x 3x 3x 3x 3x 3x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 3x 3x 3x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 5x 5x 5x 5x 5x 5x 5x 1x 1x 13x 13x 13x 13x 1x | import { ForbiddenException, Logger, NotFoundException, UnprocessableEntityException } from '@nestjs/common';
import { EventBus } from '@nestjs/cqrs';
import { InjectRepository } from '@nestjs/typeorm';
import { In, Repository } from 'typeorm';
import { Transactional } from 'typeorm-transactional';
import { Company, Team, TeamAssignment } from '@amalia/core/models';
import { toTimestamp } from '@amalia/ext/dates';
import { assert, toError } from '@amalia/ext/typescript';
import { canModifyTeams, defineAbilityFor } from '@amalia/kernel/auth/shared';
import { type AuthenticatedContext } from '@amalia/kernel/auth/types';
import { RecordObject, RecordType } from '@amalia/tenants/monitoring/audit/types';
import { getTeamDescendants, makeTeamsTree } from '@amalia/tenants/teams/shared/tree';
import { TeamTreeNode } from '@amalia/tenants/teams/types';
import { TeamUpdatedEvent } from '../events/team-updated-event';
type TeamMin = Pick<Team, 'archived' | 'id' | 'name' | 'parentTeamId'>;
export class ArchiveTeamUseCase {
private readonly logger = new Logger(ArchiveTeamUseCase.name);
public constructor(
private readonly eventBus: EventBus,
@InjectRepository(Team)
private readonly teamsRepository: Repository<Team>,
@InjectRepository(TeamAssignment)
private readonly teamAssignmentsRepository: Repository<TeamAssignment>,
) {}
/**
* Archive a team and all its active children.
* Will end all active assignments for the team and its children.
*
* @returns The number of teams effectively archived.
*/
@Transactional()
public async execute({
company,
authenticatedContext,
teamId,
}: {
company: Company;
authenticatedContext: AuthenticatedContext;
teamId: Team['id'];
}): Promise<number> {
this.assertCanExecute(authenticatedContext);
const allTeams: TeamMin[] = await this.teamsRepository.find({
where: { company: { id: company.id } },
relations: ['parentTeam'],
select: {
id: true,
name: true,
parentTeamId: true,
archived: true,
},
});
const teamsTree = makeTeamsTree(allTeams);
const teamNode = this.getAndValidateTeam(teamsTree, teamId);
const teamsToArchive = [
teamNode.team,
...getTeamDescendants<TeamMin>(teamNode).filter((childTeam) => !childTeam.archived),
];
const teamsToArchiveIds = teamsToArchive.map((child) => child.id);
await this.endAllTeamAssignments(company, teamsToArchiveIds);
await this.archiveTeams(company, teamsToArchiveIds);
this.publishAuditLog({ authenticatedContext, teams: teamsToArchive }).catch((err) => {
this.logger.error({
message: 'Failed to publish TeamUpdatedEvent',
error: toError(err),
teams: teamsToArchive,
});
});
return teamsToArchiveIds.length;
}
private async publishAuditLog({
authenticatedContext,
teams,
}: {
authenticatedContext: AuthenticatedContext;
teams: TeamMin[];
}) {
await Promise.all(
teams.map(async (team) => {
await this.eventBus.publish(
new TeamUpdatedEvent({
authenticatedContext,
object: RecordObject.TEAM,
type: RecordType.EDIT,
values: {
target: { id: team.id, name: team.name },
oldValues: { archived: false },
newValues: { archived: true },
},
}),
);
}),
);
}
private async archiveTeams(company: Company, teamIds: Team['id'][]) {
await this.teamsRepository.update(
{
company: { id: company.id },
id: In(teamIds),
},
{ archived: true },
);
}
private async endAllTeamAssignments(company: Company, teamIds: Team['id'][]) {
const endOfAssignmentTimestamp = toTimestamp(new Date());
await this.teamAssignmentsRepository
.createQueryBuilder()
.update(TeamAssignment)
.set({ effectiveUntil: endOfAssignmentTimestamp })
.where('companyId = :companyId', { companyId: company.id })
.andWhere('teamId IN (:...teamIds)', { teamIds })
.andWhere('(effectiveUntil IS NULL OR effectiveUntil > :endOfAssignmentTimestamp)', { endOfAssignmentTimestamp })
.execute();
// In the previous service implementation there were no logs for this operation, not sure if I should add them.
}
private getAndValidateTeam(teamsTree: TeamTreeNode<TeamMin>[], teamId: Team['id']) {
const teamNode = teamsTree.find((node) => node.team.id === teamId);
assert(teamNode, new NotFoundException(`Team ${teamId} not found`));
assert(!teamNode.team.archived, new UnprocessableEntityException(`Team ${teamNode.team.name} is archived`));
return teamNode;
}
private assertCanExecute(authenticatedContext: AuthenticatedContext) {
const ability = defineAbilityFor(authenticatedContext);
assert(canModifyTeams(ability), new ForbiddenException('You do not have the permission to modify teams'));
}
}
|