замечания 3
This commit is contained in:
@@ -1,16 +1,49 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { verifyToken } = require('../middleware/auth');
|
||||
const BuyProduct = require('../models/BuyProduct');
|
||||
const Request = require('../models/Request');
|
||||
|
||||
// Получить агрегированные данные для главной страницы
|
||||
router.get('/aggregates', verifyToken, async (req, res) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
const User = require('../models/User');
|
||||
const user = await User.findById(userId);
|
||||
|
||||
if (!user || !user.companyId) {
|
||||
return res.json({
|
||||
docsCount: 0,
|
||||
acceptsCount: 0,
|
||||
requestsCount: 0
|
||||
});
|
||||
}
|
||||
|
||||
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 }
|
||||
]
|
||||
})
|
||||
]);
|
||||
|
||||
res.json({
|
||||
docsCount: 0,
|
||||
acceptsCount: 0,
|
||||
requestsCount: 0
|
||||
docsCount,
|
||||
acceptsCount,
|
||||
requestsCount
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error getting aggregates:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
@@ -18,17 +51,42 @@ router.get('/aggregates', verifyToken, async (req, res) => {
|
||||
// Получить статистику компании
|
||||
router.get('/stats', verifyToken, async (req, res) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
const User = require('../models/User');
|
||||
const Company = require('../models/Company');
|
||||
const user = await User.findById(userId);
|
||||
|
||||
if (!user || !user.companyId) {
|
||||
return res.json({
|
||||
profileViews: 0,
|
||||
profileViewsChange: 0,
|
||||
sentRequests: 0,
|
||||
sentRequestsChange: 0,
|
||||
receivedRequests: 0,
|
||||
receivedRequestsChange: 0,
|
||||
newMessages: 0,
|
||||
rating: 0
|
||||
});
|
||||
}
|
||||
|
||||
const companyId = user.companyId.toString();
|
||||
const company = await Company.findById(user.companyId);
|
||||
|
||||
const sentRequests = await Request.countDocuments({ senderCompanyId: companyId });
|
||||
const receivedRequests = await Request.countDocuments({ recipientCompanyId: companyId });
|
||||
|
||||
res.json({
|
||||
profileViews: 12,
|
||||
profileViewsChange: 5,
|
||||
sentRequests: 3,
|
||||
sentRequestsChange: 1,
|
||||
receivedRequests: 7,
|
||||
receivedRequestsChange: 2,
|
||||
newMessages: 4,
|
||||
rating: 4.5
|
||||
profileViews: company?.metrics?.profileViews || 0,
|
||||
profileViewsChange: 0,
|
||||
sentRequests,
|
||||
sentRequestsChange: 0,
|
||||
receivedRequests,
|
||||
receivedRequestsChange: 0,
|
||||
newMessages: 0,
|
||||
rating: company?.rating || 0
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error getting stats:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
@@ -36,11 +94,40 @@ router.get('/stats', verifyToken, async (req, res) => {
|
||||
// Получить рекомендации партнеров (AI)
|
||||
router.get('/recommendations', verifyToken, async (req, res) => {
|
||||
try {
|
||||
const userId = req.userId;
|
||||
const User = require('../models/User');
|
||||
const Company = require('../models/Company');
|
||||
const user = await User.findById(userId);
|
||||
|
||||
if (!user || !user.companyId) {
|
||||
return res.json({
|
||||
recommendations: [],
|
||||
message: 'No recommendations available'
|
||||
});
|
||||
}
|
||||
|
||||
// Получить компании кроме текущей
|
||||
const companies = await Company.find({
|
||||
_id: { $ne: user.companyId }
|
||||
})
|
||||
.sort({ rating: -1 })
|
||||
.limit(5);
|
||||
|
||||
const recommendations = companies.map(company => ({
|
||||
id: company._id.toString(),
|
||||
name: company.fullName || company.shortName,
|
||||
industry: company.industry,
|
||||
logo: company.logo,
|
||||
matchScore: company.rating ? Math.min(100, Math.round(company.rating * 20)) : 50,
|
||||
reason: 'Matches your industry'
|
||||
}));
|
||||
|
||||
res.json({
|
||||
recommendations: [],
|
||||
message: 'No recommendations available yet'
|
||||
recommendations,
|
||||
message: recommendations.length > 0 ? 'Recommendations available' : 'No recommendations available'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error getting recommendations:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user