Download src/lib/export.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 8.12 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/export.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/lib/export.ts
-
curl -L -o export.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/export.ts
8.12 kB
| /** | |
| * Data export utilities. | |
| * | |
| * Transforms raw database records into a portable JSON export format. | |
| * All data is owned by the user β they should be able to take it with them. | |
| */ | |
| // βββ Raw DB shapes (subset of Prisma types, kept minimal) βββββββββββββββββββββ | |
| export interface RawUser { | |
| id: number; | |
| name: string; | |
| level: number; | |
| totalXp: number; | |
| currentXp: number; | |
| xpToNextLevel: number; | |
| title: string; | |
| dailyGoalXp: number; | |
| streak: number; | |
| longestStreak: number; | |
| streakFreezes: number; | |
| lichessUsername: string | null; | |
| chesscomUsername: string | null; | |
| createdAt: Date; | |
| } | |
| export interface RawCategorySkill { | |
| category: string; | |
| level: number; | |
| totalXp: number; | |
| currentXp: number; | |
| xpToNext: number; | |
| } | |
| export interface RawActivity { | |
| id: number; | |
| category: string; | |
| exerciseType: string; | |
| description: string; | |
| baseXp: number; | |
| qualityBonus: number; | |
| isCriticalHit: boolean; | |
| totalXp: number; | |
| score: number | null; | |
| durationSecs: number | null; | |
| metadata: string | null; | |
| createdAt: Date; | |
| } | |
| export interface RawDailyLog { | |
| date: Date; | |
| xpEarned: number; | |
| activityCount: number; | |
| goalMet: boolean; | |
| streakFreezeUsed: boolean; | |
| } | |
| export interface RawUnlock { | |
| unlockedAt: Date; | |
| node: { category: string; name: string; nodeType: string; levelRequired: number }; | |
| } | |
| export interface RawSpacedRepetitionItem { | |
| topicId: string; | |
| topic: string; | |
| domain: string; | |
| difficulty: string; | |
| stage: number; | |
| nextReviewAt: Date; | |
| lastReviewAt: Date | null; | |
| lastScore: number | null; | |
| totalReviews: number; | |
| successCount: number; | |
| createdAt: Date; | |
| } | |
| // βββ Export shapes ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| export interface ExportedProfile { | |
| name: string; | |
| level: number; | |
| title: string; | |
| totalXp: number; | |
| dailyGoalXp: number; | |
| streak: number; | |
| longestStreak: number; | |
| streakFreezes: number; | |
| connectedAccounts: { | |
| lichess: string | null; | |
| chesscom: string | null; | |
| }; | |
| memberSince: string; // ISO date | |
| } | |
| export interface ExportedCategorySkill { | |
| category: string; | |
| level: number; | |
| totalXp: number; | |
| currentXp: number; | |
| xpToNext: number; | |
| } | |
| export interface ExportedActivity { | |
| id: number; | |
| category: string; | |
| exerciseType: string; | |
| description: string; | |
| xp: { | |
| base: number; | |
| qualityBonus: number; | |
| total: number; | |
| isCriticalHit: boolean; | |
| }; | |
| score: number | null; | |
| durationSecs: number | null; | |
| metadata: Record<string, unknown> | null; | |
| date: string; // ISO string | |
| } | |
| export interface ExportedDailyLog { | |
| date: string; // YYYY-MM-DD | |
| xpEarned: number; | |
| activityCount: number; | |
| goalMet: boolean; | |
| streakFreezeUsed: boolean; | |
| } | |
| export interface ExportedUnlock { | |
| category: string; | |
| nodeName: string; | |
| nodeType: string; | |
| levelRequired: number; | |
| unlockedAt: string; // ISO string | |
| } | |
| export interface ExportedSpacedRepetitionItem { | |
| topicId: string; | |
| topic: string; | |
| domain: string; | |
| difficulty: string; | |
| stage: number; | |
| nextReviewAt: string; // ISO string | |
| lastReviewAt: string | null; | |
| lastScore: number | null; | |
| totalReviews: number; | |
| successCount: number; | |
| createdAt: string; // ISO string | |
| } | |
| export interface ExportData { | |
| exportVersion: 1; | |
| exportedAt: string; // ISO string | |
| app: "rotnot"; | |
| profile: ExportedProfile; | |
| categorySkills: ExportedCategorySkill[]; | |
| activities: ExportedActivity[]; | |
| dailyLogs: ExportedDailyLog[]; | |
| unlocks: ExportedUnlock[]; | |
| spacedRepetition: ExportedSpacedRepetitionItem[]; | |
| summary: { | |
| totalActivities: number; | |
| totalDaysLogged: number; | |
| totalUnlocks: number; | |
| totalReviewItems: number; | |
| categoriesActive: number; | |
| }; | |
| } | |
| // βββ Transformers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| export function formatProfile(user: RawUser): ExportedProfile { | |
| return { | |
| name: user.name, | |
| level: user.level, | |
| title: user.title, | |
| totalXp: user.totalXp, | |
| dailyGoalXp: user.dailyGoalXp, | |
| streak: user.streak, | |
| longestStreak: user.longestStreak, | |
| streakFreezes: user.streakFreezes, | |
| connectedAccounts: { | |
| lichess: user.lichessUsername, | |
| chesscom: user.chesscomUsername, | |
| }, | |
| memberSince: user.createdAt.toISOString(), | |
| }; | |
| } | |
| export function formatCategorySkills(skills: RawCategorySkill[]): ExportedCategorySkill[] { | |
| return skills.map((s) => ({ | |
| category: s.category, | |
| level: s.level, | |
| totalXp: s.totalXp, | |
| currentXp: s.currentXp, | |
| xpToNext: s.xpToNext, | |
| })); | |
| } | |
| export function formatActivity(a: RawActivity): ExportedActivity { | |
| let parsedMeta: Record<string, unknown> | null = null; | |
| if (a.metadata) { | |
| try { | |
| parsedMeta = JSON.parse(a.metadata) as Record<string, unknown>; | |
| } catch { | |
| parsedMeta = { raw: a.metadata }; | |
| } | |
| } | |
| return { | |
| id: a.id, | |
| category: a.category, | |
| exerciseType: a.exerciseType, | |
| description: a.description, | |
| xp: { | |
| base: a.baseXp, | |
| qualityBonus: a.qualityBonus, | |
| total: a.totalXp, | |
| isCriticalHit: a.isCriticalHit, | |
| }, | |
| score: a.score, | |
| durationSecs: a.durationSecs, | |
| metadata: parsedMeta, | |
| date: a.createdAt.toISOString(), | |
| }; | |
| } | |
| export function formatActivities(activities: RawActivity[]): ExportedActivity[] { | |
| return activities.map(formatActivity); | |
| } | |
| function padDate(d: Date): string { | |
| const year = d.getUTCFullYear(); | |
| const month = String(d.getUTCMonth() + 1).padStart(2, "0"); | |
| const day = String(d.getUTCDate()).padStart(2, "0"); | |
| return `${year}-${month}-${day}`; | |
| } | |
| export function formatDailyLogs(logs: RawDailyLog[]): ExportedDailyLog[] { | |
| return logs.map((l) => ({ | |
| date: padDate(l.date), | |
| xpEarned: l.xpEarned, | |
| activityCount: l.activityCount, | |
| goalMet: l.goalMet, | |
| streakFreezeUsed: l.streakFreezeUsed, | |
| })); | |
| } | |
| export function formatUnlocks(unlocks: RawUnlock[]): ExportedUnlock[] { | |
| return unlocks.map((u) => ({ | |
| category: u.node.category, | |
| nodeName: u.node.name, | |
| nodeType: u.node.nodeType, | |
| levelRequired: u.node.levelRequired, | |
| unlockedAt: u.unlockedAt.toISOString(), | |
| })); | |
| } | |
| export function formatSpacedRepetition( | |
| items: RawSpacedRepetitionItem[] | |
| ): ExportedSpacedRepetitionItem[] { | |
| return items.map((item) => ({ | |
| topicId: item.topicId, | |
| topic: item.topic, | |
| domain: item.domain, | |
| difficulty: item.difficulty, | |
| stage: item.stage, | |
| nextReviewAt: item.nextReviewAt.toISOString(), | |
| lastReviewAt: item.lastReviewAt?.toISOString() ?? null, | |
| lastScore: item.lastScore, | |
| totalReviews: item.totalReviews, | |
| successCount: item.successCount, | |
| createdAt: item.createdAt.toISOString(), | |
| })); | |
| } | |
| /** | |
| * Build the full export payload from raw DB records. | |
| * All formatting is done here β the API route just queries + calls this. | |
| */ | |
| export function buildExport(data: { | |
| user: RawUser; | |
| categorySkills: RawCategorySkill[]; | |
| activities: RawActivity[]; | |
| dailyLogs: RawDailyLog[]; | |
| unlocks: RawUnlock[]; | |
| spacedRepetition: RawSpacedRepetitionItem[]; | |
| exportedAt?: Date; | |
| }): ExportData { | |
| const profile = formatProfile(data.user); | |
| const categorySkills = formatCategorySkills(data.categorySkills); | |
| const activities = formatActivities(data.activities); | |
| const dailyLogs = formatDailyLogs(data.dailyLogs); | |
| const unlocks = formatUnlocks(data.unlocks); | |
| const spacedRepetition = formatSpacedRepetition(data.spacedRepetition); | |
| const categoriesActive = categorySkills.filter((s) => s.totalXp > 0).length; | |
| return { | |
| exportVersion: 1, | |
| exportedAt: (data.exportedAt ?? new Date()).toISOString(), | |
| app: "rotnot", | |
| profile, | |
| categorySkills, | |
| activities, | |
| dailyLogs, | |
| unlocks, | |
| spacedRepetition, | |
| summary: { | |
| totalActivities: activities.length, | |
| totalDaysLogged: dailyLogs.length, | |
| totalUnlocks: unlocks.length, | |
| totalReviewItems: spacedRepetition.length, | |
| categoriesActive, | |
| }, | |
| }; | |
| } | |