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 | 22x 22x 22x 18x 18x 1x 17x 18x 1x 16x 1x 15x 15x 1x 14x 14x 14x 14x 14x | import { BabyRepository } from '../baby/baby-repository';
import { BabyGuardianRepository } from '../baby/baby-guardian-repository';
import { BabyInviteRepository } from '../baby/baby-invite-repository';
import { InviteNotFoundError } from './errors/invite-not-found.error';
import { InviteNoLongerValidError } from './errors/invite-no-longer-valid.error';
import { AlreadyGuardianError } from './errors/already-guardian.error';
export interface RedeemInviteInput {
code: string;
requestingUserId: string;
referenceDate?: Date;
}
export interface RedeemInviteResult {
babyId: string;
babyName: string;
}
export class RedeemInviteUseCase {
constructor(
private readonly babyRepository: BabyRepository,
private readonly babyGuardianRepository: BabyGuardianRepository,
private readonly babyInviteRepository: BabyInviteRepository,
) {}
async execute(input: RedeemInviteInput): Promise<RedeemInviteResult> {
const invite = await this.babyInviteRepository.findByCode(input.code);
if (!invite) {
throw new InviteNotFoundError();
}
const referenceDate = input.referenceDate ?? new Date();
if (invite.isExpired(referenceDate)) {
throw new InviteNoLongerValidError('expired');
}
if (invite.isUsed()) {
throw new InviteNoLongerValidError('already-used');
}
const existingGuardian = await this.babyGuardianRepository.findByBabyAndUser(invite.babyId, input.requestingUserId);
if (existingGuardian) {
throw new AlreadyGuardianError();
}
const baby = await this.babyRepository.findById(invite.babyId);
Iif (!baby) {
throw new InviteNotFoundError();
}
await this.babyGuardianRepository.create(invite.babyId, input.requestingUserId, 'GUARDIAN');
await this.babyInviteRepository.save(invite.redeem(input.requestingUserId, referenceDate));
return { babyId: invite.babyId, babyName: baby.name };
}
}
|