Files
OpticZ/src/app/api/fournisseurs/route.ts
2026-05-30 14:33:11 +01:00

77 lines
1.9 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { db } from '@/lib/db'
// GET /api/fournisseurs - Get all suppliers
export async function GET(request: NextRequest) {
try {
const searchParams = request.nextUrl.searchParams
const actif = searchParams.get('actif')
const where: any = {}
if (actif !== null && actif !== 'all') {
where.actif = actif === 'true'
}
const fournisseurs = await db.fournisseur.findMany({
where,
orderBy: {
nom: 'asc',
},
include: {
_count: {
select: {
produits: true,
facturesAchat: true,
},
},
},
})
return NextResponse.json(fournisseurs)
} catch (error) {
console.error('Error fetching fournisseurs:', error)
return NextResponse.json(
{ error: 'Failed to fetch suppliers' },
{ status: 500 }
)
}
}
// POST /api/fournisseurs - Create a new supplier
export async function POST(request: NextRequest) {
try {
const body = await request.json()
// Validate required fields
if (!body.nom) {
return NextResponse.json(
{ error: 'Missing required field: nom' },
{ status: 400 }
)
}
const fournisseur = await db.fournisseur.create({
data: {
nom: body.nom,
contact: body.contact || null,
email: body.email || null,
telephone: body.telephone || null,
adresse: body.adresse || null,
ville: body.ville || null,
codePostal: body.codePostal || null,
notes: body.notes || null,
actif: body.actif !== undefined ? body.actif : true,
},
})
return NextResponse.json(fournisseur)
} catch (error) {
console.error('Error creating fournisseur:', error)
return NextResponse.json(
{ error: 'Failed to create supplier' },
{ status: 500 }
)
}
}