chore(root): migrate to biome
This commit is contained in:
@@ -43,7 +43,7 @@ export async function getProjectAccess({
|
||||
|
||||
export const getOrganizationAccessCached = cacheable(
|
||||
getOrganizationAccess,
|
||||
60 * 5
|
||||
60 * 5,
|
||||
);
|
||||
export async function getOrganizationAccess({
|
||||
userId,
|
||||
|
||||
@@ -29,13 +29,13 @@ import {
|
||||
import type { ISerieDataItem } from '@openpanel/common';
|
||||
import { alphabetIds } from '@openpanel/constants';
|
||||
import {
|
||||
TABLE_NAMES,
|
||||
chQuery,
|
||||
createSqlBuilder,
|
||||
formatClickhouseDate,
|
||||
getChartSql,
|
||||
getEventFiltersWhereClause,
|
||||
getProfiles,
|
||||
TABLE_NAMES,
|
||||
} from '@openpanel/db';
|
||||
import type {
|
||||
FinalChart,
|
||||
@@ -53,7 +53,7 @@ function getEventLegend(event: IChartEvent) {
|
||||
|
||||
export function withFormula(
|
||||
{ formula, events }: IChartInput,
|
||||
series: Awaited<ReturnType<typeof getChartSerie>>
|
||||
series: Awaited<ReturnType<typeof getChartSerie>>,
|
||||
) {
|
||||
if (!formula) {
|
||||
return series;
|
||||
@@ -131,7 +131,7 @@ export function withFormula(
|
||||
const toDynamicISODateWithTZ = (
|
||||
date: string,
|
||||
blueprint: string,
|
||||
interval: IInterval
|
||||
interval: IInterval,
|
||||
) => {
|
||||
// If we have a space in the date we know it's a date with time
|
||||
if (date.includes(' ')) {
|
||||
@@ -324,8 +324,8 @@ export async function getFunnelData({
|
||||
repeat({}, diff - 1).map((_, index) => ({
|
||||
count: acc[acc.length - 1]?.count ?? 0,
|
||||
level: item.level + index + 1,
|
||||
}))
|
||||
)
|
||||
})),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -337,7 +337,7 @@ export async function getFunnelData({
|
||||
},
|
||||
];
|
||||
},
|
||||
[] as typeof funnelRes
|
||||
[] as typeof funnelRes,
|
||||
);
|
||||
|
||||
const totalSessions = last(filledFunnelRes)?.count ?? 0;
|
||||
@@ -367,7 +367,7 @@ export async function getFunnelData({
|
||||
dropoffCount: number;
|
||||
dropoffPercent: number;
|
||||
previousCount: number;
|
||||
}[]
|
||||
}[],
|
||||
);
|
||||
|
||||
return {
|
||||
@@ -432,7 +432,7 @@ export async function getFunnelStep({
|
||||
|
||||
return getProfiles(
|
||||
res.map((r) => r.id),
|
||||
projectId
|
||||
projectId,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -444,7 +444,7 @@ export async function getChartSerie(payload: IGetChartDataInput) {
|
||||
getChartSql({
|
||||
...payload,
|
||||
breakdowns: [],
|
||||
})
|
||||
}),
|
||||
);
|
||||
}
|
||||
return result;
|
||||
@@ -452,7 +452,7 @@ export async function getChartSerie(payload: IGetChartDataInput) {
|
||||
|
||||
return getSeries()
|
||||
.then((data) =>
|
||||
completeSerie(data, payload.startDate, payload.endDate, payload.interval)
|
||||
completeSerie(data, payload.startDate, payload.endDate, payload.interval),
|
||||
)
|
||||
.then((series) => {
|
||||
return Object.keys(series).map((key) => {
|
||||
@@ -470,7 +470,7 @@ export async function getChartSerie(payload: IGetChartDataInput) {
|
||||
date: toDynamicISODateWithTZ(
|
||||
item.date,
|
||||
payload.startDate,
|
||||
payload.interval
|
||||
payload.interval,
|
||||
),
|
||||
})),
|
||||
};
|
||||
@@ -486,8 +486,8 @@ export async function getChartSeries(input: IChartInputWithDates) {
|
||||
getChartSerie({
|
||||
...input,
|
||||
event,
|
||||
})
|
||||
)
|
||||
}),
|
||||
),
|
||||
)
|
||||
).flat();
|
||||
|
||||
@@ -512,7 +512,7 @@ export async function getChart(input: IChartInput) {
|
||||
getChartSeries({
|
||||
...input,
|
||||
...previousPeriod,
|
||||
})
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -529,11 +529,11 @@ export async function getChart(input: IChartInput) {
|
||||
const final: FinalChart = {
|
||||
series: series.map((serie, index) => {
|
||||
const eventIndex = input.events.findIndex(
|
||||
(event) => event.id === serie.event.id
|
||||
(event) => event.id === serie.event.id,
|
||||
);
|
||||
const alphaId = alphabetIds[eventIndex];
|
||||
const previousSerie = previousSeries?.find(
|
||||
(prevSerie) => getSerieId(prevSerie) === getSerieId(serie)
|
||||
(prevSerie) => getSerieId(prevSerie) === getSerieId(serie),
|
||||
);
|
||||
const metrics = {
|
||||
sum: sum(serie.data.map((item) => item.count)),
|
||||
@@ -561,30 +561,30 @@ export async function getChart(input: IChartInput) {
|
||||
metrics.sum,
|
||||
previousSerie
|
||||
? sum(previousSerie?.data.map((item) => item.count))
|
||||
: null
|
||||
: null,
|
||||
),
|
||||
average: getPreviousMetric(
|
||||
metrics.average,
|
||||
previousSerie
|
||||
? round(
|
||||
average(
|
||||
previousSerie?.data.map((item) => item.count)
|
||||
previousSerie?.data.map((item) => item.count),
|
||||
),
|
||||
2
|
||||
2,
|
||||
)
|
||||
: null
|
||||
: null,
|
||||
),
|
||||
min: getPreviousMetric(
|
||||
metrics.sum,
|
||||
previousSerie
|
||||
? min(previousSerie?.data.map((item) => item.count))
|
||||
: null
|
||||
: null,
|
||||
),
|
||||
max: getPreviousMetric(
|
||||
metrics.sum,
|
||||
previousSerie
|
||||
? max(previousSerie?.data.map((item) => item.count))
|
||||
: null
|
||||
: null,
|
||||
),
|
||||
},
|
||||
}
|
||||
@@ -596,7 +596,7 @@ export async function getChart(input: IChartInput) {
|
||||
previous: previousSerie?.data[index]
|
||||
? getPreviousMetric(
|
||||
item.count ?? 0,
|
||||
previousSerie?.data[index]?.count ?? null
|
||||
previousSerie?.data[index]?.count ?? null,
|
||||
)
|
||||
: undefined,
|
||||
})),
|
||||
@@ -617,16 +617,15 @@ export async function getChart(input: IChartInput) {
|
||||
const sumA = a.data.reduce((acc, item) => acc + (item.count ?? 0), 0);
|
||||
const sumB = b.data.reduce((acc, item) => acc + (item.count ?? 0), 0);
|
||||
return sumB - sumA;
|
||||
} else {
|
||||
return b.metrics[input.metric] - a.metrics[input.metric];
|
||||
}
|
||||
return b.metrics[input.metric] - a.metrics[input.metric];
|
||||
})
|
||||
.slice(offset, limit ? offset + limit : series.length);
|
||||
|
||||
final.metrics.sum = sum(final.series.map((item) => item.metrics.sum));
|
||||
final.metrics.average = round(
|
||||
average(final.series.map((item) => item.metrics.average)),
|
||||
2
|
||||
2,
|
||||
);
|
||||
final.metrics.min = min(final.series.map((item) => item.metrics.min));
|
||||
final.metrics.max = max(final.series.map((item) => item.metrics.max));
|
||||
@@ -634,26 +633,26 @@ export async function getChart(input: IChartInput) {
|
||||
final.metrics.previous = {
|
||||
sum: getPreviousMetric(
|
||||
final.metrics.sum,
|
||||
sum(final.series.map((item) => item.metrics.previous?.sum?.value ?? 0))
|
||||
sum(final.series.map((item) => item.metrics.previous?.sum?.value ?? 0)),
|
||||
),
|
||||
average: getPreviousMetric(
|
||||
final.metrics.average,
|
||||
round(
|
||||
average(
|
||||
final.series.map(
|
||||
(item) => item.metrics.previous?.average?.value ?? 0
|
||||
)
|
||||
(item) => item.metrics.previous?.average?.value ?? 0,
|
||||
),
|
||||
),
|
||||
2
|
||||
)
|
||||
2,
|
||||
),
|
||||
),
|
||||
min: getPreviousMetric(
|
||||
final.metrics.min,
|
||||
min(final.series.map((item) => item.metrics.previous?.min?.value ?? 0))
|
||||
min(final.series.map((item) => item.metrics.previous?.min?.value ?? 0)),
|
||||
),
|
||||
max: getPreviousMetric(
|
||||
final.metrics.max,
|
||||
max(final.series.map((item) => item.metrics.previous?.max?.value ?? 0))
|
||||
max(final.series.map((item) => item.metrics.previous?.max?.value ?? 0)),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,12 +3,12 @@ import { escape } from 'sqlstring';
|
||||
import { z } from 'zod';
|
||||
|
||||
import {
|
||||
TABLE_NAMES,
|
||||
chQuery,
|
||||
createSqlBuilder,
|
||||
db,
|
||||
formatClickhouseDate,
|
||||
getSelectPropertyKey,
|
||||
TABLE_NAMES,
|
||||
toDate,
|
||||
} from '@openpanel/db';
|
||||
import { zChartInput, zRange, zTimeInterval } from '@openpanel/validation';
|
||||
@@ -33,12 +33,12 @@ export const chartRouter = createTRPCRouter({
|
||||
interval: zTimeInterval,
|
||||
startDate: z.string().nullish(),
|
||||
endDate: z.string().nullish(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.query(async ({ input: { projectId, ...input } }) => {
|
||||
const { startDate, endDate } = getChartStartEndDate(input);
|
||||
const events = await chQuery<{ name: string }>(
|
||||
`SELECT DISTINCT name FROM ${TABLE_NAMES.events} WHERE project_id = ${escape(projectId)} AND ${toDate('created_at', input.interval)} BETWEEN ${toDate(formatClickhouseDate(startDate), input.interval)} AND ${toDate(formatClickhouseDate(endDate), input.interval)};`
|
||||
`SELECT DISTINCT name FROM ${TABLE_NAMES.events} WHERE project_id = ${escape(projectId)} AND ${toDate('created_at', input.interval)} BETWEEN ${toDate(formatClickhouseDate(startDate), input.interval)} AND ${toDate(formatClickhouseDate(endDate), input.interval)};`,
|
||||
);
|
||||
|
||||
return [
|
||||
@@ -58,7 +58,7 @@ export const chartRouter = createTRPCRouter({
|
||||
interval: zTimeInterval,
|
||||
startDate: z.string().nullish(),
|
||||
endDate: z.string().nullish(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.query(async ({ input: { projectId, event, ...input } }) => {
|
||||
const { startDate, endDate } = getChartStartEndDate(input);
|
||||
@@ -66,7 +66,7 @@ export const chartRouter = createTRPCRouter({
|
||||
`SELECT distinct mapKeys(properties) as keys from ${TABLE_NAMES.events} where ${
|
||||
event && event !== '*' ? `name = ${escape(event)} AND ` : ''
|
||||
} project_id = ${escape(projectId)} AND
|
||||
${toDate('created_at', input.interval)} BETWEEN ${toDate(formatClickhouseDate(startDate), input.interval)} AND ${toDate(formatClickhouseDate(endDate), input.interval)};`
|
||||
${toDate('created_at', input.interval)} BETWEEN ${toDate(formatClickhouseDate(startDate), input.interval)} AND ${toDate(formatClickhouseDate(endDate), input.interval)};`,
|
||||
);
|
||||
|
||||
const properties = events
|
||||
@@ -93,12 +93,12 @@ export const chartRouter = createTRPCRouter({
|
||||
'browser_version',
|
||||
'device',
|
||||
'brand',
|
||||
'model'
|
||||
'model',
|
||||
);
|
||||
|
||||
return pipe(
|
||||
sort<string>((a, b) => a.length - b.length),
|
||||
uniq
|
||||
uniq,
|
||||
)(properties);
|
||||
}),
|
||||
|
||||
@@ -112,7 +112,7 @@ export const chartRouter = createTRPCRouter({
|
||||
interval: zTimeInterval,
|
||||
startDate: z.string().nullish(),
|
||||
endDate: z.string().nullish(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.query(async ({ input: { event, property, projectId, ...input } }) => {
|
||||
const { startDate, endDate } = getChartStartEndDate(input);
|
||||
@@ -136,7 +136,7 @@ export const chartRouter = createTRPCRouter({
|
||||
(data: typeof events) => map(prop('values'), data),
|
||||
flatten,
|
||||
uniq,
|
||||
sort((a, b) => a.length - b.length)
|
||||
sort((a, b) => a.length - b.length),
|
||||
)(events);
|
||||
|
||||
return {
|
||||
@@ -166,7 +166,7 @@ export const chartRouter = createTRPCRouter({
|
||||
.input(
|
||||
zChartInput.extend({
|
||||
step: z.number(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.query(async ({ input }) => {
|
||||
const currentPeriod = getChartStartEndDate(input);
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import crypto from 'crypto';
|
||||
import crypto from 'node:crypto';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { hashPassword, stripTrailingSlash } from '@openpanel/common';
|
||||
import { stripTrailingSlash } from '@openpanel/common';
|
||||
import type { Prisma } from '@openpanel/db';
|
||||
import { db } from '@openpanel/db';
|
||||
|
||||
import { hashPassword } from '@openpanel/common/server';
|
||||
import { getClientAccess } from '../access';
|
||||
import { TRPCAccessError } from '../errors';
|
||||
import { createTRPCRouter, protectedProcedure } from '../trpc';
|
||||
@@ -17,7 +18,7 @@ export const clientRouter = createTRPCRouter({
|
||||
name: z.string(),
|
||||
cors: z.string().nullable(),
|
||||
crossDomain: z.boolean().optional(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const access = await getClientAccess({
|
||||
@@ -49,7 +50,7 @@ export const clientRouter = createTRPCRouter({
|
||||
cors: z.string().nullable(),
|
||||
crossDomain: z.boolean().optional(),
|
||||
type: z.enum(['read', 'write', 'root']).optional(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input }) => {
|
||||
const secret = `sec_${crypto.randomBytes(10).toString('hex')}`;
|
||||
@@ -75,7 +76,7 @@ export const clientRouter = createTRPCRouter({
|
||||
.input(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const access = await getClientAccess({
|
||||
|
||||
@@ -18,7 +18,7 @@ export const dashboardRouter = createTRPCRouter({
|
||||
.input(
|
||||
z.object({
|
||||
projectId: z.string(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.query(({ input }) => {
|
||||
return getDashboardsByProjectId(input.projectId);
|
||||
@@ -28,7 +28,7 @@ export const dashboardRouter = createTRPCRouter({
|
||||
z.object({
|
||||
name: z.string(),
|
||||
projectId: z.string(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const access = await getProjectAccess({
|
||||
@@ -61,7 +61,7 @@ export const dashboardRouter = createTRPCRouter({
|
||||
z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const dashboard = await db.dashboard.findUniqueOrThrow({
|
||||
@@ -93,7 +93,7 @@ export const dashboardRouter = createTRPCRouter({
|
||||
z.object({
|
||||
id: z.string(),
|
||||
forceDelete: z.boolean().optional(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const dashboard = await db.dashboard.findUniqueOrThrow({
|
||||
@@ -132,7 +132,7 @@ export const dashboardRouter = createTRPCRouter({
|
||||
switch (error.code) {
|
||||
case PrismaError.ForeignConstraintViolation:
|
||||
throw new Error(
|
||||
'Cannot delete dashboard with associated reports'
|
||||
'Cannot delete dashboard with associated reports',
|
||||
);
|
||||
default:
|
||||
throw new Error('Unknown error deleting dashboard');
|
||||
|
||||
@@ -3,13 +3,13 @@ import { escape } from 'sqlstring';
|
||||
import { z } from 'zod';
|
||||
|
||||
import {
|
||||
TABLE_NAMES,
|
||||
chQuery,
|
||||
convertClickhouseDateToJs,
|
||||
db,
|
||||
getEventList,
|
||||
getEvents,
|
||||
getTopPages,
|
||||
TABLE_NAMES,
|
||||
} from '@openpanel/db';
|
||||
import { zChartEventFilter } from '@openpanel/validation';
|
||||
|
||||
@@ -26,7 +26,7 @@ export const eventRouter = createTRPCRouter({
|
||||
icon: z.string().optional(),
|
||||
color: z.string().optional(),
|
||||
conversion: z.boolean().optional(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.mutation(
|
||||
async ({ input: { projectId, name, icon, color, conversion } }) => {
|
||||
@@ -40,7 +40,7 @@ export const eventRouter = createTRPCRouter({
|
||||
create: { projectId, name, icon, color, conversion },
|
||||
update: { icon, color, conversion },
|
||||
});
|
||||
}
|
||||
},
|
||||
),
|
||||
|
||||
byId: protectedProcedure
|
||||
@@ -48,14 +48,14 @@ export const eventRouter = createTRPCRouter({
|
||||
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)};`,
|
||||
{
|
||||
meta: true,
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
if (!res?.[0]) {
|
||||
@@ -81,7 +81,7 @@ export const eventRouter = createTRPCRouter({
|
||||
endDate: z.date().optional(),
|
||||
meta: z.boolean().optional(),
|
||||
profile: z.boolean().optional(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.query(async ({ input }) => {
|
||||
return getEventList(input);
|
||||
@@ -90,7 +90,7 @@ export const eventRouter = createTRPCRouter({
|
||||
.input(
|
||||
z.object({
|
||||
projectId: z.string(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.query(async ({ input: { projectId } }) => {
|
||||
const conversions = await db.eventMeta.findMany({
|
||||
@@ -109,7 +109,7 @@ export const eventRouter = createTRPCRouter({
|
||||
{
|
||||
profile: true,
|
||||
meta: true,
|
||||
}
|
||||
},
|
||||
);
|
||||
}),
|
||||
|
||||
@@ -119,7 +119,7 @@ export const eventRouter = createTRPCRouter({
|
||||
projectId: z.string(),
|
||||
cursor: z.number().optional(),
|
||||
limit: z.number().default(8),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.query(async ({ input: { projectId, cursor, limit }, ctx }) => {
|
||||
if (ctx.session.userId) {
|
||||
@@ -151,12 +151,12 @@ export const eventRouter = createTRPCRouter({
|
||||
path: string;
|
||||
created_at: string;
|
||||
}>(
|
||||
`SELECT * FROM ${TABLE_NAMES.events_bots} WHERE project_id = ${escape(projectId)} ORDER BY created_at DESC LIMIT ${limit} OFFSET ${(cursor ?? 0) * limit}`
|
||||
`SELECT * FROM ${TABLE_NAMES.events_bots} WHERE project_id = ${escape(projectId)} ORDER BY created_at DESC LIMIT ${limit} OFFSET ${(cursor ?? 0) * limit}`,
|
||||
),
|
||||
chQuery<{
|
||||
count: number;
|
||||
}>(
|
||||
`SELECT count(*) as count FROM ${TABLE_NAMES.events_bots} WHERE project_id = ${escape(projectId)}`
|
||||
`SELECT count(*) as count FROM ${TABLE_NAMES.events_bots} WHERE project_id = ${escape(projectId)}`,
|
||||
),
|
||||
]);
|
||||
|
||||
@@ -176,7 +176,7 @@ export const eventRouter = createTRPCRouter({
|
||||
cursor: z.number().optional(),
|
||||
take: z.number().default(20),
|
||||
search: z.string().optional(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.query(async ({ input }) => {
|
||||
return getTopPages(input);
|
||||
@@ -186,19 +186,19 @@ export const eventRouter = createTRPCRouter({
|
||||
.input(
|
||||
z.object({
|
||||
projectId: z.string(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.query(async ({ input }) => {
|
||||
const res = await chQuery<{ origin: string }>(
|
||||
`SELECT DISTINCT origin FROM ${TABLE_NAMES.events} WHERE project_id = ${escape(
|
||||
input.projectId
|
||||
)} AND origin IS NOT NULL AND origin != '' AND toDate(created_at) > now() - INTERVAL 30 DAY ORDER BY origin ASC`
|
||||
input.projectId,
|
||||
)} AND origin IS NOT NULL AND origin != '' AND toDate(created_at) > now() - INTERVAL 30 DAY ORDER BY origin ASC`,
|
||||
);
|
||||
|
||||
return res.sort((a, b) =>
|
||||
a.origin
|
||||
.replace(/https?:\/\//, '')
|
||||
.localeCompare(b.origin.replace(/https?:\/\//, ''))
|
||||
.localeCompare(b.origin.replace(/https?:\/\//, '')),
|
||||
);
|
||||
}),
|
||||
});
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
import crypto from 'crypto';
|
||||
import crypto from 'node:crypto';
|
||||
import type { z } from 'zod';
|
||||
|
||||
import { hashPassword, stripTrailingSlash } from '@openpanel/common';
|
||||
import { stripTrailingSlash } from '@openpanel/common';
|
||||
import { db, getId, getOrganizationBySlug, getUserById } from '@openpanel/db';
|
||||
import type { ProjectType } from '@openpanel/db';
|
||||
import { zOnboardingProject } from '@openpanel/validation';
|
||||
|
||||
import { hashPassword } from '@openpanel/common/server';
|
||||
import { createTRPCRouter, protectedProcedure } from '../trpc';
|
||||
|
||||
async function createOrGetOrganization(
|
||||
input: z.infer<typeof zOnboardingProject>,
|
||||
userId: string
|
||||
userId: string,
|
||||
) {
|
||||
if (input.organizationSlug) {
|
||||
return await getOrganizationBySlug(input.organizationSlug);
|
||||
|
||||
@@ -15,7 +15,7 @@ export const organizationRouter = createTRPCRouter({
|
||||
z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const access = await getOrganizationAccess({
|
||||
@@ -86,7 +86,7 @@ export const organizationRouter = createTRPCRouter({
|
||||
.input(
|
||||
z.object({
|
||||
memberId: z.string(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const member = await db.member.findUniqueOrThrow({
|
||||
@@ -107,7 +107,7 @@ export const organizationRouter = createTRPCRouter({
|
||||
const invitationId = pathOr<string | undefined>(
|
||||
undefined,
|
||||
['meta', 'invitationId'],
|
||||
member
|
||||
member,
|
||||
);
|
||||
|
||||
if (invitationId) {
|
||||
@@ -130,7 +130,7 @@ export const organizationRouter = createTRPCRouter({
|
||||
z.object({
|
||||
organizationId: z.string(),
|
||||
userId: z.string(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
if (ctx.session.userId === input.userId) {
|
||||
@@ -168,7 +168,7 @@ export const organizationRouter = createTRPCRouter({
|
||||
userId: z.string(),
|
||||
organizationSlug: z.string(),
|
||||
access: z.array(z.string()),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const access = await getOrganizationAccess({
|
||||
|
||||
@@ -3,11 +3,11 @@ import { escape } from 'sqlstring';
|
||||
import { z } from 'zod';
|
||||
|
||||
import {
|
||||
TABLE_NAMES,
|
||||
chQuery,
|
||||
createSqlBuilder,
|
||||
getProfileList,
|
||||
getProfiles,
|
||||
TABLE_NAMES,
|
||||
} from '@openpanel/db';
|
||||
|
||||
import { createTRPCRouter, protectedProcedure } from '../trpc';
|
||||
@@ -17,7 +17,7 @@ export const profileRouter = createTRPCRouter({
|
||||
.input(z.object({ projectId: z.string() }))
|
||||
.query(async ({ input: { projectId } }) => {
|
||||
const events = await chQuery<{ keys: string[] }>(
|
||||
`SELECT distinct mapKeys(properties) as keys from ${TABLE_NAMES.profiles} where project_id = ${escape(projectId)};`
|
||||
`SELECT distinct mapKeys(properties) as keys from ${TABLE_NAMES.profiles} where project_id = ${escape(projectId)};`,
|
||||
);
|
||||
|
||||
const properties = events
|
||||
@@ -30,7 +30,7 @@ export const profileRouter = createTRPCRouter({
|
||||
|
||||
return pipe(
|
||||
sort<string>((a, b) => a.length - b.length),
|
||||
uniq
|
||||
uniq,
|
||||
)(properties);
|
||||
}),
|
||||
|
||||
@@ -42,7 +42,7 @@ export const profileRouter = createTRPCRouter({
|
||||
take: z.number().default(50),
|
||||
search: z.string().optional(),
|
||||
// filters: z.array(zChartEventFilter).default([]),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.query(async ({ input: { projectId, cursor, take, search } }) => {
|
||||
return getProfileList({ projectId, cursor, take, search });
|
||||
@@ -55,15 +55,15 @@ export const profileRouter = createTRPCRouter({
|
||||
cursor: z.number().optional(),
|
||||
take: z.number().default(50),
|
||||
// filters: z.array(zChartEventFilter).default([]),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.query(async ({ input: { projectId, cursor, take } }) => {
|
||||
const res = await chQuery<{ profile_id: string; count: number }>(
|
||||
`SELECT profile_id, count(*) as count from ${TABLE_NAMES.events} where profile_id != '' and project_id = ${escape(projectId)} group by profile_id order by count() DESC LIMIT ${take} ${cursor ? `OFFSET ${cursor * take}` : ''}`
|
||||
`SELECT profile_id, count(*) as count from ${TABLE_NAMES.events} where profile_id != '' and project_id = ${escape(projectId)} group by profile_id order by count() DESC LIMIT ${take} ${cursor ? `OFFSET ${cursor * take}` : ''}`,
|
||||
);
|
||||
const profiles = await getProfiles(
|
||||
res.map((r) => r.profile_id),
|
||||
projectId
|
||||
projectId,
|
||||
);
|
||||
return (
|
||||
res
|
||||
@@ -83,7 +83,7 @@ export const profileRouter = createTRPCRouter({
|
||||
z.object({
|
||||
property: z.string(),
|
||||
projectId: z.string(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.query(async ({ input: { property, projectId } }) => {
|
||||
const { sb, getSql } = createSqlBuilder();
|
||||
@@ -91,7 +91,7 @@ export const profileRouter = createTRPCRouter({
|
||||
sb.where.project_id = `project_id = ${escape(projectId)}`;
|
||||
if (property.startsWith('properties.')) {
|
||||
sb.select.values = `distinct arrayMap(x -> trim(x), mapValues(mapExtractKeyLike(properties, ${escape(
|
||||
property.replace(/^properties\./, '').replace('.*.', '.%.')
|
||||
property.replace(/^properties\./, '').replace('.*.', '.%.'),
|
||||
)}))) as values`;
|
||||
} else {
|
||||
sb.select.values = `${property} as values`;
|
||||
@@ -103,7 +103,7 @@ export const profileRouter = createTRPCRouter({
|
||||
(data: typeof profiles) => map(prop('values'), data),
|
||||
flatten,
|
||||
uniq,
|
||||
sort((a, b) => a.length - b.length)
|
||||
sort((a, b) => a.length - b.length),
|
||||
)(profiles);
|
||||
|
||||
return {
|
||||
|
||||
@@ -11,7 +11,7 @@ export const projectRouter = createTRPCRouter({
|
||||
.input(
|
||||
z.object({
|
||||
organizationSlug: z.string().nullable(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.query(async ({ input: { organizationSlug } }) => {
|
||||
if (organizationSlug === null) return [];
|
||||
@@ -23,7 +23,7 @@ export const projectRouter = createTRPCRouter({
|
||||
z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const access = await getProjectAccess({
|
||||
@@ -49,7 +49,7 @@ export const projectRouter = createTRPCRouter({
|
||||
z.object({
|
||||
name: z.string().min(1),
|
||||
organizationSlug: z.string(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input: { name, organizationSlug } }) => {
|
||||
return db.project.create({
|
||||
@@ -65,7 +65,7 @@ export const projectRouter = createTRPCRouter({
|
||||
.input(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const access = await getProjectAccess({
|
||||
|
||||
@@ -21,7 +21,7 @@ export const referenceRouter = createTRPCRouter({
|
||||
date: new Date(datetime),
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
),
|
||||
delete: protectedProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
@@ -54,7 +54,7 @@ export const referenceRouter = createTRPCRouter({
|
||||
startDate: z.string().nullish(),
|
||||
endDate: z.string().nullish(),
|
||||
range: zRange,
|
||||
})
|
||||
}),
|
||||
)
|
||||
.query(({ input: { projectId, ...input } }) => {
|
||||
const { startDate, endDate } = getChartStartEndDate(input);
|
||||
|
||||
@@ -13,7 +13,7 @@ export const reportRouter = createTRPCRouter({
|
||||
z.object({
|
||||
report: zReportInput.omit({ projectId: true }),
|
||||
dashboardId: z.string(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input: { report, dashboardId }, ctx }) => {
|
||||
const dashboard = await db.dashboard.findUniqueOrThrow({
|
||||
@@ -52,7 +52,7 @@ export const reportRouter = createTRPCRouter({
|
||||
z.object({
|
||||
reportId: z.string(),
|
||||
report: zReportInput.omit({ projectId: true }),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input: { report, reportId }, ctx }) => {
|
||||
const dbReport = await db.report.findUniqueOrThrow({
|
||||
@@ -91,7 +91,7 @@ export const reportRouter = createTRPCRouter({
|
||||
.input(
|
||||
z.object({
|
||||
reportId: z.string(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input: { reportId }, ctx }) => {
|
||||
const report = await db.report.findUniqueOrThrow({
|
||||
|
||||
@@ -15,7 +15,7 @@ export const ticketRouter = createTRPCRouter({
|
||||
subject: z.string(),
|
||||
body: z.string(),
|
||||
meta: z.record(z.string(), z.unknown()),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
if (!API_KEY) {
|
||||
|
||||
@@ -11,7 +11,7 @@ export const userRouter = createTRPCRouter({
|
||||
z.object({
|
||||
firstName: z.string(),
|
||||
lastName: z.string(),
|
||||
})
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const [updatedUser] = await Promise.all([
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { getAuth } from '@clerk/fastify';
|
||||
import { initTRPC, TRPCError } from '@trpc/server';
|
||||
import { TRPCError, initTRPC } from '@trpc/server';
|
||||
import type { CreateFastifyContextOptions } from '@trpc/server/adapters/fastify';
|
||||
import { has } from 'ramda';
|
||||
import superjson from 'superjson';
|
||||
@@ -21,10 +21,9 @@ export function createContext({ req, res }: CreateFastifyContextOptions) {
|
||||
options: {
|
||||
maxAge: number;
|
||||
path: string;
|
||||
}
|
||||
},
|
||||
) => {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line
|
||||
res.setCookie(key, value, options);
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user