-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
574 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.