File size: 2,438 Bytes
a572854
4c2a557
a572854
4c2a557
 
a572854
 
 
 
4c2a557
 
 
 
 
 
 
 
 
a572854
 
 
 
 
4c2a557
 
 
a572854
4c2a557
 
 
 
 
 
 
 
 
a572854
4c2a557
 
 
 
 
 
 
 
 
a572854
4c2a557
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a572854
4c2a557
 
 
 
 
 
a572854
4c2a557
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { query } from "@/lib/db/client";
import { NextResponse } from "next/server";
import { verifyApiToken } from "@/lib/auth";

export async function POST(req: Request) {
  const authError = verifyApiToken(req);
  if (authError) {
    return authError;
  }

  try {
    const data = await req.json();

    if (!data.version || !data.data) {
      throw new Error("Invalid import data format");
    }

    try {
      await query("BEGIN");

      await query("TRUNCATE TABLE user_usage_records CASCADE");
      await query("TRUNCATE TABLE model_prices CASCADE");
      await query("TRUNCATE TABLE users CASCADE");

      if (data.data.users?.length) {
        for (const user of data.data.users) {
          await query(
            `INSERT INTO users (id, email, name, role, balance)
             VALUES ($1, $2, $3, $4, $5)`,
            [user.id, user.email, user.name, user.role, user.balance]
          );
        }
      }

      if (data.data.model_prices?.length) {
        for (const price of data.data.model_prices) {
          await query(
            `INSERT INTO model_prices (id, name, input_price, output_price)
             VALUES ($1, $2, $3, $4)`,
            [price.id, price.name, price.input_price, price.output_price]
          );
        }
      }

      if (data.data.user_usage_records?.length) {
        for (const record of data.data.user_usage_records) {
          await query(
            `INSERT INTO user_usage_records (
              user_id, nickname, use_time, model_name, 
              input_tokens, output_tokens, cost, balance_after
            ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
            [
              record.user_id,
              record.nickname,
              record.use_time,
              record.model_name,
              record.input_tokens,
              record.output_tokens,
              record.cost,
              record.balance_after,
            ]
          );
        }
      }

      await query("COMMIT");

      return NextResponse.json({
        success: true,
        message: "Data import successful",
      });
    } catch (error) {
      await query("ROLLBACK");
      throw error;
    }
  } catch (error) {
    console.error("Fail to import database:", error);
    return NextResponse.json(
      {
        success: false,
        error:
          error instanceof Error ? error.message : "Fail to import database",
      },
      { status: 500 }
    );
  }
}