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) => {
|
router.post('/complete-checkout', async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const { userId, orderId, sessionId } = req.body;
|
const { userId, orderId, sessionId } = req.body;
|
||||||
|
|
@ -800,6 +800,36 @@ 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]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Process low stock notifications within the transaction
|
||||||
|
const productWithNotification = await client.query(
|
||||||
|
`SELECT stock_quantity, stock_notification
|
||||||
|
FROM products
|
||||||
|
WHERE id = $1 AND stock_notification IS NOT NULL`,
|
||||||
|
[item.product_id]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (productWithNotification.rows.length > 0) {
|
||||||
|
const product = productWithNotification.rows[0];
|
||||||
|
|
||||||
|
// 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 - using the current order ID (not NULL)
|
||||||
|
await client.query(
|
||||||
|
`INSERT INTO notification_logs
|
||||||
|
(order_id, notification_type, sent_at, status)
|
||||||
|
VALUES ($1, $2, NOW(), $3)`,
|
||||||
|
[orderId, 'low_stock_alert', 'pending']
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log(`Low stock notification queued for product ${item.product_id}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear cart
|
// Clear cart
|
||||||
|
|
@ -811,36 +841,6 @@ module.exports = (pool, query, authMiddleware) => {
|
||||||
|
|
||||||
await client.query('COMMIT');
|
await client.query('COMMIT');
|
||||||
|
|
||||||
for (const item of cartItemsResult.rows) {
|
|
||||||
// Get product details including notification settings
|
|
||||||
const productWithNotification = await client.query(
|
|
||||||
`SELECT stock_quantity, stock_notification
|
|
||||||
FROM products
|
|
||||||
WHERE id = $1 AND stock_notification IS NOT NULL`,
|
|
||||||
[item.product_id]
|
|
||||||
);
|
|
||||||
|
|
||||||
if (productWithNotification.rows.length > 0) {
|
|
||||||
const product = productWithNotification.rows[0];
|
|
||||||
|
|
||||||
// Check if notification is enabled and stock is below threshold
|
|
||||||
if (product.stock_notification &&
|
|
||||||
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
|
|
||||||
await client.query(
|
|
||||||
`INSERT INTO notification_logs
|
|
||||||
(order_id, notification_type, sent_at, status)
|
|
||||||
VALUES ($1, $2, NOW(), $3)`,
|
|
||||||
[orderId, 'low_stock_alert', 'pending']
|
|
||||||
);
|
|
||||||
|
|
||||||
console.log(`Low stock notification queued for product ${item.product_id}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
message: 'Order completed successfully',
|
message: 'Order completed successfully',
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue