From e5453cc766db170affd212cc61dac0697f7ca9e0 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Mon, 9 Dec 2024 13:45:11 +0000 Subject: [PATCH] only fire view updated events when 2 or more groups are applied --- .../src/api/controllers/view/viewsV2.ts | 14 +++---- .../src/api/routes/tests/viewV2.spec.ts | 41 ++++++++++++++++++- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/packages/server/src/api/controllers/view/viewsV2.ts b/packages/server/src/api/controllers/view/viewsV2.ts index 66a6194096..4c65d673e8 100644 --- a/packages/server/src/api/controllers/view/viewsV2.ts +++ b/packages/server/src/api/controllers/view/viewsV2.ts @@ -167,15 +167,11 @@ export async function create(ctx: Ctx) { async function handleViewFilterEvents(existingView: ViewV2, view: ViewV2) { const filterGroups = view.queryUI?.groups?.length || 0 const properties = { filterGroups, tableId: view.tableId } - if (!existingView?.queryUI) { - await events.view.filterCreated(properties) - } else { - if ( - filterGroups > - ((existingView && existingView?.queryUI?.groups?.length) || 0) - ) { - await events.view.filterUpdated(properties) - } + if ( + filterGroups >= 2 && + ((existingView && existingView?.queryUI?.groups?.length) || 0) + ) { + await events.view.filterUpdated(properties) } } diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 739603bb3c..244a0a23eb 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -997,6 +997,45 @@ if (descriptions.length) { expect(events.view.updated).toHaveBeenCalledTimes(1) }) + it("handles view grouped filter events", async () => { + view.queryUI = { + logicalOperator: UILogicalOperator.ALL, + onEmptyFilter: EmptyFilterOption.RETURN_ALL, + groups: [ + { + logicalOperator: UILogicalOperator.ALL, + filters: [ + { + operator: BasicOperator.EQUAL, + field: "newField", + value: "newValue", + }, + ], + }, + ], + } + await config.api.viewV2.update(view) + expect(events.view.filterUpdated).not.toHaveBeenCalled() + + // @ts-ignore + view.queryUI.groups.push({ + logicalOperator: UILogicalOperator.ALL, + filters: [ + { + operator: BasicOperator.EQUAL, + field: "otherField", + value: "otherValue", + }, + ], + }) + + await config.api.viewV2.update(view) + expect(events.view.filterUpdated).toHaveBeenCalledWith({ + filterGroups: 2, + tableId: view.tableId, + }) + }) + it("can update all fields", async () => { const tableId = table._id! @@ -1360,8 +1399,6 @@ if (descriptions.length) { }, }) - expect(events.view.filterCreated).toHaveBeenCalledTimes(1) - updatedView = await config.api.viewV2.get(view.id) expected = { onEmptyFilter: EmptyFilterOption.RETURN_ALL,