checkout notifiication error

This commit is contained in:
2ManyProjects 2025-04-28 11:04:01 -05:00
parent 56bb9b3b4a
commit b9a70d6e92

View file

@ -742,7 +742,7 @@ module.exports = (pool, query, authMiddleware) => {
} }
}); });
// New endpoint: Complete checkout after successful payment // Complete checkout after successful payment
router.post('/complete-checkout', async (req, res, next) => { router.post('/complete-checkout', async (req, res, next) => {
try { try {
const { userId, orderId, sessionId } = req.body; const { userId, orderId, sessionId } = req.body;
@ -800,19 +800,8 @@ module.exports = (pool, query, authMiddleware) => {
'UPDATE products SET stock_quantity = stock_quantity - $1 WHERE id = $2', 'UPDATE products SET stock_quantity = stock_quantity - $1 WHERE id = $2',
[item.quantity, item.product_id] [item.quantity, item.product_id]
); );
}
// Clear cart // Process low stock notifications within the transaction
await client.query(
'DELETE FROM cart_items WHERE cart_id = $1',
[cartId]
);
}
await client.query('COMMIT');
for (const item of cartItemsResult.rows) {
// Get product details including notification settings
const productWithNotification = await client.query( const productWithNotification = await client.query(
`SELECT stock_quantity, stock_notification `SELECT stock_quantity, stock_notification
FROM products FROM products
@ -825,11 +814,12 @@ module.exports = (pool, query, authMiddleware) => {
// Check if notification is enabled and stock is below threshold // Check if notification is enabled and stock is below threshold
if (product.stock_notification && if (product.stock_notification &&
typeof product.stock_notification === 'object' &&
product.stock_notification.enabled === true && product.stock_notification.enabled === true &&
product.stock_notification.threshold && product.stock_notification.threshold &&
product.stock_quantity <= product.stock_notification.threshold) { product.stock_quantity <= product.stock_notification.threshold) {
// Log the notification - it will be picked up by the notification service // Log the notification - using the current order ID (not NULL)
await client.query( await client.query(
`INSERT INTO notification_logs `INSERT INTO notification_logs
(order_id, notification_type, sent_at, status) (order_id, notification_type, sent_at, status)
@ -841,6 +831,16 @@ module.exports = (pool, query, authMiddleware) => {
} }
} }
} }
// Clear cart
await client.query(
'DELETE FROM cart_items WHERE cart_id = $1',
[cartId]
);
}
await client.query('COMMIT');
res.status(200).json({ res.status(200).json({
success: true, success: true,
message: 'Order completed successfully', message: 'Order completed successfully',