Files
stats/packages/trpc/src/routers/session.ts
Carl-Gerhard Lindesvärd 81a7e5d62e feat: dashboard v2, esm, upgrades (#211)
* esm

* wip

* wip

* wip

* wip

* wip

* wip

* subscription notice

* wip

* wip

* wip

* fix envs

* fix: update docker build

* fix

* esm/types

* delete dashboard :D

* add patches to dockerfiles

* update packages + catalogs + ts

* wip

* remove native libs

* ts

* improvements

* fix redirects and fetching session

* try fix favicon

* fixes

* fix

* order and resize reportds within a dashboard

* improvements

* wip

* added userjot to dashboard

* fix

* add op

* wip

* different cache key

* improve date picker

* fix table

* event details loading

* redo onboarding completely

* fix login

* fix

* fix

* extend session, billing and improve bars

* fix

* reduce price on 10M
2025-10-16 12:27:44 +02:00

65 lines
1.8 KiB
TypeScript

import { z } from 'zod';
import { getSessionList, sessionService } from '@openpanel/db';
import { zChartEventFilter } from '@openpanel/validation';
import { createTRPCRouter, protectedProcedure } from '../trpc';
export function encodeCursor(cursor: {
createdAt: string;
id: string;
}): string {
const json = JSON.stringify(cursor);
return Buffer.from(json, 'utf8').toString('base64url'); // URL-safe
}
export function decodeCursor(
encoded: string,
): { createdAt: string; id: string } | null {
try {
const json = Buffer.from(encoded, 'base64url').toString('utf8');
const obj = JSON.parse(json);
if (typeof obj.createdAt === 'string' && typeof obj.id === 'string') {
return obj;
}
return null;
} catch {
return null;
}
}
export const sessionRouter = createTRPCRouter({
list: protectedProcedure
.input(
z.object({
projectId: z.string(),
profileId: z.string().optional(),
cursor: z.string().nullish(),
filters: z.array(zChartEventFilter).default([]),
startDate: z.date().optional(),
endDate: z.date().optional(),
search: z.string().optional(),
take: z.number().default(50),
}),
)
.query(async ({ input }) => {
const cursor = input.cursor ? decodeCursor(input.cursor) : null;
const data = await getSessionList({
...input,
cursor,
});
return {
data: data.items,
meta: {
next: data.meta.next ? encodeCursor(data.meta.next) : undefined,
},
};
}),
byId: protectedProcedure
.input(z.object({ sessionId: z.string(), projectId: z.string() }))
.query(async ({ input: { sessionId, projectId } }) => {
return sessionService.byId(sessionId, projectId);
}),
});