parent
946efef252
commit
6df195b6ec
52
src/models/actions/create/CreateSelfServiceCitizenRunner.ts
Normal file
52
src/models/actions/create/CreateSelfServiceCitizenRunner.ts
Normal file
@ -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<Runner> {
|
||||||
|
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<RunnerOrganisation> {
|
||||||
|
return await getConnection().getRepository(RunnerOrganisation).findOne({ id: 1 });
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user