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

@@ -2,6 +2,8 @@ const express = require('express');
const router = express.Router();
const { verifyToken } = require('../middleware/auth');
const Message = require('../models/Message');
const mongoose = require('../../../utils/mongoose');
const { ObjectId } = mongoose.Types;
// Функция для логирования с проверкой DEV переменной
const log = (message, data = '') => {
@@ -18,7 +20,6 @@ const log = (message, data = '') => {
router.get('/threads', verifyToken, async (req, res) => {
try {
const companyId = req.companyId;
const { ObjectId } = require('mongoose').Types;
log('[Messages] Fetching threads for companyId:', companyId, 'type:', typeof companyId);
@@ -146,7 +147,6 @@ router.post('/:threadId', verifyToken, async (req, res) => {
// Найти recipientCompanyId по ObjectId если нужно
let recipientObjectId = recipientCompanyId;
const { ObjectId } = require('mongoose').Types;
try {
if (typeof recipientCompanyId === 'string' && ObjectId.isValid(recipientCompanyId)) {
recipientObjectId = new ObjectId(recipientCompanyId);
@@ -210,7 +210,6 @@ router.post('/admin/migrate-fix-recipients', async (req, res) => {
// If recipientCompanyId is not set or wrong - fix it
if (!message.recipientCompanyId || message.recipientCompanyId.toString() !== expectedRecipient) {
const { ObjectId } = require('mongoose').Types;
let recipientObjectId = expectedRecipient;
try {
if (typeof expectedRecipient === 'string' && ObjectId.isValid(expectedRecipient)) {