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

100% Statements 11/11
100% Branches 3/3
100% Functions 7/7
100% Lines 11/11

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                          339x                   155x     213x     213x       13x 13x       16x 16x       129x     129x       2x          
import { PrismaClient } from '../../../generated/prisma/client';
import { BabyGuardianRepository } from '../../../application/baby/baby-guardian-repository';
import { BabyGuardian, GuardianRole } from '../../../domain/baby/baby-guardian';
 
interface BabyGuardianRow {
  id: string;
  babyId: string;
  userId: string;
  role: string;
  createdAt: Date;
}
 
function toDomain(row: BabyGuardianRow): BabyGuardian {
  return BabyGuardian.restore({
    id: row.id,
    babyId: row.babyId,
    userId: row.userId,
    role: row.role as GuardianRole,
    createdAt: row.createdAt,
  });
}
 
export class PrismaBabyGuardianRepository implements BabyGuardianRepository {
  constructor(private readonly prisma: PrismaClient) {}
 
  async findByBabyAndUser(babyId: string, userId: string): Promise<BabyGuardian | null> {
    const row = await this.prisma.babyGuardian.findUnique({
      where: { babyId_userId: { babyId, userId } },
    });
    return row ? toDomain(row) : null;
  }
 
  async findAllByBaby(babyId: string): Promise<BabyGuardian[]> {
    const rows = await this.prisma.babyGuardian.findMany({ where: { babyId }, orderBy: { createdAt: 'asc' } });
    return rows.map(toDomain);
  }
 
  async findAllByUser(userId: string): Promise<BabyGuardian[]> {
    const rows = await this.prisma.babyGuardian.findMany({ where: { userId }, orderBy: { createdAt: 'asc' } });
    return rows.map(toDomain);
  }
 
  async create(babyId: string, userId: string, role: GuardianRole = 'GUARDIAN'): Promise<BabyGuardian> {
    const row = await this.prisma.babyGuardian.create({
      data: { babyId, userId, role },
    });
    return toDomain(row);
  }
 
  async delete(babyId: string, userId: string): Promise<void> {
    await this.prisma.babyGuardian.delete({
      where: { babyId_userId: { babyId, userId } },
    });
  }
}