94 lines
3.3 KiB
TypeScript
94 lines
3.3 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import prisma from '@/lib/prisma'
|
|
import { getAuthUser } from '@/lib/auth-utils'
|
|
|
|
// GET /api/offers — optionally filter by businessId
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const { searchParams } = new URL(request.url)
|
|
const businessId = searchParams.get('businessId')
|
|
|
|
const where: Record<string, unknown> = {}
|
|
if (businessId) where.businessId = businessId
|
|
|
|
const offers = await prisma.offer.findMany({
|
|
where,
|
|
include: { business: true },
|
|
orderBy: { createdAt: 'desc' },
|
|
})
|
|
return NextResponse.json(offers)
|
|
} catch (error) {
|
|
console.error('GET /api/offers error:', error)
|
|
return NextResponse.json({ error: 'Erreur serveur' }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
// POST /api/offers (Owner or Admin only)
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const authUser = await getAuthUser(request)
|
|
if (!authUser) {
|
|
return NextResponse.json({ error: 'Authentification requise' }, { status: 401 })
|
|
}
|
|
|
|
const body = await request.json()
|
|
const { businessId, title, description, price, type, imageUrl } = body
|
|
|
|
if (!businessId) {
|
|
return NextResponse.json({ error: 'businessId manquant' }, { status: 400 })
|
|
}
|
|
|
|
// 1. Fetch business and their plan's limit
|
|
const business = await prisma.business.findUnique({
|
|
where: { id: businessId },
|
|
include: {
|
|
_count: { select: { offers: true } }
|
|
}
|
|
})
|
|
|
|
if (!business) {
|
|
return NextResponse.json({ error: 'Entreprise non trouvée' }, { status: 404 })
|
|
}
|
|
|
|
if (business.ownerId !== authUser.id && authUser.role !== 'ADMIN') {
|
|
return NextResponse.json({ error: 'Non autorisé : vous n\'êtes pas propriétaire de cette entreprise' }, { status: 403 })
|
|
}
|
|
|
|
// Fetch the specific limit for this tier from the database
|
|
const planDetails = await prisma.pricingPlan.findUnique({
|
|
where: { tier: business.plan }
|
|
})
|
|
|
|
if (!planDetails) {
|
|
return NextResponse.json({ error: 'Configuration du forfait introuvable' }, { status: 500 })
|
|
}
|
|
|
|
// 2. Enforce limits
|
|
const currentCount = business._count.offers
|
|
const offerLimit = planDetails.offerLimit
|
|
|
|
if (currentCount >= offerLimit) {
|
|
return NextResponse.json({
|
|
error: `Limite d'offres atteinte (${offerLimit} max pour le plan ${planDetails.name}). Veuillez passer au plan supérieur pour publier plus d'offres.`
|
|
}, { status: 403 })
|
|
}
|
|
|
|
// 3. Create offer with whitelisted fields
|
|
const offer = await prisma.offer.create({
|
|
data: {
|
|
businessId,
|
|
title: title || '',
|
|
description: description || '',
|
|
price: price ?? '',
|
|
type: type || 'PRODUCT',
|
|
imageUrl: imageUrl || '',
|
|
},
|
|
include: { business: true },
|
|
})
|
|
return NextResponse.json(offer, { status: 201 })
|
|
} catch (error) {
|
|
console.error('POST /api/offers error:', error)
|
|
return NextResponse.json({ error: 'Erreur lors de la création' }, { status: 500 })
|
|
}
|
|
}
|