From ed830835a4ac274fd81c6b9bf19f9f23c953c8dd Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 20 Sep 2023 10:07:38 +0200 Subject: [PATCH] use bulk cache in processOutputBBReferences --- .../rowProcessor/bbReferenceProcessor.ts | 20 ++----- .../tests/bbReferenceProcessor.spec.ts | 54 ++++++++++--------- 2 files changed, 34 insertions(+), 40 deletions(-) diff --git a/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts b/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts index b2523a8548..fe783bccd1 100644 --- a/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts +++ b/packages/server/src/utilities/rowProcessor/bbReferenceProcessor.ts @@ -42,26 +42,14 @@ export async function processOutputBBReferences( return value } - const result = [] - - const validIds = value.split(",").filter(id => !!id) + const ids = value.split(",").filter(id => !!id) switch (subtype) { case FieldSubtype.USER: - for (const id of validIds) { - try { - const user = await cache.user.getUser(id) - if (user) { - result.push(user) - } - } catch { - // If user cannot be found, we just strip it - } - } - break + const { users } = await cache.user.getUsers(ids) + return users + default: throw utils.unreachable(subtype) } - - return result } diff --git a/packages/server/src/utilities/rowProcessor/tests/bbReferenceProcessor.spec.ts b/packages/server/src/utilities/rowProcessor/tests/bbReferenceProcessor.spec.ts index 3955157bec..369565eb3c 100644 --- a/packages/server/src/utilities/rowProcessor/tests/bbReferenceProcessor.spec.ts +++ b/packages/server/src/utilities/rowProcessor/tests/bbReferenceProcessor.spec.ts @@ -142,39 +142,45 @@ describe("bbReferenceProcessor", () => { describe("processOutputBBReferences", () => { describe("subtype user", () => { it("fetches user given a valid string id", async () => { - const userId = generator.guid() + const user = _.sample(users)! + const userId = user._id! - const userFromCache = structures.users.user() - mockedCacheGetUser.mockResolvedValueOnce(userFromCache) - - const result = await processOutputBBReferences( - userId, - FieldSubtype.USER + const result = await config.doInTenant(() => + processOutputBBReferences(userId, FieldSubtype.USER) ) - expect(result).toEqual([userFromCache]) - expect(mockedCacheGetUser).toBeCalledTimes(1) - expect(mockedCacheGetUser).toBeCalledWith(userId) + expect(result).toEqual([ + { + ...user, + budibaseAccess: true, + _rev: expect.any(String), + }, + ]) + expect(cacheGetUsersSpy).toBeCalledTimes(1) + expect(cacheGetUsersSpy).toBeCalledWith([userId]) }) it("fetches user given a valid string id csv", async () => { - const userId1 = generator.guid() - const userId2 = generator.guid() + const [user1, user2] = _.sampleSize(users, 2) + const userId1 = user1._id! + const userId2 = user2._id! - const userFromCache1 = structures.users.user({ _id: userId1 }) - const userFromCache2 = structures.users.user({ _id: userId2 }) - mockedCacheGetUser.mockResolvedValueOnce(userFromCache1) - mockedCacheGetUser.mockResolvedValueOnce(userFromCache2) - - const result = await processOutputBBReferences( - [userId1, userId2].join(","), - FieldSubtype.USER + const result = await config.doInTenant(() => + processOutputBBReferences( + [userId1, userId2].join(","), + FieldSubtype.USER + ) ) - expect(result).toEqual([userFromCache1, userFromCache2]) - expect(mockedCacheGetUser).toBeCalledTimes(2) - expect(mockedCacheGetUser).toBeCalledWith(userId1) - expect(mockedCacheGetUser).toBeCalledWith(userId2) + expect(result).toEqual( + [user1, user2].map(u => ({ + ...u, + budibaseAccess: true, + _rev: expect.any(String), + })) + ) + expect(cacheGetUsersSpy).toBeCalledTimes(1) + expect(cacheGetUsersSpy).toBeCalledWith([userId1, userId2]) }) }) })