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) {
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')
@ -188,15 +197,16 @@ export default class AdminOrganizationController {
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))
})
}
/**

View file

@ -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
})
})
}
}

View file

@ -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' }
})
}
}