97 lines
2.5 KiB
TypeScript
97 lines
2.5 KiB
TypeScript
import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator';
|
|
import { getConnectionManager } from 'typeorm';
|
|
import { RunnerGroupNeededError, RunnerGroupNotFoundError, RunnerOnlyOneGroupAllowedError } from '../../errors/RunnerErrors';
|
|
import { Runner } from '../entities/Runner';
|
|
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
|
|
import { RunnerTeam } from '../entities/RunnerTeam';
|
|
|
|
export class CreateRunner {
|
|
/**
|
|
* The new runner's first name.
|
|
*/
|
|
@IsString()
|
|
@IsNotEmpty()
|
|
firstname: string;
|
|
|
|
/**
|
|
* The new runner's middle name.
|
|
* Optional.
|
|
*/
|
|
@IsString()
|
|
@IsNotEmpty()
|
|
middlename?: string;
|
|
|
|
/**
|
|
* The new runner's last name.
|
|
*/
|
|
@IsString()
|
|
@IsNotEmpty()
|
|
lastname: string;
|
|
|
|
/**
|
|
* The new runner's phone number.
|
|
* Optional.
|
|
*/
|
|
@IsString()
|
|
@IsOptional()
|
|
@IsPhoneNumber("ZZ")
|
|
phone?: string;
|
|
|
|
/**
|
|
* The new runner's e-mail address.
|
|
* Optional.
|
|
*/
|
|
@IsString()
|
|
@IsOptional()
|
|
@IsEmail()
|
|
email?: string;
|
|
|
|
/**
|
|
* The new runner's team's id.
|
|
* Either provide this or his organisation's id.
|
|
*/
|
|
@IsInt()
|
|
@IsOptional()
|
|
teamId?: number;
|
|
|
|
/**
|
|
* The new runner's organisation's id.
|
|
* Either provide this or his teams's id.
|
|
*/
|
|
@IsInt()
|
|
@IsOptional()
|
|
orgId?: number;
|
|
|
|
/**
|
|
* Creates a Runner entity from this.
|
|
*/
|
|
public async toRunner(): Promise<Runner> {
|
|
let newRunner: Runner = new Runner();
|
|
|
|
if (this.teamId !== undefined && this.orgId !== undefined) {
|
|
throw new RunnerOnlyOneGroupAllowedError();
|
|
}
|
|
if (this.teamId === undefined && this.orgId === undefined) {
|
|
throw new RunnerGroupNeededError();
|
|
}
|
|
|
|
if (this.teamId) {
|
|
newRunner.group = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ id: this.teamId });
|
|
}
|
|
if (this.orgId) {
|
|
newRunner.group = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.orgId });
|
|
}
|
|
if (!newRunner.group) {
|
|
throw new RunnerGroupNotFoundError();
|
|
}
|
|
|
|
newRunner.firstname = this.firstname;
|
|
newRunner.middlename = this.middlename;
|
|
newRunner.lastname = this.lastname;
|
|
newRunner.phone = this.phone;
|
|
newRunner.email = this.email;
|
|
|
|
console.log(newRunner)
|
|
return newRunner;
|
|
}
|
|
} |