36
src/models/creation/CreateRunnerTeam.ts
Normal file
36
src/models/creation/CreateRunnerTeam.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { IsInt, IsNotEmpty, IsString } from 'class-validator';
|
||||
import { getConnectionManager } from 'typeorm';
|
||||
import { RunnerOrganisationNotFoundError } from '../../errors/RunnerOrganisationErrors';
|
||||
import { RunnerOrganisation } from '../entities/RunnerOrganisation';
|
||||
import { RunnerTeam } from '../entities/RunnerTeam';
|
||||
|
||||
export class CreateRunnerTeam {
|
||||
/**
|
||||
* The teams's name.
|
||||
*/
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* The team's parent group (organisation).
|
||||
*/
|
||||
@IsInt()
|
||||
@IsNotEmpty()
|
||||
parentId: number
|
||||
|
||||
/**
|
||||
* Creates a RunnerTeam entity from this.
|
||||
*/
|
||||
public async toRunnerTeam(): Promise<RunnerTeam> {
|
||||
let newRunnerTeam: RunnerTeam = new RunnerTeam();
|
||||
|
||||
newRunnerTeam.name = this.name;
|
||||
newRunnerTeam.parentGroup = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.parentId });
|
||||
if (!newRunnerTeam.parentGroup) {
|
||||
throw new RunnerOrganisationNotFoundError();
|
||||
}
|
||||
|
||||
return newRunnerTeam;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Entity, Column, ManyToOne, ChildEntity } from "typeorm";
|
||||
import { IsNotEmpty } from "class-validator";
|
||||
import { ChildEntity, getConnectionManager, ManyToOne } from "typeorm";
|
||||
import { Runner } from './Runner';
|
||||
import { RunnerGroup } from "./RunnerGroup";
|
||||
import { RunnerOrganisation } from "./RunnerOrganisation";
|
||||
|
||||
@@ -16,4 +17,12 @@ export class RunnerTeam extends RunnerGroup {
|
||||
@IsNotEmpty()
|
||||
@ManyToOne(() => RunnerOrganisation, org => org.teams, { nullable: true })
|
||||
parentGroup?: RunnerOrganisation;
|
||||
|
||||
/**
|
||||
* Returns all runners associated with this team.
|
||||
*/
|
||||
public async getRunners() {
|
||||
let runnerRepo = await getConnectionManager().get().getRepository(Runner);
|
||||
return await runnerRepo.find({ group: this });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user