52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
TypeScript
import { IsInt, IsPositive } from 'class-validator';
|
|
import { getConnection } from 'typeorm';
|
|
import { RunnerNotFoundError } from '../../../errors/RunnerErrors';
|
|
import { DistanceDonation } from '../../entities/DistanceDonation';
|
|
import { Runner } from '../../entities/Runner';
|
|
import { UpdateDonation } from './UpdateDonation';
|
|
|
|
/**
|
|
* This class is used to update a DistanceDonation entity (via put request).
|
|
*/
|
|
export class UpdateDistanceDonation extends UpdateDonation {
|
|
|
|
/**
|
|
* The donation's associated runner's id.
|
|
* This is important to link the runner's distance ran to the donation.
|
|
*/
|
|
@IsInt()
|
|
@IsPositive()
|
|
runner: number;
|
|
|
|
/**
|
|
* The donation's amount per distance (full kilometer aka 1000 meters).
|
|
* The unit is your currency's smallest unit (default: euro cent).
|
|
*/
|
|
@IsInt()
|
|
@IsPositive()
|
|
amountPerDistance: number;
|
|
|
|
/**
|
|
* Update a DistanceDonation entity based on this.
|
|
* @param donation The donation that shall be updated.
|
|
*/
|
|
public async update(donation: DistanceDonation): Promise<DistanceDonation> {
|
|
donation.amountPerDistance = this.amountPerDistance;
|
|
donation.paidAmount = this.paidAmount;
|
|
donation.donor = await this.getDonor();
|
|
donation.runner = await this.getRunner();
|
|
|
|
return donation;
|
|
}
|
|
|
|
/**
|
|
* Gets a runner based on the runner id provided via this.runner.
|
|
*/
|
|
public async getRunner(): Promise<Runner> {
|
|
const runner = await getConnection().getRepository(Runner).findOne({ id: this.runner });
|
|
if (!runner) {
|
|
throw new RunnerNotFoundError();
|
|
}
|
|
return runner;
|
|
}
|
|
} |