Tweaks and fixes for products table
This commit is contained in:
@@ -8,7 +8,29 @@ dotenv.config({ path: path.join(__dirname, "../../.env") });
|
||||
|
||||
// Utility functions
|
||||
const imageUrlBase = process.env.PRODUCT_IMAGE_URL_BASE || 'https://sbing.com/i/products/0000/';
|
||||
const getImageUrls = (pid, iid = 1) => {
|
||||
|
||||
// Modified to accept a db connection for querying product_images
|
||||
const getImageUrls = async (pid, prodConnection, iid = null) => {
|
||||
// If iid isn't provided, try to get it from product_images
|
||||
if (iid === null && prodConnection) {
|
||||
try {
|
||||
// Query for images with order=255 (default/primary images)
|
||||
const [primaryImages] = await prodConnection.query(
|
||||
'SELECT iid FROM product_images WHERE pid = ? AND `order` = 255 LIMIT 1',
|
||||
[pid]
|
||||
);
|
||||
|
||||
// Use the found iid or default to 1
|
||||
iid = primaryImages.length > 0 ? primaryImages[0].iid : 1;
|
||||
} catch (error) {
|
||||
console.error(`Error fetching primary image for pid ${pid}:`, error);
|
||||
iid = 1; // Fallback to default
|
||||
}
|
||||
} else {
|
||||
// Use default if connection not provided
|
||||
iid = iid || 1;
|
||||
}
|
||||
|
||||
const paddedPid = pid.toString().padStart(6, '0');
|
||||
// Use padded PID only for the first 3 digits
|
||||
const prefix = paddedPid.slice(0, 3);
|
||||
@@ -237,9 +259,11 @@ async function importMissingProducts(prodConnection, localConnection, missingPid
|
||||
return `(${Array.from({ length: 47 }, (_, i) => `$${base + i + 1}`).join(', ')})`;
|
||||
}).join(',');
|
||||
|
||||
const values = batch.flatMap(row => {
|
||||
const imageUrls = getImageUrls(row.pid);
|
||||
return [
|
||||
// Process image URLs for the batch
|
||||
const processedValues = [];
|
||||
for (const row of batch) {
|
||||
const imageUrls = await getImageUrls(row.pid, prodConnection);
|
||||
processedValues.push([
|
||||
row.pid,
|
||||
row.title,
|
||||
row.description,
|
||||
@@ -287,8 +311,10 @@ async function importMissingProducts(prodConnection, localConnection, missingPid
|
||||
imageUrls.image_full,
|
||||
null,
|
||||
null
|
||||
];
|
||||
});
|
||||
]);
|
||||
}
|
||||
|
||||
const values = processedValues.flat();
|
||||
|
||||
const [result] = await localConnection.query(`
|
||||
WITH inserted_products AS (
|
||||
@@ -442,9 +468,11 @@ async function materializeCalculations(prodConnection, localConnection, incremen
|
||||
return `(${Array.from({ length: 47 }, (_, i) => `$${base + i + 1}`).join(', ')})`;
|
||||
}).join(',');
|
||||
|
||||
const values = batch.flatMap(row => {
|
||||
const imageUrls = getImageUrls(row.pid);
|
||||
return [
|
||||
// Process image URLs for the batch
|
||||
const processedValues = [];
|
||||
for (const row of batch) {
|
||||
const imageUrls = await getImageUrls(row.pid, prodConnection);
|
||||
processedValues.push([
|
||||
row.pid,
|
||||
row.title,
|
||||
row.description,
|
||||
@@ -492,8 +520,10 @@ async function materializeCalculations(prodConnection, localConnection, incremen
|
||||
imageUrls.image_full,
|
||||
null,
|
||||
null
|
||||
];
|
||||
});
|
||||
]);
|
||||
}
|
||||
|
||||
const values = processedValues.flat();
|
||||
|
||||
await localConnection.query(`
|
||||
INSERT INTO temp_products (
|
||||
@@ -665,9 +695,11 @@ async function importProducts(prodConnection, localConnection, incrementalUpdate
|
||||
return `(${Array.from({ length: 47 }, (_, i) => `$${base + i + 1}`).join(', ')})`;
|
||||
}).join(',');
|
||||
|
||||
const values = batch.flatMap(row => {
|
||||
const imageUrls = getImageUrls(row.pid);
|
||||
return [
|
||||
// Process image URLs for the batch
|
||||
const processedValues = [];
|
||||
for (const row of batch) {
|
||||
const imageUrls = await getImageUrls(row.pid, prodConnection);
|
||||
processedValues.push([
|
||||
row.pid,
|
||||
row.title,
|
||||
row.description,
|
||||
@@ -715,8 +747,10 @@ async function importProducts(prodConnection, localConnection, incrementalUpdate
|
||||
imageUrls.image_full,
|
||||
row.options,
|
||||
row.tags
|
||||
];
|
||||
});
|
||||
]);
|
||||
}
|
||||
|
||||
const values = processedValues.flat();
|
||||
|
||||
const [result] = await localConnection.query(`
|
||||
WITH upserted AS (
|
||||
|
||||
Reference in New Issue
Block a user