1
0
Fork 0
mirror of synced 2024-06-28 11:00:55 +12:00

Updating the self auth endpoint to use the row processor.

This commit is contained in:
mike12345567 2021-02-19 10:32:24 +00:00
parent 619f68fc3b
commit a6d82f8c95
3 changed files with 19 additions and 22 deletions

View file

@ -5,6 +5,8 @@ const env = require("../../environment")
const { getAPIKey } = require("../../utilities/usageQuota")
const { generateUserID } = require("../../db/utils")
const { setCookie } = require("../../utilities")
const { outputProcessing } = require("../../utilities/rowProcessor")
const { ViewNames } = require("../../db/utils")
exports.authenticate = async ctx => {
const appId = ctx.appId
@ -62,12 +64,14 @@ exports.fetchSelf = async ctx => {
const { userId, appId } = ctx.user
if (!userId || !appId) {
ctx.body = {}
} else {
const database = new CouchDB(appId)
const user = await database.get(userId)
if (user) {
delete user.password
}
ctx.body = user
return
}
const db = new CouchDB(appId)
const user = await db.get(userId)
const userTable = await db.get(ViewNames.USERS)
if (user) {
delete user.password
}
// specifically needs to make sure is enriched
ctx.body = await outputProcessing(appId, userTable, user)
}

View file

@ -140,13 +140,6 @@ exports.updateLinks = async function({
* then an array will be output, object input -> object output.
*/
exports.attachLinkIDs = async (appId, rows) => {
// handle a single row as well as multiple
let wasArray = true
if (!(rows instanceof Array)) {
rows = [rows]
wasArray = false
}
const links = await getLinksForRows(appId, rows)
// now iterate through the rows and all field information
for (let row of rows) {
@ -162,7 +155,7 @@ exports.attachLinkIDs = async (appId, rows) => {
}
// if it was an array when it came in then handle it as an array in response
// otherwise return the first element as there was only one input
return wasArray ? rows : rows[0]
return rows
}
/**
@ -178,11 +171,6 @@ exports.attachLinkedPrimaryDisplay = async (appId, table, rows) => {
if (linkedTableIds.length === 0) {
return rows
}
let wasArray = true
if (!(rows instanceof Array)) {
rows = [rows]
wasArray = false
}
const db = new CouchDB(appId)
const linkedTables = await Promise.all(linkedTableIds.map(id => db.get(id)))
const links = (await getLinksForRows(appId, rows)).filter(link =>
@ -214,5 +202,5 @@ exports.attachLinkedPrimaryDisplay = async (appId, table, rows) => {
}
})
}
return wasArray ? rows : rows[0]
return rows
}

View file

@ -157,6 +157,11 @@ exports.inputProcessing = (user, table, row) => {
* @returns {object[]} the enriched rows will be returned.
*/
exports.outputProcessing = async (appId, table, rows) => {
let wasArray = true
if (!(rows instanceof Array)) {
rows = [rows]
wasArray = false
}
// attach any linked row information
const outputRows = await linkRows.attachLinkedPrimaryDisplay(
appId,
@ -179,5 +184,5 @@ exports.outputProcessing = async (appId, table, rows) => {
}
}
}
return outputRows
return wasArray ? outputRows : outputRows[0]
}