58
src/models/creation/CreateRunner.ts
Normal file
58
src/models/creation/CreateRunner.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsPositive, IsString } from 'class-validator';
|
||||
import { Runner } from '../entities/Runner';
|
||||
import { getConnectionManager, Repository } from 'typeorm';
|
||||
import { group } from 'console';
|
||||
import { RunnerOnlyOneGroupAllowedError, RunnerGroupNeededError, RunnerGroupNotFoundError } from '../../errors/RunnerErrors';
|
||||
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
|
||||
import { RunnerTeam } from '../entities/RunnerTeam';
|
||||
import { RunnerGroup } from '../entities/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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user