2020-05-07 21:53:34 +12:00
|
|
|
const CouchDB = require("../../db")
|
2021-04-09 03:58:33 +12:00
|
|
|
const {
|
2021-04-10 02:11:49 +12:00
|
|
|
generateUserMetadataID,
|
|
|
|
getUserMetadataParams,
|
|
|
|
getEmailFromUserMetadataID,
|
|
|
|
} = require("../../db/utils")
|
2021-04-09 03:58:33 +12:00
|
|
|
const { InternalTables } = require("../../db/utils")
|
2020-12-03 06:08:25 +13:00
|
|
|
const { getRole } = require("../../utilities/security/roles")
|
2021-04-10 02:11:49 +12:00
|
|
|
const {
|
|
|
|
getGlobalUsers,
|
|
|
|
saveGlobalUser,
|
|
|
|
deleteGlobalUser,
|
|
|
|
} = require("../../utilities/workerRequests")
|
2021-04-09 03:58:33 +12:00
|
|
|
|
|
|
|
exports.fetchMetadata = async function(ctx) {
|
2021-03-30 05:32:05 +13:00
|
|
|
const database = new CouchDB(ctx.appId)
|
2021-04-10 02:11:49 +12:00
|
|
|
const global = await getGlobalUsers(ctx, ctx.appId)
|
2021-04-09 03:58:33 +12:00
|
|
|
const metadata = (
|
2020-12-09 06:33:08 +13:00
|
|
|
await database.allDocs(
|
2021-04-10 02:11:49 +12:00
|
|
|
getUserMetadataParams(null, {
|
2020-12-09 06:33:08 +13:00
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
).rows.map(row => row.doc)
|
2021-04-09 03:58:33 +12:00
|
|
|
const users = []
|
|
|
|
for (let user of global) {
|
|
|
|
const info = metadata.find(meta => meta._id.includes(user.email))
|
|
|
|
users.push({
|
|
|
|
...user,
|
|
|
|
...info,
|
2021-04-10 03:55:56 +12:00
|
|
|
// make sure the ID is always a local ID, not a global one
|
|
|
|
_id: generateUserMetadataID(user.email),
|
2021-04-09 03:58:33 +12:00
|
|
|
})
|
2020-12-09 06:33:08 +13:00
|
|
|
}
|
|
|
|
ctx.body = users
|
2020-05-07 21:53:34 +12:00
|
|
|
}
|
2020-04-08 04:25:09 +12:00
|
|
|
|
2021-04-09 03:58:33 +12:00
|
|
|
exports.createMetadata = async function(ctx) {
|
|
|
|
const appId = ctx.appId
|
|
|
|
const db = new CouchDB(appId)
|
|
|
|
const { email, roleId } = ctx.request.body
|
2020-04-24 01:37:08 +12:00
|
|
|
|
2021-04-09 03:58:33 +12:00
|
|
|
// check role valid
|
|
|
|
const role = await getRole(appId, roleId)
|
2020-12-03 02:20:56 +13:00
|
|
|
if (!role) ctx.throw(400, "Invalid Role")
|
2020-05-22 01:31:23 +12:00
|
|
|
|
2021-04-10 02:11:49 +12:00
|
|
|
const metadata = await saveGlobalUser(ctx, appId, email, ctx.request.body)
|
2021-04-09 03:58:33 +12:00
|
|
|
|
2020-05-22 01:31:23 +12:00
|
|
|
const user = {
|
2021-04-09 03:58:33 +12:00
|
|
|
...metadata,
|
2021-04-10 02:11:49 +12:00
|
|
|
_id: generateUserMetadataID(email),
|
2020-05-07 21:53:34 +12:00
|
|
|
type: "user",
|
2021-04-09 03:58:33 +12:00
|
|
|
tableId: InternalTables.USER_METADATA,
|
2021-02-23 00:39:58 +13:00
|
|
|
}
|
2020-05-22 01:31:23 +12:00
|
|
|
|
2021-04-09 03:58:33 +12:00
|
|
|
const response = await db.post(user)
|
|
|
|
ctx.body = {
|
|
|
|
_rev: response.rev,
|
|
|
|
email,
|
2020-04-11 03:37:59 +12:00
|
|
|
}
|
2020-05-07 21:53:34 +12:00
|
|
|
}
|
2020-04-10 03:53:48 +12:00
|
|
|
|
2021-04-09 03:58:33 +12:00
|
|
|
exports.updateMetadata = async function(ctx) {
|
|
|
|
const appId = ctx.appId
|
|
|
|
const db = new CouchDB(appId)
|
2020-06-26 21:05:09 +12:00
|
|
|
const user = ctx.request.body
|
2021-04-10 02:11:49 +12:00
|
|
|
let email = user.email || getEmailFromUserMetadataID(user._id)
|
|
|
|
const metadata = await saveGlobalUser(ctx, appId, email, ctx.request.body)
|
2020-05-22 01:31:23 +12:00
|
|
|
|
2021-04-09 03:58:33 +12:00
|
|
|
if (!metadata._id) {
|
2021-04-10 02:11:49 +12:00
|
|
|
user._id = generateUserMetadataID(email)
|
2021-04-09 03:58:33 +12:00
|
|
|
}
|
|
|
|
ctx.body = await db.put({
|
|
|
|
...metadata,
|
2021-02-23 00:39:58 +13:00
|
|
|
})
|
2020-06-26 21:05:09 +12:00
|
|
|
}
|
|
|
|
|
2021-04-09 03:58:33 +12:00
|
|
|
exports.destroyMetadata = async function(ctx) {
|
|
|
|
const db = new CouchDB(ctx.appId)
|
2021-04-10 03:55:56 +12:00
|
|
|
const email =
|
|
|
|
ctx.params.email || getEmailFromUserMetadataID(ctx.params.userId)
|
2021-04-10 02:11:49 +12:00
|
|
|
await deleteGlobalUser(ctx, email)
|
2021-04-10 03:55:56 +12:00
|
|
|
try {
|
|
|
|
const dbUser = await db.get(generateUserMetadataID(email))
|
|
|
|
await db.remove(dbUser._id, dbUser._rev)
|
|
|
|
} catch (err) {
|
|
|
|
// error just means the global user has no config in this app
|
|
|
|
}
|
2021-03-10 06:09:18 +13:00
|
|
|
ctx.body = {
|
|
|
|
message: `User ${ctx.params.email} deleted.`,
|
|
|
|
}
|
2020-05-15 02:12:30 +12:00
|
|
|
}
|
|
|
|
|
2021-04-09 03:58:33 +12:00
|
|
|
exports.findMetadata = async function(ctx) {
|
2021-03-30 05:32:05 +13:00
|
|
|
const database = new CouchDB(ctx.appId)
|
2021-04-10 02:11:49 +12:00
|
|
|
const email =
|
|
|
|
ctx.params.email || getEmailFromUserMetadataID(ctx.params.userId)
|
|
|
|
const global = await getGlobalUsers(ctx, ctx.appId, email)
|
|
|
|
const user = await database.get(generateUserMetadataID(email))
|
|
|
|
ctx.body = {
|
|
|
|
...global,
|
|
|
|
...user,
|
2021-04-10 03:55:56 +12:00
|
|
|
// make sure the ID is always a local ID, not a global one
|
|
|
|
_id: generateUserMetadataID(email),
|
2020-04-11 03:37:59 +12:00
|
|
|
}
|
2020-05-07 21:53:34 +12:00
|
|
|
}
|