From 8b3b3c0b8baf47c8d0f9b71b93547ed43c07c652 Mon Sep 17 00:00:00 2001 From: Georgii Petrov Date: Wed, 29 May 2024 20:01:04 +0300 Subject: [PATCH] [feature] License expiration notification changed --- Common/config/default.json | 17 ++++- Common/config/local-tests.json | 3 - Common/sources/license.js | 2 +- Common/sources/notificationService.js | 11 +-- DocService/sources/utilsDocService.js | 105 ++++++++------------------ tests/env-setup.js | 1 - tests/unit/utilsDocService.tests.js | 51 ------------- 7 files changed, 51 insertions(+), 139 deletions(-) delete mode 100644 Common/config/local-tests.json delete mode 100644 tests/unit/utilsDocService.tests.js diff --git a/Common/config/default.json b/Common/config/default.json index 32b64e90f..3aafc9cf2 100644 --- a/Common/config/default.json +++ b/Common/config/default.json @@ -37,18 +37,29 @@ "notification": { "enable": false, "rules": { - "licenseExpired": { + "licenseExpirationWarning": { "transportType": [ "email" ], "template": { - "title": "License", - "body": "%s license expires in %s!!!" + "title": "Your license expires soon", + "body": "%s license expires on %s!!!" }, "policies": { "repeatInterval": "1d" } }, + "licenseExpired": { + "transportType": [ + "email" + ], + "template": { + "title": "License period ended", + "body": "%s license has expired!!!" + }, + "policies": { + } + }, "licenseLimit": { "transportType": [ "email" diff --git a/Common/config/local-tests.json b/Common/config/local-tests.json deleted file mode 100644 index 0db3279e4..000000000 --- a/Common/config/local-tests.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - -} diff --git a/Common/sources/license.js b/Common/sources/license.js index 35582b22f..fa318669e 100644 --- a/Common/sources/license.js +++ b/Common/sources/license.js @@ -59,7 +59,7 @@ exports.readLicense = async function () { plugins: false, buildDate: oBuildDate, startDate: startDate, - endDate: '2024-06-29T04:13:00.000Z', + endDate: null, customerId: "", alias: "" }, null]; diff --git a/Common/sources/notificationService.js b/Common/sources/notificationService.js index 7d15fc90f..48f3fc8db 100644 --- a/Common/sources/notificationService.js +++ b/Common/sources/notificationService.js @@ -41,11 +41,12 @@ const cfgMailServer = config.get('email.smtpServerConfiguration'); const cfgMailMessageDefaults = config.get('email.contactDefaults'); const cfgNotificationEnable = config.get('notification.enable'); -const defaultRepeatInterval = 1000 * 60 * 60 * 24; +const infiniteRepeatInterval = Infinity; const repeatIntervalsExpired = new Map(); const notificationTypes = { - LICENSE_EXPIRED: "licenseExpired", - LICENSE_LIMIT: "licenseLimit" + LICENSE_EXPIRATION_WARNING: 'licenseExpirationWarning', + LICENSE_EXPIRED: 'licenseExpired', + LICENSE_LIMIT: 'licenseLimit' }; class TransportInterface { @@ -118,7 +119,7 @@ async function notify(ctx, notificationType, messageParams) { function checkRulePolicies(ctx, notificationType, tenRule) { const { repeatInterval } = tenRule.policies; - const intervalMilliseconds = ms(repeatInterval) ?? defaultRepeatInterval; + const intervalMilliseconds = repeatInterval ? ms(repeatInterval) : infiniteRepeatInterval; const cacheKey = `${notificationType}_${ctx.tenant}`; const expired = repeatIntervalsExpired.get(cacheKey); @@ -127,7 +128,7 @@ function checkRulePolicies(ctx, notificationType, tenRule) { return true; } - ctx.logger.debug(`Notification service: skip rule "%s" due to repeat interval %s`, notificationType, repeatInterval); + ctx.logger.debug(`Notification service: skip rule "%s" due to repeat interval = %s`, notificationType, repeatInterval ?? "infinite"); return false; } diff --git a/DocService/sources/utilsDocService.js b/DocService/sources/utilsDocService.js index 554f8e084..e1250e429 100644 --- a/DocService/sources/utilsDocService.js +++ b/DocService/sources/utilsDocService.js @@ -37,7 +37,6 @@ const exifParser = require('exif-parser'); const Jimp = require('jimp'); const ms = require('ms'); -const utils = require('../../Common/sources/utils'); const tenantManager = require('../../Common/sources/tenantManager'); const { notificationTypes, ...notificationService } = require('../../Common/sources/notificationService'); @@ -68,60 +67,23 @@ async function fixImageExifRotation(ctx, buffer) { return buffer; } -function humanFriendlyExpirationTime(ctx, endTime) { - const timeWithPostfix = (timeName, value) => `${value} ${timeName}${value > 1 ? 's' : ''}`; - const currentTime = new Date(); - const oneMinute = 1000 * 60; - const oneHour = oneMinute * 60; - const oneDay = oneHour * 24; - const absoluteDiff = endTime.getTime() - currentTime.getTime(); - - currentTime.setUTCSeconds(0,0); - - if (endTime.getTime() < currentTime.getTime()) { - ctx.logger.warn(`humanFriendlyExpirationTime(): expiration date value is lesser than current date`); - return ''; - } - - const floatResult = absoluteDiff / oneDay; - const daysCount = floatResult < 1 ? 0 : Math.round(floatResult); - const monthDiff = utils.getMonthDiff(currentTime, endTime); - if (monthDiff >= 1 && daysCount >= currentTime.getDaysInMonth()) { - return timeWithPostfix('month', monthDiff); - } - - if (daysCount > 0) { - return timeWithPostfix('day', daysCount); - } - - // This time we cannot just round division operation to the nearest digit because we need minutes value and more accuracy. - let hoursCount = 0 - for (; hoursCount * oneHour <= absoluteDiff; hoursCount++) {} - - if (hoursCount * oneHour > absoluteDiff) { - hoursCount--; - } - - let minutesCount = Math.round((absoluteDiff - hoursCount * oneHour) / oneMinute); - if(minutesCount >= 60) { - hoursCount++; - minutesCount -= 60; - } - - let timeString = ''; - if (hoursCount > 0) { - timeString += timeWithPostfix('hour', hoursCount); - } - - if (minutesCount > 0) { - if (timeString.length !== 0) { - timeString += ' '; - } - - timeString += timeWithPostfix('minute', minutesCount); - } - - return timeString; +function humanFriendlyExpirationTime(endTime) { + const month = [ + 'January', + 'February', + 'March', + 'April', + 'May', + 'June', + 'July', + 'August', + 'September', + 'October', + 'November', + 'December' + ]; + + return `${month[endTime.getUTCMonth()]} ${endTime.getUTCDate()}, ${endTime.getUTCFullYear()}` } function notifyLicenseExpiration(ctx, endDate) { @@ -131,26 +93,19 @@ function notifyLicenseExpiration(ctx, endDate) { } const currentDate = new Date(); - const licenseEndTime = new Date(endDate); - - if (licenseEndTime < currentDate) { - ctx.logger.warn(`notifyLicenseExpiration(): expiration date(${licenseEndTime}) is lesser than current date(${currentDate})`); - return; - } - - if (currentDate.getTime() >= licenseEndTime.getTime() - cfgStartNotifyFrom) { - const formattedTimeRemaining = humanFriendlyExpirationTime(ctx, licenseEndTime); - let tenant = tenantManager.isDefaultTenant(ctx) ? 'server' : ctx.tenant; - ctx.logger.warn('%s license expires in %s!!!', tenant, formattedTimeRemaining); - notificationService.notify(ctx, notificationTypes.LICENSE_EXPIRED, [formattedTimeRemaining]); + if (currentDate.getTime() >= endDate.getTime() - cfgStartNotifyFrom) { + const formattedExpirationTime = humanFriendlyExpirationTime(endDate); + const tenant = tenantManager.isDefaultTenant(ctx) ? 'server' : ctx.tenant; + + if (endDate < currentDate) { + ctx.logger.warn('%s license has expired!!!', tenant); + notificationService.notify(ctx, notificationTypes.LICENSE_EXPIRED, [tenant]); + } else { + ctx.logger.warn('%s license expires on %s!!!', tenant, formattedExpirationTime); + notificationService.notify(ctx, notificationTypes.LICENSE_EXPIRATION_WARNING, [tenant, formattedExpirationTime]); + } } } -module.exports = { - fixImageExifRotation, - notifyLicenseExpiration -}; - -if (process.env.NODE_APP_INSTANCE === 'tests') { - module.exports.humanFriendlyExpirationTime = humanFriendlyExpirationTime; -} +module.exports.fixImageExifRotation = fixImageExifRotation; +module.exports.notifyLicenseExpiration = notifyLicenseExpiration; diff --git a/tests/env-setup.js b/tests/env-setup.js index 2c6c0e667..1dee40d20 100644 --- a/tests/env-setup.js +++ b/tests/env-setup.js @@ -39,7 +39,6 @@ const platform = platforms[process.platform]; process.env.NODE_ENV = `development-${platform}`; process.env.NODE_CONFIG_DIR = '../Common/config'; -process.env.NODE_APP_INSTANCE = 'tests'; if (platform === 'mac') { process.env.DYLD_LIBRARY_PATH = '../FileConverter/bin/'; diff --git a/tests/unit/utilsDocService.tests.js b/tests/unit/utilsDocService.tests.js deleted file mode 100644 index 3fd50602d..000000000 --- a/tests/unit/utilsDocService.tests.js +++ /dev/null @@ -1,51 +0,0 @@ -const { describe, test, expect } = require('@jest/globals'); - -const utilsDocService = require('../../DocService/sources/utilsDocService'); -const operationContext = require('../../Common/sources/operationContext'); - -const ctx = new operationContext.Context(); - -function createEndTime(day, month, year, hours, minutes) { - const date = new Date(); - date.setUTCFullYear(year); - date.setUTCMonth(month); - date.setUTCDate(day); - date.setUTCHours(hours, minutes, 0,0); - - return date; -} - -describe('DocService utils', function () { - describe('humanFriendlyExpirationTime() format', function () { - const currentDate = new Date(); - currentDate.setUTCSeconds(0, 0); - - const day = currentDate.getUTCDate(); - const month = currentDate.getUTCMonth(); - const year = currentDate.getUTCFullYear(); - const hours = currentDate.getUTCHours(); - const minutes = currentDate.getUTCMinutes(); - - const testSuite = { - '12 months': createEndTime(day, month, year + 1, hours, minutes), - '15 months': createEndTime(day, month + 3, year + 1, hours, minutes), - '6 months': createEndTime(day, month + 6, year, hours, minutes), - '1 month': createEndTime(day, month + 1, year, hours, minutes), - '10 days': createEndTime(day + 10, month, year, hours, minutes), - '2 days': createEndTime(day + 2, month, year, hours, minutes), - // '24 hours': createEndTime(day + 1, month, year, hours, minutes), - // '23 hours': createEndTime(day, month, year, hours + 23, minutes), - // '16 minutes': createEndTime(day, month, year, hours, minutes + 16), - // '1 hour 15 minutes': createEndTime(day, month, year, hours + 1, minutes + 15), - '': createEndTime(day, month, year - 1, hours, minutes), - }; - - for (const testCase in testSuite) { - test(testCase === '' ? 'wrong end date' : testCase, function () { - const result = utilsDocService.humanFriendlyExpirationTime(ctx, testSuite[testCase]); - - expect(result).toEqual(testCase); - }); - } - }); -});