Skip to content

Commit

Permalink
[feature] License expiration notification changed
Browse files Browse the repository at this point in the history
  • Loading branch information
DrToldme committed May 29, 2024
1 parent 4b45a05 commit 8b3b3c0
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 139 deletions.
17 changes: 14 additions & 3 deletions Common/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 0 additions & 3 deletions Common/config/local-tests.json

This file was deleted.

2 changes: 1 addition & 1 deletion Common/sources/license.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
11 changes: 6 additions & 5 deletions Common/sources/notificationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);

Expand All @@ -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;
}

Expand Down
105 changes: 30 additions & 75 deletions DocService/sources/utilsDocService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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) {
Expand All @@ -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;
1 change: 0 additions & 1 deletion tests/env-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/';
Expand Down
51 changes: 0 additions & 51 deletions tests/unit/utilsDocService.tests.js

This file was deleted.

0 comments on commit 8b3b3c0

Please sign in to comment.