refac
This commit is contained in:
57
src/index.js
Normal file
57
src/index.js
Normal file
@@ -0,0 +1,57 @@
|
||||
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);
|
||||
Reference in New Issue
Block a user