mirror of
https://github.com/umami-software/umami.git
synced 2026-02-18 11:35:37 +01:00
feat: add timezone canonicalization and update GET handler to use it
This commit is contained in:
parent
777515f754
commit
f7214e710e
2 changed files with 19 additions and 1 deletions
|
|
@ -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 = {
|
||||
|
|
|
|||
12
src/lib/timezone.ts
Normal file
12
src/lib/timezone.ts
Normal 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();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue