59
src/models/actions/UpdateRunner.ts
Normal file
59
src/models/actions/UpdateRunner.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { IsInt, IsObject } from 'class-validator';
|
||||
import { getConnectionManager } from 'typeorm';
|
||||
import { RunnerGroupNotFoundError } from '../../errors/RunnerGroupErrors';
|
||||
import { RunnerOrganisationWrongTypeError } from '../../errors/RunnerOrganisationErrors';
|
||||
import { RunnerTeamNeedsParentError } from '../../errors/RunnerTeamErrors';
|
||||
import { Runner } from '../entities/Runner';
|
||||
import { RunnerGroup } from '../entities/RunnerGroup';
|
||||
import { CreateParticipant } from './CreateParticipant';
|
||||
|
||||
export class UpdateRunner extends CreateParticipant {
|
||||
|
||||
/**
|
||||
* The new runner's team's id.
|
||||
* Either provide this or his organisation's id.
|
||||
*/
|
||||
@IsInt()
|
||||
id: number;
|
||||
|
||||
/**
|
||||
* The new runner's team's id.
|
||||
* Either provide this or his organisation's id.
|
||||
*/
|
||||
@IsObject()
|
||||
group: RunnerGroup;
|
||||
|
||||
/**
|
||||
* Creates a Runner entity from this.
|
||||
*/
|
||||
public async toRunner(): Promise<Runner> {
|
||||
let newRunner: Runner = new Runner();
|
||||
|
||||
newRunner.id = this.id;
|
||||
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 = await this.getAddress();
|
||||
|
||||
return newRunner;
|
||||
}
|
||||
|
||||
/**
|
||||
* Manages all the different ways a group can be provided.
|
||||
*/
|
||||
public async getGroup(): Promise<RunnerGroup> {
|
||||
if (this.group === undefined) {
|
||||
throw new RunnerTeamNeedsParentError();
|
||||
}
|
||||
if (!isNaN(this.group.id)) {
|
||||
let group = await getConnectionManager().get().getRepository(RunnerGroup).findOne({ id: this.group.id });
|
||||
if (!group) { throw new RunnerGroupNotFoundError; }
|
||||
return group;
|
||||
}
|
||||
|
||||
throw new RunnerOrganisationWrongTypeError;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user