Added few configurations & mores
This commit is contained in:
105
app/api/settings/adherent-options/route.ts
Normal file
105
app/api/settings/adherent-options/route.ts
Normal 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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user