From 7ab8b443c9792d2ea1328e73eb9ee6b5fc41ae99 Mon Sep 17 00:00:00 2001 From: mbayopanda Date: Sat, 15 Jun 2019 11:35:00 +0100 Subject: [PATCH] Form reset after submission and cron job fixing --- client/src/i18n/en/cron.json | 1 + client/src/i18n/fr/cron.json | 1 + .../bhCronEmailReport/bhCronEmailReport.html | 2 +- .../bhCronEmailReport/bhCronEmailReport.js | 20 +++---- .../admin/cronEmailReport/index.js | 17 +++--- server/lib/cronjob/index.js | 28 --------- server/models/bhima.sql | 3 +- server/models/migrations/next/migrations.sql | 5 +- yarn.lock | 57 ++++++++++++------- 9 files changed, 66 insertions(+), 68 deletions(-) delete mode 100644 server/lib/cronjob/index.js diff --git a/client/src/i18n/en/cron.json b/client/src/i18n/en/cron.json index 521b442abb..41f0f6b1a0 100644 --- a/client/src/i18n/en/cron.json +++ b/client/src/i18n/en/cron.json @@ -8,6 +8,7 @@ "WEEKLY":"Weekly", "MONTHLY":"Monthly", "YEARLY":"Yearly", + "EVERY_MINUTE":"Every Minutes", "SAVE":"Save", "SAVED_AUTO_REPORT":"Saved report for mailing", "LAST":"Last", diff --git a/client/src/i18n/fr/cron.json b/client/src/i18n/fr/cron.json index 0d7266a7ec..ddbaaf4647 100644 --- a/client/src/i18n/fr/cron.json +++ b/client/src/i18n/fr/cron.json @@ -8,6 +8,7 @@ "WEEKLY":"Chaque semaine", "MONTHLY":"Chaque mois", "YEARLY":"Chaque année", + "EVERY_MINUTE":"Chaque minutes", "SAVE":"Sauvegarder", "SAVED_AUTO_REPORT":"Rapports sauvegardés pour le mailing", "LAST":"Dernier", diff --git a/client/src/js/components/bhCronEmailReport/bhCronEmailReport.html b/client/src/js/components/bhCronEmailReport/bhCronEmailReport.html index fc96aa108c..89d1f63794 100644 --- a/client/src/js/components/bhCronEmailReport/bhCronEmailReport.html +++ b/client/src/js/components/bhCronEmailReport/bhCronEmailReport.html @@ -46,7 +46,7 @@
-
diff --git a/client/src/js/components/bhCronEmailReport/bhCronEmailReport.js b/client/src/js/components/bhCronEmailReport/bhCronEmailReport.js index 789fa02ccd..cdb73a24a3 100644 --- a/client/src/js/components/bhCronEmailReport/bhCronEmailReport.js +++ b/client/src/js/components/bhCronEmailReport/bhCronEmailReport.js @@ -35,17 +35,17 @@ function bhCronEmailReportController(CronEmailReports, Notify, Session) { $ctrl.cron.has_dynamic_dates = value; }; - $ctrl.$onInit = () => { + $ctrl.$onInit = init; + + function init() { $ctrl.cron = { report_id : $ctrl.reportId, report_url : $ctrl.reportUrl, has_dynamic_dates : 0, }; - $ctrl.isFeatureEnabled = Session.enterprise.settings.enable_auto_email_report; - load(); - }; + } function load() { CronEmailReports.read(null, { report_id : $ctrl.reportId }) @@ -87,14 +87,14 @@ function bhCronEmailReportController(CronEmailReports, Notify, Session) { }; CronEmailReports.create(params) - .then(() => reset()) - .then(() => load()) + .then(() => reset(cronForm)) + .then(() => init()) .catch(Notify.handleError); } - function reset() { - $ctrl.cron.label = undefined; - $ctrl.cron.entity_group_uuid = undefined; - $ctrl.cron.cron_id = undefined; + function reset(form) { + form.CronForm.$setPristine(); + form.EntityGroupForm.$setPristine(); + form.textValueForm.$setPristine(); } } diff --git a/server/controllers/admin/cronEmailReport/index.js b/server/controllers/admin/cronEmailReport/index.js index 8604692d2c..6d95aa8f60 100644 --- a/server/controllers/admin/cronEmailReport/index.js +++ b/server/controllers/admin/cronEmailReport/index.js @@ -3,9 +3,9 @@ * API controller for the table cron_email_report */ const debug = require('debug')('app'); +const Cron = require('cron').CronJob; const db = require('../../../lib/db'); -const CronJob = require('../../../lib/cronjob'); const Moment = require('../../../lib/moment'); const FilterParser = require('../../../lib/filter'); @@ -116,7 +116,7 @@ async function create(req, res, next) { const result = await db.exec(query, [cron]); const created = await lookup(result.insertId); - await createEmailReportJob(created, sendEmailReportDocument.call(this, created)); + await createEmailReportJob(created, sendEmailReportDocument, created); res.status(201).json({ id : result.insertId }); } catch (error) { @@ -129,11 +129,12 @@ async function create(req, res, next) { * @description add a cron job to run each time for the given pattern * @param {string} frequency Cron job pattern * * * * * * @param {function} cb the function to run + * @param {any} params params of the function */ -function addJob(frequency, cb) { - const cj = new CronJob(frequency, cb); +function addJob(frequency, cb, ...params) { + const cj = new Cron(frequency, () => cb(...params)); cj.start(); - return cj.job; + return cj; } /** @@ -149,7 +150,7 @@ async function launchCronEmailReportJobs() { const records = await find(); if (!records.length) { return; } - const jobs = records.map(record => createEmailReportJob(record, sendEmailReportDocument.call(this, record))); + const jobs = records.map(record => createEmailReportJob(record, sendEmailReportDocument, record)); await Promise.all(jobs); debug('Reports scanned successfully'); @@ -164,8 +165,8 @@ async function launchCronEmailReportJobs() { * @param {object} record A row of cron email report * @param {*} cb The function to run */ -function createEmailReportJob(record, cb) { - const job = addJob(record.cron_value, cb); +function createEmailReportJob(record, cb, ...params) { + const job = addJob(record.cron_value, cb, ...params); MAIN_EMAIL_JOBS.push({ id : record.id, label : record.label, job }); return updateCronEmailReportJobDates(record.id, job); } diff --git a/server/lib/cronjob/index.js b/server/lib/cronjob/index.js deleted file mode 100644 index 903baf119a..0000000000 --- a/server/lib/cronjob/index.js +++ /dev/null @@ -1,28 +0,0 @@ -/** - * cronjob class - */ -const Cron = require('cron').CronJob; - -class CronJob { - /** - * @param {string} frequency cron pattern * * * * * - * @param {function} cb a callback of the function to run - */ - constructor(frequency, cb) { - this.job = new Cron(frequency, cb); - } - - start() { - this.job.start(); - } - - stop() { - this.job.stop(); - } - - job() { - return this.job; - } -} - -module.exports = CronJob; diff --git a/server/models/bhima.sql b/server/models/bhima.sql index f446a92a25..f6b965cca4 100644 --- a/server/models/bhima.sql +++ b/server/models/bhima.sql @@ -341,4 +341,5 @@ INSERT INTO `cron` (`label`, `value`) VALUES ('CRON.DAILY', '0 1 * * *'), ('CRON.WEEKLY', '0 1 * * 0'), ('CRON.MONTHLY', '0 1 30 * *'), - ('CRON.YEARLY', '0 1 31 12 *'); \ No newline at end of file + ('CRON.YEARLY', '0 1 31 12 *'), + ('CRON.EVERY_MINUTE', '* * * * *'); \ No newline at end of file diff --git a/server/models/migrations/next/migrations.sql b/server/models/migrations/next/migrations.sql index cfb8e9242d..0e7ef009b4 100644 --- a/server/models/migrations/next/migrations.sql +++ b/server/models/migrations/next/migrations.sql @@ -282,7 +282,10 @@ CREATE TABLE `cron_email_report` ( `last_send` DATETIME NULL, `next_send` DATETIME NULL, `has_dynamic_dates` TINYINT NULL DEFAULT 0, - PRIMARY KEY (`id`) + PRIMARY KEY (`id`), + UNIQUE KEY `label` (`label`, `report_id`), + KEY `entity_group_uuid` (`entity_group_uuid`), + FOREIGN KEY (`entity_group_uuid`) REFERENCES `entity_group` (`uuid`) ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARACTER SET = utf8mb4 DEFAULT COLLATE = utf8mb4_unicode_ci; /* diff --git a/yarn.lock b/yarn.lock index 4b43cc7653..e13cb68d29 100644 --- a/yarn.lock +++ b/yarn.lock @@ -709,14 +709,6 @@ aws4@^1.8.0: resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.8.0.tgz#f0e003d9ca9e7f59c7a508945d7b2ef9a04a542f" integrity sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ== -axios@^0.19.0: - version "0.19.0" - resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.0.tgz#8e09bff3d9122e133f7b8101c8fbdd00ed3d2ab8" - integrity sha512-1uvKqKQta3KBxIz14F2v06AEHZ/dIoeKfbTRkK1E5oqjDnuEerLmYTgJB5AiQZHJcljpg1TuRzdjDR06qNk0DQ== - dependencies: - follow-redirects "1.5.10" - is-buffer "^2.0.2" - babel-code-frame@^6.22.0: version "6.26.0" resolved "https://registry.yarnpkg.com/babel-code-frame/-/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b" @@ -1863,6 +1855,14 @@ create-error-class@^3.0.0: dependencies: capture-stack-trace "^1.0.0" +cron-parser@^2.7.3: + version "2.12.0" + resolved "https://registry.yarnpkg.com/cron-parser/-/cron-parser-2.12.0.tgz#05ae8c008025cecdd8778746626a183249ca300b" + integrity sha512-1GU6CQJ6gT9XDEGeTuzfhZgFMf82BSs3ihFA3i2wr4qGKJLhO1kOvaIF9biIo39CaPgzZ17U8FgYxRv/+UR50A== + dependencies: + is-nan "^1.2.1" + moment-timezone "^0.5.25" + cron@^1.7.1: version "1.7.1" resolved "https://registry.yarnpkg.com/cron/-/cron-1.7.1.tgz#e85ee9df794d1bc6579896ee382053c3ce33778f" @@ -2154,7 +2154,7 @@ debug@2.6.9, debug@^2.2.0, debug@^2.3.3, debug@^2.6.8, debug@^2.6.9: dependencies: ms "2.0.0" -debug@3.1.0, debug@=3.1.0, debug@~3.1.0: +debug@3.1.0, debug@~3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261" integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g== @@ -2246,7 +2246,7 @@ default-resolution@^2.0.0: resolved "https://registry.yarnpkg.com/default-resolution/-/default-resolution-2.0.0.tgz#bcb82baa72ad79b426a76732f1a81ad6df26d684" integrity sha1-vLgrqnKtebQmp2cy8aga1t8m1oQ= -define-properties@^1.1.2, define-properties@^1.1.3: +define-properties@^1.1.1, define-properties@^1.1.2, define-properties@^1.1.3: version "1.1.3" resolved "https://registry.yarnpkg.com/define-properties/-/define-properties-1.1.3.tgz#cf88da6cbee26fe6db7094f61d870cbd84cee9f1" integrity sha512-3MqfYKj2lLzdMSf8ZIZE/V+Zuy+BgD6f164e8K2w7dgnpKArBDerGYpM46IYYcjnkdPNMjPk9A6VFB8+3SKlXQ== @@ -3396,13 +3396,6 @@ flush-write-stream@^1.0.2: inherits "^2.0.3" readable-stream "^2.3.6" -follow-redirects@1.5.10: - version "1.5.10" - resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.10.tgz#7b7a9f9aea2fdff36786a94ff643ed07f4ff5e2a" - integrity sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ== - dependencies: - debug "=3.1.0" - follow-redirects@^1.0.0: version "1.7.0" resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.7.0.tgz#489ebc198dc0e7f64167bd23b03c4c19b5784c76" @@ -4568,7 +4561,7 @@ is-buffer@^1.0.2, is-buffer@^1.1.5: resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be" integrity sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w== -is-buffer@^2.0.2, is-buffer@~2.0.3: +is-buffer@~2.0.3: version "2.0.3" resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-2.0.3.tgz#4ecf3fcf749cbd1e472689e109ac66261a25e725" integrity sha512-U15Q7MXTuZlrbymiz95PJpZxu8IlipAp4dtS3wOdgPXx3mqBnslrWU14kxfHB+Py/+2PVKSr37dMAgM2A4uArw== @@ -4697,6 +4690,13 @@ is-installed-globally@^0.1.0: global-dirs "^0.1.0" is-path-inside "^1.0.0" +is-nan@^1.2.1: + version "1.2.1" + resolved "https://registry.yarnpkg.com/is-nan/-/is-nan-1.2.1.tgz#9faf65b6fb6db24b7f5c0628475ea71f988401e2" + integrity sha1-n69ltvttskt/XAYoR16nH5iEAeI= + dependencies: + define-properties "^1.1.1" + is-negated-glob@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/is-negated-glob/-/is-negated-glob-1.0.0.tgz#6910bca5da8c95e784b5751b976cf5a10fee36d2" @@ -5524,6 +5524,11 @@ loglevel@^1.6.1: resolved "https://registry.yarnpkg.com/loglevel/-/loglevel-1.6.1.tgz#e0fc95133b6ef276cdc8887cdaf24aa6f156f8fa" integrity sha1-4PyVEztu8nbNyIh82vJKpvFW+Po= +long-timeout@0.1.1: + version "0.1.1" + resolved "https://registry.yarnpkg.com/long-timeout/-/long-timeout-0.1.1.tgz#9721d788b47e0bcb5a24c2e2bee1a0da55dab514" + integrity sha1-lyHXiLR+C8taJMLivuGg2lXatRQ= + longest@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/longest/-/longest-1.0.1.tgz#30a0b2da38f73770e8294a0d22e6625ed77d0097" @@ -5952,7 +5957,7 @@ modify-values@^1.0.0: resolved "https://registry.yarnpkg.com/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022" integrity sha512-xV2bxeN6F7oYjZWTe/YPAy6MN2M+sL4u/Rlm2AHCIVGfo2p1yGmBHQ6vHehl4bRTZBdHu3TSkWdYgkwpYzAGSw== -moment-timezone@^0.5.x: +moment-timezone@^0.5.25, moment-timezone@^0.5.x: version "0.5.25" resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.25.tgz#a11bfa2f74e088327f2cd4c08b3e7bdf55957810" integrity sha512-DgEaTyN/z0HFaVcVbSyVCUU6HeFdnNC3vE4c9cgu2dgMTvjBUBdBzWfasTBmAW45u5OIMeCJtU8yNjM22DHucw== @@ -6171,6 +6176,15 @@ node-releases@^1.1.17: dependencies: semver "^5.3.0" +node-schedule@^1.3.2: + version "1.3.2" + resolved "https://registry.yarnpkg.com/node-schedule/-/node-schedule-1.3.2.tgz#d774b383e2a6f6ade59eecc62254aea07cd758cb" + integrity sha512-GIND2pHMHiReSZSvS6dpZcDH7pGPGFfWBIEud6S00Q8zEIzAs9ommdyRK1ZbQt8y1LyZsJYZgPnyi7gpU2lcdw== + dependencies: + cron-parser "^2.7.3" + long-timeout "0.1.1" + sorted-array-functions "^1.0.0" + nodemon@^1.18.10: version "1.19.1" resolved "https://registry.yarnpkg.com/nodemon/-/nodemon-1.19.1.tgz#576f0aad0f863aabf8c48517f6192ff987cd5071" @@ -8427,6 +8441,11 @@ sort-keys@^2.0.0: dependencies: is-plain-obj "^1.0.0" +sorted-array-functions@^1.0.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/sorted-array-functions/-/sorted-array-functions-1.2.0.tgz#43265b21d6e985b7df31621b1c11cc68d8efc7c3" + integrity sha512-sWpjPhIZJtqO77GN+LD8dDsDKcWZ9GCOJNqKzi1tvtjGIzwfoyuRH8S0psunmc6Z5P+qfDqztSbwYR5X/e1UTg== + source-map-resolve@^0.5.0: version "0.5.2" resolved "https://registry.yarnpkg.com/source-map-resolve/-/source-map-resolve-0.5.2.tgz#72e2cc34095543e43b2c62b2c4c10d4a9054f259"