update
This commit is contained in:
122
server/routers/procurement/scripts/seed-activities.js
Normal file
122
server/routers/procurement/scripts/seed-activities.js
Normal file
@@ -0,0 +1,122 @@
|
||||
const mongoose = require('mongoose');
|
||||
require('dotenv').config();
|
||||
|
||||
// Подключение моделей - прямые пути без path.join и __dirname
|
||||
const Activity = require('../models/Activity');
|
||||
const User = require('../models/User');
|
||||
const Company = require('../models/Company');
|
||||
|
||||
const MONGODB_URI = process.env.MONGODB_URI || 'mongodb://localhost:27017/procurement-platform';
|
||||
|
||||
const activityTemplates = [
|
||||
{
|
||||
type: 'request_received',
|
||||
title: 'Получен новый запрос',
|
||||
description: 'Компания отправила вам запрос на поставку товаров',
|
||||
},
|
||||
{
|
||||
type: 'request_sent',
|
||||
title: 'Запрос отправлен',
|
||||
description: 'Ваш запрос был отправлен компании',
|
||||
},
|
||||
{
|
||||
type: 'request_response',
|
||||
title: 'Получен ответ на запрос',
|
||||
description: 'Компания ответила на ваш запрос',
|
||||
},
|
||||
{
|
||||
type: 'product_accepted',
|
||||
title: 'Товар акцептован',
|
||||
description: 'Ваш товар был акцептован компанией',
|
||||
},
|
||||
{
|
||||
type: 'message_received',
|
||||
title: 'Новое сообщение',
|
||||
description: 'Вы получили новое сообщение от компании',
|
||||
},
|
||||
{
|
||||
type: 'review_received',
|
||||
title: 'Новый отзыв',
|
||||
description: 'Компания оставила отзыв о сотрудничестве',
|
||||
},
|
||||
{
|
||||
type: 'profile_updated',
|
||||
title: 'Профиль обновлен',
|
||||
description: 'Информация о вашей компании была обновлена',
|
||||
},
|
||||
{
|
||||
type: 'buy_product_added',
|
||||
title: 'Добавлен товар для закупки',
|
||||
description: 'В раздел "Я покупаю" добавлен новый товар',
|
||||
},
|
||||
];
|
||||
|
||||
async function seedActivities() {
|
||||
try {
|
||||
console.log('🌱 Connecting to MongoDB...');
|
||||
await mongoose.connect(MONGODB_URI);
|
||||
console.log('✅ Connected to MongoDB');
|
||||
|
||||
// Найти тестового пользователя
|
||||
const testUser = await User.findOne({ email: 'admin@test-company.ru' });
|
||||
if (!testUser) {
|
||||
console.log('❌ Test user not found. Please run recreate-test-user.js first.');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const company = await Company.findById(testUser.companyId);
|
||||
if (!company) {
|
||||
console.log('❌ Company not found');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Найти другие компании для связанных активностей
|
||||
const otherCompanies = await Company.find({
|
||||
_id: { $ne: company._id }
|
||||
}).limit(3);
|
||||
|
||||
console.log('🗑️ Clearing existing activities...');
|
||||
await Activity.deleteMany({ companyId: company._id.toString() });
|
||||
|
||||
console.log('➕ Creating activities...');
|
||||
const activities = [];
|
||||
|
||||
for (let i = 0; i < 8; i++) {
|
||||
const template = activityTemplates[i % activityTemplates.length];
|
||||
const relatedCompany = otherCompanies[i % otherCompanies.length];
|
||||
|
||||
const activity = {
|
||||
companyId: company._id.toString(),
|
||||
userId: testUser._id.toString(),
|
||||
type: template.type,
|
||||
title: template.title,
|
||||
description: template.description,
|
||||
relatedCompanyId: relatedCompany?._id.toString(),
|
||||
relatedCompanyName: relatedCompany?.shortName || relatedCompany?.fullName,
|
||||
read: i >= 5, // Первые 5 непрочитанные
|
||||
createdAt: new Date(Date.now() - i * 3600000), // Каждый час назад
|
||||
};
|
||||
|
||||
activities.push(activity);
|
||||
}
|
||||
|
||||
await Activity.insertMany(activities);
|
||||
|
||||
console.log(`✅ Created ${activities.length} activities`);
|
||||
console.log('✨ Activities seeded successfully!');
|
||||
|
||||
await mongoose.connection.close();
|
||||
console.log('👋 Database connection closed');
|
||||
} catch (error) {
|
||||
console.error('❌ Error seeding activities:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Запуск
|
||||
if (require.main === module) {
|
||||
seedActivities();
|
||||
}
|
||||
|
||||
module.exports = { seedActivities };
|
||||
|
||||
114
server/routers/procurement/scripts/seed-requests.js
Normal file
114
server/routers/procurement/scripts/seed-requests.js
Normal file
@@ -0,0 +1,114 @@
|
||||
const mongoose = require('mongoose');
|
||||
const Request = require('../models/Request');
|
||||
const Company = require('../models/Company');
|
||||
const User = require('../models/User');
|
||||
|
||||
const mongoUri = process.env.MONGODB_URI || 'mongodb://admin:password@localhost:27017/procurement_db?authSource=admin';
|
||||
|
||||
async function seedRequests() {
|
||||
try {
|
||||
await mongoose.connect(mongoUri);
|
||||
console.log('✅ Connected to MongoDB');
|
||||
|
||||
// Получаем все компании
|
||||
const companies = await Company.find().limit(10).exec();
|
||||
if (companies.length < 2) {
|
||||
console.error('❌ Need at least 2 companies in database');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Получаем тестового пользователя
|
||||
const testUser = await User.findOne({ email: 'admin@test-company.ru' }).exec();
|
||||
if (!testUser) {
|
||||
console.error('❌ Test user not found');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const testCompanyId = testUser.companyId.toString();
|
||||
console.log('📋 Test company ID:', testCompanyId);
|
||||
console.log('📋 Found', companies.length, 'companies');
|
||||
|
||||
// Удаляем старые запросы
|
||||
await Request.deleteMany({});
|
||||
console.log('🗑️ Cleared old requests');
|
||||
|
||||
const requests = [];
|
||||
const now = new Date();
|
||||
|
||||
// Создаем отправленные запросы (от тестовой компании)
|
||||
for (let i = 0; i < 5; i++) {
|
||||
const recipientCompany = companies[i % companies.length];
|
||||
if (recipientCompany._id.toString() === testCompanyId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const createdAt = new Date(now.getTime() - i * 24 * 60 * 60 * 1000); // За последние 5 дней
|
||||
|
||||
requests.push({
|
||||
senderCompanyId: testCompanyId,
|
||||
recipientCompanyId: recipientCompany._id.toString(),
|
||||
subject: `Запрос на поставку ${i + 1}`,
|
||||
text: `Здравствуйте! Интересует поставка товаров/услуг. Запрос ${i + 1}. Прошу предоставить коммерческое предложение.`,
|
||||
files: [],
|
||||
responseFiles: [],
|
||||
status: i % 3 === 0 ? 'accepted' : i % 3 === 1 ? 'rejected' : 'pending',
|
||||
response: i % 3 === 0
|
||||
? 'Благодарим за запрос! Готовы предоставить услуги. Отправили КП на почту.'
|
||||
: i % 3 === 1
|
||||
? 'К сожалению, в данный момент не можем предоставить эти услуги.'
|
||||
: null,
|
||||
respondedAt: i % 3 !== 2 ? new Date(createdAt.getTime() + 2 * 60 * 60 * 1000) : null,
|
||||
createdAt,
|
||||
updatedAt: i % 3 !== 2 ? new Date(createdAt.getTime() + 2 * 60 * 60 * 1000) : createdAt,
|
||||
});
|
||||
}
|
||||
|
||||
// Создаем полученные запросы (к тестовой компании)
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const senderCompany = companies[(i + 2) % companies.length];
|
||||
if (senderCompany._id.toString() === testCompanyId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const createdAt = new Date(now.getTime() - (i + 1) * 12 * 60 * 60 * 1000); // За последние 1.5 дня
|
||||
|
||||
requests.push({
|
||||
senderCompanyId: senderCompany._id.toString(),
|
||||
recipientCompanyId: testCompanyId,
|
||||
subject: `Предложение о сотрудничестве ${i + 1}`,
|
||||
text: `Добрый день! Предлагаем сотрудничество. Запрос ${i + 1}. Заинтересованы в вашей продукции.`,
|
||||
files: [],
|
||||
responseFiles: [],
|
||||
status: 'pending',
|
||||
response: null,
|
||||
respondedAt: null,
|
||||
createdAt,
|
||||
updatedAt: createdAt,
|
||||
});
|
||||
}
|
||||
|
||||
// Сохраняем все запросы
|
||||
const savedRequests = await Request.insertMany(requests);
|
||||
console.log('✅ Created', savedRequests.length, 'test requests');
|
||||
|
||||
// Статистика
|
||||
const sentCount = await Request.countDocuments({ senderCompanyId: testCompanyId });
|
||||
const receivedCount = await Request.countDocuments({ recipientCompanyId: testCompanyId });
|
||||
const withResponses = await Request.countDocuments({ senderCompanyId: testCompanyId, response: { $ne: null } });
|
||||
|
||||
console.log('📊 Statistics:');
|
||||
console.log(' - Sent requests:', sentCount);
|
||||
console.log(' - Received requests:', receivedCount);
|
||||
console.log(' - With responses:', withResponses);
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Error:', error);
|
||||
process.exit(1);
|
||||
} finally {
|
||||
await mongoose.connection.close();
|
||||
console.log('👋 Disconnected from MongoDB');
|
||||
}
|
||||
}
|
||||
|
||||
seedRequests();
|
||||
|
||||
Reference in New Issue
Block a user