added CRUD Customers

This commit is contained in:
2026-01-20 19:02:49 +01:00
parent 8e8fd70c8c
commit 3a8a6d1576
7 changed files with 1157 additions and 0 deletions

View File

@@ -0,0 +1,103 @@
import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
import { getCurrentUser } from '@/lib/auth';
// GET - Obtenir un adhérent par ID
export async function GET(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const user = await getCurrentUser();
if (!user) {
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
}
const adherent = await prisma.adherent.findUnique({
where: { id: params.id },
});
if (!adherent) {
return NextResponse.json(
{ error: 'Adhérent non trouvé' },
{ status: 404 }
);
}
return NextResponse.json(adherent);
} catch (error) {
console.error('Erreur lors de la récupération de l\'adhérent:', error);
return NextResponse.json(
{ error: 'Erreur serveur' },
{ status: 500 }
);
}
}
// PUT - Mettre à jour un adhérent
export async function PUT(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const user = await getCurrentUser();
if (!user) {
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
}
const body = await request.json();
const { nom, prenom, dateNaissance, adresse, email, telephone, situation, prescripteur, facturation, commentaire, telephoneSecondaire, instructions } = body;
const updateData: any = {};
if (nom) updateData.nom = nom;
if (prenom) updateData.prenom = prenom;
if (dateNaissance) updateData.dateNaissance = new Date(dateNaissance);
if (adresse) updateData.adresse = adresse;
if (email) updateData.email = email;
if (telephone) updateData.telephone = telephone;
if (situation !== undefined) updateData.situation = situation || null;
if (prescripteur !== undefined) updateData.prescripteur = prescripteur || null;
if (facturation !== undefined) updateData.facturation = facturation || null;
if (commentaire !== undefined) updateData.commentaire = commentaire || null;
if (telephoneSecondaire !== undefined) updateData.telephoneSecondaire = telephoneSecondaire || null;
if (instructions !== undefined) updateData.instructions = instructions || null;
const adherent = await prisma.adherent.update({
where: { id: params.id },
data: updateData,
});
return NextResponse.json(adherent);
} catch (error) {
console.error('Erreur lors de la mise à jour de l\'adhérent:', error);
return NextResponse.json(
{ error: 'Erreur serveur' },
{ status: 500 }
);
}
}
// DELETE - Supprimer un adhérent
export async function DELETE(
request: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const user = await getCurrentUser();
if (!user) {
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
}
await prisma.adherent.delete({
where: { id: params.id },
});
return NextResponse.json({ success: true });
} catch (error) {
console.error('Erreur lors de la suppression de l\'adhérent:', error);
return NextResponse.json(
{ error: 'Erreur serveur' },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,91 @@
import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
import { getCurrentUser } from '@/lib/auth';
// GET - Liste tous les adhérents avec recherche
export async function GET(request: NextRequest) {
try {
const user = await getCurrentUser();
if (!user) {
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
}
const searchParams = request.nextUrl.searchParams;
const search = searchParams.get('search') || '';
// Récupérer tous les adhérents (SQLite ne supporte pas bien les recherches complexes)
const allAdherents = await prisma.adherent.findMany({
orderBy: { createdAt: 'desc' },
});
// Filtrer en JavaScript pour la recherche insensible à la casse
const adherents = search
? allAdherents.filter(
(adherent) => {
const searchLower = search.toLowerCase();
return (
adherent.nom.toLowerCase().includes(searchLower) ||
adherent.prenom.toLowerCase().includes(searchLower) ||
adherent.email.toLowerCase().includes(searchLower) ||
adherent.telephone.includes(search) ||
adherent.adresse.toLowerCase().includes(searchLower) ||
(adherent.telephoneSecondaire && adherent.telephoneSecondaire.includes(search))
);
}
)
: allAdherents;
return NextResponse.json(adherents);
} catch (error) {
console.error('Erreur lors de la récupération des adhérents:', error);
return NextResponse.json(
{ error: 'Erreur serveur' },
{ status: 500 }
);
}
}
// POST - Créer un nouvel adhérent
export async function POST(request: NextRequest) {
try {
const user = await getCurrentUser();
if (!user) {
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
}
const body = await request.json();
const { nom, prenom, dateNaissance, adresse, email, telephone, situation, prescripteur, facturation, commentaire, telephoneSecondaire, instructions } = body;
if (!nom || !prenom || !dateNaissance || !adresse || !email || !telephone) {
return NextResponse.json(
{ error: 'Tous les champs obligatoires sont requis' },
{ status: 400 }
);
}
const adherent = await prisma.adherent.create({
data: {
nom,
prenom,
dateNaissance: new Date(dateNaissance),
adresse,
email,
telephone,
situation: situation || null,
prescripteur: prescripteur || null,
facturation: facturation || null,
commentaire: commentaire || null,
telephoneSecondaire: telephoneSecondaire || null,
instructions: instructions || null,
},
});
return NextResponse.json(adherent, { status: 201 });
} catch (error) {
console.error('Erreur lors de la création de l\'adhérent:', error);
return NextResponse.json(
{ error: 'Erreur serveur' },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,27 @@
import { redirect } from 'next/navigation';
import { getCurrentUser } from '@/lib/auth';
import DashboardLayout from '@/components/DashboardLayout';
import AdherentsTable from '@/components/AdherentsTable';
export default async function AdherentsPage() {
const user = await getCurrentUser();
if (!user) {
redirect('/login');
}
return (
<DashboardLayout user={user}>
<div className="p-6">
<h1 className="text-3xl font-semibold text-cblack mb-1">
Adhérents
</h1>
<p className="text-sm text-cgray mb-6">
Base de données des adhérents
</p>
<AdherentsTable />
</div>
</DashboardLayout>
);
}