From 4794a5374e2b8104789c2f1cb2d83655a6b36a1c Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 21 Oct 2021 17:23:10 +0100 Subject: [PATCH] Fixing an issue with user metadata not always being present when user accessing app, causing weird issues. --- packages/server/src/api/controllers/auth.js | 15 ++++++++++++--- packages/server/src/db/linkedRows/index.js | 21 ++++++++++++++------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/packages/server/src/api/controllers/auth.js b/packages/server/src/api/controllers/auth.js index ac88599713..2bcdae4c05 100644 --- a/packages/server/src/api/controllers/auth.js +++ b/packages/server/src/api/controllers/auth.js @@ -28,14 +28,23 @@ exports.fetchSelf = async ctx => { ...metadata, }) } catch (err) { + let response // user didn't exist in app, don't pretend they do if (user.roleId === BUILTIN_ROLE_IDS.PUBLIC) { - ctx.body = {} + response = {} } // user has a role of some sort, return them - else { - ctx.body = user + else if (err.status === 404) { + const metadata = { + _id: userId, + } + const dbResp = await db.put(metadata) + user._rev = dbResp.rev + response = user + } else { + response = user } + ctx.body = response } } else { ctx.body = user diff --git a/packages/server/src/db/linkedRows/index.js b/packages/server/src/db/linkedRows/index.js index 303cd085c1..6835719e5f 100644 --- a/packages/server/src/db/linkedRows/index.js +++ b/packages/server/src/db/linkedRows/index.js @@ -81,7 +81,9 @@ async function getFullLinkedDocs(ctx, appId, links) { row => row.doc ) // convert the unique db rows back to a full list of linked rows - const linked = linkedRowIds.map(id => dbRows.find(row => row._id === id)) + const linked = linkedRowIds + .map(id => dbRows.find(row => row && row._id === id)) + .filter(row => row != null) // need to handle users as specific cases let [users, other] = partition(linked, linkRow => linkRow._id.startsWith(USER_METDATA_PREFIX) @@ -172,13 +174,18 @@ exports.attachFullLinkedDocs = async (ctx, table, rows) => { row[link.fieldName] = [] } const linkedRow = linked.find(row => row._id === link.id) - const linkedTableId = - linkedRow.tableId || getRelatedTableForField(table, link.fieldName) - const linkedTable = await getLinkedTable(db, linkedTableId, linkedTables) - if (!linkedRow || !linkedTable) { - continue + if (linkedRow) { + const linkedTableId = + linkedRow.tableId || getRelatedTableForField(table, link.fieldName) + const linkedTable = await getLinkedTable( + db, + linkedTableId, + linkedTables + ) + if (linkedTable) { + row[link.fieldName].push(processFormulas(linkedTable, linkedRow)) + } } - row[link.fieldName].push(processFormulas(linkedTable, linkedRow)) } } return rows