Merge pull request #11828 from Budibase/BUDI-7403/fix-deletions
Fix bb references deletions
This commit is contained in:
commit
1f2a66fc77
|
@ -20,6 +20,8 @@
|
|||
const results = await API.searchUsers({
|
||||
...searchParams,
|
||||
})
|
||||
|
||||
// Mapping to the expected data within RelationshipCell
|
||||
return {
|
||||
...results,
|
||||
data: undefined,
|
||||
|
|
|
@ -6,20 +6,29 @@ 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(idOrDoc =>
|
||||
typeof idOrDoc === "string" ? idOrDoc : idOrDoc._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 +39,7 @@ export async function processInputBBReferences(
|
|||
throw utils.unreachable(subtype)
|
||||
}
|
||||
|
||||
return result.join(",")
|
||||
return referenceIds.join(",") || undefined
|
||||
}
|
||||
|
||||
export async function processOutputBBReferences(
|
||||
|
@ -47,7 +56,17 @@ export async function processOutputBBReferences(
|
|||
switch (subtype) {
|
||||
case FieldSubtype.USER:
|
||||
const { users } = await cache.user.getUsers(ids)
|
||||
return users
|
||||
if (!users.length) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
return users.map(u => ({
|
||||
_id: u._id,
|
||||
primaryDisplay: u.email,
|
||||
email: u.email,
|
||||
firstName: u.firstName,
|
||||
lastName: u.lastName,
|
||||
}))
|
||||
|
||||
default:
|
||||
throw utils.unreachable(subtype)
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -153,9 +169,11 @@ describe("bbReferenceProcessor", () => {
|
|||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
...user,
|
||||
budibaseAccess: true,
|
||||
_rev: expect.any(String),
|
||||
_id: user._id,
|
||||
primaryDisplay: user.email,
|
||||
email: user.email,
|
||||
firstName: user.firstName,
|
||||
lastName: user.lastName,
|
||||
},
|
||||
])
|
||||
expect(cacheGetUsersSpy).toBeCalledTimes(1)
|
||||
|
@ -174,12 +192,17 @@ describe("bbReferenceProcessor", () => {
|
|||
)
|
||||
)
|
||||
|
||||
expect(result).toHaveLength(2)
|
||||
expect(result).toEqual(
|
||||
[user1, user2].map(u => ({
|
||||
...u,
|
||||
budibaseAccess: true,
|
||||
_rev: expect.any(String),
|
||||
}))
|
||||
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])
|
||||
|
|
Loading…
Reference in New Issue