segments implementation and migration update. update getRequestFilters to include filter groups.

This commit is contained in:
Francis Cao 2025-06-13 07:34:54 -07:00
parent 1ccc8a1a86
commit f61421b742
16 changed files with 188 additions and 23 deletions

View file

@ -0,0 +1,91 @@
import { z } from 'zod';
import { parseRequest } from '@/lib/request';
import { deleteReport, getReport, updateReport } from '@/queries';
import { canDeleteReport, canUpdateReport, canViewReport } from '@/lib/auth';
import { unauthorized, json, notFound, ok } from '@/lib/response';
import { reportTypeParam } from '@/lib/schema';
export async function GET(request: Request, { params }: { params: Promise<{ reportId: string }> }) {
const { auth, error } = await parseRequest(request);
if (error) {
return error();
}
const { reportId } = await params;
const report = await getReport(reportId);
if (!(await canViewReport(auth, report))) {
return unauthorized();
}
report.parameters = JSON.parse(report.parameters);
return json(report);
}
export async function POST(
request: Request,
{ params }: { params: Promise<{ reportId: string }> },
) {
const schema = z.object({
websiteId: z.string().uuid(),
type: reportTypeParam,
name: z.string().max(200),
description: z.string().max(500),
parameters: z.object({}).passthrough(),
});
const { auth, body, error } = await parseRequest(request, schema);
if (error) {
return error();
}
const { reportId } = await params;
const { websiteId, type, name, description, parameters } = body;
const report = await getReport(reportId);
if (!report) {
return notFound();
}
if (!(await canUpdateReport(auth, report))) {
return unauthorized();
}
const result = await updateReport(reportId, {
websiteId,
userId: auth.user.id,
type,
name,
description,
parameters: JSON.stringify(parameters),
} as any);
return json(result);
}
export async function DELETE(
request: Request,
{ params }: { params: Promise<{ reportId: string }> },
) {
const { auth, error } = await parseRequest(request);
if (error) {
return error();
}
const { reportId } = await params;
const report = await getReport(reportId);
if (!(await canDeleteReport(auth, report))) {
return unauthorized();
}
await deleteReport(reportId);
return ok();
}

View file

@ -0,0 +1,43 @@
import { z } from 'zod';
import { hashPassword, canCreateUser } from '@/lib/auth';
import { ROLES } from '@/lib/constants';
import { uuid } from '@/lib/crypto';
import { parseRequest } from '@/lib/request';
import { unauthorized, json, badRequest } from '@/lib/response';
import { createUser, getUserByUsername } from '@/queries';
export async function POST(request: Request) {
const schema = z.object({
id: z.string().uuid().optional(),
username: z.string().max(255),
password: z.string(),
role: z.string().regex(/admin|user|view-only/i),
});
const { auth, body, error } = await parseRequest(request, schema);
if (error) {
return error();
}
if (!(await canCreateUser(auth))) {
return unauthorized();
}
const { id, username, password, role } = body;
const existingUser = await getUserByUsername(username, { showDeleted: true });
if (existingUser) {
return badRequest('User already exists');
}
const user = await createUser({
id: id || uuid(),
username,
password: hashPassword(password),
role: role ?? ROLES.user,
});
return json(user);
}

View file

@ -32,7 +32,7 @@ export async function GET(
}
const filters = {
...getRequestFilters(query),
...(await getRequestFilters(query)),
startDate,
endDate,
timezone,

View file

@ -48,7 +48,7 @@ export async function GET(
const { startDate, endDate } = await getRequestDateRange(query);
const column = FILTER_COLUMNS[type] || type;
const filters = {
...getRequestFilters(query),
...(await getRequestFilters(query)),
startDate,
endDate,
};

View file

@ -35,7 +35,7 @@ export async function GET(
const { startDate, endDate, unit } = await getRequestDateRange(query);
const filters = {
...getRequestFilters(query),
...(await getRequestFilters(query)),
startDate,
endDate,
timezone,

View file

@ -29,7 +29,7 @@ export async function GET(
const { startDate, endDate } = await getRequestDateRange(query);
const filters = getRequestFilters(query);
const filters = await getRequestFilters(query);
const metrics = await getWebsiteSessionStats(websiteId, {
...filters,

View file

@ -37,7 +37,7 @@ export async function GET(
endDate,
);
const filters = getRequestFilters(query);
const filters = await getRequestFilters(query);
const metrics = await getWebsiteStats(websiteId, {
...filters,

View file

@ -1,9 +1,9 @@
import { z } from 'zod';
import { canViewWebsite } from '@/lib/auth';
import { EVENT_COLUMNS, FILTER_COLUMNS, GROUP_FILTERS, SESSION_COLUMNS } from '@/lib/constants';
import { getValues } from '@/queries';
import { parseRequest, getRequestDateRange } from '@/lib/request';
import { EVENT_COLUMNS, FILTER_COLUMNS, FILTER_GROUPS, SESSION_COLUMNS } from '@/lib/constants';
import { getRequestDateRange, parseRequest } from '@/lib/request';
import { badRequest, json, unauthorized } from '@/lib/response';
import { getWebsiteSegments, getValues } from '@/queries';
import { z } from 'zod';
export async function GET(
request: Request,
@ -33,12 +33,18 @@ export async function GET(
if (
!SESSION_COLUMNS.includes(type) &&
!EVENT_COLUMNS.includes(type) &&
!GROUP_FILTERS.includes(type)
!FILTER_GROUPS.includes(type)
) {
return badRequest('Invalid type.');
}
const values = await getValues(websiteId, FILTER_COLUMNS[type], startDate, endDate, search);
let values;
if (FILTER_GROUPS.includes(type)) {
values = (await getWebsiteSegments(websiteId, type)).map(segment => ({ value: segment.name }));
} else {
values = await getValues(websiteId, FILTER_COLUMNS[type], startDate, endDate, search);
}
return json(values.filter(n => n).sort());
}