diff --git a/src/controllers/RunnerTeamController.ts b/src/controllers/RunnerTeamController.ts index 3e1212f..c9c4d5c 100644 --- a/src/controllers/RunnerTeamController.ts +++ b/src/controllers/RunnerTeamController.ts @@ -1,9 +1,9 @@ import { Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, QueryParam } from 'routing-controllers'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { getConnectionManager, Repository } from 'typeorm'; -import { EntityFromBody } from 'typeorm-routing-controllers-extensions'; import { RunnerTeamHasRunnersError, RunnerTeamIdsNotMatchingError, RunnerTeamNotFoundError } from '../errors/RunnerTeamErrors'; import { CreateRunnerTeam } from '../models/actions/CreateRunnerTeam'; +import { UpdateRunnerTeam } from '../models/actions/UpdateRunnerTeam'; import { RunnerTeam } from '../models/entities/RunnerTeam'; import { ResponseEmpty } from '../models/responses/ResponseEmpty'; import { ResponseRunnerTeam } from '../models/responses/ResponseRunnerTeam'; @@ -67,7 +67,7 @@ export class RunnerTeamController { @ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 }) @ResponseSchema(RunnerTeamIdsNotMatchingError, { statusCode: 406 }) @OpenAPI({ description: "Update a runnerTeam object (id can't be changed)." }) - async put(@Param('id') id: number, @EntityFromBody() runnerTeam: RunnerTeam) { + async put(@Param('id') id: number, @Body({ validate: true }) runnerTeam: UpdateRunnerTeam) { let oldRunnerTeam = await this.runnerTeamRepository.findOne({ id: id }, { relations: ['parentGroup', 'contact'] }); if (!oldRunnerTeam) { @@ -78,10 +78,9 @@ export class RunnerTeamController { throw new RunnerTeamIdsNotMatchingError(); } - await this.runnerTeamRepository.update(oldRunnerTeam, runnerTeam); + await this.runnerTeamRepository.update(oldRunnerTeam, await runnerTeam.toRunnerTeam()); - runnerTeam = await this.runnerTeamRepository.findOne(runnerTeam, { relations: ['parentGroup', 'contact'] }); - return new ResponseRunnerTeam(runnerTeam); + return new ResponseRunnerTeam(await this.runnerTeamRepository.findOne({ id: runnerTeam.id }, { relations: ['parentGroup', 'contact'] })); } @Delete('/:id') diff --git a/src/models/actions/CreateRunnerGroup.ts b/src/models/actions/CreateRunnerGroup.ts index 288baeb..f237f40 100644 --- a/src/models/actions/CreateRunnerGroup.ts +++ b/src/models/actions/CreateRunnerGroup.ts @@ -23,7 +23,7 @@ export abstract class CreateRunnerGroup { * Get's this group's contact from this.address. */ public async getContact(): Promise { - if (this.contact === undefined) { + if (this.contact === undefined || this.contact === null) { return null; } if (!isNaN(this.contact)) { diff --git a/src/models/actions/UpdateRunnerTeam.ts b/src/models/actions/UpdateRunnerTeam.ts new file mode 100644 index 0000000..0028cf8 --- /dev/null +++ b/src/models/actions/UpdateRunnerTeam.ts @@ -0,0 +1,50 @@ +import { IsInt, IsNotEmpty, IsObject } from 'class-validator'; +import { getConnectionManager } from 'typeorm'; +import { RunnerOrganisationNotFoundError, RunnerOrganisationWrongTypeError } from '../../errors/RunnerOrganisationErrors'; +import { RunnerTeamNeedsParentError } from '../../errors/RunnerTeamErrors'; +import { RunnerOrganisation } from '../entities/RunnerOrganisation'; +import { RunnerTeam } from '../entities/RunnerTeam'; +import { CreateRunnerGroup } from './CreateRunnerGroup'; + +export class UpdateRunnerTeam extends CreateRunnerGroup { + + /** + * The updated team's id. + */ + @IsInt() + id: number; + + /** + * The team's parent group (organisation). + */ + @IsObject() + @IsNotEmpty() + parentGroup: RunnerOrganisation; + + public async getParent(): Promise { + if (this.parentGroup === undefined) { + throw new RunnerTeamNeedsParentError(); + } + if (!isNaN(this.parentGroup.id)) { + let parentGroup = await getConnectionManager().get().getRepository(RunnerOrganisation).findOne({ id: this.parentGroup.id }); + if (!parentGroup) { throw new RunnerOrganisationNotFoundError();; } + return parentGroup; + } + + throw new RunnerOrganisationWrongTypeError; + } + + /** + * Creates a RunnerTeam entity from this. + */ + public async toRunnerTeam(): Promise { + let newRunnerTeam: RunnerTeam = new RunnerTeam(); + + newRunnerTeam.id = this.id; + newRunnerTeam.name = this.name; + newRunnerTeam.parentGroup = await this.getParent(); + newRunnerTeam.contact = await this.getContact() + + return newRunnerTeam; + } +} \ No newline at end of file