feat: Enhance product management by adding WebSocket support for real-time updates, implement product loading in categories, and improve caching for category products.

This commit is contained in:
sebseb7
2025-11-23 11:40:42 +01:00
parent 49246169db
commit 71910f84a2
4 changed files with 361 additions and 7 deletions

View File

@@ -1,21 +1,26 @@
import categorySyncer from './category-syncer.js';
import pictureSyncer from './picture-syncer.js';
import categoryProductsSyncer from './category-products-syncer.js';
import { startServer } from './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...');
console.log('🎉 Event received: Sync finished (no changes). Checking images and products...');
}
// Extract all kBild IDs from unpruned tree (includes all categories)
// 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);
}
@@ -23,8 +28,14 @@ categorySyncer.on('synced', async ({ tree, unprunedTree, changed }) => {
};
traverse(unprunedTree);
console.log(`🔍 Found ${imageIds.length} images in category tree.`);
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
@@ -43,4 +54,4 @@ process.on('SIGINT', () => {
});
// Start Express server
startServer(categorySyncer);
startServer(categorySyncer, categoryProductsSyncer);