+};
+
+/**
+ * Adjust recurring charging profile startSchedule to the current recurrency time interval if needed
+ *
+ * @param chargingProfile -
+ * @param currentDate -
+ * @param logPrefix -
+ */
+const prepareRecurringChargingProfile = (
+ chargingProfile: ChargingProfile,
+ currentDate: Date,
+ logPrefix: string,
+) => {
+ const chargingSchedule = chargingProfile.chargingSchedule;
+ let recurringInterval: Interval;
+ switch (chargingProfile.recurrencyKind) {
+ case RecurrencyKindType.DAILY:
+ recurringInterval = {
+ start: chargingSchedule.startSchedule!,
+ end: addDays(chargingSchedule.startSchedule!, 1),
+ };
+ checkRecurringChargingProfileDuration(chargingProfile, recurringInterval, logPrefix);
+ if (
+ !isWithinInterval(currentDate, recurringInterval) &&
+ isBefore(recurringInterval.end, currentDate)
+ ) {
+ chargingSchedule.startSchedule = addDays(
+ recurringInterval.start,
+ differenceInDays(currentDate, recurringInterval.start),
+ );
+ recurringInterval = {
+ start: chargingSchedule.startSchedule,
+ end: addDays(chargingSchedule.startSchedule, 1),
+ };
+ }
+ break;
+ case RecurrencyKindType.WEEKLY:
+ recurringInterval = {
+ start: chargingSchedule.startSchedule!,
+ end: addWeeks(chargingSchedule.startSchedule!, 1),
+ };
+ checkRecurringChargingProfileDuration(chargingProfile, recurringInterval, logPrefix);
+ if (
+ !isWithinInterval(currentDate, recurringInterval) &&
+ isBefore(recurringInterval.end, currentDate)
+ ) {
+ chargingSchedule.startSchedule = addWeeks(
+ recurringInterval.start,
+ differenceInWeeks(currentDate, recurringInterval.start),
+ );
+ recurringInterval = {
+ start: chargingSchedule.startSchedule,
+ end: addWeeks(chargingSchedule.startSchedule, 1),
+ };
+ }
+ break;
+ }
+ if (!isWithinInterval(currentDate, recurringInterval!)) {
+ logger.error(
+ `${logPrefix} ${moduleName}.prepareRecurringChargingProfile: Recurring ${
+ chargingProfile.recurrencyKind
+ } charging profile id ${chargingProfile.chargingProfileId} recurrency time interval [${toDate(
+ recurringInterval!.start,
+ ).toISOString()}, ${toDate(
+ recurringInterval!.end,
+ ).toISOString()}] is not properly translated to current date ${currentDate.toISOString()} `,
+ );
+ }
+};
+
+const checkRecurringChargingProfileDuration = (
+ chargingProfile: ChargingProfile,
+ interval: Interval,
+ logPrefix: string,
+) => {
+ 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(
+ `${logPrefix} ${moduleName}.checkRecurringChargingProfileDuration: Recurring ${
+ chargingProfile.chargingProfileKind
+ } charging profile id ${chargingProfile.chargingProfileId} duration ${
+ chargingProfile.chargingSchedule.duration
+ } is greater than the recurrency time interval duration ${differenceInSeconds(
+ interval.end,
+ interval.start,
+ )}`,
+ );
+ chargingProfile.chargingSchedule.duration = differenceInSeconds(interval.end, interval.start);
+ }