This commit is contained in:
2ManyProjects 2025-04-28 13:33:52 -05:00
parent 0247b15e54
commit 4829dbdd16
2 changed files with 51 additions and 26 deletions

View file

@ -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,34 +802,57 @@ module.exports = (pool, query, authMiddleware) => {
[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];
// Process stock notifications after updating stock
try {
const productWithNotification = await client.query(
`SELECT id, name, stock_quantity, stock_notification
FROM products
WHERE id = $1`,
[item.product_id]
);
// 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 (productWithNotification.rows.length > 0) {
const product = productWithNotification.rows[0];
let stockNotification;
// 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}`);
// 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 (stockNotification &&
stockNotification.enabled === true &&
stockNotification.email &&
stockNotification.threshold &&
product.stock_quantity <= parseInt(stockNotification.threshold)) {
// Log the notification with the order ID
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 ${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
}
}

View file

@ -49,4 +49,5 @@ 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;
WHERE stock_notification IS NOT NULL;
ALTER TABLE notification_logs ALTER COLUMN order_id DROP NOT NULL;