@@ -4,7 +4,11 @@ import { getConnectionManager, Repository } from 'typeorm';
|
||||
import { EntityFromBody } from 'typeorm-routing-controllers-extensions';
|
||||
import { RunnerOrganisationHasRunnersError, RunnerOrganisationHasTeamsError, RunnerOrganisationIdsNotMatchingError, RunnerOrganisationNotFoundError } from '../errors/RunnerOrganisationErrors';
|
||||
import { CreateRunnerOrganisation } from '../models/creation/CreateRunnerOrganisation';
|
||||
import { Runner } from '../models/entities/Runner';
|
||||
import { RunnerOrganisation } from '../models/entities/RunnerOrganisation';
|
||||
import { RunnerTeam } from '../models/entities/RunnerTeam';
|
||||
import { RunnerController } from './RunnerController';
|
||||
import { RunnerTeamController } from './RunnerTeamController';
|
||||
|
||||
|
||||
@JsonController('/organisations')
|
||||
@@ -72,25 +76,34 @@ export class RunnerOrganisationController {
|
||||
@Delete('/:id')
|
||||
@ResponseSchema(RunnerOrganisation)
|
||||
@ResponseSchema(RunnerOrganisationNotFoundError, { statusCode: 404 })
|
||||
@ResponseSchema(RunnerOrganisationHasRunnersError, { statusCode: 406 })
|
||||
@ResponseSchema(RunnerOrganisationHasTeamsError, { statusCode: 406 })
|
||||
@OpenAPI({ description: 'Delete a specified runnerOrganisation (if it exists).' })
|
||||
async remove(@Param('id') id: number, @QueryParam("force") force) {
|
||||
async remove(@Param('id') id: number, @QueryParam("force") force: boolean) {
|
||||
let runnerOrganisation = await this.runnerOrganisationRepository.findOne({ id: id });
|
||||
|
||||
if (!runnerOrganisation) {
|
||||
throw new RunnerOrganisationNotFoundError();
|
||||
}
|
||||
|
||||
if (!force) {
|
||||
if (runnerOrganisation.runners.length != 0) {
|
||||
throw new RunnerOrganisationHasRunnersError();
|
||||
}
|
||||
let runners: Runner[] = await runnerOrganisation.getRunners()
|
||||
|
||||
if (runnerOrganisation.teams.length != 0) {
|
||||
throw new RunnerOrganisationHasTeamsError();
|
||||
}
|
||||
if (!force && runners.length != 0) {
|
||||
throw new RunnerOrganisationHasRunnersError();
|
||||
}
|
||||
const runnerController = new RunnerController()
|
||||
await runners.forEach(async runner => {
|
||||
await runnerController.remove(runner.id, true);
|
||||
});
|
||||
|
||||
//TODO: Delete runner and teams
|
||||
let teams: RunnerTeam[] = await runnerOrganisation.getTeams()
|
||||
if (!force && teams.length != 0) {
|
||||
throw new RunnerOrganisationHasTeamsError();
|
||||
}
|
||||
const teamController = new RunnerTeamController()
|
||||
await teams.forEach(async team => {
|
||||
await teamController.remove(team.id, true);
|
||||
});
|
||||
|
||||
await this.runnerOrganisationRepository.delete(runnerOrganisation);
|
||||
return runnerOrganisation;
|
||||
|
||||
Reference in New Issue
Block a user