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 | 3x 3x 2x 1x 29x 29x 29x 10x 8x 8x 1x 7x 1x 6x 10x 4x | import { BabyRepository } from '../baby/baby-repository';
import { BabyGuardianRepository } from '../baby/baby-guardian-repository';
import { ensureBabyAccess } from '../baby/ensure-baby-access';
import { Milestone, MilestoneCategory } from '../../domain/milestone/milestone';
import { InvalidMilestoneTitleError } from '../../domain/milestone/errors/invalid-milestone-title.error';
import { MilestoneRepository } from './milestone-repository';
import { MilestoneNotFoundError } from './errors/milestone-not-found.error';
export interface UpdateMilestoneInput {
babyId: string;
milestoneId: string;
requestingUserId: string;
title?: string;
description?: string | null;
achievedAt?: Date;
category?: MilestoneCategory;
photoUrl?: string | null;
referenceDate?: Date;
}
function normalizeTitle(title: string): string {
const trimmed = title.trim();
if (trimmed.length === 0) {
throw new InvalidMilestoneTitleError();
}
return trimmed;
}
export class UpdateMilestoneUseCase {
constructor(
private readonly babyRepository: BabyRepository,
private readonly babyGuardianRepository: BabyGuardianRepository,
private readonly milestoneRepository: MilestoneRepository,
) {}
async execute(input: UpdateMilestoneInput): Promise<Milestone> {
const baby = await ensureBabyAccess(
this.babyRepository,
this.babyGuardianRepository,
input.babyId,
input.requestingUserId,
);
const existingMilestone = await this.milestoneRepository.findById(input.milestoneId);
if (!existingMilestone || existingMilestone.babyId !== input.babyId) {
throw new MilestoneNotFoundError();
}
if (input.achievedAt) {
Milestone.assertAchievedAtIsValid(input.achievedAt, baby.birthDate, input.referenceDate);
}
const updatedMilestone = Milestone.restore({
id: existingMilestone.id,
babyId: existingMilestone.babyId,
title: input.title ? normalizeTitle(input.title) : existingMilestone.title,
description: input.description !== undefined ? input.description : existingMilestone.description,
achievedAt: input.achievedAt ?? existingMilestone.achievedAt,
category: input.category ?? existingMilestone.category,
photoUrl: input.photoUrl !== undefined ? input.photoUrl : existingMilestone.photoUrl,
createdAt: existingMilestone.createdAt,
});
await this.milestoneRepository.save(updatedMilestone);
return updatedMilestone;
}
}
|