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 | 10x 22x 22x 22x 22x 5x 5x 22x 6x 6x 6x | import type { App } from '../../../infrastructure/http/build-app';
import { LegalAcceptance } from '../../../domain/legal/legal-acceptance';
import { ListLegalAcceptancesUseCase } from '../../../application/legal/list-legal-acceptances.use-case';
import { RecordLegalAcceptanceUseCase } from '../../../application/legal/record-legal-acceptance.use-case';
import { PrismaLegalAcceptanceRepository } from '../../../infrastructure/database/repositories/prisma-legal-acceptance.repository';
import { prisma } from '../../../infrastructure/database/prisma-client';
import { auditLogger } from '../../../infrastructure/audit/audit-logger.instance';
import { authenticate } from '../plugins/authenticate';
import { authErrorResponseSchema } from '../schemas/auth.schema';
import {
legalAcceptanceListResponseSchema,
legalAcceptanceResponseSchema,
recordLegalAcceptanceBodySchema,
} from '../schemas/legal.schema';
function toResponse(acceptance: LegalAcceptance) {
return {
documentId: acceptance.documentId,
version: acceptance.version,
acceptedAt: acceptance.acceptedAt.toISOString(),
};
}
export async function legalRoutes(app: App) {
const legalAcceptanceRepository = new PrismaLegalAcceptanceRepository(prisma);
const listLegalAcceptancesUseCase = new ListLegalAcceptancesUseCase(legalAcceptanceRepository);
const recordLegalAcceptanceUseCase = new RecordLegalAcceptanceUseCase(legalAcceptanceRepository);
app.route({
method: 'GET',
url: '/legal/acceptances',
preHandler: authenticate,
schema: {
tags: ['Legal'],
summary: "List the documents the authenticated user has accepted",
description:
'Every (document, version) pair the user has accepted, newest first. The caller compares ' +
'these against the versions currently in force and asks again for whichever differ.',
response: {
200: legalAcceptanceListResponseSchema,
401: authErrorResponseSchema,
500: authErrorResponseSchema,
},
},
handler: async (request, reply) => {
const acceptances = await listLegalAcceptancesUseCase.execute(request.userId);
return reply.status(200).send(acceptances.map(toResponse));
},
});
app.route({
method: 'POST',
url: '/legal/acceptances',
preHandler: authenticate,
schema: {
tags: ['Legal'],
summary: 'Record that the user accepted a version of a document',
description:
'Answers 200, not 201, and is idempotent: accepting a version already accepted returns the ' +
'original record with its original timestamp. A repeated POST is what a double-tap or a ' +
'retried request looks like, and moving the timestamp would rewrite when consent was given.',
body: recordLegalAcceptanceBodySchema,
response: {
200: legalAcceptanceResponseSchema,
400: authErrorResponseSchema,
401: authErrorResponseSchema,
500: authErrorResponseSchema,
},
},
handler: async (request, reply) => {
const acceptance = await recordLegalAcceptanceUseCase.execute({
userId: request.userId,
documentId: request.body.documentId,
version: request.body.version,
});
auditLogger.log({
userId: request.userId,
action: 'legal.accept',
resourceType: 'LegalAcceptance',
resourceId: acceptance.id,
});
return reply.status(200).send(toResponse(acceptance));
},
});
}
|