6d4b74d410
Track landing downloads per platform/month, support admin delete and bulk key issue, and sort licenses by createdAtSec. Co-authored-by: Cursor <cursoragent@cursor.com>
43 lines
1.5 KiB
JavaScript
43 lines
1.5 KiB
JavaScript
export const DOWNLOAD_PLATFORMS = ['windows', 'macos', 'linux'];
|
|
|
|
export function monthKeyFromDate(date = new Date()) {
|
|
const y = date.getUTCFullYear();
|
|
const m = String(date.getUTCMonth() + 1).padStart(2, '0');
|
|
return `${y}-${m}`;
|
|
}
|
|
|
|
export function normalizeDownloadPlatform(platform) {
|
|
const p = String(platform ?? '').trim().toLowerCase();
|
|
if (DOWNLOAD_PLATFORMS.includes(p)) return p;
|
|
return null;
|
|
}
|
|
|
|
export function ensureDownloadStats(data) {
|
|
data.downloadStats ??= { byMonth: {} };
|
|
data.downloadStats.byMonth ??= {};
|
|
return data.downloadStats;
|
|
}
|
|
|
|
export function recordDownload(data, platform, date = new Date()) {
|
|
const normalized = normalizeDownloadPlatform(platform);
|
|
if (!normalized) return { ok: false, error: 'invalid_platform' };
|
|
|
|
const stats = ensureDownloadStats(data);
|
|
const month = monthKeyFromDate(date);
|
|
stats.byMonth[month] ??= { windows: 0, macos: 0, linux: 0 };
|
|
stats.byMonth[month][normalized] += 1;
|
|
return { ok: true, month, platform: normalized, count: stats.byMonth[month][normalized] };
|
|
}
|
|
|
|
export function getDownloadStatsForMonth(data, month) {
|
|
const stats = ensureDownloadStats(data);
|
|
const row = stats.byMonth[month] ?? { windows: 0, macos: 0, linux: 0 };
|
|
const total = row.windows + row.macos + row.linux;
|
|
return { month, windows: row.windows, macos: row.macos, linux: row.linux, total };
|
|
}
|
|
|
|
export function listDownloadMonths(data) {
|
|
const stats = ensureDownloadStats(data);
|
|
return Object.keys(stats.byMonth).sort((a, b) => b.localeCompare(a));
|
|
}
|