diff --git a/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts b/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts index 6f41d3d55f..5b7e852f50 100644 --- a/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts +++ b/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts @@ -1,13 +1,15 @@ -import { cache } from "@budibase/backend-core" +import { cache, db as dbCore } from "@budibase/backend-core" import { utils } from "@budibase/shared-core" -import { FieldSubtype } from "@budibase/types" +import { FieldSubtype, DocumentType, SEPARATOR } from "@budibase/types" import { InvalidBBRefError } from "./errors" +const ROW_PREFIX = DocumentType.ROW + SEPARATOR + export async function processInputBBReferences( value: string | string[] | { _id: string } | { _id: string }[], subtype: FieldSubtype ): Promise { - const referenceIds: string[] = [] + let referenceIds: string[] = [] if (Array.isArray(value)) { referenceIds.push( @@ -26,6 +28,17 @@ export async function processInputBBReferences( ) } + // make sure all reference IDs are correct global user IDs + // they may be user metadata references (start with row prefix) + // and these need to be converted to global IDs + referenceIds = referenceIds.map(id => { + if (id?.startsWith(ROW_PREFIX)) { + return dbCore.getGlobalIDFromUserMetadataID(id) + } else { + return id + } + }) + switch (subtype) { case FieldSubtype.USER: const { notFoundIds } = await cache.user.getUsers(referenceIds) diff --git a/packages/server/src/utilities/rowProcessor/tests/bbReferenceProcessor.spec.ts b/packages/server/src/utilities/rowProcessor/tests/bbReferenceProcessor.spec.ts index d0932b399c..b6174861d4 100644 --- a/packages/server/src/utilities/rowProcessor/tests/bbReferenceProcessor.spec.ts +++ b/packages/server/src/utilities/rowProcessor/tests/bbReferenceProcessor.spec.ts @@ -154,6 +154,15 @@ describe("bbReferenceProcessor", () => { expect(result).toEqual(null) }) + + it("should convert user medata IDs to global IDs", async () => { + const userId = _.sample(users)!._id! + const userMetadataId = backendCore.db.generateUserMetadataID(userId) + const result = await config.doInTenant(() => + processInputBBReferences(userMetadataId, FieldSubtype.USER) + ) + expect(result).toBe(userId) + }) }) })