import { fileURLToPath } from 'node:url';
import chalk from 'chalk';
-import { addSeconds, isAfter, isTomorrow, isYesterday } 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';
);
chargingSchedule.startSchedule = convertToDate(chargingSchedule.startSchedule)!;
}
- // Adjust the daily recurring schedule to today
- if (
- chargingProfile.chargingProfileKind === ChargingProfileKindType.RECURRING &&
- chargingProfile.recurrencyKind === RecurrencyKindType.DAILY
- ) {
- if (isYesterday(chargingSchedule.startSchedule)) {
- chargingSchedule.startSchedule.setFullYear(
- currentDate.getFullYear(),
- currentDate.getMonth(),
- currentDate.getDate(),
- );
- } else if (isTomorrow(chargingSchedule.startSchedule)) {
- 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;
}
}
// Check if the charging profile is active