diff --git a/src/incentives/bin/cronJob.js b/src/incentives/bin/cronJob.js new file mode 100644 index 0000000000..5e54881746 --- /dev/null +++ b/src/incentives/bin/cronJob.js @@ -0,0 +1,58 @@ +const cron = require("node-cron"); +const SimModel = require("@models/Sim"); +const constants = require("@config/constants"); +const log4js = require("log4js"); +const logger = log4js.getLogger( + `${constants.ENVIRONMENT} -- bin/cronJob script` +); +const checkStatus = require("@utils/create-sim").checkStatus; +const secondsDelayBetweenRequests = 20000; +const internetDataBalanceThreshold = 5; + +// Everyday at midnight +cron.schedule("0 0 * * *", async () => { + try { + const batchSize = 100; // Process 100 SIM cards at a time + let skip = 0; + + const simCards = await SimModel("airqo").find({}).select("_id").lean(); + + while (true) { + const simBatch = simCards.slice(skip, skip + batchSize); + + if (simBatch.length === 0) { + break; + } + + await processSimCardsWithDelay(simBatch); + + skip += batchSize; + } + } catch (error) { + logger.error( + `An error occurred in the cron job --- ${JSON.stringify(error)}` + ); + } +}); + +async function processSimCardsWithDelay(simBatch) { + for (const sim of simBatch) { + const responseFromCheckStatus = await checkStatus({ + query: { tenant: "airqo" }, + params: { sim_id: sim._id }, + }); + + // Check if data.balance is less than the declared threshold and log it + if ( + responseFromCheckStatus.success && + responseFromCheckStatus.data.balance < internetDataBalanceThreshold + ) { + logger.info( + `SIM card ${sim._id} has a balance less than ${internetDataBalanceThreshold}` + ); + } + await new Promise((resolve) => + setTimeout(resolve, secondsDelayBetweenRequests) + ); + } +} diff --git a/src/incentives/package-lock.json b/src/incentives/package-lock.json index 53276b6267..1a05daf3f7 100644 --- a/src/incentives/package-lock.json +++ b/src/incentives/package-lock.json @@ -35,6 +35,7 @@ "mongoose-unique-validator": "^2.0.3", "morgan": "~1.9.0", "mtn-momo": "^2.0.0", + "node-cron": "^3.0.2", "node-fetch": "^2.6.1", "node-schedule": "^2.1.1", "nodemailer": "^6.9.4", @@ -5868,6 +5869,25 @@ "node": ">=4.0.0" } }, + "node_modules/node-cron": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.2.tgz", + "integrity": "sha512-iP8l0yGlNpE0e6q1o185yOApANRe47UPbLf4YxfbiNHt/RU5eBcGB/e0oudruheSf+LQeDMezqC5BVAb5wwRcQ==", + "dependencies": { + "uuid": "8.3.2" + }, + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/node-cron/node_modules/uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/node-fetch": { "version": "2.6.7", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", @@ -13209,6 +13229,21 @@ "resolved": "https://registry.npmjs.org/nocache/-/nocache-2.1.0.tgz", "integrity": "sha512-0L9FvHG3nfnnmaEQPjT9xhfN4ISk0A8/2j4M37Np4mcDesJjHgEUfgPhdCyZuFI954tjokaIj/A3NdpFNdEh4Q==" }, + "node-cron": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.2.tgz", + "integrity": "sha512-iP8l0yGlNpE0e6q1o185yOApANRe47UPbLf4YxfbiNHt/RU5eBcGB/e0oudruheSf+LQeDMezqC5BVAb5wwRcQ==", + "requires": { + "uuid": "8.3.2" + }, + "dependencies": { + "uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==" + } + } + }, "node-fetch": { "version": "2.6.7", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", diff --git a/src/incentives/package.json b/src/incentives/package.json index d1b87fcd22..4a09aad1e8 100644 --- a/src/incentives/package.json +++ b/src/incentives/package.json @@ -54,6 +54,7 @@ "mongoose-unique-validator": "^2.0.3", "morgan": "~1.9.0", "mtn-momo": "^2.0.0", + "node-cron": "^3.0.2", "node-fetch": "^2.6.1", "node-schedule": "^2.1.1", "nodemailer": "^6.9.4",