107 lines
2.5 KiB
TypeScript
107 lines
2.5 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { prisma } from '@/lib/prisma';
|
|
import { getCurrentUser } from '@/lib/auth';
|
|
|
|
// PUT - Modifier une option
|
|
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 { value, order } = body;
|
|
|
|
if (!value) {
|
|
return NextResponse.json(
|
|
{ error: 'La valeur est requise' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Vérifier si l'option existe
|
|
const existing = await prisma.adherentOption.findUnique({
|
|
where: { id: params.id },
|
|
});
|
|
|
|
if (!existing) {
|
|
return NextResponse.json(
|
|
{ error: 'Option non trouvée' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
// Vérifier si une autre option avec le même type et valeur existe
|
|
const duplicate = await prisma.adherentOption.findFirst({
|
|
where: {
|
|
type: existing.type,
|
|
value,
|
|
id: { not: params.id },
|
|
},
|
|
});
|
|
|
|
if (duplicate) {
|
|
return NextResponse.json(
|
|
{ error: 'Cette option existe déjà' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const option = await prisma.adherentOption.update({
|
|
where: { id: params.id },
|
|
data: {
|
|
value,
|
|
order: order !== undefined ? order : existing.order,
|
|
},
|
|
});
|
|
|
|
return NextResponse.json(option);
|
|
} catch (error) {
|
|
console.error('Erreur lors de la modification de l\'option:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Erreur serveur' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
// DELETE - Supprimer une option
|
|
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 });
|
|
}
|
|
|
|
const option = await prisma.adherentOption.findUnique({
|
|
where: { id: params.id },
|
|
});
|
|
|
|
if (!option) {
|
|
return NextResponse.json(
|
|
{ error: 'Option non trouvée' },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
await prisma.adherentOption.delete({
|
|
where: { id: params.id },
|
|
});
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
console.error('Erreur lors de la suppression de l\'option:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Erreur serveur' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|