diff --git a/packages/server/src/utilities/rowProcessor/index.ts b/packages/server/src/utilities/rowProcessor/index.ts index e30eecf829..8aea0a08aa 100644 --- a/packages/server/src/utilities/rowProcessor/index.ts +++ b/packages/server/src/utilities/rowProcessor/index.ts @@ -7,7 +7,10 @@ import { InternalTables } from "../../db/utils" import { TYPE_TRANSFORM_MAP } from "./map" import { FieldSubtype, Row, RowAttachment, Table } from "@budibase/types" import { cloneDeep } from "lodash/fp" -import { processInputBBReferences } from "./bbReferenceProcessor" +import { + processInputBBReferences, + processOutputBBReferences, +} from "./bbReferenceProcessor" export * from "./utils" type AutoColumnProcessingOpts = { @@ -224,6 +227,16 @@ export async function outputProcessing( attachment.url = objectStore.getAppFileUrl(attachment.key) }) } + } else if (column.type == FieldTypes.BB_REFERENCE) { + for (let row of enriched) { + if (!row[property]) { + continue + } + row[property] = await processOutputBBReferences( + row[property], + column.subtype as FieldSubtype + ) + } } } if (opts.squash) { diff --git a/packages/server/src/utilities/rowProcessor/tests/bbReferenceProcessor.spec.ts b/packages/server/src/utilities/rowProcessor/tests/bbReferenceProcessor.spec.ts index 748bc7efb6..abba713fba 100644 --- a/packages/server/src/utilities/rowProcessor/tests/bbReferenceProcessor.spec.ts +++ b/packages/server/src/utilities/rowProcessor/tests/bbReferenceProcessor.spec.ts @@ -1,6 +1,9 @@ import * as backendCore from "@budibase/backend-core" import { FieldSubtype, User } from "@budibase/types" -import { processInputBBReferences } from "../bbReferenceProcessor" +import { + processInputBBReferences, + processOutputBBReferences, +} from "../bbReferenceProcessor" import { generator, structures } from "@budibase/backend-core/tests" import { InvalidBBRefError } from "../errors" @@ -128,4 +131,44 @@ describe("bbReferenceProcessor", () => { }) }) }) + + describe("processOutputBBReferences", () => { + describe("subtype user", () => { + it("fetches user given a valid string id", async () => { + const userId = generator.guid() + + const userFromCache = structures.users.user() + mockedCacheGetUser.mockResolvedValueOnce(userFromCache) + + const result = await processOutputBBReferences( + userId, + FieldSubtype.USER + ) + + expect(result).toEqual(userFromCache) + expect(mockedCacheGetUser).toBeCalledTimes(1) + expect(mockedCacheGetUser).toBeCalledWith(userId) + }) + + it("fetches user given a valid string id csv", async () => { + const userId1 = generator.guid() + const userId2 = generator.guid() + + const userFromCache1 = structures.users.user({ _id: userId1 }) + const userFromCache2 = structures.users.user({ _id: userId2 }) + mockedCacheGetUser.mockResolvedValueOnce(userFromCache1) + mockedCacheGetUser.mockResolvedValueOnce(userFromCache2) + + const result = await processOutputBBReferences( + [userId1, userId2].join(","), + FieldSubtype.USER + ) + + expect(result).toEqual([userFromCache1, userFromCache2]) + expect(mockedCacheGetUser).toBeCalledTimes(2) + expect(mockedCacheGetUser).toBeCalledWith(userId1) + expect(mockedCacheGetUser).toBeCalledWith(userId2) + }) + }) + }) })