Skip to content

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

fix(api): keep filter type and cohort ids when loading saved reports

Carl-Gerhard Lindesvärdacb0155678b8
diff --git a/packages/core/src/modules/report/report.service.test.ts b/packages/core/src/modules/report/report.service.test.ts
index 4456ae735..0cc1fe0ce 100644
--- a/packages/core/src/modules/report/report.service.test.ts
+++ b/packages/core/src/modules/report/report.service.test.ts
@@ -1,9 +1,11 @@
/**
- * Unit tests for mergeGlobalFilters, which combines report-level global filters
- * with each event series' own filters (AND semantics). Pure function.
+ * Unit tests for the pure report helpers: mergeGlobalFilters, which combines
+ * report-level global filters with each event series' own filters (AND
+ * semantics), and transformFilter, which normalizes a saved filter.
*/
import { describe, expect, it } from 'bun:test';
import type { IChartEventFilter, IChartEventItem } from './report.constants';
+import { transformFilter } from './report.service';
import { mergeGlobalFilters } from './src/series';
const globalFilter: IChartEventFilter = {
@@ -63,3 +65,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/core/src/modules/report/report.service.ts b/packages/core/src/modules/report/report.service.ts
index efc573749..22ef73b14 100644
--- a/packages/core/src/modules/report/report.service.ts
+++ b/packages/core/src/modules/report/report.service.ts
@@ -39,6 +39,11 @@ export function transformFilter(
operator: filter.operator ?? 'is',
value:
typeof filter.value === 'string' ? [filter.value] : (filter.value ?? []),
+ // Without these a saved date filter reloads as text and hits the untyped
+ // comparison path, and cohort filters lose their selection.
+ ...(filter.type ? { type: filter.type } : {}),
+ ...(filter.cohortId ? { cohortId: filter.cohortId } : {}),
+ ...(filter.cohortIds ? { cohortIds: filter.cohortIds } : {}),
};
}