From 522e4b058bc5ed1015cdf902e36a107490824f73 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 25 Jul 2023 14:35:39 +0200 Subject: [PATCH] fix: properly handling moving recurring charging profiles 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 | 83 +++++++++++--------- 1 file changed, 48 insertions(+), 35 deletions(-) diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index 94869574..9da406c2 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -10,13 +10,10 @@ import { addWeeks, differenceInDays, differenceInWeeks, - endOfDay, - endOfWeek, isAfter, isBefore, isWithinInterval, - startOfDay, - startOfWeek, + toDate, } from 'date-fns'; import type { ChargingStation } from './ChargingStation'; @@ -727,7 +724,6 @@ const getLimitFromChargingProfiles = ( ); continue; } - // Adjust recurring start schedule if (chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING) { prepareRecurringChargingProfile(chargingProfile, currentDate, logPrefix); } else if ( @@ -794,54 +790,71 @@ const getLimitFromChargingProfiles = ( } }; +/** + * Adjust recurring charging profile startSchedule to the current recurrency time interval if needed + * + * @param chargingProfile - + * @param currentDate - + * @param logPrefix - + */ const prepareRecurringChargingProfile = ( chargingProfile: ChargingProfile, currentDate: Date, logPrefix: string, ) => { const chargingSchedule = chargingProfile.chargingSchedule; + let recurringInterval: Interval; switch (chargingProfile.recurrencyKind) { case RecurrencyKindType.DAILY: - if (isBefore(chargingSchedule.startSchedule!, startOfDay(currentDate))) { - addDays( + recurringInterval = { + start: chargingSchedule.startSchedule!, + end: addDays(chargingSchedule.startSchedule!, 1), + }; + if ( + !isWithinInterval(currentDate, recurringInterval) && + isBefore(chargingSchedule.startSchedule!, currentDate) + ) { + chargingSchedule.startSchedule = addDays( chargingSchedule.startSchedule!, - differenceInDays(chargingSchedule.startSchedule!, endOfDay(currentDate)), + differenceInDays(chargingSchedule.startSchedule!, recurringInterval.end), ); - 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`, - ); - } + recurringInterval = { + start: chargingSchedule.startSchedule, + end: addDays(chargingSchedule.startSchedule, 1), + }; } break; case RecurrencyKindType.WEEKLY: - if (isBefore(chargingSchedule.startSchedule!, startOfWeek(currentDate))) { - addWeeks( + recurringInterval = { + start: chargingSchedule.startSchedule!, + end: addWeeks(chargingSchedule.startSchedule!, 1), + }; + if ( + !isWithinInterval(currentDate, recurringInterval) && + isBefore(chargingSchedule.startSchedule!, currentDate) + ) { + chargingSchedule.startSchedule = addWeeks( chargingSchedule.startSchedule!, - differenceInWeeks(chargingSchedule.startSchedule!, endOfWeek(currentDate)), + differenceInWeeks(chargingSchedule.startSchedule!, recurringInterval.end), ); - 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`, - ); - } + recurringInterval = { + start: chargingSchedule.startSchedule, + end: addWeeks(chargingSchedule.startSchedule, 1), + }; } break; } + if (!isWithinInterval(currentDate, recurringInterval!)) { + logger.error( + `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring ${ + chargingProfile.recurrencyKind + } charging profile id ${ + chargingProfile.chargingProfileId + } startSchedule ${chargingSchedule.startSchedule!.toISOString()} is not properly translated to current recurrency time interval [${toDate( + recurringInterval!.start, + ).toISOString()}, ${toDate(recurringInterval!.end).toISOString()}]`, + ); + } }; const getRandomSerialNumberSuffix = (params?: { -- 2.34.1