104 lines
3.1 KiB
TypeScript
104 lines
3.1 KiB
TypeScript
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 }
|
|
);
|
|
}
|
|
}
|