Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion packages/db/src/services/reports.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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'],
});
});
});
5 changes: 5 additions & 0 deletions packages/db/src/services/reports.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } : {}),
};
}

Expand Down
Loading