Added few configurations & mores

This commit is contained in:
2026-01-22 18:53:23 +01:00
parent 9051491fd0
commit d5d0d5aaf4
30 changed files with 3309 additions and 80 deletions

View File

@@ -0,0 +1,106 @@
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 }
);
}
}

View File

@@ -0,0 +1,105 @@
import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
import { getCurrentUser } from '@/lib/auth';
// GET - Récupérer toutes les options par type
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 type = searchParams.get('type'); // "situation", "prescripteur", "facturation"
const where: any = {};
if (type) {
where.type = type;
}
const options = await prisma.adherentOption.findMany({
where,
orderBy: [
{ type: 'asc' },
{ order: 'asc' },
{ value: 'asc' },
],
});
// Grouper par type
const grouped = options.reduce((acc, option) => {
if (!acc[option.type]) {
acc[option.type] = [];
}
acc[option.type].push(option);
return acc;
}, {} as Record<string, typeof options>);
return NextResponse.json(type ? options : grouped);
} catch (error) {
console.error('Erreur lors de la récupération des options:', error);
return NextResponse.json(
{ error: 'Erreur serveur' },
{ status: 500 }
);
}
}
// POST - Créer une nouvelle option
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 { type, value, order } = body;
if (!type || !value) {
return NextResponse.json(
{ error: 'Le type et la valeur sont requis' },
{ status: 400 }
);
}
if (!['situation', 'prescripteur', 'facturation'].includes(type)) {
return NextResponse.json(
{ error: 'Type invalide. Doit être: situation, prescripteur ou facturation' },
{ status: 400 }
);
}
// Vérifier si l'option existe déjà
const existing = await prisma.adherentOption.findFirst({
where: {
type,
value,
},
});
if (existing) {
return NextResponse.json(
{ error: 'Cette option existe déjà' },
{ status: 400 }
);
}
const option = await prisma.adherentOption.create({
data: {
type,
value,
order: order || 0,
},
});
return NextResponse.json(option, { status: 201 });
} catch (error) {
console.error('Erreur lors de la création de l\'option:', error);
return NextResponse.json(
{ error: 'Erreur serveur' },
{ status: 500 }
);
}
}