fix: warn abount unsupported charging profiles structure
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStationUtils.ts
index ca4ec0b200130e00b6a80ee8dbbdc15df4358d1f..0eeb205189456c4d0ea44ec96d3cfdf434722075 100644 (file)
@@ -49,6 +49,7 @@ import {
   cloneObject,
   convertToDate,
   convertToInt,
+  isArraySorted,
   isEmptyObject,
   isEmptyString,
   isNotEmptyArray,
@@ -711,7 +712,7 @@ const getLimitFromChargingProfiles = (
       logger.warn(
         `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: startSchedule is not a Date object in charging profile id ${chargingProfile.chargingProfileId}. Trying to convert it to a Date object`,
       );
-      chargingSchedule.startSchedule = convertToDate(chargingSchedule.startSchedule)!;
+      chargingSchedule.startSchedule = convertToDate(chargingSchedule?.startSchedule)!;
     }
     if (
       chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING &&
@@ -730,16 +731,36 @@ const getLimitFromChargingProfiles = (
     ) {
       chargingSchedule.startSchedule = connectorStatus?.transactionStart;
     }
+    if (isNullOrUndefined(chargingSchedule?.duration)) {
+      logger.error(
+        `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Charging profile id ${chargingProfile.chargingProfileId} has no duration defined, not yet supported`,
+      );
+      continue;
+    }
     // Check if the charging profile is active
     if (
-      isValidDate(chargingSchedule.startSchedule) &&
+      isValidDate(chargingSchedule?.startSchedule) &&
       isWithinInterval(currentDate, {
         start: chargingSchedule.startSchedule!,
         end: addSeconds(chargingSchedule.startSchedule!, chargingSchedule.duration!),
       })
     ) {
       if (isNotEmptyArray(chargingSchedule.chargingSchedulePeriod)) {
-        chargingSchedule.chargingSchedulePeriod.sort((a, b) => a.startPeriod - b.startPeriod);
+        const chargingSchedulePeriodCompareFn = (
+          a: ChargingSchedulePeriod,
+          b: ChargingSchedulePeriod,
+        ) => a.startPeriod - b.startPeriod;
+        if (
+          isArraySorted<ChargingSchedulePeriod>(
+            chargingSchedule.chargingSchedulePeriod,
+            chargingSchedulePeriodCompareFn,
+          ) === false
+        ) {
+          logger.warn(
+            `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Charging profile id ${chargingProfile.chargingProfileId} schedule periods are not sorted by start period`,
+          );
+          chargingSchedule.chargingSchedulePeriod.sort(chargingSchedulePeriodCompareFn);
+        }
         // Check if the first schedule period start period is equal to 0
         if (chargingSchedule.chargingSchedulePeriod[0].startPeriod !== 0) {
           logger.error(
@@ -747,7 +768,7 @@ const getLimitFromChargingProfiles = (
           );
           continue;
         }
-        // Handling of only one schedule period
+        // Handle only one schedule period
         if (chargingSchedule.chargingSchedulePeriod.length === 1) {
           const result: ChargingProfilesLimit = {
             limit: chargingSchedule.chargingSchedulePeriod[0].limit,
@@ -825,10 +846,10 @@ const prepareRecurringChargingProfile = (
       checkRecurringChargingProfileDuration(chargingProfile, recurringInterval, logPrefix);
       if (
         !isWithinInterval(currentDate, recurringInterval) &&
-        isBefore(chargingSchedule.startSchedule!, currentDate)
+        isBefore(recurringInterval.end, currentDate)
       ) {
         chargingSchedule.startSchedule = addDays(
-          chargingSchedule.startSchedule!,
+          recurringInterval.start,
           differenceInDays(currentDate, recurringInterval.start),
         );
         recurringInterval = {
@@ -845,10 +866,10 @@ const prepareRecurringChargingProfile = (
       checkRecurringChargingProfileDuration(chargingProfile, recurringInterval, logPrefix);
       if (
         !isWithinInterval(currentDate, recurringInterval) &&
-        isBefore(chargingSchedule.startSchedule!, currentDate)
+        isBefore(recurringInterval.end, currentDate)
       ) {
         chargingSchedule.startSchedule = addWeeks(
-          chargingSchedule.startSchedule!,
+          recurringInterval.start,
           differenceInWeeks(currentDate, recurringInterval.start),
         );
         recurringInterval = {
@@ -862,11 +883,11 @@ const prepareRecurringChargingProfile = (
     logger.error(
       `${logPrefix} ${moduleName}.prepareRecurringChargingProfile: Recurring ${
         chargingProfile.recurrencyKind
-      } charging profile id ${
-        chargingProfile.chargingProfileId
-      } startSchedule ${chargingSchedule.startSchedule!.toISOString()} is not properly translated to current recurrency time interval [${toDate(
+      } charging profile id ${chargingProfile.chargingProfileId} recurrency time interval [${toDate(
         recurringInterval!.start,
-      ).toISOString()}, ${toDate(recurringInterval!.end).toISOString()}]`,
+      ).toISOString()}, ${toDate(
+        recurringInterval!.end,
+      ).toISOString()}] is not properly translated to current date ${currentDate.toISOString()} `,
     );
   }
 };
@@ -876,7 +897,19 @@ const checkRecurringChargingProfileDuration = (
   interval: Interval,
   logPrefix: string,
 ) => {
-  if (
+  if (isNullOrUndefined(chargingProfile.chargingSchedule.duration)) {
+    logger.warn(
+      `${logPrefix} ${moduleName}.checkRecurringChargingProfileDuration: Recurring ${
+        chargingProfile.chargingProfileKind
+      } charging profile id ${
+        chargingProfile.chargingProfileId
+      } duration is not defined, set it to the recurrency time interval duration ${differenceInSeconds(
+        interval.end,
+        interval.start,
+      )}`,
+    );
+    chargingProfile.chargingSchedule.duration = differenceInSeconds(interval.end, interval.start);
+  } else if (
     chargingProfile.chargingSchedule.duration! > differenceInSeconds(interval.end, interval.start)
   ) {
     logger.warn(