Fix bulk routes matched by :userId param

Moved /balances/bulk/* routes before /balances/:userId/* so Express
doesn't treat "bulk" as a userId and try to parse it as an ObjectId.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
DJP 2026-03-17 23:14:24 -04:00
parent e94cf8f503
commit ac8bb32d3f

View file

@ -45,6 +45,32 @@ router.get('/balances/search', async (req, res) => {
}
});
router.post('/balances/bulk/add', async (req, res) => {
try {
const { amount } = req.body;
if (typeof amount !== 'number' || amount === 0) {
return res.status(400).json({ error: 'Invalid amount' });
}
const result = await balanceService.addToAll(req.db, amount);
res.json(result);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.post('/balances/bulk/set', async (req, res) => {
try {
const { amount } = req.body;
if (typeof amount !== 'number' || amount < 0) {
return res.status(400).json({ error: 'Invalid amount' });
}
const result = await balanceService.setAll(req.db, amount);
res.json(result);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.get('/balances/:userId', async (req, res) => {
try {
const balance = await balanceService.getUserBalance(req.db, req.params.userId);
@ -83,30 +109,4 @@ router.post('/balances/:userId/add', async (req, res) => {
}
});
router.post('/balances/bulk/add', async (req, res) => {
try {
const { amount } = req.body;
if (typeof amount !== 'number' || amount === 0) {
return res.status(400).json({ error: 'Invalid amount' });
}
const result = await balanceService.addToAll(req.db, amount);
res.json(result);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.post('/balances/bulk/set', async (req, res) => {
try {
const { amount } = req.body;
if (typeof amount !== 'number' || amount < 0) {
return res.status(400).json({ error: 'Invalid amount' });
}
const result = await balanceService.setAll(req.db, amount);
res.json(result);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
module.exports = router;