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

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

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                                        213x                                 68x     6x       6x       10x 10x      
import { PrismaClient } from '../../../generated/prisma/client';
import { VaccineRepository } from '../../../application/vaccine/vaccine-repository';
import { Vaccine } from '../../../domain/vaccine/vaccine';
 
interface VaccineRecord {
  id: string;
  code: string;
  name: string;
  description: string;
  guidance: string | null;
  recommendedAgeInMonths: number;
  doseNumber: number;
  recommendationKind: 'ROUTINE' | 'CONDITIONAL' | 'RECURRING';
  scheduleVersion: string;
  effectiveFrom: Date;
  effectiveTo: Date | null;
  isActive: boolean;
}
 
function toDomain(record: VaccineRecord): Vaccine {
  return Vaccine.create({
    id: record.id,
    code: record.code,
    name: record.name,
    description: record.description,
    guidance: record.guidance,
    recommendedAgeInMonths: record.recommendedAgeInMonths,
    doseNumber: record.doseNumber,
    recommendationKind: record.recommendationKind,
    scheduleVersion: record.scheduleVersion,
    effectiveFrom: record.effectiveFrom,
    effectiveTo: record.effectiveTo,
    isActive: record.isActive,
  });
}
 
export class PrismaVaccineRepository implements VaccineRepository {
  constructor(private readonly prisma: PrismaClient) {}
 
  async findAll(): Promise<Vaccine[]> {
    const records = await this.prisma.vaccine.findMany({
      where: { isActive: true },
      orderBy: [{ recommendedAgeInMonths: 'asc' }, { doseNumber: 'asc' }],
    });
    return records.map(toDomain);
  }
 
  async findById(id: string): Promise<Vaccine | null> {
    const record = await this.prisma.vaccine.findFirst({ where: { id, isActive: true } });
    return record ? toDomain(record) : null;
  }
}