From 9875b4f3926e04b502e7af64c17f54fd3c1d8e3e Mon Sep 17 00:00:00 2001 From: Philipp Dormann Date: Mon, 28 Apr 2025 11:04:22 +0200 Subject: [PATCH] wip --- src/controllers/DonationController.ts | 12 ++++++++ .../actions/create/CreateAnonymousDonation.ts | 29 +++++++++++++++++++ .../actions/create/CreateDistanceDonation.ts | 8 +++++ .../actions/create/CreateFixedDonation.ts | 9 ++++++ 4 files changed, 58 insertions(+) create mode 100644 src/models/actions/create/CreateAnonymousDonation.ts diff --git a/src/controllers/DonationController.ts b/src/controllers/DonationController.ts index 0babc14..4428639 100644 --- a/src/controllers/DonationController.ts +++ b/src/controllers/DonationController.ts @@ -4,6 +4,7 @@ import { Repository, getConnectionManager } from 'typeorm'; import { DonationIdsNotMatchingError, DonationNotFoundError } from '../errors/DonationErrors'; import { DonorNotFoundError } from '../errors/DonorErrors'; import { RunnerNotFoundError } from '../errors/RunnerErrors'; +import { CreateAnonymousDonation } from '../models/actions/create/CreateAnonymousDonation'; import { CreateDistanceDonation } from '../models/actions/create/CreateDistanceDonation'; import { CreateFixedDonation } from '../models/actions/create/CreateFixedDonation'; import { UpdateDistanceDonation } from '../models/actions/update/UpdateDistanceDonation'; @@ -76,6 +77,17 @@ export class DonationController { return (await this.donationRepository.findOne({ id: donation.id }, { relations: ['donor'] })).toResponse(); } + @Post('/anonymous') + @Authorized("DONATION:CREATE") + @ResponseSchema(ResponseDonation) + @ResponseSchema(DonorNotFoundError, { statusCode: 404 }) + @OpenAPI({ description: 'Create a anonymous donation' }) + async postAnonymous(@Body({ validate: true }) createDonation: CreateAnonymousDonation) { + let donation = await createDonation.toEntity(); + donation = await this.fixedDonationRepository.save(donation); + return (await this.donationRepository.findOne({ id: donation.id })).toResponse(); + } + @Post('/distance') @Authorized("DONATION:CREATE") @ResponseSchema(ResponseDistanceDonation) diff --git a/src/models/actions/create/CreateAnonymousDonation.ts b/src/models/actions/create/CreateAnonymousDonation.ts new file mode 100644 index 0000000..2db5d9c --- /dev/null +++ b/src/models/actions/create/CreateAnonymousDonation.ts @@ -0,0 +1,29 @@ +import { IsInt, IsPositive } from 'class-validator'; +import { FixedDonation } from '../../entities/FixedDonation'; +import { CreateDonation } from './CreateDonation'; + +/** + * This class is used to create a new FixedDonation entity from a json body (post request). + */ +export class CreateAnonymousDonation extends CreateDonation { + + /** + * The donation's amount. + * The unit is your currency's smallest unit (default: euro cent). + */ + @IsInt() + @IsPositive() + amount: number; + + /** + * Creates a new FixedDonation entity from this. + */ + public async toEntity(): Promise { + let newDonation = new FixedDonation; + + newDonation.amount = this.amount; + newDonation.paidAmount = this.paidAmount; + + return newDonation; + } +} \ No newline at end of file diff --git a/src/models/actions/create/CreateDistanceDonation.ts b/src/models/actions/create/CreateDistanceDonation.ts index 133e71b..e9920dc 100644 --- a/src/models/actions/create/CreateDistanceDonation.ts +++ b/src/models/actions/create/CreateDistanceDonation.ts @@ -10,6 +10,14 @@ import { CreateDonation } from './CreateDonation'; */ export class CreateDistanceDonation extends CreateDonation { + /** + * The donation's associated donor's id. + * This is important to link donations to donors. + */ + @IsInt() + @IsPositive() + donor: number; + /** * The donation's associated runner's id. * This is important to link the runner's distance ran to the donation. diff --git a/src/models/actions/create/CreateFixedDonation.ts b/src/models/actions/create/CreateFixedDonation.ts index bfdca9f..08c37f3 100644 --- a/src/models/actions/create/CreateFixedDonation.ts +++ b/src/models/actions/create/CreateFixedDonation.ts @@ -6,6 +6,15 @@ import { CreateDonation } from './CreateDonation'; * This class is used to create a new FixedDonation entity from a json body (post request). */ export class CreateFixedDonation extends CreateDonation { + + /** + * The donation's associated donor's id. + * This is important to link donations to donors. + */ + @IsInt() + @IsPositive() + donor: number; + /** * The donation's amount. * The unit is your currency's smallest unit (default: euro cent).