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