const User = require('../models/User') const CartItem = require('../models/CartItem') const CronJob = require('cron').CronJob const {sms} = require('./../SMSModule') const minute = 1000 * 60 const hour = minute * 60 const day = hour * 24 module.exports = { removeUser: () => { // check users for activation setInterval(() => { User.find({registration_done: false , scope : 'user'}) .then(users => { if (users.length) { /////////////////////// users.forEach((item, index) => { // item time and timeout const itemTime = Date.parse(item.created_at) const timeout = itemTime + day //////////////////////////// if (Date.now() >= timeout) { item.remove() } if (index === users.length - 1) { // console.log('Unconfirmed users deleted.') } }) } }) .catch(err => { console.log(err) }) }, hour) }, //remove cart item every hour removeCartItem: () => { setInterval(() => { CartItem.remove({}) }, hour) }, //birth date job for add point and send sms birthdateJob: () => { return new CronJob('0 12 * * *', async () => { try { const now = new Date(); // const birthPlus = new Date('2021-10-21T00:00:00.000+00:00'); // birthPlus.setDate(birthPlus.getDate() + 7); const users = await User.find({scope: ['user'], registration_done: true}) for (const user of users) { const birthPlus = new Date(user?.birth_date); const tempBirth = new Date(now.getFullYear() , birthPlus.getMonth() , birthPlus.getDate()) const tempBirthForDisableMessage = new Date(now.getFullYear() , birthPlus.getMonth() , birthPlus.getDate()) tempBirthForDisableMessage?.setDate(tempBirth?.getDate() + 7); if (user.birth_date && tempBirthForDisableMessage <= now){ user.showBirthDate = false await user.save() } if (user.birth_date && tempBirth.toDateString() === now.toDateString() && user.birthDateChange < now.getFullYear()) { //// add point user.score += pointBirthDate user.showBirthDate = true user.birthDateChange = now?.getFullYear()?.toString() user.scoreLogs = [ { score : pointBirthDate, forWhat : 'birthDate', action : 'increase' } ] await user.save() //// send sms const smsTemplate = `${user.first_name} عزیز تولدت مبارک هدیه ما به شما ${pointBirthDate} امتیاز برای سفارش در اپلیکیشن برگ من لغو11` const smsResult = await sms(user.mobile_number, smsTemplate) if (!smsResult || !smsResult.data.IsSuccessful) { console.log('birthdate sms', smsResult) } } } }catch (error) { console.log('birthdate job' , error.message) return '' } }); }, //Marriage date job for add point and send sms marriageDateJob: () => { return new CronJob('0 12 * * *', async () => { try { const now = new Date(); const users = await User.find({scope: ['user'], registration_done: true}) for (const user of users) { const marriageDate = new Date(user?.marriage_date); const tempMarriageDate = new Date(now.getFullYear() , marriageDate.getMonth() , marriageDate.getDate()) if (user.marriage_date && tempMarriageDate.toDateString() === now.toDateString() && user.marriageDateChange < now.getFullYear()) { //// add point user.score += pointMarriageDate user.marriageDateChange = now?.getFullYear()?.toString() user.scoreLogs = [ { score : pointMarriageDate, forWhat : 'marriageDate', action : 'increase' } ] await user.save() //// send sms const smsTemplate = `${user.first_name} عزیز سالروز ازدواجتان مبارک هدیه ما به شما ${pointBirthDate} امتیاز برای سفارش در اپلیکیشن برگ من لغو11` const smsResult = await sms(user.mobile_number, smsTemplate) if (!smsResult || !smsResult.data.IsSuccessful) { console.log('marriage_date sms', smsResult) } } } }catch (error) { console.log('marriage_date job' , error.message) return '' } }); } }