Files
2026-01-22 19:25:25 +01:00

93 lines
3.0 KiB
TypeScript

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, forfait, 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,
forfait: forfait || 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 }
);
}
}