feature/worker #111
@@ -133,18 +133,33 @@ const initializeTestUser = async () => {
|
||||
];
|
||||
|
||||
for (const mockCompanyData of mockCompanies) {
|
||||
const existingCompany = await Company.findOne({ inn: mockCompanyData.inn });
|
||||
if (!existingCompany) {
|
||||
await Company.create(mockCompanyData);
|
||||
log(`✅ Mock company created: ${mockCompanyData.fullName}`);
|
||||
try {
|
||||
const existingCompany = await Company.findOne({ inn: mockCompanyData.inn });
|
||||
if (!existingCompany) {
|
||||
await Company.create(mockCompanyData);
|
||||
log(`✅ Mock company created: ${mockCompanyData.fullName}`);
|
||||
}
|
||||
} catch (err) {
|
||||
// Ignore errors for mock company creation - это может быть ошибка аутентификации
|
||||
log(`ℹ️ Mock company init failed: ${mockCompanyData.fullName}`);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error initializing test data:', error.message);
|
||||
// Ошибка аутентификации или другие ошибки БД - продолжаем работу
|
||||
if (error.message && error.message.includes('authentication')) {
|
||||
log('ℹ️ Database authentication required - test data initialization deferred');
|
||||
} else {
|
||||
console.error('Error initializing test data:', error.message);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
initializeTestUser();
|
||||
// Пытаемся инициализировать с задержкой (даёт время на подключение)
|
||||
setTimeout(() => {
|
||||
initializeTestUser().catch(err => {
|
||||
log(`⚠️ Deferred test data initialization failed: ${err.message}`);
|
||||
});
|
||||
}, 2000);
|
||||
|
||||
// Регистрация
|
||||
router.post('/register', async (req, res) => {
|
||||
|
||||
@@ -106,7 +106,7 @@ module.exports = {
|
||||
|
||||
// Run directly if called as script
|
||||
if (require.main === module) {
|
||||
const mongoUrl = process.env.MONGODB_URI || 'mongodb://admin:password@localhost:27017/procurement_db?authSource=admin';
|
||||
const mongoUrl = process.env.MONGODB_URI || 'mongodb://localhost:27017/procurement_db';
|
||||
|
||||
mongoose.connect(mongoUrl, {
|
||||
useNewUrlParser: true,
|
||||
|
||||
@@ -67,14 +67,23 @@ async function runMigrations() {
|
||||
|
||||
console.log(`[${migration.name}] ✅ Completed and recorded\n`);
|
||||
} catch (error) {
|
||||
console.error(`[${migration.name}] ❌ Error: ${error.message}\n`);
|
||||
// Обработка ошибок аутентификации и других ошибок
|
||||
if (error.message && error.message.includes('authentication')) {
|
||||
console.warn(`[${migration.name}] ⚠️ Skipped (authentication required): ${error.message}\n`);
|
||||
} else {
|
||||
console.error(`[${migration.name}] ❌ Error: ${error.message}\n`);
|
||||
|
||||
// Record failed migration
|
||||
await Migration.create({
|
||||
name: migration.name,
|
||||
status: 'failed',
|
||||
message: error.message
|
||||
});
|
||||
// Record failed migration
|
||||
try {
|
||||
await Migration.create({
|
||||
name: migration.name,
|
||||
status: 'failed',
|
||||
message: error.message
|
||||
});
|
||||
} catch (recordErr) {
|
||||
// Ignore if we can't record the failure
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,9 +92,15 @@ async function runMigrations() {
|
||||
console.log('='.repeat(60) + '\n');
|
||||
|
||||
} catch (error) {
|
||||
console.error('\n❌ Fatal migration error:', error.message);
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
// Обработка ошибок подключения
|
||||
if (error.message && error.message.includes('authentication')) {
|
||||
console.warn('\n⚠️ Database authentication required - migrations skipped');
|
||||
console.warn('This is normal if the database is shared with other projects.\n');
|
||||
} else {
|
||||
console.error('\n❌ Fatal migration error:', error.message);
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
}
|
||||
} finally {
|
||||
if (mongooseConnected) {
|
||||
await mongoose.connection.close();
|
||||
|
||||
Reference in New Issue
Block a user