|
| 1 | +import cron from 'node-cron'; |
| 2 | +import Banner from '../models/bannerModel.js'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Background job that runs daily to check banner activation/deactivation dates |
| 6 | + * - Checks StartDate to automatically activate banners |
| 7 | + * - Checks EndDate to automatically deactivate banners |
| 8 | + * - Runs at 00:05 daily to ensure timely activation/deactivation |
| 9 | + */ |
| 10 | +export function startBannerActivationJob() { |
| 11 | + // Run daily at 00:05 |
| 12 | + cron.schedule('5 0 * * *', async () => { |
| 13 | + try { |
| 14 | + console.log('Running banner activation/deactivation check job...'); |
| 15 | + |
| 16 | + const today = new Date(); |
| 17 | + today.setUTCHours(0, 0, 0, 0); // Set to start of day UTC for comparison |
| 18 | + |
| 19 | + let activatedCount = 0; |
| 20 | + let deactivatedCount = 0; |
| 21 | + const errors: string[] = []; |
| 22 | + |
| 23 | + // Find all banners that have scheduling dates |
| 24 | + const banners = await Banner.find({ |
| 25 | + $or: [ |
| 26 | + { StartDate: { $exists: true, $ne: null } }, |
| 27 | + { EndDate: { $exists: true, $ne: null } } |
| 28 | + ] |
| 29 | + }); |
| 30 | + |
| 31 | + console.log(`Found ${banners.length} banner(s) with scheduled dates to check`); |
| 32 | + |
| 33 | + for (const banner of banners) { |
| 34 | + try { |
| 35 | + const updateData: any = { |
| 36 | + DocumentModifiedDate: new Date() |
| 37 | + }; |
| 38 | + let needsUpdate = false; |
| 39 | + |
| 40 | + // Check if banner should be activated today |
| 41 | + if (banner.StartDate) { |
| 42 | + const startDate = new Date(banner.StartDate); |
| 43 | + startDate.setUTCHours(0, 0, 0, 0); // Use UTC for consistent comparison |
| 44 | + |
| 45 | + // If today equals or is after the start date and banner is not active |
| 46 | + if (startDate.getTime() === today.getTime() && !banner.IsActive) { |
| 47 | + updateData.IsActive = true; |
| 48 | + needsUpdate = true; |
| 49 | + activatedCount++; |
| 50 | + console.log(`Banner activated: ${banner.Title} (ID: ${banner._id})`); |
| 51 | + console.log(` - Start date: ${banner.StartDate.toISOString()}`); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // Check if banner should be deactivated today |
| 56 | + if (banner.EndDate) { |
| 57 | + const endDate = new Date(banner.EndDate); |
| 58 | + endDate.setUTCHours(0, 0, 0, 0); // Use UTC for consistent comparison |
| 59 | + endDate.setDate(endDate.getDate() + 1); // Add 1 day to end date |
| 60 | + |
| 61 | + // If today equals or is after the end date and banner is active |
| 62 | + if (endDate.getTime() === today.getTime() && banner.IsActive) { |
| 63 | + updateData.IsActive = false; |
| 64 | + needsUpdate = true; |
| 65 | + deactivatedCount++; |
| 66 | + console.log(`Banner deactivated: ${banner.Title} (ID: ${banner._id})`); |
| 67 | + console.log(` - End date: ${banner.EndDate.toISOString()}`); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // Update the banner if needed |
| 72 | + if (needsUpdate) { |
| 73 | + await Banner.findByIdAndUpdate( |
| 74 | + banner._id, |
| 75 | + { $set: updateData }, |
| 76 | + { runValidators: true } |
| 77 | + ); |
| 78 | + } |
| 79 | + } catch (error) { |
| 80 | + const errorMsg = `Error updating ${banner.Title}: ${error instanceof Error ? error.message : 'Unknown error'}`; |
| 81 | + console.error(errorMsg); |
| 82 | + errors.push(errorMsg); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + console.log(`Banner activation check completed: |
| 87 | + - Banners checked: ${banners.length} |
| 88 | + - Banners activated: ${activatedCount} |
| 89 | + - Banners deactivated: ${deactivatedCount} |
| 90 | + - Errors: ${errors.length} |
| 91 | + `); |
| 92 | + |
| 93 | + if (errors.length > 0) { |
| 94 | + console.error('Errors during banner activation check:', errors); |
| 95 | + } |
| 96 | + } catch (error) { |
| 97 | + console.error('Fatal error in banner activation job:', error); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + console.log('Banner activation job scheduled to run daily at 00:05'); |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * One-time check for banners to activate/deactivate (useful for testing) |
| 106 | + * Returns statistics about the check |
| 107 | + */ |
| 108 | +export async function runBannerActivationCheckNow() { |
| 109 | + try { |
| 110 | + console.log('Running immediate banner activation check...'); |
| 111 | + |
| 112 | + const today = new Date(); |
| 113 | + today.setUTCHours(0, 0, 0, 0); // Use UTC for consistent comparison |
| 114 | + |
| 115 | + const stats = { |
| 116 | + total: 0, |
| 117 | + needsActivation: 0, |
| 118 | + needsDeactivation: 0, |
| 119 | + alreadyActive: 0, |
| 120 | + alreadyInactive: 0 |
| 121 | + }; |
| 122 | + |
| 123 | + const banners = await Banner.find({ |
| 124 | + $or: [ |
| 125 | + { StartDate: { $exists: true, $ne: null } }, |
| 126 | + { EndDate: { $exists: true, $ne: null } } |
| 127 | + ] |
| 128 | + }); |
| 129 | + |
| 130 | + stats.total = banners.length; |
| 131 | + |
| 132 | + for (const banner of banners) { |
| 133 | + // Check activation date |
| 134 | + if (banner.StartDate) { |
| 135 | + const startDate = new Date(banner.StartDate); |
| 136 | + startDate.setUTCHours(0, 0, 0, 0); |
| 137 | + |
| 138 | + if (startDate.getTime() === today.getTime()) { |
| 139 | + if (!banner.IsActive) { |
| 140 | + stats.needsActivation++; |
| 141 | + } else { |
| 142 | + stats.alreadyActive++; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + // Check deactivation date |
| 148 | + if (banner.EndDate) { |
| 149 | + const endDate = new Date(banner.EndDate); |
| 150 | + endDate.setUTCHours(0, 0, 0, 0); |
| 151 | + endDate.setDate(endDate.getDate() + 1); // Add 1 day |
| 152 | + |
| 153 | + if (endDate.getTime() === today.getTime()) { |
| 154 | + if (banner.IsActive) { |
| 155 | + stats.needsDeactivation++; |
| 156 | + } else { |
| 157 | + stats.alreadyInactive++; |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + console.log('Banner activation check stats:', stats); |
| 164 | + return stats; |
| 165 | + } catch (error) { |
| 166 | + console.error('Error in banner activation check:', error); |
| 167 | + throw error; |
| 168 | + } |
| 169 | +} |
0 commit comments