From 2148bf6da46066919635b1d1c8918b37ce844f56 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 2 Sep 2021 17:13:00 +0100 Subject: [PATCH] Fixing issues with search endpoint not handling users fully. --- .../server/src/api/controllers/application.js | 11 ++++++----- .../server/src/api/controllers/row/internal.js | 5 +++++ packages/server/src/api/controllers/user.js | 2 +- packages/server/src/db/linkedRows/index.js | 14 ++------------ packages/server/src/utilities/global.js | 18 ++++++++++++++++-- packages/server/src/utilities/users.js | 2 +- .../server/src/utilities/workerRequests.js | 2 +- 7 files changed, 32 insertions(+), 22 deletions(-) diff --git a/packages/server/src/api/controllers/application.js b/packages/server/src/api/controllers/application.js index d0de611d74..da0014c5f8 100644 --- a/packages/server/src/api/controllers/application.js +++ b/packages/server/src/api/controllers/application.js @@ -106,6 +106,12 @@ async function createInstance(template) { views: {}, }) + // NOTE: indexes need to be created before any tables/templates + // add view for linked rows + await createLinkView(appId) + await createRoutingView(appId) + await createAllSearchIndex(appId) + // replicate the template data to the instance DB // this is currently very hard to test, downloading and importing template files /* istanbul ignore next */ @@ -119,11 +125,6 @@ async function createInstance(template) { await db.put(USERS_TABLE_SCHEMA) } - // add view for linked rows - await createLinkView(appId) - await createRoutingView(appId) - await createAllSearchIndex(appId) - return { _id: appId } } diff --git a/packages/server/src/api/controllers/row/internal.js b/packages/server/src/api/controllers/row/internal.js index e39bc2aff9..d69b3b4a79 100644 --- a/packages/server/src/api/controllers/row/internal.js +++ b/packages/server/src/api/controllers/row/internal.js @@ -15,6 +15,7 @@ const { FieldTypes } = require("../../../constants") const { isEqual } = require("lodash") const { validate, findRow } = require("./utils") const { fullSearch, paginatedSearch } = require("./internalSearch") +const { getGlobalUsersFromMetadata } = require("../../../utilities/global") const CALCULATION_TYPES = { SUM: "sum", @@ -290,6 +291,10 @@ exports.search = async ctx => { // Enrich search results with relationships if (response.rows && response.rows.length) { + // enrich with global users if from users table + if (tableId === InternalTables.USER_METADATA) { + response.rows = await getGlobalUsersFromMetadata(appId, response.rows) + } const table = await db.get(tableId) response.rows = await outputProcessing(ctx, table, response.rows) } diff --git a/packages/server/src/api/controllers/user.js b/packages/server/src/api/controllers/user.js index 935ead38b6..6778f983c2 100644 --- a/packages/server/src/api/controllers/user.js +++ b/packages/server/src/api/controllers/user.js @@ -17,7 +17,7 @@ function removeGlobalProps(user) { exports.fetchMetadata = async function (ctx) { const database = new CouchDB(ctx.appId) - const global = await getGlobalUsers(ctx, ctx.appId) + const global = await getGlobalUsers(ctx.appId) const metadata = ( await database.allDocs( getUserMetadataParams(null, { diff --git a/packages/server/src/db/linkedRows/index.js b/packages/server/src/db/linkedRows/index.js index dece78dcff..9f17f0ed45 100644 --- a/packages/server/src/db/linkedRows/index.js +++ b/packages/server/src/db/linkedRows/index.js @@ -13,7 +13,7 @@ const CouchDB = require("../../db") const { FieldTypes } = require("../../constants") const { getMultiIDParams, USER_METDATA_PREFIX } = require("../../db/utils") const { partition } = require("lodash") -const { getGlobalUsers } = require("../../utilities/global") +const { getGlobalUsersFromMetadata } = require("../../utilities/global") const processor = require("../../utilities/rowProcessor") /** @@ -71,17 +71,7 @@ async function getFullLinkedDocs(ctx, appId, links) { let [users, other] = partition(linked, linkRow => linkRow._id.startsWith(USER_METDATA_PREFIX) ) - const globalUsers = await getGlobalUsers(ctx, appId, users) - users = users.map(user => { - const globalUser = globalUsers.find( - globalUser => globalUser && user._id.includes(globalUser._id) - ) - return { - ...globalUser, - // doing user second overwrites the id and rev (always metadata) - ...user, - } - }) + users = await getGlobalUsersFromMetadata(appId, users) return [...other, ...users] } diff --git a/packages/server/src/utilities/global.js b/packages/server/src/utilities/global.js index 2757398586..8f032df7f0 100644 --- a/packages/server/src/utilities/global.js +++ b/packages/server/src/utilities/global.js @@ -39,13 +39,13 @@ exports.getCachedSelf = async (ctx, appId) => { return processUser(appId, user) } -exports.getGlobalUser = async (ctx, appId, userId) => { +exports.getGlobalUser = async (appId, userId) => { const db = getGlobalDB() let user = await db.get(getGlobalIDFromUserMetadataID(userId)) return processUser(appId, user) } -exports.getGlobalUsers = async (ctx, appId = null, users = null) => { +exports.getGlobalUsers = async (appId = null, users = null) => { const db = getGlobalDB() let globalUsers if (users) { @@ -73,3 +73,17 @@ exports.getGlobalUsers = async (ctx, appId = null, users = null) => { } return globalUsers.map(user => exports.updateAppRole(appId, user)) } + +exports.getGlobalUsersFromMetadata = async (appId, users) => { + const globalUsers = await exports.getGlobalUsers(appId, users) + return users.map(user => { + const globalUser = globalUsers.find( + globalUser => globalUser && user._id.includes(globalUser._id) + ) + return { + ...globalUser, + // doing user second overwrites the id and rev (always metadata) + ...user, + } + }) +} diff --git a/packages/server/src/utilities/users.js b/packages/server/src/utilities/users.js index 64fbfb7ea2..6144397bf1 100644 --- a/packages/server/src/utilities/users.js +++ b/packages/server/src/utilities/users.js @@ -3,7 +3,7 @@ const { InternalTables } = require("../db/utils") const { getGlobalUser } = require("../utilities/global") exports.getFullUser = async (ctx, userId) => { - const global = await getGlobalUser(ctx, ctx.appId, userId) + const global = await getGlobalUser(ctx.appId, userId) let metadata try { // this will throw an error if the db doesn't exist, or there is no appId diff --git a/packages/server/src/utilities/workerRequests.js b/packages/server/src/utilities/workerRequests.js index 066f6e23d4..377658084f 100644 --- a/packages/server/src/utilities/workerRequests.js +++ b/packages/server/src/utilities/workerRequests.js @@ -104,7 +104,7 @@ exports.addAppRoleToUser = async (ctx, appId, roleId, userId = null) => { user = await exports.getGlobalSelf(ctx) endpoint = `/api/global/users/self` } else { - user = await getGlobalUser(ctx, appId, userId) + user = await getGlobalUser(appId, userId) body._id = userId endpoint = `/api/global/users` }