1
0
Fork 0
mirror of synced 2024-08-09 15:17:57 +12:00

Handle output processing

This commit is contained in:
Adria Navarro 2023-09-15 13:31:22 +02:00
parent 9510c37b7a
commit bebe342b33
3 changed files with 172 additions and 1 deletions

View file

@ -1,6 +1,6 @@
import { cache } from "@budibase/backend-core"
import { utils } from "@budibase/shared-core"
import { Document, FieldSubtype } from "@budibase/types"
import { FieldSubtype } from "@budibase/types"
import { InvalidBBRefError } from "./errors"
export async function processInputBBReferences(
@ -39,3 +39,31 @@ export async function processInputBBReferences(
return result.join(",")
}
export async function processOutputBBReferences(
value: string,
subtype: FieldSubtype
) {
const result = []
switch (subtype) {
case FieldSubtype.USER:
for (const id of value.split(",")) {
try {
const user = await cache.user.getUser(id)
if (user) {
result.push(user)
}
} catch {}
}
break
default:
throw utils.unreachable(subtype)
}
if (result.length > 1) {
return result
}
return result[0]
}

View file

@ -5,6 +5,7 @@ import * as bbReferenceProcessor from "../bbReferenceProcessor"
jest.mock("../bbReferenceProcessor", (): typeof bbReferenceProcessor => ({
processInputBBReferences: jest.fn(),
processOutputBBReferences: jest.fn(),
}))
describe("rowProcessor - inputProcessing", () => {

View file

@ -0,0 +1,142 @@
import {
FieldSubtype,
FieldType,
FieldTypeSubtypes,
Table,
} from "@budibase/types"
import { outputProcessing } from ".."
import { generator, structures } from "@budibase/backend-core/tests"
import * as bbReferenceProcessor from "../bbReferenceProcessor"
jest.mock("../bbReferenceProcessor", (): typeof bbReferenceProcessor => ({
processInputBBReferences: jest.fn(),
processOutputBBReferences: jest.fn(),
}))
describe("rowProcessor - outputProcessing", () => {
beforeEach(() => {
jest.resetAllMocks()
})
const processOutputBBReferencesMock =
bbReferenceProcessor.processOutputBBReferences as jest.Mock
it("fetches bb user references given a populated field", async () => {
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: false,
type: "string",
},
},
},
}
const row = {
name: "Jack",
user: "123",
}
const user = structures.users.user()
processOutputBBReferencesMock.mockResolvedValue(user)
const result = await outputProcessing(table, row, { squash: false })
expect(result).toEqual({ name: "Jack", user })
expect(bbReferenceProcessor.processOutputBBReferences).toBeCalledTimes(1)
expect(bbReferenceProcessor.processOutputBBReferences).toBeCalledWith(
"123",
FieldSubtype.USER
)
})
it("does not fetch bb references when fields are empty", async () => {
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: false,
type: "string",
},
},
},
}
const row = {
name: "Jack",
}
const result = await outputProcessing(table, row, { squash: false })
expect(result).toEqual({ name: "Jack" })
expect(bbReferenceProcessor.processOutputBBReferences).not.toBeCalled()
})
it("does not fetch bb references when not in the schema", async () => {
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.NUMBER,
name: "user",
constraints: {
presence: false,
type: "string",
},
},
},
}
const row = {
name: "Jack",
user: "123",
}
const result = await outputProcessing(table, row, { squash: false })
expect(result).toEqual({ name: "Jack", user: "123" })
expect(bbReferenceProcessor.processOutputBBReferences).not.toBeCalled()
})
})