refactor: add sanity check on recurring charging profile duration
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 25 Jul 2023 14:35:03 +0000 (16:35 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 25 Jul 2023 14:35:03 +0000 (16:35 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStationUtils.ts

index 9da406c282d448b8b713dd116658433c4e291994..0d349d22518def767afb3d535ba0fb5abbd96525 100644 (file)
@@ -9,6 +9,7 @@ import {
   addSeconds,
   addWeeks,
   differenceInDays,
+  differenceInSeconds,
   differenceInWeeks,
   isAfter,
   isBefore,
@@ -810,6 +811,7 @@ const prepareRecurringChargingProfile = (
         start: chargingSchedule.startSchedule!,
         end: addDays(chargingSchedule.startSchedule!, 1),
       };
+      checkRecurringChargingProfileDuration(chargingProfile, recurringInterval, logPrefix);
       if (
         !isWithinInterval(currentDate, recurringInterval) &&
         isBefore(chargingSchedule.startSchedule!, currentDate)
@@ -829,6 +831,7 @@ const prepareRecurringChargingProfile = (
         start: chargingSchedule.startSchedule!,
         end: addWeeks(chargingSchedule.startSchedule!, 1),
       };
+      checkRecurringChargingProfileDuration(chargingProfile, recurringInterval, logPrefix);
       if (
         !isWithinInterval(currentDate, recurringInterval) &&
         isBefore(chargingSchedule.startSchedule!, currentDate)
@@ -857,6 +860,27 @@ const prepareRecurringChargingProfile = (
   }
 };
 
+const checkRecurringChargingProfileDuration = (
+  chargingProfile: ChargingProfile,
+  interval: Interval,
+  logPrefix: string,
+) => {
+  if (
+    chargingProfile.chargingSchedule.duration! > differenceInSeconds(interval.end, interval.start)
+  ) {
+    logger.warn(
+      `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Recurring ${
+        chargingProfile.chargingProfileKind
+      } charging profile id ${chargingProfile.chargingProfileId} duration ${
+        chargingProfile.chargingSchedule.duration
+      } is greater than the recurrency time interval ${differenceInSeconds(
+        interval.end,
+        interval.start,
+      )} duration`,
+    );
+  }
+};
+
 const getRandomSerialNumberSuffix = (params?: {
   randomBytesLength?: number;
   upperCase?: boolean;