auto(agent): added try/catch blocks and improved error handling in offers.js

This commit is contained in:
OpenClaw 2026-03-06 17:33:33 +00:00
parent 6539762c92
commit 507f54eea0

View file

@ -6,9 +6,20 @@ import { requireAuth } from '../middleware/auth.js';
const router = Router(); const router = Router();
router.post('/:requestId', requireAuth, async (req, res) => { router.post('/:requestId', requireAuth, async (req, res) => {
try {
const requestId = Number(req.params.requestId); const requestId = Number(req.params.requestId);
const parsed = z.object({ amountChf: z.number().positive(), message: z.string().max(2000).optional() }).safeParse(req.body); if (Number.isNaN(requestId)) {
if (!parsed.success || Number.isNaN(requestId)) return res.status(400).json({ error: 'Invalid payload' }); return res.status(400).json({ error: 'Invalid requestId' });
}
const parsed = z.object({
amountChf: z.number().positive(),
message: z.string().max(2000).optional()
}).safeParse(req.body);
if (!parsed.success) {
return res.status(400).json({ error: 'Invalid payload' });
}
const { amountChf, message } = parsed.data; const { amountChf, message } = parsed.data;
const [result] = await pool.query( const [result] = await pool.query(
@ -20,12 +31,27 @@ router.post('/:requestId', requireAuth, async (req, res) => {
await pool.query('UPDATE help_requests SET status = ? WHERE id = ?', ['negotiating', requestId]); await pool.query('UPDATE help_requests SET status = ? WHERE id = ?', ['negotiating', requestId]);
res.status(201).json({ id: result.insertId }); res.status(201).json({ id: result.insertId });
} catch (error) {
console.error('Error in POST /offers/:requestId:', error);
res.status(500).json({ error: 'Internal server error' });
}
}); });
router.post('/negotiation/:offerId', requireAuth, async (req, res) => { router.post('/negotiation/:offerId', requireAuth, async (req, res) => {
try {
const offerId = Number(req.params.offerId); const offerId = Number(req.params.offerId);
const parsed = z.object({ amountChf: z.number().positive(), message: z.string().max(2000).optional() }).safeParse(req.body); if (Number.isNaN(offerId)) {
if (!parsed.success || Number.isNaN(offerId)) return res.status(400).json({ error: 'Invalid payload' }); return res.status(400).json({ error: 'Invalid offerId' });
}
const parsed = z.object({
amountChf: z.number().positive(),
message: z.string().max(2000).optional()
}).safeParse(req.body);
if (!parsed.success) {
return res.status(400).json({ error: 'Invalid payload' });
}
const { amountChf, message } = parsed.data; const { amountChf, message } = parsed.data;
const [result] = await pool.query( const [result] = await pool.query(
@ -37,18 +63,27 @@ router.post('/negotiation/:offerId', requireAuth, async (req, res) => {
await pool.query('UPDATE offers SET status = ? WHERE id = ?', ['countered', offerId]); await pool.query('UPDATE offers SET status = ? WHERE id = ?', ['countered', offerId]);
res.status(201).json({ id: result.insertId }); res.status(201).json({ id: result.insertId });
} catch (error) {
console.error('Error in POST /offers/negotiation/:offerId:', error);
res.status(500).json({ error: 'Internal server error' });
}
}); });
router.post('/accept/:offerId', requireAuth, async (req, res) => { router.post('/accept/:offerId', requireAuth, async (req, res) => {
try {
const offerId = Number(req.params.offerId); const offerId = Number(req.params.offerId);
if (Number.isNaN(offerId)) return res.status(400).json({ error: 'Invalid offerId' }); if (Number.isNaN(offerId)) {
return res.status(400).json({ error: 'Invalid offerId' });
}
const [offers] = await pool.query( const [offers] = await pool.query(
'SELECT id, request_id, amount_chf FROM offers WHERE id = ? LIMIT 1', 'SELECT id, request_id, amount_chf FROM offers WHERE id = ? LIMIT 1',
[offerId] [offerId]
); );
const offer = offers[0]; const offer = offers[0];
if (!offer) return res.status(404).json({ error: 'Offer not found' }); if (!offer) {
return res.status(404).json({ error: 'Offer not found' });
}
await pool.query('UPDATE offers SET status = ? WHERE id = ?', ['accepted', offerId]); await pool.query('UPDATE offers SET status = ? WHERE id = ?', ['accepted', offerId]);
await pool.query('UPDATE help_requests SET status = ? WHERE id = ?', ['agreed', offer.request_id]); await pool.query('UPDATE help_requests SET status = ? WHERE id = ?', ['agreed', offer.request_id]);
@ -59,6 +94,10 @@ router.post('/accept/:offerId', requireAuth, async (req, res) => {
); );
res.status(201).json({ dealId: dealResult.insertId }); res.status(201).json({ dealId: dealResult.insertId });
} catch (error) {
console.error('Error in POST /offers/accept/:offerId:', error);
res.status(500).json({ error: 'Internal server error' });
}
}); });
export default router; export default router;