From 31b29662d6b4218902fec226d082b375f0a79b2a Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 20 Sep 2023 11:07:32 +0200 Subject: [PATCH] Handle delete behaviours --- .../rowProcessor/bbReferenceProcessor.ts | 33 ++++++++++++------- .../src/utilities/rowProcessor/index.ts | 2 +- .../tests/bbReferenceProcessor.spec.ts | 33 +++++++++++++++---- 3 files changed, 48 insertions(+), 20 deletions(-) diff --git a/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts b/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts index 62deb63358..66ffbfe4cf 100644 --- a/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts +++ b/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts @@ -6,20 +6,25 @@ import { InvalidBBRefError } from "./errors" export async function processInputBBReferences( value: string | string[] | { _id: string } | { _id: string }[], subtype: FieldSubtype -): Promise { - const result: string[] = [] +): Promise { + 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, diff --git a/packages/server/src/utilities/rowProcessor/index.ts b/packages/server/src/utilities/rowProcessor/index.ts index 8aea0a08aa..7098eefa47 100644 --- a/packages/server/src/utilities/rowProcessor/index.ts +++ b/packages/server/src/utilities/rowProcessor/index.ts @@ -229,7 +229,7 @@ export async function outputProcessing( } } else if (column.type == FieldTypes.BB_REFERENCE) { for (let row of enriched) { - if (!row[property]) { + if (!row[property] == null) { continue } row[property] = await processOutputBBReferences( diff --git a/packages/server/src/utilities/rowProcessor/tests/bbReferenceProcessor.spec.ts b/packages/server/src/utilities/rowProcessor/tests/bbReferenceProcessor.spec.ts index b27de26b2f..67a44a86f2 100644 --- a/packages/server/src/utilities/rowProcessor/tests/bbReferenceProcessor.spec.ts +++ b/packages/server/src/utilities/rowProcessor/tests/bbReferenceProcessor.spec.ts @@ -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])