Fix types
This commit is contained in:
parent
34d97ab16a
commit
ad44b7ab81
|
@ -10,34 +10,20 @@ import { InvalidBBRefError } from "./errors"
|
||||||
|
|
||||||
const ROW_PREFIX = DocumentType.ROW + SEPARATOR
|
const ROW_PREFIX = DocumentType.ROW + SEPARATOR
|
||||||
|
|
||||||
export function processInputBBReferences<T = FieldType.BB_REFERENCE_SINGLE>(
|
export function processInputBBReferences(
|
||||||
value: string,
|
value: string | { _id: string },
|
||||||
type: T
|
type: FieldType.BB_REFERENCE_SINGLE
|
||||||
): Promise<string | null>
|
): Promise<string | null>
|
||||||
export function processInputBBReferences<
|
export function processInputBBReferences(
|
||||||
T = FieldType.BB_REFERENCE,
|
|
||||||
TS = FieldSubtype.USER
|
|
||||||
>(
|
|
||||||
value: string | string[] | { _id: string } | { _id: string }[],
|
value: string | string[] | { _id: string } | { _id: string }[],
|
||||||
type: T,
|
type: FieldType.BB_REFERENCE,
|
||||||
subtype: TS
|
subtype: FieldSubtype.USER | FieldSubtype.USERS
|
||||||
): Promise<string | null>
|
): 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<
|
export async function processInputBBReferences(
|
||||||
T extends FieldType.BB_REFERENCE | FieldType.BB_REFERENCE_SINGLE,
|
|
||||||
TS extends FieldSubtype.USER | FieldSubtype.USERS
|
|
||||||
>(
|
|
||||||
value: string | string[] | { _id: string } | { _id: string }[],
|
value: string | string[] | { _id: string } | { _id: string }[],
|
||||||
type: T,
|
type: FieldType.BB_REFERENCE | FieldType.BB_REFERENCE_SINGLE,
|
||||||
subtype?: TS
|
subtype?: FieldSubtype
|
||||||
): Promise<string | string[] | null> {
|
): Promise<string | string[] | null> {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case FieldType.BB_REFERENCE: {
|
case FieldType.BB_REFERENCE: {
|
||||||
|
@ -125,23 +111,20 @@ interface UserReferenceInfo {
|
||||||
lastName: string
|
lastName: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export function processOutputBBReferences<T = FieldType.BB_REFERENCE_SINGLE>(
|
export function processOutputBBReferences(
|
||||||
value: string,
|
value: string,
|
||||||
type: T
|
type: FieldType.BB_REFERENCE_SINGLE
|
||||||
): Promise<UserReferenceInfo>
|
): Promise<UserReferenceInfo>
|
||||||
export function processOutputBBReferences<
|
export function processOutputBBReferences(
|
||||||
T = FieldType.BB_REFERENCE,
|
value: string,
|
||||||
TS = FieldSubtype.USER
|
type: FieldType.BB_REFERENCE,
|
||||||
>(value: string, type: T, subtype: TS): Promise<UserReferenceInfo[]>
|
subtype: FieldSubtype.USER | FieldSubtype.USERS
|
||||||
export function processOutputBBReferences<
|
): Promise<UserReferenceInfo[]>
|
||||||
T = FieldType.BB_REFERENCE,
|
|
||||||
TS = FieldSubtype.USERS
|
|
||||||
>(value: string[], type: T, subtype: TS): Promise<UserReferenceInfo[]>
|
|
||||||
|
|
||||||
export async function processOutputBBReferences(
|
export async function processOutputBBReferences(
|
||||||
value: string | string[],
|
value: string | string[],
|
||||||
type: FieldType.BB_REFERENCE | FieldType.BB_REFERENCE_SINGLE,
|
type: FieldType.BB_REFERENCE | FieldType.BB_REFERENCE_SINGLE,
|
||||||
subtype?: FieldSubtype.USER | FieldSubtype.USERS
|
subtype?: FieldSubtype
|
||||||
) {
|
) {
|
||||||
if (value === null || value === undefined) {
|
if (value === null || value === undefined) {
|
||||||
// Already processed or nothing to process
|
// Already processed or nothing to process
|
||||||
|
|
|
@ -160,18 +160,14 @@ export async function inputProcessing(
|
||||||
if (attachment?.url) {
|
if (attachment?.url) {
|
||||||
delete clonedRow[key].url
|
delete clonedRow[key].url
|
||||||
}
|
}
|
||||||
}
|
} else if (field.type === FieldType.BB_REFERENCE && value) {
|
||||||
|
|
||||||
if (
|
|
||||||
(field.type === FieldType.BB_REFERENCE ||
|
|
||||||
field.type === FieldType.BB_REFERENCE_SINGLE) &&
|
|
||||||
value
|
|
||||||
) {
|
|
||||||
clonedRow[key] = await processInputBBReferences(
|
clonedRow[key] = await processInputBBReferences(
|
||||||
value,
|
value,
|
||||||
field.type,
|
field.type,
|
||||||
field.subtype
|
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 (
|
} else if (
|
||||||
!opts.skipBBReferences &&
|
!opts.skipBBReferences &&
|
||||||
(column.type == FieldType.BB_REFERENCE ||
|
column.type == FieldType.BB_REFERENCE
|
||||||
column.type == FieldType.BB_REFERENCE_SINGLE)
|
|
||||||
) {
|
) {
|
||||||
for (let row of enriched) {
|
for (let row of enriched) {
|
||||||
row[property] = await processOutputBBReferences(
|
row[property] = await processOutputBBReferences(
|
||||||
|
@ -262,6 +257,16 @@ export async function outputProcessing<T extends Row[] | Row>(
|
||||||
column.subtype
|
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
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import _ from "lodash"
|
import _ from "lodash"
|
||||||
import * as backendCore from "@budibase/backend-core"
|
import * as backendCore from "@budibase/backend-core"
|
||||||
import { FieldSubtype, User } from "@budibase/types"
|
import { FieldSubtype, FieldType, User } from "@budibase/types"
|
||||||
import {
|
import {
|
||||||
processInputBBReferences,
|
processInputBBReferences,
|
||||||
processOutputBBReferences,
|
processOutputBBReferences,
|
||||||
|
@ -63,7 +63,11 @@ describe("bbReferenceProcessor", () => {
|
||||||
const userId = user!._id!
|
const userId = user!._id!
|
||||||
|
|
||||||
const result = await config.doInTenant(() =>
|
const result = await config.doInTenant(() =>
|
||||||
processInputBBReferences(userId, FieldSubtype.USER)
|
processInputBBReferences(
|
||||||
|
userId,
|
||||||
|
FieldType.BB_REFERENCE,
|
||||||
|
FieldSubtype.USER
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(result).toEqual(userId)
|
expect(result).toEqual(userId)
|
||||||
|
@ -76,7 +80,11 @@ describe("bbReferenceProcessor", () => {
|
||||||
|
|
||||||
await expect(
|
await expect(
|
||||||
config.doInTenant(() =>
|
config.doInTenant(() =>
|
||||||
processInputBBReferences(userId, FieldSubtype.USER)
|
processInputBBReferences(
|
||||||
|
userId,
|
||||||
|
FieldType.BB_REFERENCE,
|
||||||
|
FieldSubtype.USER
|
||||||
|
)
|
||||||
)
|
)
|
||||||
).rejects.toThrow(new InvalidBBRefError(userId, FieldSubtype.USER))
|
).rejects.toThrow(new InvalidBBRefError(userId, FieldSubtype.USER))
|
||||||
expect(cacheGetUsersSpy).toHaveBeenCalledTimes(1)
|
expect(cacheGetUsersSpy).toHaveBeenCalledTimes(1)
|
||||||
|
@ -88,7 +96,11 @@ describe("bbReferenceProcessor", () => {
|
||||||
|
|
||||||
const userIdCsv = userIds.join(" , ")
|
const userIdCsv = userIds.join(" , ")
|
||||||
const result = await config.doInTenant(() =>
|
const result = await config.doInTenant(() =>
|
||||||
processInputBBReferences(userIdCsv, FieldSubtype.USER)
|
processInputBBReferences(
|
||||||
|
userIdCsv,
|
||||||
|
FieldType.BB_REFERENCE,
|
||||||
|
FieldSubtype.USER
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(result).toEqual(userIds.join(","))
|
expect(result).toEqual(userIds.join(","))
|
||||||
|
@ -108,7 +120,11 @@ describe("bbReferenceProcessor", () => {
|
||||||
|
|
||||||
await expect(
|
await expect(
|
||||||
config.doInTenant(() =>
|
config.doInTenant(() =>
|
||||||
processInputBBReferences(userIdCsv, FieldSubtype.USER)
|
processInputBBReferences(
|
||||||
|
userIdCsv,
|
||||||
|
FieldType.BB_REFERENCE,
|
||||||
|
FieldSubtype.USER
|
||||||
|
)
|
||||||
)
|
)
|
||||||
).rejects.toThrow(new InvalidBBRefError(wrongId, FieldSubtype.USER))
|
).rejects.toThrow(new InvalidBBRefError(wrongId, FieldSubtype.USER))
|
||||||
})
|
})
|
||||||
|
@ -117,7 +133,11 @@ describe("bbReferenceProcessor", () => {
|
||||||
const userId = _.sample(users)!._id!
|
const userId = _.sample(users)!._id!
|
||||||
|
|
||||||
const result = await config.doInTenant(() =>
|
const result = await config.doInTenant(() =>
|
||||||
processInputBBReferences({ _id: userId }, FieldSubtype.USER)
|
processInputBBReferences(
|
||||||
|
{ _id: userId },
|
||||||
|
FieldType.BB_REFERENCE,
|
||||||
|
FieldSubtype.USER
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(result).toEqual(userId)
|
expect(result).toEqual(userId)
|
||||||
|
@ -129,7 +149,11 @@ describe("bbReferenceProcessor", () => {
|
||||||
const userIds = _.sampleSize(users, 3).map(x => x._id!)
|
const userIds = _.sampleSize(users, 3).map(x => x._id!)
|
||||||
|
|
||||||
const result = await config.doInTenant(() =>
|
const result = await config.doInTenant(() =>
|
||||||
processInputBBReferences(userIds, FieldSubtype.USER)
|
processInputBBReferences(
|
||||||
|
userIds,
|
||||||
|
FieldType.BB_REFERENCE,
|
||||||
|
FieldSubtype.USER
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(result).toEqual(userIds.join(","))
|
expect(result).toEqual(userIds.join(","))
|
||||||
|
@ -139,7 +163,11 @@ describe("bbReferenceProcessor", () => {
|
||||||
|
|
||||||
it("empty strings will return null", async () => {
|
it("empty strings will return null", async () => {
|
||||||
const result = await config.doInTenant(() =>
|
const result = await config.doInTenant(() =>
|
||||||
processInputBBReferences("", FieldSubtype.USER)
|
processInputBBReferences(
|
||||||
|
"",
|
||||||
|
FieldType.BB_REFERENCE,
|
||||||
|
FieldSubtype.USER
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(result).toEqual(null)
|
expect(result).toEqual(null)
|
||||||
|
@ -147,7 +175,11 @@ describe("bbReferenceProcessor", () => {
|
||||||
|
|
||||||
it("empty arrays will return null", async () => {
|
it("empty arrays will return null", async () => {
|
||||||
const result = await config.doInTenant(() =>
|
const result = await config.doInTenant(() =>
|
||||||
processInputBBReferences([], FieldSubtype.USER)
|
processInputBBReferences(
|
||||||
|
[],
|
||||||
|
FieldType.BB_REFERENCE,
|
||||||
|
FieldSubtype.USER
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(result).toEqual(null)
|
expect(result).toEqual(null)
|
||||||
|
@ -157,7 +189,11 @@ describe("bbReferenceProcessor", () => {
|
||||||
const userId = _.sample(users)!._id!
|
const userId = _.sample(users)!._id!
|
||||||
const userMetadataId = backendCore.db.generateUserMetadataID(userId)
|
const userMetadataId = backendCore.db.generateUserMetadataID(userId)
|
||||||
const result = await config.doInTenant(() =>
|
const result = await config.doInTenant(() =>
|
||||||
processInputBBReferences(userMetadataId, FieldSubtype.USER)
|
processInputBBReferences(
|
||||||
|
userMetadataId,
|
||||||
|
FieldType.BB_REFERENCE,
|
||||||
|
FieldSubtype.USER
|
||||||
|
)
|
||||||
)
|
)
|
||||||
expect(result).toBe(userId)
|
expect(result).toBe(userId)
|
||||||
})
|
})
|
||||||
|
@ -171,7 +207,11 @@ describe("bbReferenceProcessor", () => {
|
||||||
const userId = user._id!
|
const userId = user._id!
|
||||||
|
|
||||||
const result = await config.doInTenant(() =>
|
const result = await config.doInTenant(() =>
|
||||||
processOutputBBReferences(userId, FieldSubtype.USER)
|
processOutputBBReferences(
|
||||||
|
userId,
|
||||||
|
FieldType.BB_REFERENCE,
|
||||||
|
FieldSubtype.USER
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(result).toEqual([
|
expect(result).toEqual([
|
||||||
|
@ -195,6 +235,7 @@ describe("bbReferenceProcessor", () => {
|
||||||
const result = await config.doInTenant(() =>
|
const result = await config.doInTenant(() =>
|
||||||
processOutputBBReferences(
|
processOutputBBReferences(
|
||||||
[userId1, userId2].join(","),
|
[userId1, userId2].join(","),
|
||||||
|
FieldType.BB_REFERENCE,
|
||||||
FieldSubtype.USER
|
FieldSubtype.USER
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue