backend/src/models/actions/UpdateRunner.ts

54 lines
1.7 KiB
TypeScript

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 updated runner's id.
*/
@IsInt()
id: number;
/**
* The updated runner's new team/org.
*/
@IsObject()
group: RunnerGroup;
/**
* Creates a Runner entity from this.
*/
public async updateRunner(runner: Runner): Promise<Runner> {
runner.firstname = this.firstname;
runner.middlename = this.middlename;
runner.lastname = this.lastname;
runner.phone = this.phone;
runner.email = this.email;
runner.group = await this.getGroup();
runner.address = await this.getAddress();
return runner;
}
/**
* 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;
}
}