Add UPC validation and automatic item number generation/validation

This commit is contained in:
2025-03-01 19:37:51 -05:00
parent 8271c9f95a
commit 98e3b89d46
5 changed files with 1225 additions and 143 deletions

View File

@@ -899,6 +899,100 @@ router.get('/search-products', async (req, res) => {
}
});
// Endpoint to check UPC and generate item number
router.get('/check-upc-and-generate-sku', async (req, res) => {
const { upc, supplierId } = req.query;
if (!upc || !supplierId) {
return res.status(400).json({ error: 'UPC and supplier ID are required' });
}
try {
const { connection } = await getDbConnection();
// Step 1: Check if the UPC already exists
const [upcCheck] = await connection.query(
'SELECT pid, itemnumber FROM products WHERE upc = ? LIMIT 1',
[upc]
);
if (upcCheck.length > 0) {
return res.status(409).json({
error: 'UPC already exists',
existingProductId: upcCheck[0].pid,
existingItemNumber: upcCheck[0].itemnumber
});
}
// Step 2: Generate item number - supplierId-last6DigitsOfUPC minus last digit
let itemNumber = '';
const upcStr = String(upc);
// Extract the last 6 digits of the UPC, removing the last digit (checksum)
// So we get 5 digits from positions: length-7 to length-2
if (upcStr.length >= 7) {
const lastSixMinusOne = upcStr.substring(upcStr.length - 7, upcStr.length - 1);
itemNumber = `${supplierId}-${lastSixMinusOne}`;
} else if (upcStr.length >= 6) {
// If UPC is shorter, use as many digits as possible
const digitsToUse = upcStr.substring(0, upcStr.length - 1);
itemNumber = `${supplierId}-${digitsToUse}`;
} else {
// Very short UPC, just use the whole thing
itemNumber = `${supplierId}-${upcStr}`;
}
// Step 3: Check if the generated item number exists
const [itemNumberCheck] = await connection.query(
'SELECT pid FROM products WHERE itemnumber = ? LIMIT 1',
[itemNumber]
);
// Step 4: If the item number exists, modify it to use the last 5 digits of the UPC
if (itemNumberCheck.length > 0) {
console.log(`Item number ${itemNumber} already exists, using alternative format`);
if (upcStr.length >= 5) {
// Use the last 5 digits (including the checksum)
const lastFive = upcStr.substring(upcStr.length - 5);
itemNumber = `${supplierId}-${lastFive}`;
// Check again if this new item number also exists
const [altItemNumberCheck] = await connection.query(
'SELECT pid FROM products WHERE itemnumber = ? LIMIT 1',
[itemNumber]
);
if (altItemNumberCheck.length > 0) {
// If even the alternative format exists, add a timestamp suffix for uniqueness
const timestamp = Date.now().toString().substring(8, 13); // Get last 5 digits of timestamp
itemNumber = `${supplierId}-${timestamp}`;
console.log(`Alternative item number also exists, using timestamp: ${itemNumber}`);
}
} else {
// For very short UPCs, add a timestamp
const timestamp = Date.now().toString().substring(8, 13); // Get last 5 digits of timestamp
itemNumber = `${supplierId}-${timestamp}`;
}
}
// Return the generated item number
res.json({
success: true,
itemNumber,
upc,
supplierId
});
} catch (error) {
console.error('Error checking UPC and generating item number:', error);
res.status(500).json({
error: 'Failed to check UPC and generate item number',
details: error.message
});
}
});
// Get product categories for a specific product
router.get('/product-categories/:pid', async (req, res) => {
try {