1
0
Fork 0
mirror of synced 2024-07-20 21:55:54 +12:00

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,14 +192,17 @@ describe("bbReferenceProcessor", () => {
)
)
expect(result).toHaveLength(2)
expect(result).toEqual(
[user1, user2].map(u => ({
_id: u._id,
primaryDisplay: u.email,
email: u.email,
firstName: u.firstName,
lastName: u.lastName,
}))
expect.arrayContaining(
[user1, user2].map(u => ({
_id: u._id,
primaryDisplay: u.email,
email: u.email,
firstName: u.firstName,
lastName: u.lastName,
}))
)
)
expect(cacheGetUsersSpy).toBeCalledTimes(1)
expect(cacheGetUsersSpy).toBeCalledWith([userId1, userId2])