All files / src/domain/baby baby-invite.ts

100% Statements 14/14
83.33% Branches 5/6
100% Functions 6/6
100% Lines 14/14

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 83 84 85 86                                                                    53x 53x 53x 53x 53x 53x 53x 53x 53x       20x                           19x       19x       18x       14x                          
export interface BabyInviteProps {
  id: string;
  babyId: string;
  code: string;
  createdByUserId: string;
  inviteeEmail: string | null;
  expiresAt: Date;
  usedAt: Date | null;
  usedByUserId: string | null;
  createdAt: Date;
}
 
export interface CreateBabyInviteProps {
  id: string;
  babyId: string;
  code: string;
  createdByUserId: string;
  inviteeEmail?: string | null;
  expiresAt: Date;
  createdAt?: Date;
}
 
export class BabyInvite {
  readonly id: string;
  readonly babyId: string;
  readonly code: string;
  readonly createdByUserId: string;
  readonly inviteeEmail: string | null;
  readonly expiresAt: Date;
  readonly usedAt: Date | null;
  readonly usedByUserId: string | null;
  readonly createdAt: Date;
 
  private constructor(props: BabyInviteProps) {
    this.id = props.id;
    this.babyId = props.babyId;
    this.code = props.code;
    this.createdByUserId = props.createdByUserId;
    this.inviteeEmail = props.inviteeEmail;
    this.expiresAt = props.expiresAt;
    this.usedAt = props.usedAt;
    this.usedByUserId = props.usedByUserId;
    this.createdAt = props.createdAt;
  }
 
  static create(props: CreateBabyInviteProps): BabyInvite {
    return new BabyInvite({
      id: props.id,
      babyId: props.babyId,
      code: props.code,
      createdByUserId: props.createdByUserId,
      inviteeEmail: props.inviteeEmail ?? null,
      expiresAt: props.expiresAt,
      usedAt: null,
      usedByUserId: null,
      createdAt: props.createdAt ?? new Date(),
    });
  }
 
  static restore(props: BabyInviteProps): BabyInvite {
    return new BabyInvite(props);
  }
 
  isExpired(referenceDate: Date = new Date()): boolean {
    return this.expiresAt.getTime() < referenceDate.getTime();
  }
 
  isUsed(): boolean {
    return this.usedAt !== null;
  }
 
  redeem(usedByUserId: string, referenceDate: Date = new Date()): BabyInvite {
    return new BabyInvite({
      id: this.id,
      babyId: this.babyId,
      code: this.code,
      createdByUserId: this.createdByUserId,
      inviteeEmail: this.inviteeEmail,
      expiresAt: this.expiresAt,
      usedAt: referenceDate,
      usedByUserId,
      createdAt: this.createdAt,
    });
  }
}