File size: 3,130 Bytes
fe9acc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a380234
fe9acc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
89
90
91
import { NextRequest, NextResponse } from "next/server";
import { db } from "@/lib/db";

// GET /api/admin/settings — retrieve admin notification settings
export async function GET() {
  try {
    let settings = await db.adminSettings.findUnique({
      where: { id: "default" },
    });

    if (!settings) {
      settings = await db.adminSettings.create({
        data: { id: "default" },
      });
    }

    const { smtpPass: _, ...safeSettings } = settings;
    return NextResponse.json({
      ...safeSettings,
      smtpPass: settings.smtpPass ? "••••••••" : "",
      hasSmtpPass: !!settings.smtpPass,
    });
  } catch (error) {
    console.error("Failed to fetch admin settings:", error);
    return NextResponse.json(
      { error: "Erreur lors du chargement des paramètres" },
      { status: 500 }
    );
  }
}

// PUT /api/admin/settings — update admin notification settings
export async function PUT(request: NextRequest) {
  try {
    const authHeader = request.headers.get("authorization");
    const token = authHeader?.replace("Bearer ", "");
    const adminPassword = process.env.ADMIN_PASSWORD || "ia-admin-2025";
    if (token !== adminPassword) {
      return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
    }

    const body = await request.json();
    const {
      notifyEmail, smtpHost, smtpPort, smtpUser, smtpPass,
      notifyOnCreate, notifyOnUpdate, notifyOnDelete,
    } = body;

    const existing = await db.adminSettings.findUnique({ where: { id: "default" } });
    const finalSmtpPass = smtpPass === "••••••••" ? (existing?.smtpPass ?? "") : (smtpPass ?? "");

    const settings = await db.adminSettings.upsert({
      where: { id: "default" },
      update: {
        ...(notifyEmail !== undefined && { notifyEmail }),
        ...(smtpHost !== undefined && { smtpHost }),
        ...(smtpPort !== undefined && { smtpPort: Number(smtpPort) || 587 }),
        ...(smtpUser !== undefined && { smtpUser }),
        ...(smtpPass !== undefined && { smtpPass: finalSmtpPass }),
        ...(notifyOnCreate !== undefined && { notifyOnCreate }),
        ...(notifyOnUpdate !== undefined && { notifyOnUpdate }),
        ...(notifyOnDelete !== undefined && { notifyOnDelete }),
      },
      create: {
        id: "default",
        notifyEmail: notifyEmail ?? "",
        smtpHost: smtpHost ?? "",
        smtpPort: Number(smtpPort) || 587,
        smtpUser: smtpUser ?? "",
        smtpPass: finalSmtpPass,
        notifyOnCreate: notifyOnCreate ?? true,
        notifyOnUpdate: notifyOnUpdate ?? true,
        notifyOnDelete: notifyOnDelete ?? true,
      },
    });

    const { smtpPass: _, ...safeSettings } = settings;
    return NextResponse.json({
      ...safeSettings,
      smtpPass: "••••••••",
      hasSmtpPass: !!settings.smtpPass,
      message: "Paramètres enregistrés avec succès",
    });
  } catch (error) {
    console.error("Failed to update admin settings:", error);
    return NextResponse.json(
      { error: "Erreur lors de la mise à jour des paramètres" },
      { status: 500 }
    );
  }
}