Refactor file handling in BuyProduct and Request models; implement file schema for better structure. Update routes to handle file uploads and downloads with improved error handling and logging. Adjust MongoDB connection management across scripts and routes for consistency.

This commit is contained in:
2025-11-05 19:06:11 +03:00
parent 41b5cb6fae
commit 284be82e1e
15 changed files with 630 additions and 184 deletions

View File

@@ -7,7 +7,7 @@ const Request = require('../models/Request');
const BuyProduct = require('../models/BuyProduct');
const Message = require('../models/Message');
const Review = require('../models/Review');
const mongoose = require('mongoose');
const mongoose = require('../../../utils/mongoose');
const { Types } = mongoose;
const PRESET_COMPANY_ID = new Types.ObjectId('68fe2ccda3526c303ca06796');
@@ -116,9 +116,6 @@ const waitForDatabaseConnection = async () => {
const verifyAuth = async () => {
try {
if (!mongoose.connection.db) {
return false;
}
await mongoose.connection.db.admin().command({ listDatabases: 1 });
return true;
} catch (error) {
@@ -139,15 +136,17 @@ const waitForDatabaseConnection = async () => {
}
try {
// Ожидаем подключения (подключение происходит автоматически через server/utils/mongoose.ts)
await new Promise(resolve => setTimeout(resolve, 500));
if (mongoose.connection.readyState === 1) {
const authed = await verifyAuth();
if (authed) {
return;
}
const connection = await connectDB();
if (!connection) {
break;
}
const authed = await verifyAuth();
if (authed) {
return;
}
await mongoose.connection.close().catch(() => {});
} catch (error) {
if (!isAuthFailure(error)) {
throw error;
@@ -218,8 +217,12 @@ const initializeTestUser = async () => {
} catch (error) {
console.error('Error initializing test data:', error.message);
if (error?.code === 13 || /auth/i.test(error?.message || '')) {
if (process.env.DEV === 'true') {
console.error('Auth error detected. Connection managed by server/utils/mongoose.ts');
try {
await connectDB();
} catch (connectError) {
if (process.env.DEV === 'true') {
console.error('Failed to re-connect after auth error:', connectError.message);
}
}
}
}