import { Authorized, 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 { 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'; import { RunnerController } from './RunnerController'; @JsonController('/teams') @OpenAPI({ security: [{ "AuthToken": [] }, { "RefreshTokenCookie": [] }] }) export class RunnerTeamController { private runnerTeamRepository: Repository; /** * Gets the repository of this controller's model/entity. */ constructor() { this.runnerTeamRepository = getConnectionManager().get().getRepository(RunnerTeam); } @Get() @Authorized("TEAM:GET") @ResponseSchema(ResponseRunnerTeam, { isArray: true }) @OpenAPI({ description: 'Lists all teams.
This includes their parent organisation and contact (if existing/associated).' }) async getAll() { let responseTeams: ResponseRunnerTeam[] = new Array(); const runners = await this.runnerTeamRepository.find({ relations: ['parentGroup', 'contact'] }); runners.forEach(runner => { responseTeams.push(new ResponseRunnerTeam(runner)); }); return responseTeams; } @Get('/:id') @Authorized("TEAM:GET") @ResponseSchema(ResponseRunnerTeam) @ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 }) @OnUndefined(RunnerTeamNotFoundError) @OpenAPI({ description: 'Lists all information about the team whose id got provided.' }) async getOne(@Param('id') id: number) { let runnerTeam = await this.runnerTeamRepository.findOne({ id: id }, { relations: ['parentGroup', 'contact'] }); if (!runnerTeam) { throw new RunnerTeamNotFoundError(); } return new ResponseRunnerTeam(runnerTeam); } @Post() @Authorized("TEAM:CREATE") @ResponseSchema(ResponseRunnerTeam) @OpenAPI({ description: 'Create a new organsisation.
Please remember to provide it\'s parent group\'s id.' }) async post(@Body({ validate: true }) createRunnerTeam: CreateRunnerTeam) { let runnerTeam; try { runnerTeam = await createRunnerTeam.toRunnerTeam(); } catch (error) { throw error; } runnerTeam = await this.runnerTeamRepository.save(runnerTeam); runnerTeam = await this.runnerTeamRepository.findOne(runnerTeam, { relations: ['parentGroup', 'contact'] }); return new ResponseRunnerTeam(runnerTeam); } @Put('/:id') @Authorized("TEAM:UPDATE") @ResponseSchema(ResponseRunnerTeam) @ResponseSchema(RunnerTeamNotFoundError, { statusCode: 404 }) @ResponseSchema(RunnerTeamIdsNotMatchingError, { statusCode: 406 }) @OpenAPI({ description: "Update the team whose id you provided.
Please remember that id's can't be changed." }) async put(@Param('id') id: number, @Body({ validate: true }) runnerTeam: UpdateRunnerTeam) { let oldRunnerTeam = await this.runnerTeamRepository.findOne({ id: id }, { relations: ['parentGroup', 'contact'] }); if (!oldRunnerTeam) { throw new RunnerTeamNotFoundError(); } if (oldRunnerTeam.id != runnerTeam.id) { throw new RunnerTeamIdsNotMatchingError(); } await this.runnerTeamRepository.save(await runnerTeam.updateRunnerTeam(oldRunnerTeam)); return new ResponseRunnerTeam(await this.runnerTeamRepository.findOne({ id: runnerTeam.id }, { relations: ['parentGroup', 'contact'] })); } @Delete('/:id') @Authorized("TEAM:DELETE") @ResponseSchema(ResponseRunnerTeam) @ResponseSchema(ResponseEmpty, { statusCode: 204 }) @ResponseSchema(RunnerTeamHasRunnersError, { statusCode: 406 }) @OnUndefined(204) @OpenAPI({ description: 'Delete the team whose id you provided.
If the team still has runners associated this will fail.
To delete the team with all associated runners set the force QueryParam to true (cascading deletion might take a while).
If no team with this id exists it will just return 204(no content).' }) async remove(@Param("id") id: number, @QueryParam("force") force: boolean) { let team = await this.runnerTeamRepository.findOne({ id: id }); if (!team) { return null; } let runnerTeam = await this.runnerTeamRepository.findOne(team, { relations: ['parentGroup', 'contact', 'runners'] }); if (!force) { if (runnerTeam.runners.length != 0) { throw new RunnerTeamHasRunnersError(); } } const runnerController = new RunnerController() for (let runner of runnerTeam.runners) { await runnerController.remove(runner.id, true); } const responseTeam = new ResponseRunnerTeam(runnerTeam); await this.runnerTeamRepository.delete(team); return responseTeam; } }