diff --git a/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts b/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts index e028359d60..d9464df3ef 100644 --- a/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts +++ b/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts @@ -10,34 +10,20 @@ import { InvalidBBRefError } from "./errors" const ROW_PREFIX = DocumentType.ROW + SEPARATOR -export function processInputBBReferences( - value: string, - type: T +export function processInputBBReferences( + value: string | { _id: string }, + type: FieldType.BB_REFERENCE_SINGLE ): Promise -export function processInputBBReferences< - T = FieldType.BB_REFERENCE, - TS = FieldSubtype.USER ->( +export function processInputBBReferences( value: string | string[] | { _id: string } | { _id: string }[], - type: T, - subtype: TS + type: FieldType.BB_REFERENCE, + subtype: FieldSubtype.USER | FieldSubtype.USERS ): Promise -export function processInputBBReferences< - T = FieldType.BB_REFERENCE, - TS = FieldSubtype.USERS ->( - value: string | string[] | { _id: string } | { _id: string }[], - type: T, - subtype: TS -): Promise -export async function processInputBBReferences< - T extends FieldType.BB_REFERENCE | FieldType.BB_REFERENCE_SINGLE, - TS extends FieldSubtype.USER | FieldSubtype.USERS ->( +export async function processInputBBReferences( value: string | string[] | { _id: string } | { _id: string }[], - type: T, - subtype?: TS + type: FieldType.BB_REFERENCE | FieldType.BB_REFERENCE_SINGLE, + subtype?: FieldSubtype ): Promise { switch (type) { case FieldType.BB_REFERENCE: { @@ -125,23 +111,20 @@ interface UserReferenceInfo { lastName: string } -export function processOutputBBReferences( +export function processOutputBBReferences( value: string, - type: T + type: FieldType.BB_REFERENCE_SINGLE ): Promise -export function processOutputBBReferences< - T = FieldType.BB_REFERENCE, - TS = FieldSubtype.USER ->(value: string, type: T, subtype: TS): Promise -export function processOutputBBReferences< - T = FieldType.BB_REFERENCE, - TS = FieldSubtype.USERS ->(value: string[], type: T, subtype: TS): Promise +export function processOutputBBReferences( + value: string, + type: FieldType.BB_REFERENCE, + subtype: FieldSubtype.USER | FieldSubtype.USERS +): Promise export async function processOutputBBReferences( value: string | string[], type: FieldType.BB_REFERENCE | FieldType.BB_REFERENCE_SINGLE, - subtype?: FieldSubtype.USER | FieldSubtype.USERS + subtype?: FieldSubtype ) { if (value === null || value === undefined) { // Already processed or nothing to process diff --git a/packages/server/src/utilities/rowProcessor/index.ts b/packages/server/src/utilities/rowProcessor/index.ts index 57cafef8ad..499ea0e4c5 100644 --- a/packages/server/src/utilities/rowProcessor/index.ts +++ b/packages/server/src/utilities/rowProcessor/index.ts @@ -160,18 +160,14 @@ export async function inputProcessing( if (attachment?.url) { delete clonedRow[key].url } - } - - if ( - (field.type === FieldType.BB_REFERENCE || - field.type === FieldType.BB_REFERENCE_SINGLE) && - value - ) { + } else if (field.type === FieldType.BB_REFERENCE && value) { clonedRow[key] = await processInputBBReferences( value, field.type, field.subtype ) + } else if (field.type === FieldType.BB_REFERENCE_SINGLE && value) { + clonedRow[key] = await processInputBBReferences(value, field.type) } } @@ -252,8 +248,7 @@ export async function outputProcessing( } } else if ( !opts.skipBBReferences && - (column.type == FieldType.BB_REFERENCE || - column.type == FieldType.BB_REFERENCE_SINGLE) + column.type == FieldType.BB_REFERENCE ) { for (let row of enriched) { row[property] = await processOutputBBReferences( @@ -262,6 +257,16 @@ export async function outputProcessing( column.subtype ) } + } else if ( + !opts.skipBBReferences && + column.type == FieldType.BB_REFERENCE_SINGLE + ) { + for (let row of enriched) { + row[property] = await processOutputBBReferences( + row[property], + column.type + ) + } } } diff --git a/packages/server/src/utilities/rowProcessor/tests/bbReferenceProcessor.spec.ts b/packages/server/src/utilities/rowProcessor/tests/bbReferenceProcessor.spec.ts index e6ceaae323..c0090ab4e0 100644 --- a/packages/server/src/utilities/rowProcessor/tests/bbReferenceProcessor.spec.ts +++ b/packages/server/src/utilities/rowProcessor/tests/bbReferenceProcessor.spec.ts @@ -1,6 +1,6 @@ import _ from "lodash" import * as backendCore from "@budibase/backend-core" -import { FieldSubtype, User } from "@budibase/types" +import { FieldSubtype, FieldType, User } from "@budibase/types" import { processInputBBReferences, processOutputBBReferences, @@ -63,7 +63,11 @@ describe("bbReferenceProcessor", () => { const userId = user!._id! const result = await config.doInTenant(() => - processInputBBReferences(userId, FieldSubtype.USER) + processInputBBReferences( + userId, + FieldType.BB_REFERENCE, + FieldSubtype.USER + ) ) expect(result).toEqual(userId) @@ -76,7 +80,11 @@ describe("bbReferenceProcessor", () => { await expect( config.doInTenant(() => - processInputBBReferences(userId, FieldSubtype.USER) + processInputBBReferences( + userId, + FieldType.BB_REFERENCE, + FieldSubtype.USER + ) ) ).rejects.toThrow(new InvalidBBRefError(userId, FieldSubtype.USER)) expect(cacheGetUsersSpy).toHaveBeenCalledTimes(1) @@ -88,7 +96,11 @@ describe("bbReferenceProcessor", () => { const userIdCsv = userIds.join(" , ") const result = await config.doInTenant(() => - processInputBBReferences(userIdCsv, FieldSubtype.USER) + processInputBBReferences( + userIdCsv, + FieldType.BB_REFERENCE, + FieldSubtype.USER + ) ) expect(result).toEqual(userIds.join(",")) @@ -108,7 +120,11 @@ describe("bbReferenceProcessor", () => { await expect( config.doInTenant(() => - processInputBBReferences(userIdCsv, FieldSubtype.USER) + processInputBBReferences( + userIdCsv, + FieldType.BB_REFERENCE, + FieldSubtype.USER + ) ) ).rejects.toThrow(new InvalidBBRefError(wrongId, FieldSubtype.USER)) }) @@ -117,7 +133,11 @@ describe("bbReferenceProcessor", () => { const userId = _.sample(users)!._id! const result = await config.doInTenant(() => - processInputBBReferences({ _id: userId }, FieldSubtype.USER) + processInputBBReferences( + { _id: userId }, + FieldType.BB_REFERENCE, + FieldSubtype.USER + ) ) expect(result).toEqual(userId) @@ -129,7 +149,11 @@ describe("bbReferenceProcessor", () => { const userIds = _.sampleSize(users, 3).map(x => x._id!) const result = await config.doInTenant(() => - processInputBBReferences(userIds, FieldSubtype.USER) + processInputBBReferences( + userIds, + FieldType.BB_REFERENCE, + FieldSubtype.USER + ) ) expect(result).toEqual(userIds.join(",")) @@ -139,7 +163,11 @@ describe("bbReferenceProcessor", () => { it("empty strings will return null", async () => { const result = await config.doInTenant(() => - processInputBBReferences("", FieldSubtype.USER) + processInputBBReferences( + "", + FieldType.BB_REFERENCE, + FieldSubtype.USER + ) ) expect(result).toEqual(null) @@ -147,7 +175,11 @@ describe("bbReferenceProcessor", () => { it("empty arrays will return null", async () => { const result = await config.doInTenant(() => - processInputBBReferences([], FieldSubtype.USER) + processInputBBReferences( + [], + FieldType.BB_REFERENCE, + FieldSubtype.USER + ) ) expect(result).toEqual(null) @@ -157,7 +189,11 @@ describe("bbReferenceProcessor", () => { const userId = _.sample(users)!._id! const userMetadataId = backendCore.db.generateUserMetadataID(userId) const result = await config.doInTenant(() => - processInputBBReferences(userMetadataId, FieldSubtype.USER) + processInputBBReferences( + userMetadataId, + FieldType.BB_REFERENCE, + FieldSubtype.USER + ) ) expect(result).toBe(userId) }) @@ -171,7 +207,11 @@ describe("bbReferenceProcessor", () => { const userId = user._id! const result = await config.doInTenant(() => - processOutputBBReferences(userId, FieldSubtype.USER) + processOutputBBReferences( + userId, + FieldType.BB_REFERENCE, + FieldSubtype.USER + ) ) expect(result).toEqual([ @@ -195,6 +235,7 @@ describe("bbReferenceProcessor", () => { const result = await config.doInTenant(() => processOutputBBReferences( [userId1, userId2].join(","), + FieldType.BB_REFERENCE, FieldSubtype.USER ) )