Files
multy-stub/server/routers/procurement/scripts/validate-companies.js
2025-10-27 19:37:21 +03:00

94 lines
3.0 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const mongoose = require('mongoose');
const Company = require('../models/Company');
require('dotenv').config({ path: '../../.env' });
const mongoUrl = process.env.MONGODB_URI || 'mongodb://admin:password@localhost:27017/procurement_db?authSource=admin';
const industryMap = {
'it': 'IT',
'finance': 'Финансы',
'manufacturing': 'Производство',
'construction': 'Строительство',
'retail': 'Розничная торговля',
'wholesale': 'Оптовая торговля',
'logistics': 'Логистика',
'healthcare': 'Здравоохранение',
'education': 'Образование',
'consulting': 'Консалтинг',
'marketing': 'Маркетинг',
'realestate': 'Недвижимость',
'food': 'Пищевая промышленность',
'agriculture': 'Сельское хозяйство',
'energy': 'Энергетика',
'telecom': 'Телекоммуникации',
'media': 'Медиа',
'tourism': 'Туризм',
'legal': 'Юридические услуги',
'other': 'Другое'
};
async function validateCompanies() {
try {
console.log('[Validation] Connecting to MongoDB...');
await mongoose.connect(mongoUrl, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 5000,
connectTimeoutMS: 5000,
});
console.log('[Validation] Connected to MongoDB\n');
const allCompanies = await Company.find().exec();
console.log(`Found ${allCompanies.length} total companies\n`);
console.log('=== COMPANY DATA VALIDATION REPORT ===\n');
let issuesFound = 0;
let validCompanies = 0;
for (const company of allCompanies) {
console.log(`📋 Company: ${company.fullName}`);
console.log(` ID: ${company._id}`);
console.log(` Industry: ${company.industry} (type: ${typeof company.industry})`);
console.log(` Company Size: ${company.companySize}`);
let hasIssues = false;
if (company.industry) {
if (Array.isArray(company.industry)) {
console.log(` ⚠️ WARNING: industry is array!`);
issuesFound++;
hasIssues = true;
} else if (!Object.values(industryMap).includes(company.industry)) {
console.log(` ⚠️ industry value unknown: "${company.industry}"`);
issuesFound++;
hasIssues = true;
} else {
console.log(` ✅ industry OK`);
}
}
if (!hasIssues) validCompanies++;
console.log('');
}
console.log('\n=== SUMMARY ===');
console.log(`Total: ${allCompanies.length}`);
console.log(`Valid: ${validCompanies}`);
console.log(`Issues: ${issuesFound}`);
if (issuesFound === 0) {
console.log('\n✅ All data OK. No migration needed.');
} else {
console.log('\n⚠ Migration recommended.');
}
await mongoose.connection.close();
} catch (err) {
console.error('❌ Error:', err.message);
process.exit(1);
}
}
validateCompanies();