backend/src/models/CreateRunner.ts

58 lines
1.9 KiB
TypeScript

import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsPositive, IsString } from 'class-validator';
import { Runner } from '../models/Runner';
import { getConnectionManager, Repository } from 'typeorm';
import { group } from 'console';
import { RunnerOnlyOneGroupAllowedError, RunnerGroupNeededError, RunnerGroupNotFoundError } from '../errors/RunnerErrors';
import { RunnerOrganisation } from './RunnerOrganisation';
import { RunnerTeam } from './RunnerTeam';
import { RunnerGroup } from './RunnerGroup';
import { Address } from 'cluster';
export class CreateRunner {
@IsString()
firstname: string;
@IsString()
middlename?: string;
@IsString()
lastname: string;
@IsString()
phone?: string;
@IsString()
email?: string;
@IsInt()
@IsOptional()
teamId?: number
@IsInt()
@IsOptional()
orgId?: number
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;
}
}