Handle delete behaviours
This commit is contained in:
parent
244af30b6a
commit
31b29662d6
|
@ -6,20 +6,25 @@ import { InvalidBBRefError } from "./errors"
|
||||||
export async function processInputBBReferences(
|
export async function processInputBBReferences(
|
||||||
value: string | string[] | { _id: string } | { _id: string }[],
|
value: string | string[] | { _id: string } | { _id: string }[],
|
||||||
subtype: FieldSubtype
|
subtype: FieldSubtype
|
||||||
): Promise<string> {
|
): Promise<string | undefined> {
|
||||||
const result: string[] = []
|
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) {
|
switch (subtype) {
|
||||||
case FieldSubtype.USER:
|
case FieldSubtype.USER:
|
||||||
if (Array.isArray(value)) {
|
const { notFoundIds } = await cache.user.getUsers(referenceIds)
|
||||||
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)
|
|
||||||
|
|
||||||
if (notFoundIds?.length) {
|
if (notFoundIds?.length) {
|
||||||
throw new InvalidBBRefError(notFoundIds[0], FieldSubtype.USER)
|
throw new InvalidBBRefError(notFoundIds[0], FieldSubtype.USER)
|
||||||
|
@ -30,7 +35,7 @@ export async function processInputBBReferences(
|
||||||
throw utils.unreachable(subtype)
|
throw utils.unreachable(subtype)
|
||||||
}
|
}
|
||||||
|
|
||||||
return result.join(",")
|
return referenceIds.join(",") || undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function processOutputBBReferences(
|
export async function processOutputBBReferences(
|
||||||
|
@ -47,6 +52,10 @@ export async function processOutputBBReferences(
|
||||||
switch (subtype) {
|
switch (subtype) {
|
||||||
case FieldSubtype.USER:
|
case FieldSubtype.USER:
|
||||||
const { users } = await cache.user.getUsers(ids)
|
const { users } = await cache.user.getUsers(ids)
|
||||||
|
if (!users.length) {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
|
||||||
return users.map(u => ({
|
return users.map(u => ({
|
||||||
_id: u._id,
|
_id: u._id,
|
||||||
primaryDisplay: u.email,
|
primaryDisplay: u.email,
|
||||||
|
|
|
@ -229,7 +229,7 @@ export async function outputProcessing<T extends Row[] | Row>(
|
||||||
}
|
}
|
||||||
} else if (column.type == FieldTypes.BB_REFERENCE) {
|
} else if (column.type == FieldTypes.BB_REFERENCE) {
|
||||||
for (let row of enriched) {
|
for (let row of enriched) {
|
||||||
if (!row[property]) {
|
if (!row[property] == null) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
row[property] = await processOutputBBReferences(
|
row[property] = await processOutputBBReferences(
|
||||||
|
|
|
@ -138,6 +138,22 @@ describe("bbReferenceProcessor", () => {
|
||||||
expect(cacheGetUsersSpy).toBeCalledTimes(1)
|
expect(cacheGetUsersSpy).toBeCalledTimes(1)
|
||||||
expect(cacheGetUsersSpy).toBeCalledWith(userIds)
|
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(result).toEqual(
|
||||||
|
expect.arrayContaining(
|
||||||
[user1, user2].map(u => ({
|
[user1, user2].map(u => ({
|
||||||
_id: u._id,
|
_id: u._id,
|
||||||
primaryDisplay: u.email,
|
primaryDisplay: u.email,
|
||||||
|
@ -185,6 +203,7 @@ describe("bbReferenceProcessor", () => {
|
||||||
lastName: u.lastName,
|
lastName: u.lastName,
|
||||||
}))
|
}))
|
||||||
)
|
)
|
||||||
|
)
|
||||||
expect(cacheGetUsersSpy).toBeCalledTimes(1)
|
expect(cacheGetUsersSpy).toBeCalledTimes(1)
|
||||||
expect(cacheGetUsersSpy).toBeCalledWith([userId1, userId2])
|
expect(cacheGetUsersSpy).toBeCalledWith([userId1, userId2])
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue