Added donor donation amount to the donor response
Some checks failed
continuous-integration/drone/pr Build is failing

ref #66
This commit is contained in:
2021-01-13 17:32:10 +01:00
parent cd5e4bbd60
commit bba35d189e
4 changed files with 26 additions and 7 deletions

View File

@@ -1,4 +1,4 @@
import { IsBoolean } from "class-validator";
import { IsBoolean, IsInt } from "class-validator";
import { ChildEntity, Column, OneToMany } from "typeorm";
import { ResponseDonor } from '../responses/ResponseDonor';
import { Donation } from './Donation';
@@ -24,6 +24,15 @@ export class Donor extends Participant {
@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);
}
/**
* Turns this entity into it's response class.
*/

View File

@@ -1,5 +1,5 @@
import {
IsBoolean
IsBoolean, IsInt
} from "class-validator";
import { Donor } from '../entities/Donor';
import { ResponseParticipant } from './ResponseParticipant';
@@ -15,6 +15,12 @@ export class ResponseDonor extends ResponseParticipant {
@IsBoolean()
receiptNeeded: boolean;
/**
* Returns the total donations of a donor based on his linked donations.
*/
@IsInt()
donationAmount: number;
/**
* Creates a ResponseRunner object from a runner.
* @param runner The user the response shall be build for.
@@ -22,5 +28,6 @@ export class ResponseDonor extends ResponseParticipant {
public constructor(donor: Donor) {
super(donor);
this.receiptNeeded = donor.receiptNeeded;
this.donationAmount = donor.donationAmount;
}
}