Deprovisioning working minus apps

This commit is contained in:
Rory Powell 2021-09-29 11:36:24 +01:00
parent 9942a2d85c
commit 591203f5cd
2 changed files with 65 additions and 21 deletions

View File

@ -93,32 +93,70 @@ const getGlobalUserParams = (globalId, otherProps = {}) => {
} }
} }
exports.deleteTenant = async tenantId => { const removeTenantFromInfoDB = async tenantId => {
const globalDb = exports.getGlobalDB() try {
let promises = []
// remove the tenant entry from global info
const infoDb = getDB(PLATFORM_INFO_DB) const infoDb = getDB(PLATFORM_INFO_DB)
let tenants = await infoDb.get(TENANT_DOC) let tenants = await infoDb.get(TENANT_DOC)
tenants.tenantIds = tenants.tenantIds.filter(id => id !== tenantId) tenants.tenantIds = tenants.tenantIds.filter(id => id !== tenantId)
promises.push(infoDb.put(tenants))
// remove the users await infoDb.put(tenants)
} catch (err) {
console.error(`Error removing tenant ${tenantId} from info db`, err)
throw err
}
}
const removeUsersFromInfoDB = async tenantId => {
try {
const globalDb = exports.getGlobalDB(tenantId)
const infoDb = getDB(PLATFORM_INFO_DB)
const allUsers = await globalDb.allDocs( const allUsers = await globalDb.allDocs(
getGlobalUserParams(null, { getGlobalUserParams(null, {
include_docs: true, include_docs: true,
}) })
) )
allUsers.rows.map(row => { const allEmails = allUsers.rows.map(row => row.doc.email)
promises.push(infoDb.remove(row.id, row.value.rev)) // get the id docs
promises.push(infoDb.remove(row.doc.email, row.value.rev)) let keys = allUsers.rows.map(row => row.id)
// and the email docs
keys = keys.concat(allEmails)
// retrieve the docs and delete them
const userDocs = await infoDb.allDocs({
keys,
include_docs: true,
}) })
const toDelete = userDocs.rows.map(row => {
return {
...row.doc,
_deleted: true,
}
})
await infoDb.bulkDocs(toDelete)
} catch (err) {
console.error(`Error removing tenant ${tenantId} users from info db`, err)
throw err
}
}
// remove the global db const removeGlobalDB = async tenantId => {
promises.push(globalDb.destroy()) try {
const globalDb = exports.getGlobalDB(tenantId)
await globalDb.destroy()
} catch (err) {
console.error(`Error removing tenant ${tenantId} users from info db`, err)
throw err
}
}
await Promise.all(promises) const removeTenantApps = async () => {
// TODO: Delete all apps // TODO
}
exports.deleteTenant = async tenantId => {
await removeTenantFromInfoDB(tenantId)
await removeUsersFromInfoDB(tenantId)
await removeGlobalDB(tenantId)
await removeTenantApps(tenantId)
} }
exports.getGlobalDB = (tenantId = null) => { exports.getGlobalDB = (tenantId = null) => {

View File

@ -40,5 +40,11 @@ exports.delete = async ctx => {
ctx.throw(403, "Unauthorized") ctx.throw(403, "Unauthorized")
} }
try {
await deleteTenant(tenantId) await deleteTenant(tenantId)
ctx.status = 204
} catch (err) {
ctx.log.error(err)
throw err
}
} }