1
0
Fork 0
mirror of synced 2024-09-26 06:11:49 +12:00
budibase/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts

101 lines
2.6 KiB
TypeScript
Raw Normal View History

import { cache, db as dbCore } from "@budibase/backend-core"
2023-09-15 21:21:10 +12:00
import { utils } from "@budibase/shared-core"
2024-04-26 22:23:11 +12:00
import {
BBReferenceFieldSubType,
DocumentType,
SEPARATOR,
} from "@budibase/types"
2023-09-15 22:07:25 +12:00
import { InvalidBBRefError } from "./errors"
2023-09-15 20:33:36 +12:00
const ROW_PREFIX = DocumentType.ROW + SEPARATOR
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 }[],
2024-04-26 22:23:11 +12:00
subtype: BBReferenceFieldSubType.USER | BBReferenceFieldSubType.USERS
2023-10-05 03:50:05 +13:00
): Promise<string | string[] | null> {
let referenceIds: string[] = []
2023-09-20 21:07:32 +12:00
if (Array.isArray(value)) {
2023-09-23 03:33:13 +12:00
referenceIds.push(
...value.map(idOrDoc =>
typeof idOrDoc === "string" ? idOrDoc : idOrDoc._id
)
)
2023-09-20 21:07:32 +12:00
} else if (typeof value !== "string") {
referenceIds.push(value._id)
} else {
referenceIds.push(
...value
.split(",")
.filter(x => x)
.map((id: string) => id.trim())
)
}
2023-09-15 21:21:10 +12:00
// make sure all reference IDs are correct global user IDs
// they may be user metadata references (start with row prefix)
// and these need to be converted to global IDs
referenceIds = referenceIds.map(id => {
if (id?.startsWith(ROW_PREFIX)) {
return dbCore.getGlobalIDFromUserMetadataID(id)
} else {
return id
}
})
2023-09-15 21:21:10 +12:00
switch (subtype) {
2024-04-26 22:23:11 +12:00
case BBReferenceFieldSubType.USER:
case BBReferenceFieldSubType.USERS: {
2023-09-20 21:07:32 +12:00
const { notFoundIds } = await cache.user.getUsers(referenceIds)
if (notFoundIds?.length) {
2024-04-26 22:23:11 +12:00
throw new InvalidBBRefError(
notFoundIds[0],
BBReferenceFieldSubType.USER
)
2023-09-15 21:21:10 +12:00
}
2023-10-05 03:50:05 +13:00
2024-04-26 22:23:11 +12:00
if (subtype === BBReferenceFieldSubType.USERS) {
2023-10-05 03:50:05 +13:00
return referenceIds
}
2023-10-05 03:14:28 +13:00
return referenceIds.join(",") || null
2024-03-20 04:58:25 +13:00
}
2023-09-15 21:21:10 +12:00
default:
2023-09-15 22:07:25 +12:00
throw utils.unreachable(subtype)
2023-09-15 21:21:10 +12:00
}
}
2023-09-15 23:31:22 +12:00
export async function processOutputBBReferences(
2023-10-05 03:50:05 +13:00
value: string | string[],
2024-04-26 22:23:11 +12:00
subtype: BBReferenceFieldSubType.USER | BBReferenceFieldSubType.USERS
2023-09-15 23:31:22 +12:00
) {
2023-10-05 03:50:05 +13:00
if (value === null || value === undefined) {
2023-09-19 23:17:07 +12:00
// Already processed or nothing to process
2023-09-30 03:39:18 +13:00
return value || undefined
2023-09-15 23:47:08 +12:00
}
2023-10-05 03:50:05 +13:00
const ids =
typeof value === "string" ? value.split(",").filter(id => !!id) : value
2023-09-19 23:17:07 +12:00
2023-09-15 23:31:22 +12:00
switch (subtype) {
2024-04-26 22:23:11 +12:00
case BBReferenceFieldSubType.USER:
case BBReferenceFieldSubType.USERS: {
const { users } = await cache.user.getUsers(ids)
2023-09-20 21:07:32 +12:00
if (!users.length) {
return undefined
}
2023-09-20 20:35:22 +12:00
return users.map(u => ({
_id: u._id,
primaryDisplay: u.email,
email: u.email,
firstName: u.firstName,
lastName: u.lastName,
2023-09-20 20:35:22 +12:00
}))
2024-03-20 04:58:25 +13:00
}
2023-09-15 23:31:22 +12:00
default:
throw utils.unreachable(subtype)
}
}