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 | 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 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 13x 13x 5x 5x 5x 5x 5x 5x 5x 5x 13x 13x 13x 13x 13x 13x 13x 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 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 } from '@amalia/core/models';
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, UnarchiveTeamRequest } from '@amalia/tenants/teams/types';
import { TeamUpdatedEvent } from '../events/team-updated-event';
type TeamMin = Pick<Team, 'archived' | 'id' | 'name' | 'parentTeamId'>;
export class UnarchiveTeamUseCase {
private readonly logger = new Logger(UnarchiveTeamUseCase.name);
public constructor(
private readonly eventBus: EventBus,
@InjectRepository(Team)
private readonly teamsRepository: Repository<Team>,
) {}
/**
* Unarchive a team and all its children if requested.
*
* @returns The number of teams effectively unarchived.
*/
@Transactional()
public async execute({
company,
authenticatedContext,
teamId,
unarchiveTeamRequest,
}: {
company: Company;
authenticatedContext: AuthenticatedContext;
teamId: Team['id'];
unarchiveTeamRequest: UnarchiveTeamRequest;
}): 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);
// Unarchive the team, and its children if requested.
const teamsToUnarchive = [
teamNode.team,
...(unarchiveTeamRequest.unarchiveChildrenTeams ? getTeamDescendants<TeamMin>(teamNode) : []),
];
await this.unarchiveTeams(
company,
teamsToUnarchive.map((team) => team.id),
);
this.publishAuditLog({ authenticatedContext, teams: teamsToUnarchive }).catch((err) => {
this.logger.error({
message: 'Failed to publish TeamUpdatedEvent',
error: toError(err),
teams: teamsToUnarchive,
});
});
return teamsToUnarchive.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: true },
newValues: { archived: false },
},
}),
);
}),
);
}
private async unarchiveTeams(company: Company, teamIds: Team['id'][]) {
await this.teamsRepository.update(
{
company: { id: company.id },
id: In(teamIds),
},
{ archived: false },
);
}
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 not 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'));
}
}
|