feat: add timezone canonicalization and update GET handler to use it

This commit is contained in:
Om Mishra 2025-10-19 10:58:39 +05:30
parent 777515f754
commit f7214e710e
2 changed files with 19 additions and 1 deletions

View file

@ -5,11 +5,13 @@ import { unitParam, timezoneParam, filterParams } from '@/lib/schema';
import { getCompareDate } from '@/lib/date'; import { getCompareDate } from '@/lib/date';
import { unauthorized, json } from '@/lib/response'; import { unauthorized, json } from '@/lib/response';
import { getPageviewStats, getSessionStats } from '@/queries'; import { getPageviewStats, getSessionStats } from '@/queries';
import { canonicalizeTimeZone } from '@/lib/timezone';
export async function GET( export async function GET(
request: Request, request: Request,
{ params }: { params: Promise<{ websiteId: string }> }, { params }: { params: Promise<{ websiteId: string }> },
) { ) {
// Define and validate request query parameters
const schema = z.object({ const schema = z.object({
startAt: z.coerce.number().int(), startAt: z.coerce.number().int(),
endAt: z.coerce.number().int(), endAt: z.coerce.number().int(),
@ -26,12 +28,16 @@ export async function GET(
} }
const { websiteId } = await params; 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))) { if (!(await canViewWebsite(auth, websiteId))) {
return unauthorized(); return unauthorized();
} }
// Compute date range and unit from request
const { startDate, endDate, unit } = await getRequestDateRange(query); const { startDate, endDate, unit } = await getRequestDateRange(query);
const filters = { const filters = {

12
src/lib/timezone.ts Normal file
View file

@ -0,0 +1,12 @@
const ALIASES: Record<string, string> = {
'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();
}