import categorySyncer from './syncers/category-syncer.js'; import pictureSyncer from './syncers/picture-syncer.js'; import categoryProductsSyncer from './syncers/category-products-syncer.js'; import { startServer } from './server/server.js'; 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...'); } // Extract all kBild IDs and kKategorie IDs from unpruned tree const imageIds = []; const categoryIds = []; const traverse = (nodes) => { for (const node of nodes) { if (node.kBild) { imageIds.push(node.kBild); } if (node.kKategorie) { categoryIds.push(node.kKategorie); } if (node.children && node.children.length > 0) { traverse(node.children); } } }; traverse(unprunedTree); console.log(`šŸ” Found ${imageIds.length} images and ${categoryIds.length} categories.`); await pictureSyncer.syncImages(imageIds, 'categories'); await categoryProductsSyncer.syncProducts(categoryIds); }); categoryProductsSyncer.on('categoryUpdated', ({ id, products }) => { console.log(`šŸ“ Category ${id} updated. Products count: ${products.length}`); }); // Trigger immediate sync categorySyncer.triggerSync(); // Schedule periodic sync const syncInterval = parseInt(process.env.SYNC_INTERVAL_MS) || 60000; setInterval(() => { categorySyncer.triggerSync(); }, syncInterval); // Handle graceful shutdown process.on('SIGINT', () => { console.log('\nšŸ‘‹ Bye!'); process.exit(0); }); // Start Express server startServer(categorySyncer, categoryProductsSyncer);