File size: 1,809 Bytes
9580dce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { NextResponse } from "next/server";
import { prisma } from "@/lib/db";
import { withApiError, ApiError } from "@/lib/api-error";
import { buildExport } from "@/lib/export";

/**
 * GET /api/export
 *
 * Returns a full JSON export of the user's data.
 * Intended for data portability — download everything in one file.
 */
export const GET = withApiError(async () => {
  const user = await prisma.user.findFirst({ where: { id: 1 } });
  if (!user) throw new ApiError(404, "No user found");

  const [categorySkills, activities, dailyLogs, unlocks, spacedRepetition] =
    await Promise.all([
      prisma.categorySkill.findMany({
        where: { userId: 1 },
        orderBy: { category: "asc" },
      }),
      prisma.activity.findMany({
        where: { userId: 1 },
        orderBy: { createdAt: "desc" },
      }),
      prisma.dailyLog.findMany({
        where: { userId: 1 },
        orderBy: { date: "asc" },
      }),
      prisma.unlock.findMany({
        where: { userId: 1 },
        include: {
          node: {
            select: {
              category: true,
              name: true,
              nodeType: true,
              levelRequired: true,
            },
          },
        },
        orderBy: { unlockedAt: "asc" },
      }),
      prisma.spacedRepetitionItem.findMany({
        where: { userId: 1 },
        orderBy: { createdAt: "asc" },
      }),
    ]);

  const exportData = buildExport({
    user,
    categorySkills,
    activities,
    dailyLogs,
    unlocks,
    spacedRepetition,
  });

  return new NextResponse(JSON.stringify(exportData, null, 2), {
    status: 200,
    headers: {
      "Content-Type": "application/json",
      "Content-Disposition": `attachment; filename="rotnot-export-${new Date().toISOString().slice(0, 10)}.json"`,
    },
  });
});