From 0379786cbda057ad95d709fa135d34beb0db8de1 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Tue, 19 Jan 2021 16:09:23 +0100 Subject: [PATCH] Implemented contact deletion ref #104 --- src/controllers/ContactController.ts | 44 ++++++++++------------------ 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/src/controllers/ContactController.ts b/src/controllers/ContactController.ts index f9fc752..1a20d82 100644 --- a/src/controllers/ContactController.ts +++ b/src/controllers/ContactController.ts @@ -1,8 +1,9 @@ -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 { GroupContactNotFoundError } from '../errors/GroupContactErrors'; import { GroupContact } from '../models/entities/GroupContact'; +import { ResponseEmpty } from '../models/responses/ResponseEmpty'; import { ResponseGroupContact } from '../models/responses/ResponseGroupContact'; @JsonController('/contacts') @@ -31,7 +32,7 @@ export class ContactController { } @Get('/:id') - @Authorized("DONOR:GET") + @Authorized("CONTACT:GET") @ResponseSchema(ResponseGroupContact) @ResponseSchema(GroupContactNotFoundError, { statusCode: 404 }) @OnUndefined(GroupContactNotFoundError) @@ -79,31 +80,18 @@ export class ContactController { // return new ResponseDonor(await this.contactRepository.findOne({ id: id }, { relations: ['donations', 'donations.runner', 'donations.runner.scans', 'donations.runner.scans.track'] })); // } - // @Delete('/:id') - // @Authorized("DONOR:DELETE") - // @ResponseSchema(ResponseDonor) - // @ResponseSchema(ResponseEmpty, { statusCode: 204 }) - // @OnUndefined(204) - // @OpenAPI({ description: 'Delete the contact whose id you provided.
If no contact with this id exists it will just return 204(no content).
If the contact still has donations associated this will fail, please provide the query param ?force=true to delete the contact with all associated donations.' }) - // async remove(@Param("id") id: number, @QueryParam("force") force: boolean) { - // let contact = await this.contactRepository.findOne({ id: id }); - // if (!contact) { return null; } - // const responseDonor = await this.contactRepository.findOne(contact, { relations: ['donations', 'donations.runner', 'donations.runner.scans', 'donations.runner.scans.track'] }); + @Delete('/:id') + @Authorized("CONTACT:DELETE") + @ResponseSchema(ResponseGroupContact) + @ResponseSchema(ResponseEmpty, { statusCode: 204 }) + @OnUndefined(204) + @OpenAPI({ description: 'Delete the contact whose id you provided.
If no contact with this id exists it will just return 204(no content).
This won\'t delete any groups associated with the contact.' }) + async remove(@Param("id") id: number, @QueryParam("force") force: boolean) { + let contact = await this.contactRepository.findOne({ id: id }); + if (!contact) { return null; } + const responseContact = await this.contactRepository.findOne(contact, { relations: ['groups'] }); - // if (!contact) { - // throw new DonorNotFoundError(); - // } - - // const contactDonations = (await this.contactRepository.findOne({ id: contact.id }, { relations: ["donations"] })).donations; - // if (contactDonations.length > 0 && !force) { - // throw new DonorHasDonationsError(); - // } - // const donationController = new DonationController(); - // for (let donation of contactDonations) { - // await donationController.remove(donation.id, force); - // } - - // await this.contactRepository.delete(contact); - // return new ResponseDonor(responseDonor); - // } + await this.contactRepository.delete(contact); + return responseContact.toResponse(); + } }