auto(agent): added try/catch block and improved error handling in reviews.js
This commit is contained in:
parent
507f54eea0
commit
00661fd99e
1 changed files with 25 additions and 14 deletions
|
|
@ -6,10 +6,17 @@ import { requireAuth } from '../middleware/auth.js';
|
|||
const router = Router();
|
||||
|
||||
router.post('/:dealId', requireAuth, async (req, res) => {
|
||||
try {
|
||||
const dealId = Number(req.params.dealId);
|
||||
const parsed = z.object({ revieweeId: z.number().int().positive(), rating: z.number().int().min(1).max(5), comment: z.string().max(2000).optional() }).safeParse(req.body);
|
||||
const parsed = z.object({
|
||||
revieweeId: z.number().int().positive(),
|
||||
rating: z.number().int().min(1).max(5),
|
||||
comment: z.string().max(2000).optional()
|
||||
}).safeParse(req.body);
|
||||
|
||||
if (!parsed.success || Number.isNaN(dealId)) return res.status(400).json({ error: 'Invalid payload' });
|
||||
if (!parsed.success || Number.isNaN(dealId)) {
|
||||
return res.status(400).json({ error: 'Invalid payload' });
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
const earliest = new Date(now.getTime() + 2 * 24 * 60 * 60 * 1000);
|
||||
|
|
@ -24,6 +31,10 @@ router.post('/:dealId', requireAuth, async (req, res) => {
|
|||
);
|
||||
|
||||
res.status(201).json({ id: result.insertId });
|
||||
} catch (error) {
|
||||
console.error('Error creating review:', error);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
Loading…
Add table
Add a link
Reference in a new issue