From a6d82f8c95a6c15d030e38bdc04124bd75384d5c Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 19 Feb 2021 10:32:24 +0000 Subject: [PATCH] Updating the self auth endpoint to use the row processor. --- packages/server/src/api/controllers/auth.js | 18 +++++++++++------- packages/server/src/db/linkedRows/index.js | 16 ++-------------- packages/server/src/utilities/rowProcessor.js | 7 ++++++- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/packages/server/src/api/controllers/auth.js b/packages/server/src/api/controllers/auth.js index 5c3674d1b9..1dfe6f12d8 100644 --- a/packages/server/src/api/controllers/auth.js +++ b/packages/server/src/api/controllers/auth.js @@ -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) } diff --git a/packages/server/src/db/linkedRows/index.js b/packages/server/src/db/linkedRows/index.js index 65985dead2..8e4c64af14 100644 --- a/packages/server/src/db/linkedRows/index.js +++ b/packages/server/src/db/linkedRows/index.js @@ -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 } diff --git a/packages/server/src/utilities/rowProcessor.js b/packages/server/src/utilities/rowProcessor.js index 5824885d05..362f7e8820 100644 --- a/packages/server/src/utilities/rowProcessor.js +++ b/packages/server/src/utilities/rowProcessor.js @@ -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] }