Handle delete behaviours

This commit is contained in:
Adria Navarro 2023-09-20 11:07:32 +02:00
parent 244af30b6a
commit 31b29662d6
3 changed files with 48 additions and 20 deletions

View File

@ -6,20 +6,25 @@ import { InvalidBBRefError } from "./errors"
export async function processInputBBReferences(
value: string | string[] | { _id: string } | { _id: string }[],
subtype: FieldSubtype
): Promise<string> {
const result: string[] = []
): Promise<string | undefined> {
const referenceIds: string[] = []
if (Array.isArray(value)) {
referenceIds.push(...value.map(x => (typeof x === "string" ? x : x._id)))
} else if (typeof value !== "string") {
referenceIds.push(value._id)
} else {
referenceIds.push(
...value
.split(",")
.filter(x => x)
.map((id: string) => id.trim())
)
}
switch (subtype) {
case FieldSubtype.USER:
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()))
}
const { notFoundIds } = await cache.user.getUsers(result)
const { notFoundIds } = await cache.user.getUsers(referenceIds)
if (notFoundIds?.length) {
throw new InvalidBBRefError(notFoundIds[0], FieldSubtype.USER)
@ -30,7 +35,7 @@ export async function processInputBBReferences(
throw utils.unreachable(subtype)
}
return result.join(",")
return referenceIds.join(",") || undefined
}
export async function processOutputBBReferences(
@ -47,6 +52,10 @@ export async function processOutputBBReferences(
switch (subtype) {
case FieldSubtype.USER:
const { users } = await cache.user.getUsers(ids)
if (!users.length) {
return undefined
}
return users.map(u => ({
_id: u._id,
primaryDisplay: u.email,

View File

@ -229,7 +229,7 @@ export async function outputProcessing<T extends Row[] | Row>(
}
} else if (column.type == FieldTypes.BB_REFERENCE) {
for (let row of enriched) {
if (!row[property]) {
if (!row[property] == null) {
continue
}
row[property] = await processOutputBBReferences(

View File

@ -138,6 +138,22 @@ describe("bbReferenceProcessor", () => {
expect(cacheGetUsersSpy).toBeCalledTimes(1)
expect(cacheGetUsersSpy).toBeCalledWith(userIds)
})
it("empty strings will return undefined", async () => {
const result = await config.doInTenant(() =>
processInputBBReferences("", FieldSubtype.USER)
)
expect(result).toEqual(undefined)
})
it("empty arrays will return undefined", async () => {
const result = await config.doInTenant(() =>
processInputBBReferences([], FieldSubtype.USER)
)
expect(result).toEqual(undefined)
})
})
})
@ -176,7 +192,9 @@ describe("bbReferenceProcessor", () => {
)
)
expect(result).toHaveLength(2)
expect(result).toEqual(
expect.arrayContaining(
[user1, user2].map(u => ({
_id: u._id,
primaryDisplay: u.email,
@ -185,6 +203,7 @@ describe("bbReferenceProcessor", () => {
lastName: u.lastName,
}))
)
)
expect(cacheGetUsersSpy).toBeCalledTimes(1)
expect(cacheGetUsersSpy).toBeCalledWith([userId1, userId2])
})