refactor: add type for charging profiles limit
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStationUtils.ts
index aa590b7819b197cf59d4bf2150288eada03b4d2c..b33a51083562e41668f5e441118f3da86937ae04 100644 (file)
@@ -665,24 +665,24 @@ const convertDeprecatedTemplateKey = (
   }
 };
 
+interface ChargingProfilesLimit {
+  limit: number;
+  matchingChargingProfile: ChargingProfile;
+}
+
 /**
  * Charging profiles should already be sorted by connector id and stack level (highest stack level has priority)
  *
  * @param chargingProfiles -
  * @param logPrefix -
- * @returns
+ * @returns ChargingProfilesLimit
  */
 const getLimitFromChargingProfiles = (
   chargingStation: ChargingStation,
   connectorId: number,
   chargingProfiles: ChargingProfile[],
   logPrefix: string,
-):
-  | {
-      limit: number;
-      matchingChargingProfile: ChargingProfile;
-    }
-  | undefined => {
+): ChargingProfilesLimit | undefined => {
   const debugLogMsg = `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Matching charging profile found for power limitation: %j`;
   const currentDate = new Date();
   for (const chargingProfile of chargingProfiles) {
@@ -746,10 +746,13 @@ const getLimitFromChargingProfiles = (
           }
           break;
       }
+    } else if (chargingProfile.chargingProfileKind === ChargingProfileKindType.RELATIVE) {
+      chargingSchedule.startSchedule =
+        chargingStation.getConnectorStatus(connectorId)?.transactionStart;
     }
     // Check if the charging profile is active
     if (
-      isAfter(addSeconds(chargingSchedule.startSchedule, chargingSchedule.duration!), currentDate)
+      isAfter(addSeconds(chargingSchedule.startSchedule!, chargingSchedule.duration!), currentDate)
     ) {
       let lastButOneSchedule: ChargingSchedulePeriod | undefined;
       // Search the right schedule period
@@ -769,7 +772,7 @@ const getLimitFromChargingProfiles = (
         // Find the right schedule period
         if (
           isAfter(
-            addSeconds(chargingSchedule.startSchedule, schedulePeriod.startPeriod),
+            addSeconds(chargingSchedule.startSchedule!, schedulePeriod.startPeriod),
             currentDate,
           )
         ) {