миграция
This commit is contained in:
93
server/routers/procurement/scripts/validate-companies.js
Normal file
93
server/routers/procurement/scripts/validate-companies.js
Normal file
@@ -0,0 +1,93 @@
|
||||
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();
|
||||
Reference in New Issue
Block a user