All files / src/application/baby delete-baby.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                        26x 26x       9x       5x   5x 2x     3x      
import { BabyRepository } from './baby-repository';
import { BabyGuardianRepository } from './baby-guardian-repository';
import { ensureBabyAccess } from './ensure-baby-access';
import { GuardianForbiddenError } from './errors/guardian-forbidden.error';
 
export interface DeleteBabyInput {
  babyId: string;
  requestingUserId: string;
}
 
export class DeleteBabyUseCase {
  constructor(
    private readonly babyRepository: BabyRepository,
    private readonly babyGuardianRepository: BabyGuardianRepository,
  ) {}
 
  async execute(input: DeleteBabyInput): Promise<void> {
    await ensureBabyAccess(this.babyRepository, this.babyGuardianRepository, input.babyId, input.requestingUserId);
 
    // Deleting the whole profile is OWNER-only — a shared GUARDIAN can read/write health data but
    // shouldn't be able to wipe another guardian's (or the creator's) child's profile.
    const guardian = await this.babyGuardianRepository.findByBabyAndUser(input.babyId, input.requestingUserId);
 
    if (!guardian?.isOwner()) {
      throw new GuardianForbiddenError('Only the baby profile owner can delete it');
    }
 
    await this.babyRepository.delete(input.babyId);
  }
}