fix: properly translate schedule start date to current recurring time
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStationUtils.ts
index 1d1119d32bcea57b7fb19b9c54c1ea210f49f836..1dffb0377949d4dc72dff8d84e2ec1235ff7161e 100644 (file)
@@ -4,7 +4,19 @@ import { basename, dirname, join } from 'node:path';
 import { fileURLToPath } from 'node:url';
 
 import chalk from 'chalk';
-import { addSeconds, isAfter, isBefore } from 'date-fns';
+import {
+  addDays,
+  addSeconds,
+  addWeeks,
+  differenceInDays,
+  differenceInWeeks,
+  endOfDay,
+  endOfWeek,
+  isAfter,
+  isBefore,
+  startOfDay,
+  startOfWeek,
+} from 'date-fns';
 
 import type { ChargingStation } from './ChargingStation';
 import { BaseError } from '../exception';
@@ -657,10 +669,12 @@ const convertDeprecatedTemplateKey = (
 const getLimitFromChargingProfiles = (
   chargingProfiles: ChargingProfile[],
   logPrefix: string,
-): {
-  limit: number;
-  matchingChargingProfile: ChargingProfile;
-} | null => {
+):
+  | {
+      limit: number;
+      matchingChargingProfile: ChargingProfile;
+    }
+  | undefined => {
   const debugLogMsg = `${logPrefix} ${moduleName}.getLimitFromChargingProfiles: Matching charging profile found for power limitation: %j`;
   const currentDate = new Date();
   for (const chargingProfile of chargingProfiles) {
@@ -677,26 +691,27 @@ const getLimitFromChargingProfiles = (
       );
       chargingSchedule.startSchedule = convertToDate(chargingSchedule.startSchedule)!;
     }
-    // Check type (recurring) and if it is already active
-    // Adjust the daily recurring schedule
-    if (
-      chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING &&
-      chargingProfile.recurrencyKind === RecurrencyKindType.DAILY
-    ) {
-      if (isBefore(chargingSchedule.startSchedule, currentDate)) {
-        chargingSchedule.startSchedule.setFullYear(
-          currentDate.getFullYear(),
-          currentDate.getMonth(),
-          currentDate.getDate(),
-        );
-        // Check if the start of the schedule must be set to yesterday
-      } else if (isAfter(chargingSchedule.startSchedule, currentDate)) {
-        chargingSchedule.startSchedule.setDate(currentDate.getDate() - 1);
+    // 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)),
+            );
+          }
+          break;
+        case RecurrencyKindType.WEEKLY:
+          if (isBefore(chargingSchedule.startSchedule, startOfWeek(currentDate))) {
+            addWeeks(
+              chargingSchedule.startSchedule,
+              differenceInWeeks(chargingSchedule.startSchedule, endOfWeek(currentDate)),
+            );
+          }
+          break;
       }
     }
-    // if (isAfter(chargingSchedule.startSchedule, currentDate)) {
-    //   return null;
-    // }
     // Check if the charging profile is active
     if (
       isAfter(addSeconds(chargingSchedule.startSchedule, chargingSchedule.duration!), currentDate)
@@ -750,7 +765,6 @@ const getLimitFromChargingProfiles = (
       }
     }
   }
-  return null;
 };
 
 const getRandomSerialNumberSuffix = (params?: {