From 4dc6000976ba28a578d2339cbff8863d6d21d4b8 Mon Sep 17 00:00:00 2001 From: Mr Swift Date: Fri, 4 Oct 2024 13:43:09 +0330 Subject: [PATCH] New : Send SMS after 3 days to user for register sim card --- package-lock.json | 23 +++++++++++++++++++ package.json | 1 + server/models/GPS.InstalledDevice.js | 4 ++++ server/plugins/cronJobs.js | 34 ++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+) diff --git a/package-lock.json b/package-lock.json index 07b0817..42080f3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,6 +20,7 @@ "ckeditor4-vue": "^1.2.0", "core-js": "^3.6.5", "cors": "^2.8.5", + "cron": "^3.1.7", "dateformat": "^4.4.1", "element-ui": "^2.15.13", "express": "^4.17.1", @@ -5202,6 +5203,11 @@ "@types/geojson": "*" } }, + "node_modules/@types/luxon": { + "version": "3.4.2", + "resolved": "https://registry.npmjs.org/@types/luxon/-/luxon-3.4.2.tgz", + "integrity": "sha512-TifLZlFudklWlMBfhubvgqTXRzLDI5pCbGa4P8a3wPyUQSW+1xQ5eDsreP9DWHX3tjq1ke96uYG/nwundroWcA==" + }, "node_modules/@types/mongodb": { "version": "3.6.20", "resolved": "https://registry.npmjs.org/@types/mongodb/-/mongodb-3.6.20.tgz", @@ -7910,6 +7916,15 @@ "resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz", "integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==" }, + "node_modules/cron": { + "version": "3.1.7", + "resolved": "https://registry.npmjs.org/cron/-/cron-3.1.7.tgz", + "integrity": "sha512-tlBg7ARsAMQLzgwqVxy8AZl/qlTc5nibqYwtNGoCrd+cV+ugI+tvZC1oT/8dFH8W455YrywGykx/KMmAqOr7Jw==", + "dependencies": { + "@types/luxon": "~3.4.0", + "luxon": "~3.4.0" + } + }, "node_modules/cross-spawn": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", @@ -13022,6 +13037,14 @@ "yallist": "^3.0.2" } }, + "node_modules/luxon": { + "version": "3.4.4", + "resolved": "https://registry.npmjs.org/luxon/-/luxon-3.4.4.tgz", + "integrity": "sha512-zobTr7akeGHnv7eBOXcRgMeCP6+uyYsczwmeRCauvpvaAltgNyTbLH/+VaEAPUeWBT+1GuNmz4wC/6jtQzbbVA==", + "engines": { + "node": ">=12" + } + }, "node_modules/make-dir": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-3.1.0.tgz", diff --git a/package.json b/package.json index d991601..7b9d364 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "ckeditor4-vue": "^1.2.0", "core-js": "^3.6.5", "cors": "^2.8.5", + "cron": "^3.1.7", "dateformat": "^4.4.1", "element-ui": "^2.15.13", "express": "^4.17.1", diff --git a/server/models/GPS.InstalledDevice.js b/server/models/GPS.InstalledDevice.js index 38f3f80..9e0e70d 100644 --- a/server/models/GPS.InstalledDevice.js +++ b/server/models/GPS.InstalledDevice.js @@ -17,6 +17,10 @@ const InstalledDeviceSchema = mongoose.Schema({ }, registerDate: Date, installationDate: Date, + check:{ + type:Boolean, + default:false + }, userId: { type: String, ref: 'UserGPS' diff --git a/server/plugins/cronJobs.js b/server/plugins/cronJobs.js index 5da3317..f003c16 100644 --- a/server/plugins/cronJobs.js +++ b/server/plugins/cronJobs.js @@ -1,5 +1,6 @@ /* eslint-disable no-unused-vars */ const User = require('../models/User') +const InstalledDevice = require('../models/GPS.InstalledDevice') const Lottery = require('../models/Lottery') const SurveyCheck = require('../models/SurveyCheck') const Survey = require('../models/Survey') @@ -8,6 +9,8 @@ const { saveUserToArpa, updateUserOnArpa } = require('../ArpaWebservice') const { generateRandomDigitsInRangeOf } = require('./controllersHelperFunctions') const { SMS } = require('./SMS_Module') const { phrases } = require('./SMS_Phrases') +// eslint-disable-next-line import/order +const Cron = require("cron").CronJob; /// /////// times const minute = 1000 * 60 @@ -15,6 +18,35 @@ const hour = minute * 60 const day = hour * 24 /// /////// jobs + +const job = new Cron("0 0 * * *", async () => { + console.log('✅ --job: check devices for activation is running') + + try { + const devices = await InstalledDevice.find({ + $or: [ + { status: 0 }, + { status: 2 } + ] + }).populate('userId', 'mobile_number').populate('deviceId', 'model') + + for await (const device of devices) { + // item time and timeout + const itemTime = Date.parse(device.created_at) + const timeout = itemTime + (day * 3) + if (Date.now() >= timeout && !device.check) { + device.check = true; + await device.save() + SMS([device.userId.mobile_number], `کاربر گرامی\n \n \nجهت ثبت سیمکارت برای دستگاه ${device.deviceId.model} \nبا شناسه : ${device.IMEI} \nلطفا زود تر اقدام کنید `) + } + } + + } catch (e) { + console.log(e) + } + + +}) const jobs = { checkUserActivation() { console.log('✅ --job: check users for activation is running') @@ -277,6 +309,8 @@ const devTools = { } module.exports = () => { + job.start() + jobs.checkDeviceActivation() jobs.checkUserActivation() jobs.drawLottery() jobs.checkIfAnnouncementsExpired()