55 lines
1.8 KiB
TypeScript
55 lines
1.8 KiB
TypeScript
import { IsInt } from 'class-validator';
|
|
import { getConnectionManager } from 'typeorm';
|
|
import { RunnerGroupNotFoundError } from '../../../errors/RunnerGroupErrors';
|
|
import { RunnerOrganizationWrongTypeError } from '../../../errors/RunnerOrganizationErrors';
|
|
import { RunnerTeamNeedsParentError } from '../../../errors/RunnerTeamErrors';
|
|
import { Address } from '../../entities/Address';
|
|
import { Runner } from '../../entities/Runner';
|
|
import { RunnerGroup } from '../../entities/RunnerGroup';
|
|
import { CreateParticipant } from './CreateParticipant';
|
|
|
|
/**
|
|
* This classed is used to create a new Runner entity from a json body (post request).
|
|
*/
|
|
export class CreateRunner extends CreateParticipant {
|
|
|
|
/**
|
|
* The new runner's group's id.
|
|
*/
|
|
@IsInt()
|
|
group: number;
|
|
|
|
/**
|
|
* 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;
|
|
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<RunnerGroup> {
|
|
if (this.group === undefined || this.group === null) {
|
|
throw new RunnerTeamNeedsParentError();
|
|
}
|
|
if (!isNaN(this.group)) {
|
|
let group = await getConnectionManager().get().getRepository(RunnerGroup).findOne({ id: this.group });
|
|
if (!group) { throw new RunnerGroupNotFoundError; }
|
|
return group;
|
|
}
|
|
|
|
throw new RunnerOrganizationWrongTypeError;
|
|
}
|
|
} |