From efcff861640973aa595cfeafc3f566693512df3a Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Tue, 25 Aug 2020 15:08:22 +0200 Subject: [PATCH] feat(Organization): add user deletion of organization --- .../AdminOrganizationController.ts | 36 ++++++++++++------- src/controllers/DelegateController.ts | 25 ++++++++++++- src/middlewares/DelegateAuthMiddleware.ts | 5 +-- 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/controllers/AdminOrganizationController.ts b/src/controllers/AdminOrganizationController.ts index 8ea50e5..4becbff 100644 --- a/src/controllers/AdminOrganizationController.ts +++ b/src/controllers/AdminOrganizationController.ts @@ -169,11 +169,20 @@ export default class AdminOrganizationController { } static destroy(req: express.Request, res: express.Response) { + console.log('> ADMIN DESTROY ASKED') Organization.findById(req.params.id).then(organization => { - Organization.deleteOne({ _id: req.params.id }).then(data => { - if (organization === null) { - return - } + AdminOrganizationController.universalDestroy(req.params.id, organization).then((data) => { + res.json({ + success: true, + data + }) + }).catch(err => res.status(400).json({ success: false, errors: err.errors })) + }).catch(err => res.status(400).json({ success: false, errors: err })) + } + + static universalDestroy(id: string, organization: any) { + return new Promise((resolve, reject) => { + Organization.deleteOne({ _id: id }).then(data => { // delete all media from this organization let keys: string[] = [] const proposedVersion: any = organization.get('proposedVersion') @@ -187,16 +196,17 @@ export default class AdminOrganizationController { if (Array.isArray(proposedVersion.gallery)) { keys = keys.concat(proposedVersion.gallery.map((m: any) => m.key)) } - - MediaService.deleteMany(keys, 'destroyOrganizationFromAdmin') - + let isSuccess = data.deletedCount !== undefined && data.deletedCount > 0 - res.status(isSuccess ? 200 : 400).json({ - success: isSuccess, - data - }) - }).catch(err => res.status(400).json({ success: false, errors: err.errors })) - }).catch(err => res.status(400).json({ success: false, errors: err })) + if (!isSuccess) { + return reject([{ code: 'invalid-destroy', message: 'Something wrong with the destroy operation occured' }]) + } + + MediaService.deleteMany(keys, 'destroyOrganizationUniversal') + + return resolve(data) + }).catch(err => reject(err)) + }) } /** diff --git a/src/controllers/DelegateController.ts b/src/controllers/DelegateController.ts index 6aa464b..ad54101 100644 --- a/src/controllers/DelegateController.ts +++ b/src/controllers/DelegateController.ts @@ -6,6 +6,7 @@ import MediaService from '../MediaService' import slugify from 'slugify' import Utils from '../Utils' import sanitizeHtml from 'sanitize-html' +import AdminOrganizationController from './AdminOrganizationController' export default class DelegateController { @@ -346,7 +347,29 @@ export default class DelegateController { ] } + /** + * Will delete the record of this organization, and all the medias + * + * @param req + * @param res + */ static destroy(req: express.Request, res: express.Response) { - res.json({ success: true }) + console.log('> USER DESTROY ASKED') + console.log(res.locals.organization._id) + AdminOrganizationController.universalDestroy( + res.locals.organization._id, + res.locals.organization.proposedVersion + ).then(data => { + return res.json({ + success: true, + data + }) + }).catch(err => { + console.error(err) + return res.status(400).json({ + success: false, + data: err + }) + }) } } \ No newline at end of file diff --git a/src/middlewares/DelegateAuthMiddleware.ts b/src/middlewares/DelegateAuthMiddleware.ts index 7f198e6..d143ea3 100644 --- a/src/middlewares/DelegateAuthMiddleware.ts +++ b/src/middlewares/DelegateAuthMiddleware.ts @@ -6,7 +6,8 @@ export default class DelegateAuthMiddleware { let token: string | undefined = req.get('Authorization') // fetch the token if (token !== undefined) { - let data = await Organization.findOne({ token: token.replace('Bearer ', '') }) + token = token.replace('Bearer ', '') + let data = await Organization.findOne({ token }) if (data !== null) { res.locals.organization = data next() @@ -17,7 +18,7 @@ export default class DelegateAuthMiddleware { .status(400) .json({ success: false, - errors: { code: 'invalid-auth', message: 'Invalid admin Authorization header' } + errors: { code: 'invalid-auth', message: 'Invalid delegate Authorization header' } }) } } \ No newline at end of file