Fix types

This commit is contained in:
Adria Navarro 2024-04-26 09:47:46 +02:00
parent 34d97ab16a
commit ad44b7ab81
3 changed files with 83 additions and 54 deletions

View File

@ -10,34 +10,20 @@ import { InvalidBBRefError } from "./errors"
const ROW_PREFIX = DocumentType.ROW + SEPARATOR
export function processInputBBReferences<T = FieldType.BB_REFERENCE_SINGLE>(
value: string,
type: T
export function processInputBBReferences(
value: string | { _id: string },
type: FieldType.BB_REFERENCE_SINGLE
): Promise<string | null>
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<string | null>
export function processInputBBReferences<
T = FieldType.BB_REFERENCE,
TS = FieldSubtype.USERS
>(
value: string | string[] | { _id: string } | { _id: string }[],
type: T,
subtype: TS
): Promise<string[] | null>
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<string | string[] | null> {
switch (type) {
case FieldType.BB_REFERENCE: {
@ -125,23 +111,20 @@ interface UserReferenceInfo {
lastName: string
}
export function processOutputBBReferences<T = FieldType.BB_REFERENCE_SINGLE>(
export function processOutputBBReferences(
value: string,
type: T
type: FieldType.BB_REFERENCE_SINGLE
): Promise<UserReferenceInfo>
export function processOutputBBReferences<
T = FieldType.BB_REFERENCE,
TS = FieldSubtype.USER
>(value: string, type: T, subtype: TS): Promise<UserReferenceInfo[]>
export function processOutputBBReferences<
T = FieldType.BB_REFERENCE,
TS = FieldSubtype.USERS
>(value: string[], type: T, subtype: TS): Promise<UserReferenceInfo[]>
export function processOutputBBReferences(
value: string,
type: FieldType.BB_REFERENCE,
subtype: FieldSubtype.USER | FieldSubtype.USERS
): Promise<UserReferenceInfo[]>
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

View File

@ -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<T extends Row[] | Row>(
}
} 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<T extends Row[] | Row>(
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
)
}
}
}

View File

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