Added donor donation amount to the donor response
continuous-integration/drone/pr Build is failing
Details
continuous-integration/drone/pr Build is failing
Details
ref #66
This commit is contained in:
parent
cd5e4bbd60
commit
bba35d189e
|
@ -27,7 +27,7 @@ export class DonorController {
|
|||
@OpenAPI({ description: 'Lists all runners from all teams/orgs. <br> This includes the runner\'s group and distance ran.' })
|
||||
async getAll() {
|
||||
let responseDonors: ResponseDonor[] = new Array<ResponseDonor>();
|
||||
const donors = await this.donorRepository.find();
|
||||
const donors = await this.donorRepository.find({ relations: ['donations', 'donations.runner', 'donations.runner.scans', 'donations.runner.scans.track'] });
|
||||
donors.forEach(donor => {
|
||||
responseDonors.push(new ResponseDonor(donor));
|
||||
});
|
||||
|
@ -41,7 +41,7 @@ export class DonorController {
|
|||
@OnUndefined(DonorNotFoundError)
|
||||
@OpenAPI({ description: 'Lists all information about the runner whose id got provided.' })
|
||||
async getOne(@Param('id') id: number) {
|
||||
let donor = await this.donorRepository.findOne({ id: id })
|
||||
let donor = await this.donorRepository.findOne({ id: id }, { relations: ['donations', 'donations.runner', 'donations.runner.scans', 'donations.runner.scans.track'] })
|
||||
if (!donor) { throw new DonorNotFoundError(); }
|
||||
return new ResponseDonor(donor);
|
||||
}
|
||||
|
@ -59,7 +59,7 @@ export class DonorController {
|
|||
}
|
||||
|
||||
donor = await this.donorRepository.save(donor)
|
||||
return new ResponseDonor(await this.donorRepository.findOne(donor));
|
||||
return new ResponseDonor(await this.donorRepository.findOne(donor, { relations: ['donations', 'donations.runner', 'donations.runner.scans', 'donations.runner.scans.track'] }));
|
||||
}
|
||||
|
||||
@Put('/:id')
|
||||
|
@ -80,7 +80,7 @@ export class DonorController {
|
|||
}
|
||||
|
||||
await this.donorRepository.save(await donor.update(oldDonor));
|
||||
return new ResponseDonor(await this.donorRepository.findOne({ id: id }));
|
||||
return new ResponseDonor(await this.donorRepository.findOne({ id: id }, { relations: ['donations', 'donations.runner', 'donations.runner.scans', 'donations.runner.scans.track'] }));
|
||||
}
|
||||
|
||||
@Delete('/:id')
|
||||
|
@ -92,7 +92,7 @@ export class DonorController {
|
|||
async remove(@Param("id") id: number, @QueryParam("force") force: boolean) {
|
||||
let donor = await this.donorRepository.findOne({ id: id });
|
||||
if (!donor) { return null; }
|
||||
const responseDonor = await this.donorRepository.findOne(donor);
|
||||
const responseDonor = await this.donorRepository.findOne(donor, { relations: ['donations', 'donations.runner', 'donations.runner.scans', 'donations.runner.scans.track'] });
|
||||
|
||||
if (!donor) {
|
||||
throw new DonorNotFoundError();
|
||||
|
|
|
@ -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.
|
||||
*/
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -186,6 +186,7 @@ describe('adding + updating fixed donation valid', () => {
|
|||
"donor": added_donor.id,
|
||||
"amount": 1000
|
||||
}, axios_config);
|
||||
delete res.data.donor.donationAmount;
|
||||
added_donation = res.data;
|
||||
expect(res.status).toEqual(200);
|
||||
expect(res.headers['content-type']).toContain("application/json")
|
||||
|
@ -236,6 +237,7 @@ describe('adding + updating distance donation valid', () => {
|
|||
"firstname": "first",
|
||||
"lastname": "last"
|
||||
}, axios_config);
|
||||
delete res.data.donationAmount;
|
||||
added_donor = res.data
|
||||
expect(res.status).toEqual(200);
|
||||
expect(res.headers['content-type']).toContain("application/json")
|
||||
|
@ -245,6 +247,7 @@ describe('adding + updating distance donation valid', () => {
|
|||
"firstname": "first",
|
||||
"lastname": "last"
|
||||
}, axios_config);
|
||||
delete res.data.donationAmount;
|
||||
added_donor2 = res.data
|
||||
expect(res.status).toEqual(200);
|
||||
expect(res.headers['content-type']).toContain("application/json")
|
||||
|
|
Loading…
Reference in New Issue