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) => {
|
router.post('/complete-checkout', async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const { userId, orderId, sessionId } = req.body;
|
const { userId, orderId, sessionId } = req.body;
|
||||||
|
console.log("Complete Checkout ", `${userId} ${orderId} ${sessionId}`)
|
||||||
|
|
||||||
if (req.user.id !== userId) {
|
if (req.user.id !== userId) {
|
||||||
return res.status(403).json({
|
return res.status(403).json({
|
||||||
|
|
@ -801,25 +802,40 @@ module.exports = (pool, query, authMiddleware) => {
|
||||||
[item.quantity, item.product_id]
|
[item.quantity, item.product_id]
|
||||||
);
|
);
|
||||||
|
|
||||||
// Process low stock notifications within the transaction
|
// Process stock notifications after updating stock
|
||||||
|
try {
|
||||||
const productWithNotification = await client.query(
|
const productWithNotification = await client.query(
|
||||||
`SELECT stock_quantity, stock_notification
|
`SELECT id, name, stock_quantity, stock_notification
|
||||||
FROM products
|
FROM products
|
||||||
WHERE id = $1 AND stock_notification IS NOT NULL`,
|
WHERE id = $1`,
|
||||||
[item.product_id]
|
[item.product_id]
|
||||||
);
|
);
|
||||||
|
|
||||||
if (productWithNotification.rows.length > 0) {
|
if (productWithNotification.rows.length > 0) {
|
||||||
const product = productWithNotification.rows[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
|
// Check if notification is enabled and stock is below threshold
|
||||||
if (product.stock_notification &&
|
if (stockNotification &&
|
||||||
typeof product.stock_notification === 'object' &&
|
stockNotification.enabled === true &&
|
||||||
product.stock_notification.enabled === true &&
|
stockNotification.email &&
|
||||||
product.stock_notification.threshold &&
|
stockNotification.threshold &&
|
||||||
product.stock_quantity <= product.stock_notification.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(
|
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)
|
||||||
|
|
@ -827,8 +843,16 @@ module.exports = (pool, query, authMiddleware) => {
|
||||||
[orderId, 'low_stock_alert', 'pending']
|
[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 for faster lookups of products with notifications
|
||||||
CREATE INDEX IF NOT EXISTS idx_products_stock_notification ON products ((stock_notification IS NOT NULL))
|
CREATE INDEX IF NOT EXISTS idx_products_stock_notification ON products ((stock_notification IS NOT NULL))
|
||||||
WHERE 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