35 lines
823 B
TypeScript
35 lines
823 B
TypeScript
import { canViewWebsite } from '@/lib/auth';
|
|
import { unauthorized, json } from '@/lib/response';
|
|
import { parseRequest } from '@/lib/request';
|
|
import { getGoal } from '@/queries/sql/reports/getGoal';
|
|
import { reportResultSchema } from '@/lib/schema';
|
|
|
|
export async function POST(request: Request) {
|
|
const { auth, body, error } = await parseRequest(request, reportResultSchema);
|
|
|
|
if (error) {
|
|
return error();
|
|
}
|
|
|
|
const {
|
|
websiteId,
|
|
dateRange: { startDate, endDate },
|
|
parameters: { type, value, property, operator },
|
|
} = body;
|
|
|
|
if (!(await canViewWebsite(auth, websiteId))) {
|
|
return unauthorized();
|
|
}
|
|
|
|
const data = await getGoal(websiteId, {
|
|
type,
|
|
value,
|
|
property,
|
|
operator,
|
|
startDate: new Date(startDate),
|
|
endDate: new Date(endDate),
|
|
});
|
|
|
|
return json(data);
|
|
}
|