#!/usr/bin/env node const BygmaImportService = require('./backend/src/services/bygmaImportService'); const databaseService = require('./backend/src/services/databaseService'); async function testMaterialsSync() { console.log('๐Ÿงช Testing Bygma materials sync...\n'); try { // Initialize database service await databaseService.initialize(); console.log('โœ… Database connection established'); // Check current materials count const beforeMaterials = await databaseService.query( 'SELECT COUNT(*) as count FROM materials' ); const beforePrices = await databaseService.query( 'SELECT COUNT(*) as count FROM material_prices WHERE supplier_name = "Bygma A/S"' ); console.log(`๐Ÿ“Š Before import:`); console.log(` - Materials: ${beforeMaterials[0].count}`); console.log(` - Bygma prices: ${beforePrices[0].count}`); // Test import with a small sample const csvPath = '/home/alex/git/tilbudgivern/frontend/firmadata/subvendor/bygma/bygma_esg_data.csv'; // Create a small test file with just a few rows for testing const testCsvPath = '/tmp/bygma_test_sample.csv'; // Read first 10 lines of the original file const fs = require('fs'); const originalContent = fs.readFileSync(csvPath, 'utf-8'); const lines = originalContent.split('\n'); const testContent = lines.slice(0, 11).join('\n'); // Header + 10 data rows fs.writeFileSync(testCsvPath, testContent); console.log(`๐Ÿ“‚ Created test file with 10 sample rows`); // Import the test data const bygmaImporter = new BygmaImportService(databaseService); const result = await bygmaImporter.importESGData(testCsvPath, 'bygma_test_sample.csv'); console.log('\n๐ŸŽ‰ Import completed!'); console.log(`๐Ÿ“Š Import Statistics:`); console.log(` - Processed: ${result.importStats.processed} records`); console.log(` - Imported: ${result.importStats.imported} records`); console.log(` - Failed: ${result.importStats.failed} records`); // Check results in materials system const afterMaterials = await databaseService.query( 'SELECT COUNT(*) as count FROM materials' ); const afterPrices = await databaseService.query( 'SELECT COUNT(*) as count FROM material_prices WHERE supplier_name = "Bygma A/S"' ); console.log(`\n๐Ÿ“Š After import:`); console.log(` - Materials: ${afterMaterials[0].count} (+${afterMaterials[0].count - beforeMaterials[0].count})`); console.log(` - Bygma prices: ${afterPrices[0].count} (+${afterPrices[0].count - beforePrices[0].count})`); // Show some sample materials const sampleMaterials = await databaseService.query(` SELECT m.sku, m.name, m.unit, m.category, m.brand, mp.price, mp.valid_from FROM materials m LEFT JOIN material_prices mp ON m.id = mp.material_id AND mp.is_active = 1 WHERE m.brand = 'Bygma A/S' ORDER BY m.created_at DESC LIMIT 5 `); console.log('\n๐Ÿ“ฆ Sample materials in system:'); sampleMaterials.forEach((material, index) => { console.log(` ${index + 1}. ${material.sku}: ${material.name.substring(0, 50)}...`); console.log(` Category: ${material.category}, Unit: ${material.unit}`); console.log(` Price: ${material.price ? material.price + ' DKK' : 'N/A'} (from ${material.valid_from || 'N/A'})`); }); // Clean up test file fs.unlinkSync(testCsvPath); console.log('\n๐Ÿงน Test file cleaned up'); } catch (error) { console.error('โŒ Test failed:', error.message); console.error('Stack:', error.stack); } console.log('\n๐Ÿ Test completed'); } testMaterialsSync();