All files / src/infrastructure/database/repositories prisma-milestone.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                                17x                         44x     6x 6x       9x                           9x       18x                                             1x      
import { PrismaClient } from '../../../generated/prisma/client';
import { MilestoneRepository } from '../../../application/milestone/milestone-repository';
import { Milestone, MilestoneCategory } from '../../../domain/milestone/milestone';
 
interface MilestoneRow {
  id: string;
  babyId: string;
  title: string;
  description: string | null;
  achievedAt: Date;
  category: string;
  photoUrl: string | null;
  createdAt: Date;
}
 
function toDomain(row: MilestoneRow): Milestone {
  return Milestone.restore({
    id: row.id,
    babyId: row.babyId,
    title: row.title,
    description: row.description,
    achievedAt: row.achievedAt,
    category: row.category as MilestoneCategory,
    photoUrl: row.photoUrl,
    createdAt: row.createdAt,
  });
}
 
export class PrismaMilestoneRepository implements MilestoneRepository {
  constructor(private readonly prisma: PrismaClient) {}
 
  async findById(id: string): Promise<Milestone | null> {
    const row = await this.prisma.milestone.findUnique({ where: { id } });
    return row ? toDomain(row) : null;
  }
 
  async findAllByBabyId(babyId: string, search?: string): Promise<Milestone[]> {
    const rows = await this.prisma.milestone.findMany({
      where: {
        babyId,
        ...(search
          ? {
              OR: [
                { title: { contains: search, mode: 'insensitive' } },
                { description: { contains: search, mode: 'insensitive' } },
              ],
            }
          : {}),
      },
      orderBy: { achievedAt: 'asc' },
    });
    return rows.map(toDomain);
  }
 
  async save(milestone: Milestone): Promise<void> {
    await this.prisma.milestone.upsert({
      where: { id: milestone.id },
      create: {
        id: milestone.id,
        babyId: milestone.babyId,
        title: milestone.title,
        description: milestone.description,
        achievedAt: milestone.achievedAt,
        category: milestone.category,
        photoUrl: milestone.photoUrl,
        createdAt: milestone.createdAt,
      },
      update: {
        title: milestone.title,
        description: milestone.description,
        achievedAt: milestone.achievedAt,
        category: milestone.category,
        photoUrl: milestone.photoUrl,
      },
    });
  }
 
  async delete(id: string): Promise<void> {
    await this.prisma.milestone.delete({ where: { id } });
  }
}