All files / src/infrastructure/database/repositories prisma-appointment.repository.ts

100% Statements 8/8
100% Branches 4/4
100% Functions 6/6
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 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104                                          23x                                   45x     9x 9x       12x                               12x       23x                                                                 2x      
import { PrismaClient } from '../../../generated/prisma/client';
import { AppointmentRepository } from '../../../application/appointment/appointment-repository';
import { Appointment, AppointmentStatus } from '../../../domain/appointment/appointment';
 
interface AppointmentRow {
  id: string;
  babyId: string;
  scheduledAt: Date;
  doctorName: string;
  specialty: string | null;
  location: string | null;
  reason: string | null;
  notes: string | null;
  status: string;
  specialistId: string | null;
  weightGrams: number | null;
  heightMillimeters: number | null;
  createdAt: Date;
}
 
function toDomain(row: AppointmentRow): Appointment {
  return Appointment.restore({
    id: row.id,
    babyId: row.babyId,
    scheduledAt: row.scheduledAt,
    doctorName: row.doctorName,
    specialty: row.specialty,
    location: row.location,
    reason: row.reason,
    notes: row.notes,
    status: row.status as AppointmentStatus,
    specialistId: row.specialistId,
    weightGrams: row.weightGrams,
    heightMillimeters: row.heightMillimeters,
    createdAt: row.createdAt,
  });
}
 
export class PrismaAppointmentRepository implements AppointmentRepository {
  constructor(private readonly prisma: PrismaClient) {}
 
  async findById(id: string): Promise<Appointment | null> {
    const row = await this.prisma.appointment.findUnique({ where: { id } });
    return row ? toDomain(row) : null;
  }
 
  async findAllByBabyId(babyId: string, search?: string): Promise<Appointment[]> {
    const rows = await this.prisma.appointment.findMany({
      where: {
        babyId,
        ...(search
          ? {
              OR: [
                { doctorName: { contains: search, mode: 'insensitive' } },
                { specialty: { contains: search, mode: 'insensitive' } },
                { location: { contains: search, mode: 'insensitive' } },
                { reason: { contains: search, mode: 'insensitive' } },
              ],
            }
          : {}),
      },
      orderBy: { scheduledAt: 'asc' },
    });
    return rows.map(toDomain);
  }
 
  async save(appointment: Appointment): Promise<void> {
    await this.prisma.appointment.upsert({
      where: { id: appointment.id },
      create: {
        id: appointment.id,
        babyId: appointment.babyId,
        scheduledAt: appointment.scheduledAt,
        doctorName: appointment.doctorName,
        specialty: appointment.specialty,
        location: appointment.location,
        reason: appointment.reason,
        notes: appointment.notes,
        status: appointment.status,
        specialistId: appointment.specialistId,
        weightGrams: appointment.weightGrams,
        heightMillimeters: appointment.heightMillimeters,
        createdAt: appointment.createdAt,
      },
      update: {
        scheduledAt: appointment.scheduledAt,
        doctorName: appointment.doctorName,
        specialty: appointment.specialty,
        location: appointment.location,
        reason: appointment.reason,
        notes: appointment.notes,
        status: appointment.status,
        specialistId: appointment.specialistId,
        weightGrams: appointment.weightGrams,
        heightMillimeters: appointment.heightMillimeters,
      },
    });
  }
 
  async delete(id: string): Promise<void> {
    await this.prisma.appointment.delete({ where: { id } });
  }
}