Skip to content

Commit

Permalink
Converted reports and share routes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecao committed Jan 28, 2025
1 parent dcac7b7 commit 6c9f1ad
Show file tree
Hide file tree
Showing 23 changed files with 574 additions and 5 deletions.
91 changes: 91 additions & 0 deletions src/app/api/reports/[reportId]/route.ts
Original file line number Diff line number Diff line change
@@ -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();
}
50 changes: 50 additions & 0 deletions src/app/api/reports/funnel/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { z } from 'zod';
import { canViewWebsite } from 'lib/auth';
import { unauthorized, json } from 'lib/response';
import { parseRequest } from 'lib/request';
import { getFunnel } from 'queries';

export async function POST(request: Request) {
const schema = z.object({
websiteId: z.string().uuid(),
steps: z
.array(
z.object({
type: z.string(),
value: z.string(),
}),
)
.min(2),
window: z.number().positive(),
dateRange: z.object({
startDate: z.date(),
endDate: z.date(),
}),
});

const { auth, body, error } = await parseRequest(request, schema);

if (error) {
return error();
}

const {
websiteId,
steps,
window,
dateRange: { startDate, endDate },
} = body;

if (!(await canViewWebsite(auth, websiteId))) {
return unauthorized();
}

const data = await getFunnel(websiteId, {
startDate: new Date(startDate),
endDate: new Date(endDate),
steps,
windowMinutes: +window,
});

return json(data);
}
53 changes: 53 additions & 0 deletions src/app/api/reports/goals/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { z } from 'zod';
import { canViewWebsite } from 'lib/auth';
import { unauthorized, json } from 'lib/response';
import { parseRequest } from 'lib/request';
import { getGoals } from 'queries/analytics/reports/getGoals';

export async function POST(request: Request) {
const schema = z.object({
websiteId: z.string().uuid(),
dateRange: z.object({
startDate: z.date(),
endDate: z.date(),
}),
goals: z
.array(
z.object({
type: z.string().regex(/url|event|event-data/),
value: z.string(),
goal: z.number(),
operator: z
.string()
.regex(/count|sum|average/)
.refine(data => data['type'] === 'event-data'),
property: z.string().refine(data => data['type'] === 'event-data'),
}),
)
.min(1),
});

const { auth, body, error } = await parseRequest(request, schema);

if (error) {
return error();
}

const {
websiteId,
dateRange: { startDate, endDate },
goals,
} = body;

if (!(await canViewWebsite(auth, websiteId))) {
return unauthorized();
}

const data = await getGoals(websiteId, {
startDate: new Date(startDate),
endDate: new Date(endDate),
goals,
});

return json(data);
}
71 changes: 71 additions & 0 deletions src/app/api/reports/insights/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { z } from 'zod';
import { canViewWebsite } from 'lib/auth';
import { unauthorized, json } from 'lib/response';
import { parseRequest } from 'lib/request';
import { getInsights } from 'queries';

function convertFilters(filters: any[]) {
return filters.reduce((obj, filter) => {
obj[filter.name] = filter;

return obj;
}, {});
}

export async function POST(request: Request) {
const schema = z.object({
websiteId: z.string().uuid(),
dateRange: z.object({
startDate: z.date(),
endDate: z.date(),
}),
fields: z
.array(
z.object({
name: z.string(),
type: z.string(),
label: z.string(),
}),
)
.min(1),
filters: z.array(
z.object({
name: z.string(),
type: z.string(),
operator: z.string(),
value: z.string(),
}),
),
groups: z.array(
z.object({
name: z.string(),
type: z.string(),
}),
),
});

const { auth, body, error } = await parseRequest(request, schema);

if (error) {
return error();
}

const {
websiteId,
dateRange: { startDate, endDate },
fields,
filters,
} = body;

if (!(await canViewWebsite(auth, websiteId))) {
return unauthorized();
}

const data = await getInsights(websiteId, fields, {
...convertFilters(filters),
startDate: new Date(startDate),
endDate: new Date(endDate),
});

return json(data);
}
46 changes: 46 additions & 0 deletions src/app/api/reports/journey/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { z } from 'zod';
import { canViewWebsite } from 'lib/auth';
import { unauthorized, json } from 'lib/response';
import { parseRequest } from 'lib/request';
import { getJourney } from 'queries';

export async function POST(request: Request) {
const schema = z.object({
websiteId: z.string().uuid(),
dateRange: z.object({
startDate: z.date(),
endDate: z.date(),
}),
steps: z.number().min(3).max(7),
startStep: z.string(),
endStep: z.string(),
});

const { auth, body, error } = await parseRequest(request, schema);

if (error) {
return error();
}

const {
websiteId,
dateRange: { startDate, endDate },
steps,
startStep,
endStep,
} = body;

if (!(await canViewWebsite(auth, websiteId))) {
return unauthorized();
}

const data = await getJourney(websiteId, {
startDate: new Date(startDate),
endDate: new Date(endDate),
steps,
startStep,
endStep,
});

return json(data);
}
41 changes: 41 additions & 0 deletions src/app/api/reports/retention/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { z } from 'zod';
import { canViewWebsite } from 'lib/auth';
import { unauthorized, json } from 'lib/response';
import { parseRequest } from 'lib/request';
import { getRetention } from 'queries';
import { timezoneParam } from 'lib/schema';

export async function POST(request: Request) {
const schema = z.object({
websiteId: z.string().uuid(),
dateRange: z.object({
startDate: z.date(),
endDate: z.date(),
}),
timezone: timezoneParam,
});

const { auth, body, error } = await parseRequest(request, schema);

if (error) {
return error();
}

const {
websiteId,
dateRange: { startDate, endDate },
timezone,
} = body;

if (!(await canViewWebsite(auth, websiteId))) {
return unauthorized();
}

const data = await getRetention(websiteId, {
startDate: new Date(startDate),
endDate: new Date(endDate),
timezone,
});

return json(data);
}
Loading

0 comments on commit 6c9f1ad

Please sign in to comment.