1
0
Fork 0
mirror of synced 2024-09-25 22:01:43 +12:00
budibase/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts

35 lines
998 B
TypeScript
Raw Normal View History

2023-09-15 21:21:10 +12:00
import { cache } from "@budibase/backend-core"
import { utils } from "@budibase/shared-core"
2023-09-15 22:07:25 +12:00
import { Document, FieldSubtype } from "@budibase/types"
import { InvalidBBRefError } from "./errors"
2023-09-15 20:33:36 +12:00
2023-09-15 21:21:10 +12:00
export async function processInputBBReferences(
2023-09-15 22:07:25 +12:00
value: string | string[] | { _id: string } | { _id: string }[],
2023-09-15 20:33:36 +12:00
subtype: FieldSubtype
2023-09-15 22:07:25 +12:00
): Promise<string> {
const result: string[] = []
2023-09-15 21:21:10 +12:00
switch (subtype) {
case FieldSubtype.USER:
2023-09-15 22:07:25 +12:00
if (Array.isArray(value)) {
result.push(...value.map(x => (typeof x === "string" ? x : x._id)))
} else if (typeof value !== "string") {
result.push(value._id)
} else {
result.push(...value.split(",").map((id: string) => id.trim()))
}
for (const id of result) {
const user = await cache.user.getUser(id)
if (!user) {
throw new InvalidBBRefError(id, FieldSubtype.USER)
}
2023-09-15 21:21:10 +12:00
}
break
default:
2023-09-15 22:07:25 +12:00
throw utils.unreachable(subtype)
2023-09-15 21:21:10 +12:00
}
2023-09-15 22:07:25 +12:00
return result.join(",")
2023-09-15 21:21:10 +12:00
}