Updating the self auth endpoint to use the row processor.
This commit is contained in:
parent
619f68fc3b
commit
a6d82f8c95
|
@ -5,6 +5,8 @@ const env = require("../../environment")
|
||||||
const { getAPIKey } = require("../../utilities/usageQuota")
|
const { getAPIKey } = require("../../utilities/usageQuota")
|
||||||
const { generateUserID } = require("../../db/utils")
|
const { generateUserID } = require("../../db/utils")
|
||||||
const { setCookie } = require("../../utilities")
|
const { setCookie } = require("../../utilities")
|
||||||
|
const { outputProcessing } = require("../../utilities/rowProcessor")
|
||||||
|
const { ViewNames } = require("../../db/utils")
|
||||||
|
|
||||||
exports.authenticate = async ctx => {
|
exports.authenticate = async ctx => {
|
||||||
const appId = ctx.appId
|
const appId = ctx.appId
|
||||||
|
@ -62,12 +64,14 @@ exports.fetchSelf = async ctx => {
|
||||||
const { userId, appId } = ctx.user
|
const { userId, appId } = ctx.user
|
||||||
if (!userId || !appId) {
|
if (!userId || !appId) {
|
||||||
ctx.body = {}
|
ctx.body = {}
|
||||||
} else {
|
return
|
||||||
const database = new CouchDB(appId)
|
|
||||||
const user = await database.get(userId)
|
|
||||||
if (user) {
|
|
||||||
delete user.password
|
|
||||||
}
|
|
||||||
ctx.body = user
|
|
||||||
}
|
}
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -140,13 +140,6 @@ exports.updateLinks = async function({
|
||||||
* then an array will be output, object input -> object output.
|
* then an array will be output, object input -> object output.
|
||||||
*/
|
*/
|
||||||
exports.attachLinkIDs = async (appId, rows) => {
|
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)
|
const links = await getLinksForRows(appId, rows)
|
||||||
// now iterate through the rows and all field information
|
// now iterate through the rows and all field information
|
||||||
for (let row of rows) {
|
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
|
// 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
|
// 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) {
|
if (linkedTableIds.length === 0) {
|
||||||
return rows
|
return rows
|
||||||
}
|
}
|
||||||
let wasArray = true
|
|
||||||
if (!(rows instanceof Array)) {
|
|
||||||
rows = [rows]
|
|
||||||
wasArray = false
|
|
||||||
}
|
|
||||||
const db = new CouchDB(appId)
|
const db = new CouchDB(appId)
|
||||||
const linkedTables = await Promise.all(linkedTableIds.map(id => db.get(id)))
|
const linkedTables = await Promise.all(linkedTableIds.map(id => db.get(id)))
|
||||||
const links = (await getLinksForRows(appId, rows)).filter(link =>
|
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
|
||||||
}
|
}
|
||||||
|
|
|
@ -157,6 +157,11 @@ exports.inputProcessing = (user, table, row) => {
|
||||||
* @returns {object[]} the enriched rows will be returned.
|
* @returns {object[]} the enriched rows will be returned.
|
||||||
*/
|
*/
|
||||||
exports.outputProcessing = async (appId, table, rows) => {
|
exports.outputProcessing = async (appId, table, rows) => {
|
||||||
|
let wasArray = true
|
||||||
|
if (!(rows instanceof Array)) {
|
||||||
|
rows = [rows]
|
||||||
|
wasArray = false
|
||||||
|
}
|
||||||
// attach any linked row information
|
// attach any linked row information
|
||||||
const outputRows = await linkRows.attachLinkedPrimaryDisplay(
|
const outputRows = await linkRows.attachLinkedPrimaryDisplay(
|
||||||
appId,
|
appId,
|
||||||
|
@ -179,5 +184,5 @@ exports.outputProcessing = async (appId, table, rows) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return outputRows
|
return wasArray ? outputRows : outputRows[0]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue