1
0
Fork 0
mirror of synced 2024-07-06 15:00:49 +12:00

Process bb ref on input processing

This commit is contained in:
Adria Navarro 2023-09-15 10:33:36 +02:00
parent 9860023c9e
commit a1bb33bb4a
4 changed files with 92 additions and 1 deletions

View file

@ -0,0 +1,6 @@
import { FieldSubtype } from "@budibase/types"
export async function processOutputBBReferences(
value: string,
subtype: FieldSubtype
) {}

View file

@ -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) {

View file

@ -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 })
})
})

View file

@ -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,
},
}