Ajout interface admin déployable sur serveur
- API routes pour gestion clients (CRUD complet) - Système de stockage JSON persistant (data/clients.json) - Configuration Next.js serveur (next.config.server.js) - Script de build pour déploiement (scripts/build-server.sh) - Documentation complète de déploiement (ADMIN_DEPLOY.md) Fonctionnalités admin: - Création/modification/suppression de clients - Génération automatique de tokens uniques - QR codes pour configuration clients - Authentification par mot de passe - Backend Node.js avec API REST Déploiement prévu: marama.syoul.fr
This commit is contained in:
131
app/api/admin/clients/route.ts
Normal file
131
app/api/admin/clients/route.ts
Normal file
@ -0,0 +1,131 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { writeFile, readFile, mkdir } from "fs/promises";
|
||||
import { existsSync } from "fs";
|
||||
import path from "path";
|
||||
import { Client, ClientInput } from "@/lib/types/client";
|
||||
|
||||
// Mot de passe admin (à changer en production via variable d'environnement)
|
||||
const ADMIN_PASSWORD = process.env.ADMIN_PASSWORD || "admin123";
|
||||
|
||||
// Chemin vers le fichier de stockage
|
||||
const DATA_DIR = path.join(process.cwd(), "data");
|
||||
const CLIENTS_FILE = path.join(DATA_DIR, "clients.json");
|
||||
|
||||
// Vérifier l'authentification
|
||||
function verifyAuth(request: NextRequest): boolean {
|
||||
const authHeader = request.headers.get("authorization");
|
||||
if (!authHeader) return false;
|
||||
|
||||
const token = authHeader.replace("Bearer ", "");
|
||||
return token === ADMIN_PASSWORD;
|
||||
}
|
||||
|
||||
// Charger les clients depuis le fichier
|
||||
async function loadClients(): Promise<Client[]> {
|
||||
try {
|
||||
if (!existsSync(CLIENTS_FILE)) {
|
||||
// Créer le répertoire et le fichier si nécessaire
|
||||
if (!existsSync(DATA_DIR)) {
|
||||
await mkdir(DATA_DIR, { recursive: true });
|
||||
}
|
||||
await writeFile(CLIENTS_FILE, JSON.stringify([], null, 2));
|
||||
return [];
|
||||
}
|
||||
|
||||
const data = await readFile(CLIENTS_FILE, "utf-8");
|
||||
return JSON.parse(data);
|
||||
} catch (error) {
|
||||
console.error("Erreur lecture clients:", error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
// Sauvegarder les clients dans le fichier
|
||||
async function saveClients(clients: Client[]): Promise<void> {
|
||||
try {
|
||||
if (!existsSync(DATA_DIR)) {
|
||||
await mkdir(DATA_DIR, { recursive: true });
|
||||
}
|
||||
await writeFile(CLIENTS_FILE, JSON.stringify(clients, null, 2));
|
||||
} catch (error) {
|
||||
console.error("Erreur sauvegarde clients:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// GET - Récupérer tous les clients
|
||||
export async function GET(request: NextRequest) {
|
||||
if (!verifyAuth(request)) {
|
||||
return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
|
||||
}
|
||||
|
||||
try {
|
||||
const clients = await loadClients();
|
||||
return NextResponse.json(clients);
|
||||
} catch (error) {
|
||||
console.error("Erreur GET clients:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Erreur serveur" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// POST - Créer un nouveau client
|
||||
export async function POST(request: NextRequest) {
|
||||
if (!verifyAuth(request)) {
|
||||
return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
|
||||
}
|
||||
|
||||
try {
|
||||
const input: ClientInput = await request.json();
|
||||
|
||||
// Validation
|
||||
if (!input.email || !input.bungalowNumber) {
|
||||
return NextResponse.json(
|
||||
{ error: "Email et numéro de bungalow requis" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const clients = await loadClients();
|
||||
|
||||
// Vérifier si l'email existe déjà
|
||||
if (clients.some(c => c.email === input.email)) {
|
||||
return NextResponse.json(
|
||||
{ error: "Un client avec cet email existe déjà" },
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
// Créer le nouveau client
|
||||
const newClient: Client = {
|
||||
id: `client-${Date.now()}`,
|
||||
token: generateToken(),
|
||||
email: input.email,
|
||||
bungalowNumber: input.bungalowNumber,
|
||||
wifiName: input.wifiName || "Lagon-WiFi",
|
||||
wifiPassword: input.wifiPassword || "",
|
||||
gerantMessage: input.gerantMessage || "Bienvenue dans notre pension de famille !",
|
||||
createdAt: new Date().toISOString(),
|
||||
};
|
||||
|
||||
clients.push(newClient);
|
||||
await saveClients(clients);
|
||||
|
||||
return NextResponse.json(newClient, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error("Erreur POST client:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Erreur serveur" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Générer un token unique
|
||||
function generateToken(): string {
|
||||
return Math.random().toString(36).substring(2, 15) +
|
||||
Math.random().toString(36).substring(2, 15);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user