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

100% Statements 8/8
100% Branches 2/2
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                                      7x                               22x     6x 6x           2x 2x       9x                                                         1x      
import { PrismaClient } from '../../../generated/prisma/client';
import { MedicationRepository } from '../../../application/medication/medication-repository';
import { Medication } from '../../../domain/medication/medication';
 
interface MedicationRow {
  id: string;
  babyId: string;
  name: string;
  dosage: string | null;
  frequency: string | null;
  reason: string | null;
  prescriberName: string | null;
  startedOn: Date;
  endedOn: Date | null;
  notes: string | null;
  createdAt: Date;
}
 
function toDomain(row: MedicationRow): Medication {
  return Medication.restore({
    id: row.id,
    babyId: row.babyId,
    name: row.name,
    dosage: row.dosage,
    frequency: row.frequency,
    reason: row.reason,
    prescriberName: row.prescriberName,
    startedOn: row.startedOn,
    endedOn: row.endedOn,
    notes: row.notes,
    createdAt: row.createdAt,
  });
}
 
export class PrismaMedicationRepository implements MedicationRepository {
  constructor(private readonly prisma: PrismaClient) {}
 
  async findById(id: string): Promise<Medication | null> {
    const row = await this.prisma.medication.findUnique({ where: { id } });
    return row ? toDomain(row) : null;
  }
 
  // Most recently started first: what a parent is looking for is almost always the current course
  // or the one just before it, and the oldest entries are the ones nobody scrolls to.
  async findAllByBabyId(babyId: string): Promise<Medication[]> {
    const rows = await this.prisma.medication.findMany({ where: { babyId }, orderBy: { startedOn: 'desc' } });
    return rows.map(toDomain);
  }
 
  async save(medication: Medication): Promise<void> {
    await this.prisma.medication.upsert({
      where: { id: medication.id },
      create: {
        id: medication.id,
        babyId: medication.babyId,
        name: medication.name,
        dosage: medication.dosage,
        frequency: medication.frequency,
        reason: medication.reason,
        prescriberName: medication.prescriberName,
        startedOn: medication.startedOn,
        endedOn: medication.endedOn,
        notes: medication.notes,
        createdAt: medication.createdAt,
      },
      update: {
        name: medication.name,
        dosage: medication.dosage,
        frequency: medication.frequency,
        reason: medication.reason,
        prescriberName: medication.prescriberName,
        startedOn: medication.startedOn,
        endedOn: medication.endedOn,
        notes: medication.notes,
      },
    });
  }
 
  async delete(id: string): Promise<void> {
    await this.prisma.medication.delete({ where: { id } });
  }
}