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) => { 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,34 +802,57 @@ 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
const productWithNotification = await client.query( try {
`SELECT stock_quantity, stock_notification const productWithNotification = await client.query(
FROM products `SELECT id, name, stock_quantity, stock_notification
WHERE id = $1 AND stock_notification IS NOT NULL`, FROM products
[item.product_id] WHERE id = $1`,
); [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;
// Check if notification is enabled and stock is below threshold // Handle different ways the JSON could be stored
if (product.stock_notification && if (product.stock_notification) {
typeof product.stock_notification === 'object' && try {
product.stock_notification.enabled === true && // If it's a string, parse it
product.stock_notification.threshold && if (typeof product.stock_notification === 'string') {
product.stock_quantity <= product.stock_notification.threshold) { stockNotification = JSON.parse(product.stock_notification);
} else {
// Otherwise use as is
stockNotification = product.stock_notification;
}
// Log the notification - using the current order ID (not NULL) console.log("Stock notification for product:", product.id, stockNotification);
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}`); // 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

@ -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;