From 410b773da01b7b4672518b13bfdc873a49b332b9 Mon Sep 17 00:00:00 2001 From: Sabeera Date: Fri, 5 Sep 2025 14:54:39 +0530 Subject: [PATCH] modify the generate order number --- utils/generateUniqueNumber.js | 65 +++++++++++++++-------------------- 1 file changed, 28 insertions(+), 37 deletions(-) diff --git a/utils/generateUniqueNumber.js b/utils/generateUniqueNumber.js index a70422c..89a3aaf 100644 --- a/utils/generateUniqueNumber.js +++ b/utils/generateUniqueNumber.js @@ -120,52 +120,46 @@ module.exports.generateRefundNumberService = async () => { }; /** - * Generate a unique order number + * Generate a unique order number using atomic sequence generation * Format: ORD-YYYYMMDD (e.g., ORD-20250901, ORD-20250902) + * Uses UNIQUE_NUMBER_MODEL to prevent race conditions */ module.exports.generateOrderNumber = async () => { try { const today = new Date(); const year = today.getFullYear(); const month = String(today.getMonth() + 1).padStart(2, "0"); - - // Find the last order number for this month - const lastOrder = await ORDER_MODEL.findOne({ - where: { - order_number: { - [Op.like]: `ORD-${year}${month}%`, - }, - }, - order: [["order_number", "DESC"]], + const day = String(today.getDate()).padStart(2, "0"); + + // Create a unique key for this month's order sequence + const sequenceKey = `orderNumber_${year}${month}`; + + // Get or create the sequence record for this month + let record = await UNIQUE_NUMBER_MODEL.findOne({ + where: { type: sequenceKey } }); - let sequence = 1; - if (lastOrder) { - // Extract the day sequence from the last order number - const lastOrderNumber = lastOrder.order_number; - - // Check if the order number follows the expected format ORD-YYYYMMDD - const orderNumberPattern = /^ORD-(\d{4})(\d{2})(\d{2})$/; - const match = lastOrderNumber.match(orderNumberPattern); - - if (match) { - // Extract the day part (last 2 characters) and parse it - const lastDay = parseInt(match[3]); // Get the DD part from the regex match - sequence = lastDay + 1; - } else { - // If the last order number doesn't follow the expected format, - // start from today's date - sequence = today.getDate(); - } + if (!record) { + // First order for this month, start from today's day + record = await UNIQUE_NUMBER_MODEL.create({ + type: sequenceKey, + number: parseInt(day) + }); } else { - // No previous orders for this month, start from today's date - sequence = today.getDate(); + // Atomically increment the sequence number + await record.increment("number", { by: 1 }); + await record.reload(); } + // Get the current sequence number + let sequence = record.number; + // Ensure sequence doesn't exceed the number of days in the month const daysInMonth = new Date(year, month, 0).getDate(); if (sequence > daysInMonth) { - sequence = 1; // Reset to 1 if we exceed the month + // Reset to 1 if we exceed the month and update the record + sequence = 1; + await record.update({ number: sequence }); } // Format sequence as 2-digit day (01, 02, 03, etc.) @@ -175,11 +169,8 @@ module.exports.generateOrderNumber = async () => { return orderNumber; } catch (error) { console.error("Error generating order number:", error); - // Fallback: use today's date instead of timestamp - const today = new Date(); - const year = today.getFullYear(); - const month = String(today.getMonth() + 1).padStart(2, "0"); - const day = String(today.getDate()).padStart(2, "0"); - return `ORD-${year}${month}${day}`; + // Fallback: use timestamp-based order number to ensure uniqueness + const timestamp = Date.now(); + return `ORD-${timestamp}`; } }; -- GitLab