All files / src/application/invite get-invite-by-code.use-case.ts

88.88% Statements 8/9
80% Branches 4/5
100% Functions 2/2
88.88% Lines 8/9

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                          22x 22x       3x   3x 1x     2x   2x       2x                
import { BabyRepository } from '../baby/baby-repository';
import { BabyInviteRepository } from '../baby/baby-invite-repository';
import { InviteNotFoundError } from './errors/invite-not-found.error';
 
export interface InvitePreview {
  babyName: string;
  babyAvatarUrl: string | null;
  expired: boolean;
  alreadyUsed: boolean;
}
 
export class GetInviteByCodeUseCase {
  constructor(
    private readonly babyRepository: BabyRepository,
    private readonly babyInviteRepository: BabyInviteRepository,
  ) {}
 
  async execute(code: string, referenceDate: Date = new Date()): Promise<InvitePreview> {
    const invite = await this.babyInviteRepository.findByCode(code);
 
    if (!invite) {
      throw new InviteNotFoundError();
    }
 
    const baby = await this.babyRepository.findById(invite.babyId);
 
    Iif (!baby) {
      throw new InviteNotFoundError();
    }
 
    return {
      babyName: baby.name,
      babyAvatarUrl: baby.avatarUrl,
      expired: invite.isExpired(referenceDate),
      alreadyUsed: invite.isUsed(),
    };
  }
}