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

@@ -21,21 +21,23 @@ router.get('/aggregates', verifyToken, async (req, res) => {
const companyId = user.companyId.toString();
const [docsCount, acceptsCount, requestsCount] = await Promise.all([
BuyProduct.countDocuments({ companyId }),
Request.countDocuments({
$or: [
{ senderCompanyId: companyId, status: 'accepted' },
{ recipientCompanyId: companyId, status: 'accepted' }
]
}),
Request.countDocuments({
$or: [
{ senderCompanyId: companyId },
{ recipientCompanyId: companyId }
]
})
]);
// Получить все BuyProduct для подсчета файлов и акцептов
const buyProducts = await BuyProduct.find({ companyId });
// Подсчет документов - сумма всех файлов во всех BuyProduct
const docsCount = buyProducts.reduce((total, product) => {
return total + (product.files ? product.files.length : 0);
}, 0);
// Подсчет акцептов - сумма всех acceptedBy во всех BuyProduct
const acceptsCount = buyProducts.reduce((total, product) => {
return total + (product.acceptedBy ? product.acceptedBy.length : 0);
}, 0);
// Подсчет исходящих запросов (только отправленные этой компанией)
const requestsCount = await Request.countDocuments({
senderCompanyId: companyId
});
res.json({
docsCount,