refactor: cleanup reservation check condition
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStation.ts
index aad6a5370617841e4f7480e2c90bd9daebd52e9c..7eef4100355d0113f23848f72bac8c26c9b38acc 100644 (file)
@@ -26,20 +26,6 @@ import {
   getConfigurationKey,
   setConfigurationKeyValue,
 } from './ConfigurationKeyUtils';
-import { IdTagsCache } from './IdTagsCache';
-import {
-  OCPP16IncomingRequestService,
-  OCPP16RequestService,
-  OCPP16ResponseService,
-  OCPP16ServiceUtils,
-  OCPP20IncomingRequestService,
-  OCPP20RequestService,
-  OCPP20ResponseService,
-  type OCPPIncomingRequestService,
-  type OCPPRequestService,
-  OCPPServiceUtils,
-} from './ocpp';
-import { SharedLRUCache } from './SharedLRUCache';
 import {
   buildConnectorsMap,
   checkConnectorsConfiguration,
@@ -62,7 +48,21 @@ import {
   propagateSerialNumber,
   stationTemplateToStationInfo,
   warnTemplateKeysDeprecation,
-} from './Utils';
+} from './Helpers';
+import { IdTagsCache } from './IdTagsCache';
+import {
+  OCPP16IncomingRequestService,
+  OCPP16RequestService,
+  OCPP16ResponseService,
+  OCPP16ServiceUtils,
+  OCPP20IncomingRequestService,
+  OCPP20RequestService,
+  OCPP20ResponseService,
+  type OCPPIncomingRequestService,
+  type OCPPRequestService,
+  OCPPServiceUtils,
+} from './ocpp';
+import { SharedLRUCache } from './SharedLRUCache';
 import { BaseError, OCPPError } from '../exception';
 import { PerformanceStatistics } from '../performance';
 import {
@@ -104,7 +104,7 @@ import {
   RegistrationStatusEnumType,
   RequestCommand,
   type Reservation,
-  ReservationFilterKey,
+  type ReservationFilterKey,
   ReservationTerminationReason,
   type Response,
   StandardParametersKey,
@@ -936,15 +936,15 @@ export class ChargingStation {
     );
   }
 
-  public getReservationOnConnectorId0Enabled(): boolean {
+  public getReserveConnectorZeroSupported(): boolean {
     return convertToBoolean(
       getConfigurationKey(this, StandardParametersKey.ReserveConnectorZeroSupported)!.value,
     );
   }
 
   public async addReservation(reservation: Reservation): Promise<void> {
-    const [exists, reservationFound] = this.doesReservationExists(reservation);
-    if (exists) {
+    const reservationFound = this.getReservationBy('reservationId', reservation.reservationId);
+    if (!isUndefined(reservationFound)) {
       await this.removeReservation(
         reservationFound!,
         ReservationTerminationReason.REPLACE_EXISTING,
@@ -994,30 +994,20 @@ export class ChargingStation {
     if (this.hasEvses) {
       for (const evseStatus of this.evses.values()) {
         for (const connectorStatus of evseStatus.connectors.values()) {
-          if (connectorStatus?.reservation?.[filterKey as keyof Reservation] === value) {
+          if (connectorStatus?.reservation?.[filterKey] === value) {
             return connectorStatus.reservation;
           }
         }
       }
     } else {
       for (const connectorStatus of this.connectors.values()) {
-        if (connectorStatus?.reservation?.[filterKey as keyof Reservation] === value) {
+        if (connectorStatus?.reservation?.[filterKey] === value) {
           return connectorStatus.reservation;
         }
       }
     }
   }
 
-  public doesReservationExists(
-    reservation: Partial<Reservation>,
-  ): [boolean, Reservation | undefined] {
-    const foundReservation = this.getReservationBy(
-      ReservationFilterKey.RESERVATION_ID,
-      reservation.reservationId!,
-    );
-    return isUndefined(foundReservation) ? [false, undefined] : [true, foundReservation];
-  }
-
   public startReservationExpirationSetInterval(customInterval?: number): void {
     const interval =
       customInterval ?? Constants.DEFAULT_RESERVATION_EXPIRATION_OBSERVATION_INTERVAL;
@@ -1065,27 +1055,24 @@ export class ChargingStation {
     this.startReservationExpirationSetInterval();
   }
 
-  public validateIncomingRequestWithReservation(connectorId: number, idTag: string): boolean {
-    return this.getReservationBy(ReservationFilterKey.CONNECTOR_ID, connectorId)?.idTag === idTag;
-  }
-
   public isConnectorReservable(
     reservationId: number,
     idTag?: string,
     connectorId?: number,
   ): boolean {
-    const [alreadyExists] = this.doesReservationExists({ id: reservationId });
-    if (alreadyExists) {
-      return alreadyExists;
+    const reservationExists = !isUndefined(this.getReservationBy('reservationId', reservationId));
+    if (arguments.length === 1) {
+      return !reservationExists;
+    } else if (arguments.length > 1) {
+      const userReservationExists =
+        !isUndefined(idTag) && isUndefined(this.getReservationBy('idTag', idTag!)) ? false : true;
+      const notConnectorZero = isUndefined(connectorId) ? true : connectorId! > 0;
+      const freeConnectorsAvailable = this.getNumberOfReservableConnectors() > 0;
+      return (
+        !reservationExists && !userReservationExists && notConnectorZero && freeConnectorsAvailable
+      );
     }
-    const userReservedAlready = isUndefined(
-      this.getReservationBy(ReservationFilterKey.ID_TAG, idTag!),
-    )
-      ? false
-      : true;
-    const notConnectorZero = isUndefined(connectorId) ? true : connectorId! > 0;
-    const freeConnectorsAvailable = this.getNumberOfReservableConnectors() > 0;
-    return !alreadyExists && !userReservedAlready && notConnectorZero && freeConnectorsAvailable;
+    return false;
   }
 
   private getNumberOfReservableConnectors(): number {