100 lines
3.7 KiB
JavaScript
100 lines
3.7 KiB
JavaScript
const mongoose = require('mongoose');
|
||
require('dotenv').config();
|
||
|
||
// Импорт моделей
|
||
const User = require('../models/User');
|
||
const Company = require('../models/Company');
|
||
|
||
const recreateTestUser = async () => {
|
||
try {
|
||
console.log('[Migration] Processing test user creation...');
|
||
|
||
// Удалить старого тестового пользователя
|
||
console.log('[Migration] Removing old test user...');
|
||
const oldUser = await User.findOne({ email: 'admin@test-company.ru' });
|
||
if (oldUser) {
|
||
// Удалить связанную компанию
|
||
if (oldUser.companyId) {
|
||
await Company.findByIdAndDelete(oldUser.companyId);
|
||
console.log('[Migration] ✓ Old company removed');
|
||
}
|
||
await User.findByIdAndDelete(oldUser._id);
|
||
console.log('[Migration] ✓ Old user removed');
|
||
} else {
|
||
console.log('[Migration] ℹ️ Old user not found');
|
||
}
|
||
|
||
// Создать новую компанию с правильной кодировкой UTF-8
|
||
console.log('[Migration] Creating test company...');
|
||
const company = await Company.create({
|
||
fullName: 'ООО "Тестовая Компания"',
|
||
inn: '1234567890',
|
||
ogrn: '1234567890123',
|
||
legalForm: 'ООО',
|
||
industry: 'IT',
|
||
companySize: '50-100',
|
||
website: 'https://test-company.ru',
|
||
description: 'Тестовая компания для разработки',
|
||
address: 'г. Москва, ул. Тестовая, д. 1',
|
||
rating: 4.5,
|
||
reviewsCount: 10,
|
||
dealsCount: 25,
|
||
});
|
||
console.log('[Migration] ✓ Company created:', company.fullName);
|
||
|
||
// Создать нового пользователя с правильной кодировкой UTF-8
|
||
console.log('[Migration] Creating test user...');
|
||
const user = await User.create({
|
||
email: 'admin@test-company.ru',
|
||
password: 'SecurePass123!',
|
||
firstName: 'Иван',
|
||
lastName: 'Иванов',
|
||
position: 'Директор',
|
||
phone: '+7 (999) 123-45-67',
|
||
companyId: company._id,
|
||
});
|
||
console.log('[Migration] ✓ User created:', user.firstName, user.lastName);
|
||
|
||
// Обновить существующие mock компании
|
||
console.log('[Migration] Updating existing companies...');
|
||
const updates = [
|
||
{ inn: '7707083894', updates: { companySize: '51-250', partnerGeography: ['moscow', 'russia_all'] } },
|
||
{ inn: '7707083895', updates: { companySize: '500+', partnerGeography: ['moscow', 'russia_all'] } },
|
||
{ inn: '7707083896', updates: { companySize: '11-50', partnerGeography: ['moscow', 'russia_all'] } },
|
||
{ inn: '7707083897', updates: { companySize: '51-250', partnerGeography: ['moscow', 'russia_all'] } },
|
||
{ inn: '7707083898', updates: { companySize: '251-500', partnerGeography: ['moscow', 'russia_all'] } },
|
||
];
|
||
|
||
for (const item of updates) {
|
||
await Company.updateOne({ inn: item.inn }, { $set: item.updates });
|
||
console.log(`[Migration] ✓ Company updated: INN ${item.inn}`);
|
||
}
|
||
|
||
console.log('[Migration] ✅ Test user migration completed!');
|
||
} catch (error) {
|
||
console.error('[Migration] ❌ Error:', error.message);
|
||
throw error;
|
||
}
|
||
};
|
||
|
||
module.exports = { recreateTestUser };
|
||
|
||
// Run directly if called as script
|
||
if (require.main === module) {
|
||
const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/procurement_db';
|
||
|
||
mongoose.connect(mongoUri, {
|
||
useNewUrlParser: true,
|
||
useUnifiedTopology: true,
|
||
}).then(async () => {
|
||
console.log('[Migration] Connected to MongoDB\n');
|
||
await recreateTestUser();
|
||
await mongoose.connection.close();
|
||
process.exit(0);
|
||
}).catch(err => {
|
||
console.error('[Migration] ❌ Error:', err.message);
|
||
process.exit(1);
|
||
});
|
||
}
|
||
|