2020-05-07 21:53:34 +12:00
|
|
|
const CouchDB = require("../../db")
|
2021-02-19 23:32:24 +13:00
|
|
|
const { outputProcessing } = require("../../utilities/rowProcessor")
|
2021-04-10 04:33:21 +12:00
|
|
|
const { InternalTables } = require("../../db/utils")
|
2021-04-13 02:54:14 +12:00
|
|
|
const { getFullUser } = require("../../utilities/users")
|
2020-04-08 07:34:21 +12:00
|
|
|
|
2021-05-04 22:32:22 +12:00
|
|
|
exports.fetchSelf = async ctx => {
|
2021-04-16 03:25:48 +12:00
|
|
|
if (!ctx.user) {
|
|
|
|
ctx.throw(403, "No user logged in")
|
|
|
|
}
|
2021-04-14 02:27:47 +12:00
|
|
|
const appId = ctx.appId
|
|
|
|
const { userId } = ctx.user
|
2021-03-11 01:20:07 +13:00
|
|
|
/* istanbul ignore next */
|
2021-04-13 22:56:57 +12:00
|
|
|
if (!userId) {
|
2021-01-29 03:29:35 +13:00
|
|
|
ctx.body = {}
|
2021-02-19 23:32:24 +13:00
|
|
|
return
|
|
|
|
}
|
2021-04-13 22:56:57 +12:00
|
|
|
|
2021-04-20 03:26:33 +12:00
|
|
|
const user = await getFullUser(ctx, userId)
|
2021-04-14 04:11:55 +12:00
|
|
|
|
|
|
|
if (appId) {
|
|
|
|
const db = new CouchDB(appId)
|
|
|
|
// remove the full roles structure
|
|
|
|
delete user.roles
|
|
|
|
try {
|
|
|
|
const userTable = await db.get(InternalTables.USER_METADATA)
|
|
|
|
const metadata = await db.get(userId)
|
|
|
|
// specifically needs to make sure is enriched
|
|
|
|
ctx.body = await outputProcessing(appId, userTable, {
|
|
|
|
...user,
|
|
|
|
...metadata,
|
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
ctx.body = user
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ctx.body = user
|
2021-01-29 03:29:35 +13:00
|
|
|
}
|
|
|
|
}
|