All files / src/application/legal record-legal-acceptance.use-case.ts

100% Statements 7/7
100% Branches 2/2
100% Functions 2/2
100% Lines 7/7

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                      22x             6x           6x 1x     5x             5x   5x      
import { randomUUID } from 'node:crypto';
import { LegalAcceptance, LegalDocumentId } from '../../domain/legal/legal-acceptance';
import { LegalAcceptanceRepository } from './legal-acceptance-repository';
 
export interface RecordLegalAcceptanceInput {
  userId: string;
  documentId: LegalDocumentId;
  version: string;
}
 
export class RecordLegalAcceptanceUseCase {
  constructor(private readonly legalAcceptanceRepository: LegalAcceptanceRepository) {}
 
  async execute(input: RecordLegalAcceptanceInput): Promise<LegalAcceptance> {
    // Accepting the same version twice returns the first acceptance rather than replacing it. A
    // second POST is what a double-tap or a retried request looks like, and moving the timestamp
    // forward would quietly rewrite when the consent was actually given — the one fact the record
    // exists to hold.
    const existing = await this.legalAcceptanceRepository.findByUserDocumentAndVersion(
      input.userId,
      input.documentId,
      input.version,
    );
 
    if (existing) {
      return existing;
    }
 
    const acceptance = LegalAcceptance.record({
      id: randomUUID(),
      userId: input.userId,
      documentId: input.documentId,
      version: input.version,
    });
 
    await this.legalAcceptanceRepository.save(acceptance);
 
    return acceptance;
  }
}