Skip to content

Commit 8b3b3c0

Browse files
author
Georgii Petrov
committed
[feature] License expiration notification changed
1 parent 4b45a05 commit 8b3b3c0

7 files changed

Lines changed: 51 additions & 139 deletions

File tree

Common/config/default.json

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,29 @@
3737
"notification": {
3838
"enable": false,
3939
"rules": {
40-
"licenseExpired": {
40+
"licenseExpirationWarning": {
4141
"transportType": [
4242
"email"
4343
],
4444
"template": {
45-
"title": "License",
46-
"body": "%s license expires in %s!!!"
45+
"title": "Your license expires soon",
46+
"body": "%s license expires on %s!!!"
4747
},
4848
"policies": {
4949
"repeatInterval": "1d"
5050
}
5151
},
52+
"licenseExpired": {
53+
"transportType": [
54+
"email"
55+
],
56+
"template": {
57+
"title": "License period ended",
58+
"body": "%s license has expired!!!"
59+
},
60+
"policies": {
61+
}
62+
},
5263
"licenseLimit": {
5364
"transportType": [
5465
"email"

Common/config/local-tests.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

Common/sources/license.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ exports.readLicense = async function () {
5959
plugins: false,
6060
buildDate: oBuildDate,
6161
startDate: startDate,
62-
endDate: '2024-06-29T04:13:00.000Z',
62+
endDate: null,
6363
customerId: "",
6464
alias: ""
6565
}, null];

Common/sources/notificationService.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ const cfgMailServer = config.get('email.smtpServerConfiguration');
4141
const cfgMailMessageDefaults = config.get('email.contactDefaults');
4242
const cfgNotificationEnable = config.get('notification.enable');
4343

44-
const defaultRepeatInterval = 1000 * 60 * 60 * 24;
44+
const infiniteRepeatInterval = Infinity;
4545
const repeatIntervalsExpired = new Map();
4646
const notificationTypes = {
47-
LICENSE_EXPIRED: "licenseExpired",
48-
LICENSE_LIMIT: "licenseLimit"
47+
LICENSE_EXPIRATION_WARNING: 'licenseExpirationWarning',
48+
LICENSE_EXPIRED: 'licenseExpired',
49+
LICENSE_LIMIT: 'licenseLimit'
4950
};
5051

5152
class TransportInterface {
@@ -118,7 +119,7 @@ async function notify(ctx, notificationType, messageParams) {
118119

119120
function checkRulePolicies(ctx, notificationType, tenRule) {
120121
const { repeatInterval } = tenRule.policies;
121-
const intervalMilliseconds = ms(repeatInterval) ?? defaultRepeatInterval;
122+
const intervalMilliseconds = repeatInterval ? ms(repeatInterval) : infiniteRepeatInterval;
122123
const cacheKey = `${notificationType}_${ctx.tenant}`;
123124
const expired = repeatIntervalsExpired.get(cacheKey);
124125

@@ -127,7 +128,7 @@ function checkRulePolicies(ctx, notificationType, tenRule) {
127128
return true;
128129
}
129130

130-
ctx.logger.debug(`Notification service: skip rule "%s" due to repeat interval %s`, notificationType, repeatInterval);
131+
ctx.logger.debug(`Notification service: skip rule "%s" due to repeat interval = %s`, notificationType, repeatInterval ?? "infinite");
131132
return false;
132133
}
133134

DocService/sources/utilsDocService.js

Lines changed: 30 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ const exifParser = require('exif-parser');
3737
const Jimp = require('jimp');
3838
const ms = require('ms');
3939

40-
const utils = require('../../Common/sources/utils');
4140
const tenantManager = require('../../Common/sources/tenantManager');
4241
const { notificationTypes, ...notificationService } = require('../../Common/sources/notificationService');
4342

@@ -68,60 +67,23 @@ async function fixImageExifRotation(ctx, buffer) {
6867
return buffer;
6968
}
7069

71-
function humanFriendlyExpirationTime(ctx, endTime) {
72-
const timeWithPostfix = (timeName, value) => `${value} ${timeName}${value > 1 ? 's' : ''}`;
73-
const currentTime = new Date();
74-
const oneMinute = 1000 * 60;
75-
const oneHour = oneMinute * 60;
76-
const oneDay = oneHour * 24;
77-
const absoluteDiff = endTime.getTime() - currentTime.getTime();
78-
79-
currentTime.setUTCSeconds(0,0);
80-
81-
if (endTime.getTime() < currentTime.getTime()) {
82-
ctx.logger.warn(`humanFriendlyExpirationTime(): expiration date value is lesser than current date`);
83-
return '';
84-
}
85-
86-
const floatResult = absoluteDiff / oneDay;
87-
const daysCount = floatResult < 1 ? 0 : Math.round(floatResult);
88-
const monthDiff = utils.getMonthDiff(currentTime, endTime);
89-
if (monthDiff >= 1 && daysCount >= currentTime.getDaysInMonth()) {
90-
return timeWithPostfix('month', monthDiff);
91-
}
92-
93-
if (daysCount > 0) {
94-
return timeWithPostfix('day', daysCount);
95-
}
96-
97-
// This time we cannot just round division operation to the nearest digit because we need minutes value and more accuracy.
98-
let hoursCount = 0
99-
for (; hoursCount * oneHour <= absoluteDiff; hoursCount++) {}
100-
101-
if (hoursCount * oneHour > absoluteDiff) {
102-
hoursCount--;
103-
}
104-
105-
let minutesCount = Math.round((absoluteDiff - hoursCount * oneHour) / oneMinute);
106-
if(minutesCount >= 60) {
107-
hoursCount++;
108-
minutesCount -= 60;
109-
}
110-
111-
let timeString = '';
112-
if (hoursCount > 0) {
113-
timeString += timeWithPostfix('hour', hoursCount);
114-
}
115-
116-
if (minutesCount > 0) {
117-
if (timeString.length !== 0) {
118-
timeString += ' ';
119-
}
120-
121-
timeString += timeWithPostfix('minute', minutesCount);
122-
}
123-
124-
return timeString;
70+
function humanFriendlyExpirationTime(endTime) {
71+
const month = [
72+
'January',
73+
'February',
74+
'March',
75+
'April',
76+
'May',
77+
'June',
78+
'July',
79+
'August',
80+
'September',
81+
'October',
82+
'November',
83+
'December'
84+
];
85+
86+
return `${month[endTime.getUTCMonth()]} ${endTime.getUTCDate()}, ${endTime.getUTCFullYear()}`
12587
}
12688

12789
function notifyLicenseExpiration(ctx, endDate) {
@@ -131,26 +93,19 @@ function notifyLicenseExpiration(ctx, endDate) {
13193
}
13294

13395
const currentDate = new Date();
134-
const licenseEndTime = new Date(endDate);
135-
136-
if (licenseEndTime < currentDate) {
137-
ctx.logger.warn(`notifyLicenseExpiration(): expiration date(${licenseEndTime}) is lesser than current date(${currentDate})`);
138-
return;
139-
}
140-
141-
if (currentDate.getTime() >= licenseEndTime.getTime() - cfgStartNotifyFrom) {
142-
const formattedTimeRemaining = humanFriendlyExpirationTime(ctx, licenseEndTime);
143-
let tenant = tenantManager.isDefaultTenant(ctx) ? 'server' : ctx.tenant;
144-
ctx.logger.warn('%s license expires in %s!!!', tenant, formattedTimeRemaining);
145-
notificationService.notify(ctx, notificationTypes.LICENSE_EXPIRED, [formattedTimeRemaining]);
96+
if (currentDate.getTime() >= endDate.getTime() - cfgStartNotifyFrom) {
97+
const formattedExpirationTime = humanFriendlyExpirationTime(endDate);
98+
const tenant = tenantManager.isDefaultTenant(ctx) ? 'server' : ctx.tenant;
99+
100+
if (endDate < currentDate) {
101+
ctx.logger.warn('%s license has expired!!!', tenant);
102+
notificationService.notify(ctx, notificationTypes.LICENSE_EXPIRED, [tenant]);
103+
} else {
104+
ctx.logger.warn('%s license expires on %s!!!', tenant, formattedExpirationTime);
105+
notificationService.notify(ctx, notificationTypes.LICENSE_EXPIRATION_WARNING, [tenant, formattedExpirationTime]);
106+
}
146107
}
147108
}
148109

149-
module.exports = {
150-
fixImageExifRotation,
151-
notifyLicenseExpiration
152-
};
153-
154-
if (process.env.NODE_APP_INSTANCE === 'tests') {
155-
module.exports.humanFriendlyExpirationTime = humanFriendlyExpirationTime;
156-
}
110+
module.exports.fixImageExifRotation = fixImageExifRotation;
111+
module.exports.notifyLicenseExpiration = notifyLicenseExpiration;

tests/env-setup.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ const platform = platforms[process.platform];
3939

4040
process.env.NODE_ENV = `development-${platform}`;
4141
process.env.NODE_CONFIG_DIR = '../Common/config';
42-
process.env.NODE_APP_INSTANCE = 'tests';
4342

4443
if (platform === 'mac') {
4544
process.env.DYLD_LIBRARY_PATH = '../FileConverter/bin/';

tests/unit/utilsDocService.tests.js

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)