All files / src/infrastructure/database/repositories prisma-baby-vaccine-record.repository.ts

92.3% Statements 12/13
75% Branches 6/8
100% Functions 8/8
92.3% Lines 12/13

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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120                                          53x                                   45x     3x 3x       18x           18x       4x     4x       4x       4x                                                       10x                                       1x      
import { PrismaClient } from '../../../generated/prisma/client';
import { BabyVaccineRecordRepository } from '../../../application/vaccine/baby-vaccine-record-repository';
import { BabyVaccineRecord, VaccineRecordSource, VaccineRecordStatus } from '../../../domain/vaccine/baby-vaccine-record';
 
interface BabyVaccineRecordRow {
  id: string;
  babyId: string;
  vaccineId: string | null;
  source: string;
  customName: string | null;
  customDose: string | null;
  status: string;
  applicationDate: Date | null;
  notes: string | null;
  batchNumber: string | null;
  location: string | null;
  professional: string | null;
  photoUrl: string | null;
}
 
function toDomain(record: BabyVaccineRecordRow): BabyVaccineRecord {
  return BabyVaccineRecord.restore({
    id: record.id,
    babyId: record.babyId,
    vaccineId: record.vaccineId,
    source: record.source as VaccineRecordSource,
    customName: record.customName,
    customDose: record.customDose,
    status: record.status as VaccineRecordStatus,
    applicationDate: record.applicationDate,
    notes: record.notes,
    batchNumber: record.batchNumber,
    location: record.location,
    professional: record.professional,
    photoUrl: record.photoUrl,
  });
}
 
export class PrismaBabyVaccineRecordRepository implements BabyVaccineRecordRepository {
  constructor(private readonly prisma: PrismaClient) {}
 
  async findById(id: string): Promise<BabyVaccineRecord | null> {
    const record = await this.prisma.babyVaccineRecord.findUnique({ where: { id } });
    return record ? toDomain(record) : null;
  }
 
  async findAllByBabyId(babyId: string, search?: string): Promise<BabyVaccineRecord[]> {
    const records = await this.prisma.babyVaccineRecord.findMany({
      where: {
        babyId,
        ...(search ? { customName: { contains: search, mode: 'insensitive' } } : {}),
      },
    });
    return records.map(toDomain);
  }
 
  async findByBabyAndVaccine(babyId: string, vaccineId: string): Promise<BabyVaccineRecord | null> {
    const record = await this.prisma.babyVaccineRecord.findUnique({
      where: { babyId_vaccineId: { babyId, vaccineId } },
    });
    return record ? toDomain(record) : null;
  }
 
  async save(record: BabyVaccineRecord): Promise<void> {
    Iif (!record.vaccineId) {
      throw new Error('save() requires a catalog record with a vaccineId — use create() for adhoc records');
    }
 
    await this.prisma.babyVaccineRecord.upsert({
      where: { babyId_vaccineId: { babyId: record.babyId, vaccineId: record.vaccineId } },
      create: {
        id: record.id,
        babyId: record.babyId,
        vaccineId: record.vaccineId,
        source: record.source,
        status: record.status,
        applicationDate: record.applicationDate,
        notes: record.notes,
        batchNumber: record.batchNumber,
        location: record.location,
        professional: record.professional,
        photoUrl: record.photoUrl,
      },
      update: {
        status: record.status,
        applicationDate: record.applicationDate,
        notes: record.notes,
        batchNumber: record.batchNumber,
        location: record.location,
        professional: record.professional,
        photoUrl: record.photoUrl,
      },
    });
  }
 
  async create(record: BabyVaccineRecord): Promise<void> {
    await this.prisma.babyVaccineRecord.create({
      data: {
        id: record.id,
        babyId: record.babyId,
        vaccineId: record.vaccineId,
        source: record.source,
        customName: record.customName,
        customDose: record.customDose,
        status: record.status,
        applicationDate: record.applicationDate,
        notes: record.notes,
        batchNumber: record.batchNumber,
        location: record.location,
        professional: record.professional,
        photoUrl: record.photoUrl,
      },
    });
  }
 
  async delete(id: string): Promise<void> {
    await this.prisma.babyVaccineRecord.delete({ where: { id } });
  }
}