From 24107ee3b2a3c096076402cb30c0e03e132de6c4 Mon Sep 17 00:00:00 2001 From: Alexandre Brandizzi Date: Wed, 30 Sep 2026 17:04:44 -0300 Subject: [PATCH] fix(workorders): match renamed service filter aliases --- .../utils/advanced-filter-predicates.ts | 13 +++++-- .../work-orders/utils/table-filter-utils.ts | 4 ++- .../utils/table-filter-utils.test.ts | 36 +++++++++++++++++++ 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/domain/work-orders/utils/advanced-filter-predicates.ts b/src/domain/work-orders/utils/advanced-filter-predicates.ts index 530bbd70..67891972 100644 --- a/src/domain/work-orders/utils/advanced-filter-predicates.ts +++ b/src/domain/work-orders/utils/advanced-filter-predicates.ts @@ -1,4 +1,5 @@ import type { WorkOrderAdvancedFilters } from "@/domain/work-orders/types/work-order-advanced-filters"; +import type { Service } from "@/domain/services/types/service"; import { ASSIGNEE_FILTER_UNASSIGNED } from "@/domain/work-orders/types/work-order-list"; import type { WorkOrderTableRow } from "@/domain/work-orders/types/work-order-table-row"; import { dateRangeFor } from "@/domain/work-orders/utils/advanced-filters-utils"; @@ -12,6 +13,7 @@ import { addDaysIso } from "@/domain/work-orders/utils/wizard-date-utils"; type AdvancedFilterPredicate = ( row: WorkOrderTableRow, filters: WorkOrderAdvancedFilters, + serviceHistory: Service[], ) => boolean; const isOpenUndated = (row: WorkOrderTableRow): boolean => @@ -60,8 +62,15 @@ const matchesDispatcher: AdvancedFilterPredicate = (row, filters) => { const matchesStatus: AdvancedFilterPredicate = (row, filters) => !filters.statuses.size || (!!row.status && filters.statuses.has(row.status as never)); -const matchesPmType: AdvancedFilterPredicate = (row, filters) => - !filters.pmTypes.size || filters.pmTypes.has(row.pm); +const matchesPmType: AdvancedFilterPredicate = (row, filters, serviceHistory) => { + if (!filters.pmTypes.size || filters.pmTypes.has(row.pm)) return true; + if (row.serviceId == null) return false; + + const linkedService = serviceHistory.find( + (service) => String(service.id) === String(row.serviceId), + ); + return linkedService != null && filters.pmTypes.has(linkedService.name); +}; const matchesVendorTech: AdvancedFilterPredicate = (row, filters) => !filters.vendorTechs.size || diff --git a/src/domain/work-orders/utils/table-filter-utils.ts b/src/domain/work-orders/utils/table-filter-utils.ts index 2300335f..78196dc1 100644 --- a/src/domain/work-orders/utils/table-filter-utils.ts +++ b/src/domain/work-orders/utils/table-filter-utils.ts @@ -1,4 +1,5 @@ import type { WorkOrderAdvancedFilters } from "@/domain/work-orders/types/work-order-advanced-filters"; +import type { Service } from "@/domain/services/types/service"; import { ASSIGNEE_FILTER_UNASSIGNED } from "@/domain/work-orders/types/work-order-list"; import type { WorkOrderBarFilters } from "@/domain/work-orders/types/work-order-list-filters"; import type { WOType } from "@/domain/work-orders/types/work-order-wizard"; @@ -36,8 +37,9 @@ export function enrichTableRow(row: WorkOrderTableRow): WorkOrderTableRow { export function matchesAdvancedFilters( row: WorkOrderTableRow, filters: WorkOrderAdvancedFilters, + serviceHistory: Service[] = [], ): boolean { - return ADVANCED_FILTER_PREDICATES.every((predicate) => predicate(row, filters)); + return ADVANCED_FILTER_PREDICATES.every((predicate) => predicate(row, filters, serviceHistory)); } function rowMatchesSearch(row: WorkOrderTableRow, query: string): boolean { diff --git a/src/test/domain/work-orders/utils/table-filter-utils.test.ts b/src/test/domain/work-orders/utils/table-filter-utils.test.ts index 9798afb8..56097d24 100644 --- a/src/test/domain/work-orders/utils/table-filter-utils.test.ts +++ b/src/test/domain/work-orders/utils/table-filter-utils.test.ts @@ -370,6 +370,42 @@ describe("buildDayGroups", () => { }); describe("matchesAdvancedFilters", () => { + it("matches inactive renamed registry services across the global date range", () => { + const row = makeRow({ + pm: "Historic Service Name", + serviceId: 701, + scheduledOn: "2024-03-12", + }); + const filters = { + ...EMPTY_ADVANCED_FILTERS, + pmTypes: new Set(["Current Service Name"]), + }; + const serviceHistory = [ + { + id: 701, + name: "Current Service Name", + trade: "Electrical", + iconKey: "wrench", + requiresCompletionDocument: false, + completionDocTemplate: null, + isActive: false, + supportedWorkOrderTypes: ["PM" as const], + }, + ]; + + expect(matchesAdvancedFilters(row, filters, serviceHistory)).toBe(true); + expect(row.pm).toBe("Historic Service Name"); + }); + + it("continues matching legacy service labels without a linked registry identity", () => { + const filters = { + ...EMPTY_ADVANCED_FILTERS, + pmTypes: new Set(["Legacy Problem"]), + }; + + expect(matchesAdvancedFilters(makeRow({ pm: "Legacy Problem" }), filters, [])).toBe(true); + }); + it("filters by site and type", () => { const weekMonday = mondayOf(todayIso()); const row = makeRow({