99 lines
2.8 KiB
TypeScript
99 lines
2.8 KiB
TypeScript
/** Preset period keys for admin dashboard filters. */
|
|
export type AnalyticsDatePreset =
|
|
| 'today'
|
|
| 'yesterday'
|
|
| 'this_week'
|
|
| 'this_month'
|
|
| 'last_30_days'
|
|
| 'last_90_days'
|
|
| 'this_year'
|
|
| 'custom';
|
|
|
|
export type ResolvedDateRange = {
|
|
preset: AnalyticsDatePreset;
|
|
from: Date;
|
|
to: Date;
|
|
};
|
|
|
|
function startOfDay(d: Date): Date {
|
|
const x = new Date(d);
|
|
x.setUTCHours(0, 0, 0, 0);
|
|
return x;
|
|
}
|
|
|
|
function endOfDay(d: Date): Date {
|
|
const x = new Date(d);
|
|
x.setUTCHours(23, 59, 59, 999);
|
|
return x;
|
|
}
|
|
|
|
function startOfWeekMonday(d: Date): Date {
|
|
const x = startOfDay(d);
|
|
const day = x.getUTCDay();
|
|
const diff = day === 0 ? 6 : day - 1;
|
|
x.setUTCDate(x.getUTCDate() - diff);
|
|
return x;
|
|
}
|
|
|
|
/** Resolves preset or custom ISO bounds into inclusive UTC range. */
|
|
export function resolveAnalyticsDateRange(
|
|
presetInput: string | undefined,
|
|
fromInput?: string,
|
|
toInput?: string
|
|
): ResolvedDateRange {
|
|
const now = new Date();
|
|
const preset = (presetInput ?? 'today') as AnalyticsDatePreset;
|
|
|
|
if (preset === 'custom') {
|
|
if (!fromInput || !toInput) {
|
|
throw new Error('Custom range requires from and to (ISO date)');
|
|
}
|
|
const from = startOfDay(new Date(fromInput));
|
|
const to = endOfDay(new Date(toInput));
|
|
if (Number.isNaN(from.getTime()) || Number.isNaN(to.getTime())) {
|
|
throw new Error('Invalid custom date range');
|
|
}
|
|
if (from > to) throw new Error('from must be before to');
|
|
return { preset, from, to };
|
|
}
|
|
|
|
switch (preset) {
|
|
case 'yesterday': {
|
|
const y = new Date(now);
|
|
y.setUTCDate(y.getUTCDate() - 1);
|
|
return { preset, from: startOfDay(y), to: endOfDay(y) };
|
|
}
|
|
case 'this_week':
|
|
return { preset, from: startOfWeekMonday(now), to: endOfDay(now) };
|
|
case 'this_month': {
|
|
const from = startOfDay(new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1)));
|
|
return { preset, from, to: endOfDay(now) };
|
|
}
|
|
case 'last_30_days': {
|
|
const from = startOfDay(new Date(now));
|
|
from.setUTCDate(from.getUTCDate() - 29);
|
|
return { preset, from, to: endOfDay(now) };
|
|
}
|
|
case 'last_90_days': {
|
|
const from = startOfDay(new Date(now));
|
|
from.setUTCDate(from.getUTCDate() - 89);
|
|
return { preset, from, to: endOfDay(now) };
|
|
}
|
|
case 'this_year': {
|
|
const from = startOfDay(new Date(Date.UTC(now.getUTCFullYear(), 0, 1)));
|
|
return { preset, from, to: endOfDay(now) };
|
|
}
|
|
case 'today':
|
|
default:
|
|
return { preset: 'today', from: startOfDay(now), to: endOfDay(now) };
|
|
}
|
|
}
|
|
|
|
export type AnalyticsGameKey = 'all' | 'candle-predict';
|
|
|
|
export function parseAnalyticsGameKey(value: string | undefined): AnalyticsGameKey {
|
|
if (!value || value === 'all') return 'all';
|
|
if (value === 'candle-predict') return 'candle-predict';
|
|
throw new Error('gameKey must be all or candle-predict');
|
|
}
|