Feature/move list to client (#50)

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-09-01 15:02:12 +02:00
committed by GitHub
parent c2abdaadf2
commit 668434d246
181 changed files with 2922 additions and 1959 deletions

View File

@@ -1,7 +1,16 @@
import { TRPCError } from '@trpc/server';
import { escape } from 'sqlstring';
import { z } from 'zod';
import { chQuery, convertClickhouseDateToJs, db } from '@openpanel/db';
import {
chQuery,
convertClickhouseDateToJs,
db,
getEventList,
getEvents,
TABLE_NAMES,
} from '@openpanel/db';
import { zChartEventFilter } from '@openpanel/validation';
import { getProjectAccessCached } from '../access';
import { TRPCAccessError } from '../errors';
@@ -31,6 +40,72 @@ export const eventRouter = createTRPCRouter({
});
}),
byId: protectedProcedure
.input(
z.object({
id: z.string(),
projectId: z.string(),
})
)
.query(async ({ input: { id, projectId } }) => {
const res = await getEvents(
`SELECT * FROM ${TABLE_NAMES.events} WHERE id = ${escape(id)} AND project_id = ${escape(projectId)};`
);
if (!res?.[0]) {
throw new TRPCError({
code: 'NOT_FOUND',
message: 'Event not found',
});
}
return res[0];
}),
events: protectedProcedure
.input(
z.object({
projectId: z.string(),
cursor: z.number().optional(),
limit: z.number().default(8),
profileId: z.string().optional(),
take: z.number().default(50),
events: z.array(z.string()).optional(),
filters: z.array(zChartEventFilter).default([]),
startDate: z.date().optional(),
endDate: z.date().optional(),
meta: z.boolean().optional(),
profile: z.boolean().optional(),
})
)
.query(async ({ input }) => getEventList(input)),
conversions: publicProcedure
.input(
z.object({
projectId: z.string(),
})
)
.query(async ({ input: { projectId } }) => {
const conversions = await db.eventMeta.findMany({
where: {
projectId,
conversion: true,
},
});
if (conversions.length === 0) {
return [];
}
return getEvents(
`SELECT * FROM ${TABLE_NAMES.events} WHERE project_id = ${escape(projectId)} AND name IN (${conversions.map((c) => escape(c.name)).join(', ')}) ORDER BY created_at DESC LIMIT 20;`,
{
profile: true,
meta: true,
}
);
}),
bots: publicProcedure
.input(
z.object({