From 320bcd46419091d585a90027dda1f19cfa9d3cc8 Mon Sep 17 00:00:00 2001 From: sebseb7 Date: Sun, 23 Nov 2025 13:28:35 +0100 Subject: [PATCH] refactor: Comment out console logs for improved performance and clarity during sync operations --- src/index.js | 4 ++-- src/syncers/category-products-syncer.js | 16 ++++++++-------- src/syncers/category-syncer.js | 6 +++--- src/syncers/picture-syncer.js | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/index.js b/src/index.js index dd85f42..9dba6f2 100644 --- a/src/index.js +++ b/src/index.js @@ -7,7 +7,7 @@ categorySyncer.on('synced', async ({ tree, unprunedTree, changed }) => { if (changed) { console.log('🎉 Event received: Category tree updated! Root nodes:', tree.length); } else { - console.log('🎉 Event received: Sync finished (no changes). Checking images and products...'); + //console.log('🎉 Event received: Sync finished (no changes). Checking images and products...'); } // Extract all kBild IDs and kKategorie IDs from unpruned tree @@ -28,7 +28,7 @@ categorySyncer.on('synced', async ({ tree, unprunedTree, changed }) => { }; traverse(unprunedTree); - console.log(`🔍 Found ${imageIds.length} images and ${categoryIds.length} categories.`); + //console.log(`🔍 Found ${imageIds.length} images and ${categoryIds.length} categories.`); await pictureSyncer.syncImages(imageIds, 'categories'); await categoryProductsSyncer.syncProducts(categoryIds); diff --git a/src/syncers/category-products-syncer.js b/src/syncers/category-products-syncer.js index 4bf192e..883f85e 100644 --- a/src/syncers/category-products-syncer.js +++ b/src/syncers/category-products-syncer.js @@ -11,7 +11,7 @@ class CategoryProductsSyncer extends EventEmitter { return CategoryProductsSyncer.instance; } this.cacheBaseDir = process.env.CACHE_LOCATION || '.'; - + // Track syncing state this.isSyncing = false; this.queuedCategoryIds = null; @@ -79,14 +79,14 @@ class CategoryProductsSyncer extends EventEmitter { // 2. Update/Create product lists for all valid categories // We update all because product assignments might have changed even if category exists if (validIds.size > 0) { - console.log(`📦 Syncing products for ${validIds.size} categories...`); + //console.log(`📦 Syncing products for ${validIds.size} categories...`); await this._fetchAndWriteProducts([...validIds], productsDir); } else { console.log(`✅ No categories to sync products for.`); } const duration = Date.now() - startTime; - console.log(`✅ Product sync completed in ${duration}ms.`); + //console.log(`✅ Product sync completed in ${duration}ms.`); } async _fetchAndWriteProducts(ids, dir) { @@ -116,7 +116,7 @@ class CategoryProductsSyncer extends EventEmitter { AND ab.kPlattform = ${process.env.JTL_PLATTFORM_ID} AND ab.kShop = ${process.env.JTL_SHOP_ID} AND ka.kKategorie IN (${list}) - ORDER BY a.bRowversion DESC, ab.bRowversion DESC + ORDER BY (CASE WHEN a.bRowversion > ab.bRowversion THEN a.bRowversion ELSE ab.bRowversion END) DESC `); // Collect all kArtikel IDs to fetch images @@ -146,7 +146,7 @@ class CategoryProductsSyncer extends EventEmitter { // Group results by kKategorie const productsByCategory = {}; - + // Initialize arrays for all requested IDs (so we create empty files for empty categories) chunk.forEach(id => { productsByCategory[id] = []; @@ -187,15 +187,15 @@ class CategoryProductsSyncer extends EventEmitter { const processed = Math.min(i + chunkSize, ids.length); if (processed === ids.length) { - console.log(`✅ Processed products for ${processed}/${ids.length} categories.`); + //console.log(`✅ Processed products for ${processed}/${ids.length} categories.`); } else { - console.log(`⏳ Processed products for ${processed}/${ids.length} categories...`); + //console.log(`⏳ Processed products for ${processed}/${ids.length} categories...`); } } // Sync all collected images at once if (globalImageIds.size > 0) { - console.log(`🖼️ Syncing ${globalImageIds.size} product images...`); + //console.log(`🖼️ Syncing ${globalImageIds.size} product images...`); await pictureSyncer.syncImages(Array.from(globalImageIds), 'products'); } diff --git a/src/syncers/category-syncer.js b/src/syncers/category-syncer.js index 979cd84..e0cdf8d 100644 --- a/src/syncers/category-syncer.js +++ b/src/syncers/category-syncer.js @@ -56,7 +56,7 @@ class CategorySyncer extends EventEmitter { async _doSync() { this.isSyncing = true; const startTime = Date.now(); - console.log('🚀 Starting sync...'); + //console.log('🚀 Starting sync...'); try { await this._syncFromDb(); @@ -65,7 +65,7 @@ class CategorySyncer extends EventEmitter { // Log completion and next sync time const syncInterval = parseInt(process.env.SYNC_INTERVAL_MS) || 60000; const minutes = Math.round(syncInterval / 60000); - console.log(`✅ Sync completed in ${duration}ms. Next sync in ${minutes} minute${minutes !== 1 ? 's' : ''}`); + //console.log(`✅ Sync completed in ${duration}ms. Next sync in ${minutes} minute${minutes !== 1 ? 's' : ''}`); } catch (err) { console.error('❌ Sync failed:', err); } finally { @@ -159,7 +159,7 @@ class CategorySyncer extends EventEmitter { this.lastTreeString = treeString; console.log('📢 Tree updated.'); } else { - console.log('🤷 No changes detected in category tree.'); + //console.log('🤷 No changes detected in category tree.'); } this.emit('synced', { tree, unprunedTree, changed }); diff --git a/src/syncers/picture-syncer.js b/src/syncers/picture-syncer.js index 3e501dd..1f5fe23 100644 --- a/src/syncers/picture-syncer.js +++ b/src/syncers/picture-syncer.js @@ -88,7 +88,7 @@ class PictureSyncer { console.log(`📥 Downloading ${toDownload.length} new images for group '${groupName}'...`); await this._downloadImages(toDownload, groupDir); } else { - console.log(`✅ No new images to download for group '${groupName}'.`); + //console.log(`✅ No new images to download for group '${groupName}'.`); } }