From 76dab5a927a69adf1a540ff597a2a2cfa8cdaf7f Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 25 Jul 2023 13:11:48 +0200 Subject: [PATCH] refactor: factor out recurring charging profile handling MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStationUtils.ts | 103 ++++++++++--------- src/utils/index.ts | 1 + 2 files changed, 57 insertions(+), 47 deletions(-) diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index 939c7b8e..94869574 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -57,10 +57,10 @@ import { isNotEmptyString, isNullOrUndefined, isUndefined, + isValidDate, logger, secureRandom, } from '../utils'; -import { isValidDate } from '../utils/Utils'; const moduleName = 'ChargingStationUtils'; @@ -690,11 +690,11 @@ const getLimitFromChargingProfiles = ( const connectorStatus = chargingStation.getConnectorStatus(connectorId); for (const chargingProfile of chargingProfiles) { if ( - chargingProfile.validFrom && - chargingProfile.validTo && + isValidDate(chargingProfile.validFrom) && + isValidDate(chargingProfile.validTo) && !isWithinInterval(currentDate, { - start: chargingProfile.validFrom, - end: chargingProfile.validTo, + start: chargingProfile.validFrom!, + end: chargingProfile.validTo!, }) ) { logger.debug( @@ -729,48 +729,7 @@ const getLimitFromChargingProfiles = ( } // Adjust recurring start schedule if (chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING) { - switch (chargingProfile.recurrencyKind) { - case RecurrencyKindType.DAILY: - if (isBefore(chargingSchedule.startSchedule, startOfDay(currentDate))) { - addDays( - chargingSchedule.startSchedule, - differenceInDays(chargingSchedule.startSchedule, endOfDay(currentDate)), - ); - if ( - isBefore(chargingSchedule.startSchedule, startOfDay(currentDate)) || - isAfter(chargingSchedule.startSchedule, endOfDay(currentDate)) - ) { - logger.error( - `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring ${ - chargingProfile.recurrencyKind - } charging profile id ${ - chargingProfile.chargingProfileId - } startSchedule ${chargingSchedule.startSchedule.toISOString()} is not properly translated to the current day`, - ); - } - } - break; - case RecurrencyKindType.WEEKLY: - if (isBefore(chargingSchedule.startSchedule, startOfWeek(currentDate))) { - addWeeks( - chargingSchedule.startSchedule, - differenceInWeeks(chargingSchedule.startSchedule, endOfWeek(currentDate)), - ); - if ( - isBefore(chargingSchedule.startSchedule, startOfWeek(currentDate)) || - isAfter(chargingSchedule.startSchedule, endOfWeek(currentDate)) - ) { - logger.error( - `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring ${ - chargingProfile.recurrencyKind - } charging profile id ${ - chargingProfile.chargingProfileId - } startSchedule ${chargingSchedule.startSchedule.toISOString()} is not properly translated to the current week`, - ); - } - } - break; - } + prepareRecurringChargingProfile(chargingProfile, currentDate, logPrefix); } else if ( chargingProfile.chargingProfileKind === ChargingProfileKindType.RELATIVE && connectorStatus?.transactionStarted @@ -835,6 +794,56 @@ const getLimitFromChargingProfiles = ( } }; +const prepareRecurringChargingProfile = ( + chargingProfile: ChargingProfile, + currentDate: Date, + logPrefix: string, +) => { + const chargingSchedule = chargingProfile.chargingSchedule; + switch (chargingProfile.recurrencyKind) { + case RecurrencyKindType.DAILY: + if (isBefore(chargingSchedule.startSchedule!, startOfDay(currentDate))) { + addDays( + chargingSchedule.startSchedule!, + differenceInDays(chargingSchedule.startSchedule!, endOfDay(currentDate)), + ); + if ( + isBefore(chargingSchedule.startSchedule!, startOfDay(currentDate)) || + isAfter(chargingSchedule.startSchedule!, endOfDay(currentDate)) + ) { + logger.error( + `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring ${ + chargingProfile.recurrencyKind + } charging profile id ${ + chargingProfile.chargingProfileId + } startSchedule ${chargingSchedule.startSchedule!.toISOString()} is not properly translated to the current day`, + ); + } + } + break; + case RecurrencyKindType.WEEKLY: + if (isBefore(chargingSchedule.startSchedule!, startOfWeek(currentDate))) { + addWeeks( + chargingSchedule.startSchedule!, + differenceInWeeks(chargingSchedule.startSchedule!, endOfWeek(currentDate)), + ); + if ( + isBefore(chargingSchedule.startSchedule!, startOfWeek(currentDate)) || + isAfter(chargingSchedule.startSchedule!, endOfWeek(currentDate)) + ) { + logger.error( + `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring ${ + chargingProfile.recurrencyKind + } charging profile id ${ + chargingProfile.chargingProfileId + } startSchedule ${chargingSchedule.startSchedule!.toISOString()} is not properly translated to the current week`, + ); + } + } + break; + } +}; + const getRandomSerialNumberSuffix = (params?: { randomBytesLength?: number; upperCase?: boolean; diff --git a/src/utils/index.ts b/src/utils/index.ts index 4d5a38ac..cdc98711 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -46,6 +46,7 @@ export { isNotEmptyString, isNullOrUndefined, isUndefined, + isValidDate, logPrefix, promiseWithTimeout, roundTo, -- 2.34.1