feat(Organization): add user deletion of organization

This commit is contained in:
Matthieu Bessat 2020-08-25 15:08:22 +02:00
parent 3101da4d0b
commit efcff86164
3 changed files with 50 additions and 16 deletions

View file

@ -169,11 +169,20 @@ export default class AdminOrganizationController {
} }
static destroy(req: express.Request, res: express.Response) { static destroy(req: express.Request, res: express.Response) {
console.log('> ADMIN DESTROY ASKED')
Organization.findById(req.params.id).then(organization => { Organization.findById(req.params.id).then(organization => {
Organization.deleteOne({ _id: req.params.id }).then(data => { AdminOrganizationController.universalDestroy(req.params.id, organization).then((data) => {
if (organization === null) { res.json({
return 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 // delete all media from this organization
let keys: string[] = [] let keys: string[] = []
const proposedVersion: any = organization.get('proposedVersion') const proposedVersion: any = organization.get('proposedVersion')
@ -188,15 +197,16 @@ export default class AdminOrganizationController {
keys = keys.concat(proposedVersion.gallery.map((m: any) => m.key)) keys = keys.concat(proposedVersion.gallery.map((m: any) => m.key))
} }
MediaService.deleteMany(keys, 'destroyOrganizationFromAdmin')
let isSuccess = data.deletedCount !== undefined && data.deletedCount > 0 let isSuccess = data.deletedCount !== undefined && data.deletedCount > 0
res.status(isSuccess ? 200 : 400).json({ if (!isSuccess) {
success: isSuccess, return reject([{ code: 'invalid-destroy', message: 'Something wrong with the destroy operation occured' }])
data }
})
}).catch(err => res.status(400).json({ success: false, errors: err.errors })) MediaService.deleteMany(keys, 'destroyOrganizationUniversal')
}).catch(err => res.status(400).json({ success: false, errors: err }))
return resolve(data)
}).catch(err => reject(err))
})
} }
/** /**

View file

@ -6,6 +6,7 @@ import MediaService from '../MediaService'
import slugify from 'slugify' import slugify from 'slugify'
import Utils from '../Utils' import Utils from '../Utils'
import sanitizeHtml from 'sanitize-html' import sanitizeHtml from 'sanitize-html'
import AdminOrganizationController from './AdminOrganizationController'
export default class DelegateController { 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) { 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
})
})
} }
} }

View file

@ -6,7 +6,8 @@ export default class DelegateAuthMiddleware {
let token: string | undefined = req.get('Authorization') let token: string | undefined = req.get('Authorization')
// fetch the token // fetch the token
if (token !== undefined) { 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) { if (data !== null) {
res.locals.organization = data res.locals.organization = data
next() next()
@ -17,7 +18,7 @@ export default class DelegateAuthMiddleware {
.status(400) .status(400)
.json({ .json({
success: false, success: false,
errors: { code: 'invalid-auth', message: 'Invalid admin Authorization header' } errors: { code: 'invalid-auth', message: 'Invalid delegate Authorization header' }
}) })
} }
} }