diff --git a/src/app/api/websites/[websiteId]/pageviews/route.ts b/src/app/api/websites/[websiteId]/pageviews/route.ts index eaa618797..5e0eb47a2 100644 --- a/src/app/api/websites/[websiteId]/pageviews/route.ts +++ b/src/app/api/websites/[websiteId]/pageviews/route.ts @@ -5,11 +5,13 @@ import { unitParam, timezoneParam, filterParams } from '@/lib/schema'; import { getCompareDate } from '@/lib/date'; import { unauthorized, json } from '@/lib/response'; import { getPageviewStats, getSessionStats } from '@/queries'; +import { canonicalizeTimeZone } from '@/lib/timezone'; export async function GET( request: Request, { params }: { params: Promise<{ websiteId: string }> }, ) { + // Define and validate request query parameters const schema = z.object({ startAt: z.coerce.number().int(), endAt: z.coerce.number().int(), @@ -26,12 +28,16 @@ export async function GET( } const { websiteId } = await params; - const { timezone, compare } = query; + const { timezone: requestedTz, compare } = query; + const timezone = canonicalizeTimeZone(requestedTz || 'UTC'); + + // Authorization check if (!(await canViewWebsite(auth, websiteId))) { return unauthorized(); } + // Compute date range and unit from request const { startDate, endDate, unit } = await getRequestDateRange(query); const filters = { diff --git a/src/lib/timezone.ts b/src/lib/timezone.ts new file mode 100644 index 000000000..0ad056f9b --- /dev/null +++ b/src/lib/timezone.ts @@ -0,0 +1,12 @@ +const ALIASES: Record = { + 'asia/calcutta': 'Asia/Kolkata', + 'utc': 'UTC', + 'gmt': 'UTC', + 'greenwich': 'UTC', +}; + +export function canonicalizeTimeZone(tz?: string | null): string { + if (!tz) return 'UTC'; + const key = tz.trim().toLowerCase(); + return ALIASES[key] ?? tz.trim(); +}