All materials
cooperatives.ts
tscooperatives.ts
import { Router, Request, Response } from 'express'
import { prisma } from '../lib/db'
import { requireAuth } from '../lib/auth'
const router = Router()
router.get('/', requireAuth, async (_req: Request, res: Response) => {
try {
const cooperatives = await prisma.cooperative.findMany({
include: { batches: { select: { id: true, stage: true } } },
orderBy: { nom: 'asc' },
})
res.json(cooperatives)
} catch (error) {
res.status(500).json({ error: 'Failed to fetch cooperatives' })
}
})
router.get('/:id', requireAuth, async (req: Request, res: Response) => {
try {
const cooperative = await prisma.cooperative.findUnique({
where: { id: parseInt(req.params.id) },
include: {
batches: {
orderBy: { date_reception: 'desc' },
},
},
})
if (!cooperative) {
return res.status(404).json({ error: 'Cooperative not found' })
}
res.json(cooperative)
} catch (error) {
res.status(500).json({ error: 'Failed to fetch cooperative' })
}
})
export default router