All files / src/application/appointment delete-appointment.use-case.ts

100% Statements 8/8
100% Branches 4/4
100% Functions 2/2
100% Lines 8/8

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                                                          27x 27x 27x       8x   7x         7x 3x     4x      
import { BabyRepository } from '../baby/baby-repository';
import { BabyGuardianRepository } from '../baby/baby-guardian-repository';
import { ensureBabyAccess } from '../baby/ensure-baby-access';
import { AppointmentRepository } from './appointment-repository';
import { AppointmentNotFoundError } from './errors/appointment-not-found.error';
 
export interface DeleteAppointmentInput {
  babyId: string;
  appointmentId: string;
  requestingUserId: string;
}
 
/**
 * Removes an appointment for good.
 *
 * Distinct from cancelling, which is what this API offered until now: cancelling
 * is `PATCH status=CANCELLED` and keeps the row, because "the visit was called
 * off" is part of the history. Deleting is for the other case — the record
 * should not exist, usually because it was entered by mistake. Since #21 an
 * appointment can also be recorded after the fact, and cancelling something that
 * already happened says nothing at all.
 *
 * Hard delete, and no status is exempt. That mirrors milestones, which are just
 * as much a health record and have always been deletable: the data belongs to
 * the family, and a typo in a completed visit is exactly as wrong as a typo in a
 * scheduled one.
 */
export class DeleteAppointmentUseCase {
  constructor(
    private readonly babyRepository: BabyRepository,
    private readonly babyGuardianRepository: BabyGuardianRepository,
    private readonly appointmentRepository: AppointmentRepository,
  ) {}
 
  async execute(input: DeleteAppointmentInput): Promise<void> {
    await ensureBabyAccess(this.babyRepository, this.babyGuardianRepository, input.babyId, input.requestingUserId);
 
    const existingAppointment = await this.appointmentRepository.findById(input.appointmentId);
 
    // The babyId check is not redundant with the access check above: knowing an
    // appointment id must not be enough to delete it from under a baby the
    // caller does have access to.
    if (!existingAppointment || existingAppointment.babyId !== input.babyId) {
      throw new AppointmentNotFoundError();
    }
 
    await this.appointmentRepository.delete(input.appointmentId);
  }
}