refactor: factor out recurring charging profile handling
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 25 Jul 2023 11:11:48 +0000 (13:11 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 25 Jul 2023 11:11:48 +0000 (13:11 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStationUtils.ts
src/utils/index.ts

index 939c7b8e433da90eb7f8b561a021306acb0cabb6..948695744b303e04f5564b83abedb0fe7b95ff8c 100644 (file)
@@ -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;
index 4d5a38acfe4c35ca977ab64b5139fd8602518028..cdc98711a6b14afdff5c2a1048bdc7d936794185 100644 (file)
@@ -46,6 +46,7 @@ export {
   isNotEmptyString,
   isNullOrUndefined,
   isUndefined,
+  isValidDate,
   logPrefix,
   promiseWithTimeout,
   roundTo,