diff --git a/src/controllers/RunnerController.ts b/src/controllers/RunnerController.ts index 5b38bf6..ea5a70d 100644 --- a/src/controllers/RunnerController.ts +++ b/src/controllers/RunnerController.ts @@ -16,7 +16,10 @@ import { EntityFromBody } from 'typeorm-routing-controllers-extensions'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { Runner } from '../models/Runner'; import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsPositive, IsString } from 'class-validator'; -import { RunnerIdsNotMatchingError, RunnerNotFoundError } from '../errors/RunnerErrors'; +import { RunnerGroupNeededError, RunnerGroupNotFoundError, RunnerIdsNotMatchingError, RunnerNotFoundError, RunnerOnlyOneGroupAllowedError } from '../errors/RunnerErrors'; +import { RunnerGroup } from '../models/RunnerGroup'; +import { RunnerOrganisation } from '../models/RunnerOrganisation'; +import { RunnerTeam } from '../models/RunnerTeam'; class CreateRunner { /** @@ -59,7 +62,11 @@ class CreateRunner { @IsInt() @IsOptional() - groupId?: number; + teamId?: number; + + @IsInt() + @IsOptional() + orgId?: number; } @JsonController('/runners') @@ -93,10 +100,35 @@ export class RunnerController { @Post() @ResponseSchema(Runner) @OpenAPI({ description: 'Create a new runner object (id will be generated automagicly).' }) - post( + async post( @Body({ validate: true }) - runner: CreateRunner + createRunner: CreateRunner ) { + let group: RunnerGroup; + + if(createRunner.teamId && createRunner.orgId){ + throw new RunnerOnlyOneGroupAllowedError(); + } + if(!createRunner.teamId && !createRunner.orgId){ + throw new RunnerGroupNeededError(); + } + + if(createRunner.teamId){ + group = await getConnectionManager().get().getRepository(RunnerTeam).findOne({ id: createRunner.teamId }); + } + if(createRunner.orgId){ + group = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: createRunner.orgId }); + } + if(!group){ + throw new RunnerGroupNotFoundError(); + } + + delete createRunner.teamId; + delete createRunner.orgId; + let runner = createRunner; + runner.group=group; + console.log(runner) + return this.runnerRepository.save(runner); }