Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

fix(db): keep filter type and cohort ids when loading saved reports (#524)

Carl-Gerhard Lindesvärd7c4d22ae4b6b
diff --git a/packages/db/src/services/reports.service.test.ts b/packages/db/src/services/reports.service.test.ts
index ccc2fada9..d7230c738 100644
--- a/packages/db/src/services/reports.service.test.ts
+++ b/packages/db/src/services/reports.service.test.ts
@@ -5,7 +5,7 @@
*/
import type { IChartEventFilter, IChartEventItem } from '@openpanel/validation';
import { describe, expect, it } from 'vitest';
-import { mergeGlobalFilters } from './reports.service';
+import { mergeGlobalFilters, transformFilter } from './reports.service';
const globalFilter: IChartEventFilter = {
id: 'g1',
@@ -64,3 +64,48 @@ describe('mergeGlobalFilters', () => {
expect(eventSeries.filters).toEqual([eventFilter]);
});
});
+
+describe('transformFilter', () => {
+ it('keeps the cast type so a saved date filter stays a date filter', () => {
+ const result = transformFilter(
+ {
+ id: 'f1',
+ name: 'profile.properties.subscription_expire',
+ operator: 'gte',
+ value: ['2026-09-27'],
+ type: 'date',
+ },
+ 0,
+ );
+ expect(result.type).toBe('date');
+ });
+
+ it('keeps cohort ids', () => {
+ const result = transformFilter(
+ {
+ id: 'f1',
+ name: 'cohort',
+ operator: 'inCohort',
+ value: [],
+ cohortIds: ['c1', 'c2'],
+ cohortId: 'c0',
+ },
+ 0,
+ );
+ expect(result.cohortIds).toEqual(['c1', 'c2']);
+ expect(result.cohortId).toBe('c0');
+ });
+
+ it('leaves the optional fields off legacy filters', () => {
+ const result = transformFilter(
+ { name: 'path', operator: 'is', value: '/pricing' as never },
+ 1,
+ );
+ expect(result).toEqual({
+ id: 'B',
+ name: 'path',
+ operator: 'is',
+ value: ['/pricing'],
+ });
+ });
+});
diff --git a/packages/db/src/services/reports.service.ts b/packages/db/src/services/reports.service.ts
index bd25ddc94..a9ae9a2fc 100644
--- a/packages/db/src/services/reports.service.ts
+++ b/packages/db/src/services/reports.service.ts
@@ -50,6 +50,11 @@ export function transformFilter(
operator: filter.operator ?? 'is',
value:
typeof filter.value === 'string' ? [filter.value] : (filter.value ?? []),
+ // Pass the optional fields through so a saved report keeps its cast type
+ // and cohort selection after it is reloaded.
+ ...(filter.type ? { type: filter.type } : {}),
+ ...(filter.cohortId ? { cohortId: filter.cohortId } : {}),
+ ...(filter.cohortIds ? { cohortIds: filter.cohortIds } : {}),
};
}