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

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

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                                      25x     5x   5x 3x     2x      
import { SpecialistRepository } from './specialist-repository';
import { SpecialistNotFoundError } from './errors/specialist-not-found.error';
 
export interface DeleteSpecialistInput {
  specialistId: string;
  requestingUserId: string;
}
 
/**
 * Removing a professional edits the address book and nothing else.
 *
 * Appointments they attended keep their `doctorName` — the name as written on the day — and only
 * lose the `specialistId` link, which the schema nulls. A health record must not lose information
 * because somebody tidied a list.
 *
 * Owner only, and 404 rather than 403 for the same reason as editing: seeing an entry does not make
 * it yours to remove from somebody else's list.
 */
export class DeleteSpecialistUseCase {
  constructor(private readonly specialistRepository: SpecialistRepository) {}
 
  async execute(input: DeleteSpecialistInput): Promise<void> {
    const existing = await this.specialistRepository.findById(input.specialistId);
 
    if (!existing || existing.userId !== input.requestingUserId) {
      throw new SpecialistNotFoundError();
    }
 
    await this.specialistRepository.delete(input.specialistId);
  }
}