From 6df195b6ec5fde27f84cbe54992558715b843b87 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Thu, 21 Jan 2021 17:18:25 +0100 Subject: [PATCH] Created a citizenrunner selfservice create action ref #112 --- .../create/CreateSelfServiceCitizenRunner.ts | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/models/actions/create/CreateSelfServiceCitizenRunner.ts diff --git a/src/models/actions/create/CreateSelfServiceCitizenRunner.ts b/src/models/actions/create/CreateSelfServiceCitizenRunner.ts new file mode 100644 index 0000000..1b9514b --- /dev/null +++ b/src/models/actions/create/CreateSelfServiceCitizenRunner.ts @@ -0,0 +1,52 @@ +import { IsEmail, IsNotEmpty, IsString } from 'class-validator'; +import { getConnection } from 'typeorm'; +import { RunnerEmailNeededError } from '../../../errors/RunnerErrors'; +import { Address } from '../../entities/Address'; +import { Runner } from '../../entities/Runner'; +import { RunnerOrganisation } from '../../entities/RunnerOrganisation'; +import { CreateParticipant } from './CreateParticipant'; + +/** + * This classed is used to create a new Runner entity from a json body (post request). + */ +export class CreateSelfServiceCitizenRunner extends CreateParticipant { + + /** + * The new runners's e-mail address. + * Must be provided for email-verification to work. + */ + @IsString() + @IsNotEmpty() + @IsEmail() + email: string; + + /** + * Creates a new Runner entity from this. + */ + public async toEntity(): Promise { + let newRunner: Runner = new Runner(); + + newRunner.firstname = this.firstname; + newRunner.middlename = this.middlename; + newRunner.lastname = this.lastname; + newRunner.phone = this.phone; + newRunner.email = this.email; + + if (!newRunner.email) { + throw new RunnerEmailNeededError(); + } + + newRunner.group = await this.getGroup(); + newRunner.address = this.address; + Address.validate(newRunner.address); + + return newRunner; + } + + /** + * Gets the new runner's group by it's id. + */ + public async getGroup(): Promise { + return await getConnection().getRepository(RunnerOrganisation).findOne({ id: 1 }); + } +} \ No newline at end of file