fix: ensure daily recurring charging profiles are adjusted on a day
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 24 Jul 2023 10:41:15 +0000 (12:41 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 24 Jul 2023 10:41:15 +0000 (12:41 +0200)
granularity

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStationUtils.ts

index 1d1119d32bcea57b7fb19b9c54c1ea210f49f836..7865eb1f3d14433226aad8ddd16a8db496360645 100644 (file)
@@ -4,7 +4,7 @@ import { basename, dirname, join } from 'node:path';
 import { fileURLToPath } from 'node:url';
 
 import chalk from 'chalk';
-import { addSeconds, isAfter, isBefore } from 'date-fns';
+import { addSeconds, isAfter, isTomorrow, isYesterday } from 'date-fns';
 
 import type { ChargingStation } from './ChargingStation';
 import { BaseError } from '../exception';
@@ -678,25 +678,21 @@ const getLimitFromChargingProfiles = (
       chargingSchedule.startSchedule = convertToDate(chargingSchedule.startSchedule)!;
     }
     // Check type (recurring) and if it is already active
-    // Adjust the daily recurring schedule
+    // Adjust the daily recurring schedule to today
     if (
       chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING &&
       chargingProfile.recurrencyKind === RecurrencyKindType.DAILY
     ) {
-      if (isBefore(chargingSchedule.startSchedule, currentDate)) {
+      if (isYesterday(chargingSchedule.startSchedule)) {
         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)) {
+      } else if (isTomorrow(chargingSchedule.startSchedule)) {
         chargingSchedule.startSchedule.setDate(currentDate.getDate() - 1);
       }
     }
-    // if (isAfter(chargingSchedule.startSchedule, currentDate)) {
-    //   return null;
-    // }
     // Check if the charging profile is active
     if (
       isAfter(addSeconds(chargingSchedule.startSchedule, chargingSchedule.duration!), currentDate)