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