checkout notifiication error
This commit is contained in:
parent
56bb9b3b4a
commit
b9a70d6e92
1 changed files with 31 additions and 31 deletions
|
|
@ -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) => {
|
||||
try {
|
||||
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',
|
||||
[item.quantity, item.product_id]
|
||||
);
|
||||
}
|
||||
|
||||
// Clear cart
|
||||
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
|
||||
// Process low stock notifications within the transaction
|
||||
const productWithNotification = await client.query(
|
||||
`SELECT stock_quantity, stock_notification
|
||||
FROM products
|
||||
|
|
@ -825,11 +814,12 @@ module.exports = (pool, query, authMiddleware) => {
|
|||
|
||||
// Check if notification is enabled and stock is below threshold
|
||||
if (product.stock_notification &&
|
||||
typeof product.stock_notification === 'object' &&
|
||||
product.stock_notification.enabled === true &&
|
||||
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(
|
||||
`INSERT INTO notification_logs
|
||||
(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({
|
||||
success: true,
|
||||
message: 'Order completed successfully',
|
||||
|
|
|
|||
Loading…
Reference in a new issue