backend/src/models/entities/Donor.ts

51 lines
1.4 KiB
TypeScript

import { IsBoolean, IsInt } from "class-validator";
import { ChildEntity, Column, OneToMany } from "typeorm";
import { ResponseDonor } from '../responses/ResponseDonor';
import { Donation } from './Donation';
import { Participant } from "./Participant";
/**
* Defines the Donor entity.
*/
@ChildEntity()
export class Donor extends Participant {
/**
* Does this donor need a receipt?
* Will later be used to automaticly generate donation receipts.
*/
@Column()
@IsBoolean()
receiptNeeded: boolean = false;
/**
* Used to link the participant as the donor of a donation.
* Attention: Only runner's can be associated as a distanceDonations distance source.
*/
@OneToMany(() => Donation, donation => donation.donor, { nullable: true })
donations: Donation[];
/**
* Returns the total donations of a donor based on his linked donations.
*/
@IsInt()
public get donationAmount(): number {
if (!this.donations) { return 0; }
return this.donations.reduce((sum, current) => sum + current.amount, 0);
}
/**
* Returns the total paid donations of a donor based on his linked donations.
*/
@IsInt()
public get paidDonationAmount(): number {
if (!this.donations) { return 0; }
return this.donations.reduce((sum, current) => sum + current.paidAmount, 0);
}
/**
* Turns this entity into it's response class.
*/
public toResponse(): ResponseDonor {
return new ResponseDonor(this);
}
}