Add full project files
This commit is contained in:
44
services/ProjectCron.js
Normal file
44
services/ProjectCron.js
Normal file
@@ -0,0 +1,44 @@
|
||||
const cron = require('node-cron')
|
||||
const ProjectModel = require('../models/ProjectModel')
|
||||
const NotificationModel = require('../models/NotificationModel')
|
||||
|
||||
// تابع برای بهروزرسانی پروژههای منقضی شده
|
||||
const updateExpiredProjects = async () => {
|
||||
const now = new Date()
|
||||
const oneMonthAgo = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000)
|
||||
|
||||
try {
|
||||
// جستجوی پروژههای منقضی شده که هنوز بهروزرسانی نشدهاند
|
||||
const expiredProjects = await ProjectModel.find({
|
||||
isExpired: false, // فقط پروژههایی که هنوز منقضی نشدهاند
|
||||
acceptedAt: { $lte: oneMonthAgo } // بر اساس تاریخ انقضا
|
||||
})
|
||||
|
||||
for (const project of expiredProjects) {
|
||||
project.isExpired = true // تنظیم وضعیت منقضی شده
|
||||
await project.save()
|
||||
|
||||
// ایجاد و ذخیره نوتیفیکیشن
|
||||
const notification = new NotificationModel({
|
||||
user_id: project.creator_id,
|
||||
project_post_id: project._id,
|
||||
type: 'project',
|
||||
title: 'انقضای پروژه',
|
||||
description: `پروژه شما با عنوان: ${project.title} منقضی شد.`
|
||||
})
|
||||
await notification.save()
|
||||
}
|
||||
|
||||
console.log(`Updated ${expiredProjects.length} expired projects successfully`)
|
||||
} catch (error) {
|
||||
console.error('Error updating expired projects:', error)
|
||||
}
|
||||
}
|
||||
|
||||
// تنظیم کرون جاب برای اجرای هر ساعت
|
||||
cron.schedule('0 * * * *', () => {
|
||||
console.log('Running cron job to update expired projects')
|
||||
updateExpiredProjects()
|
||||
})
|
||||
|
||||
module.exports = updateExpiredProjects
|
||||
Reference in New Issue
Block a user