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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 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 14x 14x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import {
Body,
Controller,
ForbiddenException,
Get,
Inject,
Logger,
Param,
Patch,
Post,
Query,
UseGuards,
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { Company, Period, Plan, Statement } from '@amalia/core/models';
import { StatementToReview, type StatementResponse } from '@amalia/core/types';
import { assert } from '@amalia/ext/typescript';
import { BooleanPipe, NumberPipe, UuidPipe } from '@amalia/kernel/api';
import {
AmaliaAuthGuard,
CheckPolicies,
CurrentAuthenticatedContext,
CurrentUserCompany,
PoliciesGuard,
} from '@amalia/kernel/auth/core';
import {
canForceReviewStatements,
canListStatementReviewers,
canNotifyStatementReviewers,
canViewStatementReviews,
canViewStatements,
canViewThisStatement,
defineAbilityFor,
} from '@amalia/kernel/auth/shared';
import { type AuthenticatedContext } from '@amalia/kernel/auth/types';
import { UserSummary } from '@amalia/tenants/users/types';
import { formatStatementResponse } from '../statements/dto/statementResponse.dto';
import { StatementsService } from '../statements/statements.service';
import { PatchStatementDto } from './dto/patchStatement.dto';
import { ReviewStatementsDto } from './dto/reviewStatements.dto';
import { StatementNotifyReviewersDto } from './dto/statement-notify-reviewers.dto';
import { StatementsReviewService } from './statements-review.service';
import { FullyReviewOneStatementUseCase } from './use-cases/fully-review-one-statement/fully-review-one-statement.use-case';
import { GetStatementReviewersUseCase } from './use-cases/get-statement-reviewers/get-statement-reviewers.use-case';
import { GetStatementsToReviewUseCase } from './use-cases/get-statements-to-review/get-statements-to-review.use-case';
import { NotifyStatementReviewersUseCase } from './use-cases/notify-statement-reviewers/notify-statement-reviewers.use-case';
@UseGuards(AmaliaAuthGuard, PoliciesGuard)
@ApiBearerAuth()
@ApiTags('statements-review')
@Controller()
export class StatementsReviewController {
private readonly logger = new Logger(StatementsReviewController.name);
public constructor(
@Inject(StatementsService)
private readonly statementService: StatementsService,
private readonly statementReviewService: StatementsReviewService,
private readonly getStatementsToReviewUseCase: GetStatementsToReviewUseCase,
private readonly notifyStatementReviewersUseCase: NotifyStatementReviewersUseCase,
private readonly getStatementReviewersUseCase: GetStatementReviewersUseCase,
private readonly fullyReviewOneStatementUseCase: FullyReviewOneStatementUseCase,
) {}
@Get('statements-review')
@CheckPolicies((ability) => canViewStatementReviews(ability))
public async getStatementsToReview(
@CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
@Query('planId', new UuidPipe({ optional: true })) planId?: Plan['id'],
@Query('periodId', new UuidPipe({ optional: true })) periodId?: Period['id'],
@Query('limit', new NumberPipe({ optional: true })) limit?: number,
): Promise<StatementToReview[]> {
return this.getStatementsToReviewUseCase.execute({ authenticatedContext, planId, periodId, limit });
}
@Patch('statements/:id/fully-review')
@CheckPolicies(canForceReviewStatements)
public async fullyReviewOne(
@Param('id', new UuidPipe()) id: string,
@CurrentUserCompany() company: Company,
@CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
): Promise<StatementResponse> {
const statement = await this.fullyReviewOneStatementUseCase.execute({
authenticatedContext,
statementId: id,
company,
});
return formatStatementResponse(statement);
}
@Patch('statements/:id')
@CheckPolicies((ability) => canViewStatements(ability))
public async reviewOne(
@Param('id', new UuidPipe()) id: string,
@CurrentUserCompany() company: Company,
@CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
@Body() patchStatementDto: PatchStatementDto,
@Query('isNotify', new BooleanPipe({ optional: true })) isNotify: boolean,
): Promise<StatementResponse> {
const statement = await this.statementService.findById(company, id, false, false, [
'plan.workflow',
'user.teamAssignments',
'user.teamAssignments.team',
'user.teamAssignments.team.teamAssignments',
'period',
]);
assert(
canViewThisStatement(defineAbilityFor(authenticatedContext), statement),
new ForbiddenException("You don't have access to this statement"),
);
if (patchStatementDto.workflowAction) {
await this.statementReviewService.reviewOne(
company,
statement,
authenticatedContext,
patchStatementDto.workflowAction,
isNotify,
);
}
const patchedStatement = await this.statementService.findById(company, id);
return formatStatementResponse(patchedStatement);
}
@Post('statements/reviews')
@CheckPolicies((ability) => canViewStatements(ability))
public async reviewMany(
@Body() reviewStatementsRequest: ReviewStatementsDto,
@CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
@CurrentUserCompany() company: Company,
@Query('isNotify', new BooleanPipe({ optional: true })) isNotify: boolean,
): Promise<void> {
await this.statementReviewService.reviewMany({
...reviewStatementsRequest,
company,
authenticatedContext,
isNotify,
});
}
@Post('statements-review/:id/notify-reviewers')
@CheckPolicies((ability) => canNotifyStatementReviewers(ability))
public async notifyReviewers(
@Param('id', new UuidPipe()) statementId: Statement['id'],
@CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
@Body() body: StatementNotifyReviewersDto,
): Promise<UserSummary[]> {
return this.notifyStatementReviewersUseCase.execute({
authenticatedContext,
statementId,
dryRun: body.dryRun,
stepNumberToValidate: body.stepNumberToValidate,
});
}
@Get('statements-review/:id/reviewers')
@CheckPolicies((ability) => canListStatementReviewers(ability))
public async listStatementReviewers(
@Param('id', new UuidPipe()) statementId: Statement['id'],
@CurrentAuthenticatedContext() authenticatedContext: AuthenticatedContext,
): Promise<UserSummary[][]> {
return (await this.getStatementReviewersUseCase.execute({ authenticatedContext, statementId })).reviewersByStep;
}
}
|