diff --git a/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts b/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts new file mode 100644 index 0000000000..ae31810856 --- /dev/null +++ b/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts @@ -0,0 +1,6 @@ +import { FieldSubtype } from "@budibase/types" + +export async function processOutputBBReferences( + value: string, + subtype: FieldSubtype +) {} diff --git a/packages/server/src/utilities/rowProcessor/index.ts b/packages/server/src/utilities/rowProcessor/index.ts index 92afd0dcb1..702c6c1d7d 100644 --- a/packages/server/src/utilities/rowProcessor/index.ts +++ b/packages/server/src/utilities/rowProcessor/index.ts @@ -5,8 +5,9 @@ import { ObjectStoreBuckets } from "../../constants" import { context, db as dbCore, objectStore } from "@budibase/backend-core" import { InternalTables } from "../../db/utils" import { TYPE_TRANSFORM_MAP } from "./map" -import { Row, RowAttachment, Table } from "@budibase/types" +import { FieldSubtype, Row, RowAttachment, Table } from "@budibase/types" import { cloneDeep } from "lodash/fp" +import { processOutputBBReferences } from "./bbReferenceProcessor" export * from "./utils" type AutoColumnProcessingOpts = { @@ -166,6 +167,13 @@ export async function inputProcessing( }) } } + + if (field.type === FieldTypes.BB_REFERENCE) { + clonedRow[key] = await processOutputBBReferences( + value, + field.subtype as FieldSubtype + ) + } } if (!clonedRow._id || !clonedRow._rev) { diff --git a/packages/server/src/utilities/rowProcessor/tests/inputProcessing.spec.ts b/packages/server/src/utilities/rowProcessor/tests/inputProcessing.spec.ts new file mode 100644 index 0000000000..d04221c1d0 --- /dev/null +++ b/packages/server/src/utilities/rowProcessor/tests/inputProcessing.spec.ts @@ -0,0 +1,67 @@ +import TestConfiguration from "../../../tests/utilities/TestConfiguration" +import { inputProcessing } from ".." +import { generator, structures } from "@budibase/backend-core/tests" +import { FieldType, FieldTypeSubtypes, Table } from "@budibase/types" +import * as bbReferenceProcessor from "../bbReferenceProcessor" + +const config = new TestConfiguration() + +jest.mock("../bbReferenceProcessor", (): typeof bbReferenceProcessor => ({ + processOutputBBReferences: jest.fn(), +})) + +describe("rowProcessor - inputProcessing", () => { + beforeAll(async () => { + await config.init() + }) + + it("populate user BB references", async () => { + const userId = generator.guid() + + const table: Table = { + _id: generator.guid(), + name: "TestTable", + type: "table", + schema: { + name: { + type: FieldType.STRING, + name: "name", + constraints: { + presence: true, + type: "string", + }, + }, + user: { + type: FieldType.BB_REFERENCE, + subtype: FieldTypeSubtypes.BB_REFERENCE.USER, + name: "user", + constraints: { + presence: true, + type: "string", + }, + }, + }, + } + + const newRow = { + name: "Jack", + user: "123", + } + + const user = structures.users.user() + + ;( + bbReferenceProcessor.processOutputBBReferences as jest.Mock + ).mockResolvedValue(user) + + const { row } = await inputProcessing(userId, table, newRow) + + expect(bbReferenceProcessor.processOutputBBReferences).toBeCalledTimes(1) + expect(bbReferenceProcessor.processOutputBBReferences).toBeCalledWith( + "123", + "user" + ) + + expect(row).toEqual({ ...newRow, user }) + }) +}) diff --git a/packages/types/src/documents/app/row.ts b/packages/types/src/documents/app/row.ts index 6001695b13..708cbc3b9e 100644 --- a/packages/types/src/documents/app/row.ts +++ b/packages/types/src/documents/app/row.ts @@ -34,3 +34,13 @@ export interface Row extends Document { _viewId?: string [key: string]: any } + +export enum FieldSubtype { + USER = "user", +} + +export const FieldTypeSubtypes = { + BB_REFERENCE: { + USER: FieldSubtype.USER, + }, +}