import { IsBoolean, IsOptional } from 'class-validator'; import { DonorReceiptAddressNeededError } from '../../../errors/DonorErrors'; import { Donor } from '../../entities/Donor'; import { CreateParticipant } from './CreateParticipant'; /** * This classed is used to create a new Donor entity from a json body (post request). */ export class CreateDonor extends CreateParticipant { /** * Does this donor need a receipt? */ @IsBoolean() @IsOptional() receiptNeeded?: boolean = false; /** * Creates a new Donor entity from this. */ public async toEntity(): Promise { let newDonor: Donor = new Donor(); newDonor.firstname = this.firstname; newDonor.middlename = this.middlename; newDonor.lastname = this.lastname; newDonor.phone = this.phone; newDonor.email = this.email; newDonor.address = await this.getAddress(); newDonor.receiptNeeded = this.receiptNeeded; if (this.receiptNeeded == true && this.address == null) { throw new DonorReceiptAddressNeededError() } return newDonor; } }