Added card deletion + errors

ref #77
This commit is contained in:
Nicolai Ort 2021-01-09 11:28:59 +01:00
parent 4faeddc3f3
commit a5bfe4e3d5
2 changed files with 37 additions and 23 deletions

View File

@ -1,9 +1,11 @@
import { Authorized, Get, JsonController, OnUndefined, Param } from 'routing-controllers';
import { Authorized, Delete, Get, JsonController, OnUndefined, Param, QueryParam } from 'routing-controllers';
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm';
import { RunnerCardNotFoundError } from '../errors/RunnerCardErrors';
import { RunnerCardHasScansError, RunnerCardNotFoundError } from '../errors/RunnerCardErrors';
import { RunnerCard } from '../models/entities/RunnerCard';
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
import { ResponseRunnerCard } from '../models/responses/ResponseRunnerCard';
import { ScanController } from './ScanController';
@JsonController('/cards')
@OpenAPI({ security: [{ "AuthToken": [] }, { "RefreshTokenCookie": [] }] })
@ -76,26 +78,27 @@ export class RunnerCardController {
// return new ResponseTrack(await this.trackRepository.findOne({ id: id }));
// }
// @Delete('/:id')
// @Authorized("CARD:DELETE")
// @ResponseSchema(ResponseTrack)
// @ResponseSchema(ResponseEmpty, { statusCode: 204 })
// @OnUndefined(204)
// @OpenAPI({ description: "Delete the track whose id you provided. <br> If no track with this id exists it will just return 204(no content)." })
// async remove(@Param("id") id: number, @QueryParam("force") force: boolean) {
// let track = await this.trackRepository.findOne({ id: id });
// if (!track) { return null; }
@Delete('/:id')
@Authorized("CARD:DELETE")
@ResponseSchema(ResponseRunnerCard)
@ResponseSchema(ResponseEmpty, { statusCode: 204 })
@ResponseSchema(RunnerCardHasScansError, { statusCode: 406 })
@OnUndefined(204)
@OpenAPI({ description: "Delete the card whose id you provided. <br> If no card with this id exists it will just return 204(no content). <br> If the card still has scans associated you have to provide the force=true query param (warning: this deletes all scans associated with by this card - please disable it instead or just remove the runner association)." })
async remove(@Param("id") id: number, @QueryParam("force") force: boolean) {
let card = await this.cardRepository.findOne({ id: id });
if (!card) { return null; }
// const trackStations = (await this.trackRepository.findOne({ id: id }, { relations: ["stations"] })).stations;
// if (trackStations.length != 0 && !force) {
// throw new TrackHasScanStationsError();
// }
// const scanController = new ScanStationController;
// for (let station of trackStations) {
// scanController.remove(station.id, force);
// }
const cardScans = (await this.cardRepository.findOne({ id: id }, { relations: ["scans"] })).scans;
if (cardScans.length != 0 && !force) {
throw new RunnerCardHasScansError();
}
const scanController = new ScanController;
for (let scan of cardScans) {
scanController.remove(scan.id, force);
}
// await this.trackRepository.delete(track);
// return new ResponseTrack(track);
// }
await this.cardRepository.delete(card);
return card.toResponse();
}
}

View File

@ -22,4 +22,15 @@ export class RunnerCardIdsNotMatchingError extends NotAcceptableError {
@IsString()
message = "The ids don't match! \n And if you wanted to change a cards's id: This isn't allowed"
}
}
/**
* Error to throw when a station still has scans associated.
*/
export class RunnerCardHasScansError extends NotAcceptableError {
@IsString()
name = "RunnerCardHasScansError"
@IsString()
message = "This card still has scans associated with it. \n If you want to delete this card with all it's scans add `?force` to your query. \n Otherwise please consider just diableing it."
}