From 8c6460ddb1a68f5251b0f046a65b1c7ad8b7375c Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Tue, 10 Aug 2021 13:00:15 +0100 Subject: [PATCH] ensure user metadata is created before link documents are created --- packages/server/src/db/linkedRows/LinkController.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/server/src/db/linkedRows/LinkController.js b/packages/server/src/db/linkedRows/LinkController.js index d526bf159b..27b230f30a 100644 --- a/packages/server/src/db/linkedRows/LinkController.js +++ b/packages/server/src/db/linkedRows/LinkController.js @@ -1,6 +1,6 @@ const CouchDB = require("../index") const { IncludeDocs, getLinkDocuments } = require("./linkUtils") -const { generateLinkID } = require("../utils") +const { generateLinkID, InternalTables, generateUserMetadataID, getUserMetadataParams } = require("../utils") const Sentry = require("@sentry/node") const { FieldTypes, RelationshipTypes } = require("../../constants") @@ -208,6 +208,15 @@ class LinkController { const linkedTable = await this._db.get(field.tableId) const linkedSchema = linkedTable.schema[field.fieldName] + // We need to map the global users to metadata in each app for relationships + if (field.tableId === InternalTables.USER_METADATA) { + const users = await this._db.allDocs(getUserMetadataParams(null, {})) + const metadataRequired = rowField.filter(userId => !users.rows.some(user => user.id === userId)) + + // ensure non-existing user metadata is created in the app DB + await this._db.bulkDocs(metadataRequired.map(userId => ({ _id: userId }))) + } + // iterate through the link IDs in the row field, see if any don't exist already for (let linkId of rowField) { if (linkedSchema.relationshipType === RelationshipTypes.ONE_TO_MANY) {