refactor: use keyof to build the reservation filter key
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 30 Jul 2023 11:22:32 +0000 (13:22 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 30 Jul 2023 11:22:32 +0000 (13:22 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/types/index.ts
src/types/ocpp/Reservation.ts

index e728885121ed76676d6ffbbaab848f5380509671..ccc9526369c24ceae553ed2f702b310be654c7b6 100644 (file)
@@ -104,7 +104,7 @@ import {
   RegistrationStatusEnumType,
   RequestCommand,
   type Reservation,
-  ReservationFilterKey,
+  type ReservationFilterKey,
   ReservationTerminationReason,
   type Response,
   StandardParametersKey,
@@ -943,10 +943,7 @@ export class ChargingStation {
   }
 
   public async addReservation(reservation: Reservation): Promise<void> {
-    const reservationFound = this.getReservationBy(
-      ReservationFilterKey.RESERVATION_ID,
-      reservation.reservationId,
-    );
+    const reservationFound = this.getReservationBy('reservationId', reservation.reservationId);
     if (!isUndefined(reservationFound)) {
       await this.removeReservation(
         reservationFound!,
@@ -997,14 +994,14 @@ 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;
         }
       }
@@ -1059,7 +1056,7 @@ export class ChargingStation {
   }
 
   public validateIncomingRequestWithReservation(connectorId: number, idTag: string): boolean {
-    return this.getReservationBy(ReservationFilterKey.CONNECTOR_ID, connectorId)?.idTag === idTag;
+    return this.getReservationBy('connectorId', connectorId)?.idTag === idTag;
   }
 
   public isConnectorReservable(
@@ -1067,13 +1064,9 @@ export class ChargingStation {
     idTag?: string,
     connectorId?: number,
   ): boolean {
-    const reservationExists = !isUndefined(
-      this.getReservationBy(ReservationFilterKey.RESERVATION_ID, reservationId),
-    );
+    const reservationExists = !isUndefined(this.getReservationBy('reservationId', reservationId));
     const userReservationExists =
-      !isUndefined(idTag) && isUndefined(this.getReservationBy(ReservationFilterKey.ID_TAG, idTag!))
-        ? false
-        : true;
+      !isUndefined(idTag) && isUndefined(this.getReservationBy('idTag', idTag!)) ? false : true;
     const notConnectorZero = isUndefined(connectorId) ? true : connectorId! > 0;
     const freeConnectorsAvailable = this.getNumberOfReservableConnectors() > 0;
     return (
index 6e9e64cde55fd07c0ced291b86a75c424a9375a1..2659458f70401bdad6fbd7d6b250ccce6888f9cb 100644 (file)
@@ -80,7 +80,6 @@ import {
   OCPPVersion,
   type RemoteStartTransactionRequest,
   type RemoteStopTransactionRequest,
-  ReservationFilterKey,
   ReservationTerminationReason,
   type ResetRequest,
   type SetChargingProfileRequest,
@@ -886,7 +885,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
         };
         if (reserved || reservedOnConnectorZero) {
           const reservation = chargingStation.getReservationBy(
-            ReservationFilterKey.CONNECTOR_ID,
+            'connectorId',
             reservedOnConnectorZero ? 0 : transactionConnectorId,
           )!;
           startTransactionPayload.reservationId = reservation.reservationId;
@@ -1615,10 +1614,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
     }
     try {
       const { reservationId } = commandPayload;
-      const reservation = chargingStation.getReservationBy(
-        ReservationFilterKey.RESERVATION_ID,
-        reservationId,
-      );
+      const reservation = chargingStation.getReservationBy('reservationId', reservationId);
       if (isUndefined(reservation)) {
         logger.error(
           `${chargingStation.logPrefix()} Reservation with ID ${reservationId}
index f65e4caef879da22ef3f40abe9ed378f782e440f..04608aaba3289fc9910c246b2bfdf1a9d21383a4 100644 (file)
@@ -256,6 +256,6 @@ export {
 } from './WebSocket';
 export {
   type Reservation,
-  ReservationFilterKey,
+  type ReservationFilterKey,
   ReservationTerminationReason,
 } from './ocpp/Reservation';
index 0bcf101c4fce452636e03dec3ecab806776835f6..0d79375e15b826cef5e97fd60bf6a610c2c3b9d9 100644 (file)
@@ -10,10 +10,4 @@ export enum ReservationTerminationReason {
   REPLACE_EXISTING = 'ReplaceExisting',
 }
 
-export enum ReservationFilterKey {
-  RESERVATION_ID = 'reservationId',
-  ID_TAG = 'idTag',
-  PARENT_ID_TAG = 'parentIdTag',
-  CONNECTOR_ID = 'connectorId',
-  EVSE_ID = 'evseId',
-}
+export type ReservationFilterKey = keyof OCPP16ReserveNowRequest;