lkogging
This commit is contained in:
parent
0247b15e54
commit
4829dbdd16
2 changed files with 51 additions and 26 deletions
|
|
@ -746,6 +746,7 @@ module.exports = (pool, query, authMiddleware) => {
|
|||
router.post('/complete-checkout', async (req, res, next) => {
|
||||
try {
|
||||
const { userId, orderId, sessionId } = req.body;
|
||||
console.log("Complete Checkout ", `${userId} ${orderId} ${sessionId}`)
|
||||
|
||||
if (req.user.id !== userId) {
|
||||
return res.status(403).json({
|
||||
|
|
@ -801,25 +802,40 @@ module.exports = (pool, query, authMiddleware) => {
|
|||
[item.quantity, item.product_id]
|
||||
);
|
||||
|
||||
// Process low stock notifications within the transaction
|
||||
// Process stock notifications after updating stock
|
||||
try {
|
||||
const productWithNotification = await client.query(
|
||||
`SELECT stock_quantity, stock_notification
|
||||
`SELECT id, name, stock_quantity, stock_notification
|
||||
FROM products
|
||||
WHERE id = $1 AND stock_notification IS NOT NULL`,
|
||||
WHERE id = $1`,
|
||||
[item.product_id]
|
||||
);
|
||||
|
||||
if (productWithNotification.rows.length > 0) {
|
||||
const product = productWithNotification.rows[0];
|
||||
let stockNotification;
|
||||
|
||||
// Handle different ways the JSON could be stored
|
||||
if (product.stock_notification) {
|
||||
try {
|
||||
// If it's a string, parse it
|
||||
if (typeof product.stock_notification === 'string') {
|
||||
stockNotification = JSON.parse(product.stock_notification);
|
||||
} else {
|
||||
// Otherwise use as is
|
||||
stockNotification = product.stock_notification;
|
||||
}
|
||||
|
||||
console.log("Stock notification for product:", product.id, stockNotification);
|
||||
|
||||
// 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) {
|
||||
if (stockNotification &&
|
||||
stockNotification.enabled === true &&
|
||||
stockNotification.email &&
|
||||
stockNotification.threshold &&
|
||||
product.stock_quantity <= parseInt(stockNotification.threshold)) {
|
||||
|
||||
// Log the notification - using the current order ID (not NULL)
|
||||
// Log the notification with the order ID
|
||||
await client.query(
|
||||
`INSERT INTO notification_logs
|
||||
(order_id, notification_type, sent_at, status)
|
||||
|
|
@ -827,8 +843,16 @@ module.exports = (pool, query, authMiddleware) => {
|
|||
[orderId, 'low_stock_alert', 'pending']
|
||||
);
|
||||
|
||||
console.log(`Low stock notification queued for product ${item.product_id}`);
|
||||
console.log(`Low stock notification queued for product ${product.id} - ${product.name}`);
|
||||
}
|
||||
} catch (parseError) {
|
||||
console.error("Error parsing stock notification JSON:", parseError);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (notificationError) {
|
||||
console.error("Error processing stock notification:", notificationError);
|
||||
// Continue with checkout even if notification processing fails
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -50,3 +50,4 @@ ALTER TABLE products ADD COLUMN IF NOT EXISTS stock_notification JSONB;
|
|||
-- Create index for faster lookups of products with notifications
|
||||
CREATE INDEX IF NOT EXISTS idx_products_stock_notification ON products ((stock_notification IS NOT NULL))
|
||||
WHERE stock_notification IS NOT NULL;
|
||||
ALTER TABLE notification_logs ALTER COLUMN order_id DROP NOT NULL;
|
||||
Loading…
Reference in a new issue