@@ -37,7 +37,6 @@ const exifParser = require('exif-parser');
3737const Jimp = require ( 'jimp' ) ;
3838const ms = require ( 'ms' ) ;
3939
40- const utils = require ( '../../Common/sources/utils' ) ;
4140const tenantManager = require ( '../../Common/sources/tenantManager' ) ;
4241const { 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
12789function 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 ;
0 commit comments