archive products

This commit is contained in:
Jake Kasper
2025-09-03 14:39:20 -04:00
parent a0bde7db23
commit 19314b301f
4 changed files with 57 additions and 6 deletions

View File

@@ -78,7 +78,7 @@ router.get('/', async (req, res, next) => {
FROM products p
JOIN product_categories pc ON p.category_id = pc.id
LEFT JOIN product_rates pr ON p.id = pr.product_id
WHERE 1=1 ${whereClause}
WHERE 1=1 ${whereClause} AND COALESCE(p.archived,false) = false
GROUP BY p.id, pc.name
ORDER BY p.name
`;
@@ -93,7 +93,7 @@ router.get('/', async (req, res, next) => {
LEFT JOIN products p ON up.product_id = p.id
LEFT JOIN product_categories pc ON p.category_id = pc.id
LEFT JOIN product_categories upc ON up.category_id = upc.id
WHERE up.user_id = $1
WHERE up.user_id = $1 AND COALESCE(up.archived,false) = false
ORDER BY COALESCE(up.custom_name, p.name)
`;
@@ -662,7 +662,9 @@ router.delete('/user/:id', validateParams(idParamSchema), async (req, res, next)
);
if (parseInt(usageCheck.rows[0].count) > 0) {
throw new AppError('Cannot delete product that has been used in applications', 400);
// Soft-archive instead of delete when used
await pool.query('UPDATE user_products SET archived = true, updated_at = CURRENT_TIMESTAMP WHERE id = $1', [userProductId]);
return res.json({ success: true, message: 'Custom product archived (in use by applications)' });
}
await pool.query('DELETE FROM user_products WHERE id = $1', [userProductId]);
@@ -676,6 +678,30 @@ router.delete('/user/:id', validateParams(idParamSchema), async (req, res, next)
}
});
// @route PUT /api/products/user/:id/archive
// @desc Archive user's custom product
// @access Private
router.put('/user/:id/archive', validateParams(idParamSchema), async (req, res, next) => {
try {
const userProductId = req.params.id;
const upd = await pool.query('UPDATE user_products SET archived = true, updated_at = CURRENT_TIMESTAMP WHERE id = $1 AND user_id = $2', [userProductId, req.user.id]);
if (upd.rowCount === 0) throw new AppError('User product not found', 404);
res.json({ success: true, message: 'Custom product archived' });
} catch (error) { next(error); }
});
// @route PUT /api/products/user/:id/unarchive
// @desc Unarchive user's custom product
// @access Private
router.put('/user/:id/unarchive', validateParams(idParamSchema), async (req, res, next) => {
try {
const userProductId = req.params.id;
const upd = await pool.query('UPDATE user_products SET archived = false, updated_at = CURRENT_TIMESTAMP WHERE id = $1 AND user_id = $2', [userProductId, req.user.id]);
if (upd.rowCount === 0) throw new AppError('User product not found', 404);
res.json({ success: true, message: 'Custom product unarchived' });
} catch (error) { next(error); }
});
// @route GET /api/products/search
// @desc Search products by name or ingredients
// @access Private
@@ -721,4 +747,4 @@ router.get('/search', async (req, res, next) => {
}
});
module.exports = router;
module.exports = router;