parent
f7370bc802
commit
e716fae1c5
|
@ -1,12 +1,13 @@
|
|||
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 { DonorIdsNotMatchingError, DonorNotFoundError } from '../errors/DonorErrors';
|
||||
import { DonorHasDonationsError, DonorIdsNotMatchingError, DonorNotFoundError } from '../errors/DonorErrors';
|
||||
import { CreateDonor } from '../models/actions/create/CreateDonor';
|
||||
import { UpdateDonor } from '../models/actions/update/UpdateDonor';
|
||||
import { Donor } from '../models/entities/Donor';
|
||||
import { ResponseDonor } from '../models/responses/ResponseDonor';
|
||||
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
|
||||
import { DonationController } from './DonationController';
|
||||
|
||||
@JsonController('/donors')
|
||||
@OpenAPI({ security: [{ "AuthToken": [] }, { "RefreshTokenCookie": [] }] })
|
||||
|
@ -97,7 +98,14 @@ export class DonorController {
|
|||
throw new DonorNotFoundError();
|
||||
}
|
||||
|
||||
//TODO: DELETE DONATIONS AND WARN FOR FORCE (https://git.odit.services/lfk/backend/issues/66)
|
||||
const donorDonations = (await this.donorRepository.findOne({ id: donor.id }, { relations: ["donations"] })).donations;
|
||||
if (donorDonations.length > 0 && !force) {
|
||||
throw new DonorHasDonationsError();
|
||||
}
|
||||
const donationController = new DonationController();
|
||||
for (let donation of donorDonations) {
|
||||
await donationController.remove(donation.id, force);
|
||||
}
|
||||
|
||||
await this.donorRepository.delete(donor);
|
||||
return new ResponseDonor(responseDonor);
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
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 { RunnerGroupNeededError, RunnerIdsNotMatchingError, RunnerNotFoundError } from '../errors/RunnerErrors';
|
||||
import { RunnerGroupNeededError, RunnerHasDistanceDonationsError, RunnerIdsNotMatchingError, RunnerNotFoundError } from '../errors/RunnerErrors';
|
||||
import { RunnerGroupNotFoundError } from '../errors/RunnerGroupErrors';
|
||||
import { CreateRunner } from '../models/actions/create/CreateRunner';
|
||||
import { UpdateRunner } from '../models/actions/update/UpdateRunner';
|
||||
import { Runner } from '../models/entities/Runner';
|
||||
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
|
||||
import { ResponseRunner } from '../models/responses/ResponseRunner';
|
||||
import { DonationController } from './DonationController';
|
||||
import { RunnerCardController } from './RunnerCardController';
|
||||
import { ScanController } from './ScanController';
|
||||
|
||||
|
@ -91,6 +92,7 @@ export class RunnerController {
|
|||
@Authorized("RUNNER:DELETE")
|
||||
@ResponseSchema(ResponseRunner)
|
||||
@ResponseSchema(ResponseEmpty, { statusCode: 204 })
|
||||
@ResponseSchema(RunnerHasDistanceDonationsError, { statusCode: 406 })
|
||||
@OnUndefined(204)
|
||||
@OpenAPI({ description: 'Delete the runner whose id you provided. <br> This will also delete all scans and cards associated with the runner. <br> If no runner with this id exists it will just return 204(no content).' })
|
||||
async remove(@Param("id") id: number, @QueryParam("force") force: boolean) {
|
||||
|
@ -102,10 +104,19 @@ export class RunnerController {
|
|||
throw new RunnerNotFoundError();
|
||||
}
|
||||
|
||||
const runnerDonations = (await this.runnerRepository.findOne({ id: runner.id }, { relations: ["distanceDonations"] })).distanceDonations;
|
||||
if (runnerDonations.length > 0 && !force) {
|
||||
throw new RunnerHasDistanceDonationsError();
|
||||
}
|
||||
const donationController = new DonationController();
|
||||
for (let donation of runnerDonations) {
|
||||
await donationController.remove(donation.id, force);
|
||||
}
|
||||
|
||||
const runnerCards = (await this.runnerRepository.findOne({ id: runner.id }, { relations: ["cards"] })).cards;
|
||||
const cardController = new RunnerCardController;
|
||||
for (let scan of runnerCards) {
|
||||
await cardController.remove(scan.id, force);
|
||||
for (let card of runnerCards) {
|
||||
await cardController.remove(card.id, force);
|
||||
}
|
||||
|
||||
const runnerScans = (await this.runnerRepository.findOne({ id: runner.id }, { relations: ["scans"] })).scans;
|
||||
|
|
|
@ -34,3 +34,14 @@ export class DonorReceiptAddressNeededError extends NotAcceptableError {
|
|||
@IsString()
|
||||
message = "An address is needed to create a receipt for a donor. \n You didn't provide one."
|
||||
}
|
||||
|
||||
/**
|
||||
* Error to throw when a donor still has donations associated.
|
||||
*/
|
||||
export class DonorHasDonationsError extends NotAcceptableError {
|
||||
@IsString()
|
||||
name = "DonorHasDonationsError"
|
||||
|
||||
@IsString()
|
||||
message = "This donor still has donations associated with it. \n If you want to delete this donor with all it's donations and teams add `?force` to your query."
|
||||
}
|
|
@ -34,3 +34,14 @@ export class RunnerGroupNeededError extends NotAcceptableError {
|
|||
@IsString()
|
||||
message = "Runner's need to be part of one group (team or organisation)! \n You provided neither."
|
||||
}
|
||||
|
||||
/**
|
||||
* Error to throw when a runner still has distance donations associated.
|
||||
*/
|
||||
export class RunnerHasDistanceDonationsError extends NotAcceptableError {
|
||||
@IsString()
|
||||
name = "RunnerHasDistanceDonationsError"
|
||||
|
||||
@IsString()
|
||||
message = "This runner still has distance donations associated with it. \n If you want to delete this runner with all it's donations and teams add `?force` to your query."
|
||||
}
|
Loading…
Reference in New Issue
Block a user