From 530e5fbb749e20d337aff18f643dc69a5b5444d6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 30 Jul 2023 12:51:19 +0200 Subject: [PATCH] refactor: cleanup reservation code MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 56adcc9c..e7288851 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -947,8 +947,11 @@ export class ChargingStation { ReservationFilterKey.RESERVATION_ID, reservation.reservationId, ); - if (reservationFound) { - await this.removeReservation(reservationFound, ReservationTerminationReason.REPLACE_EXISTING); + if (!isUndefined(reservationFound)) { + await this.removeReservation( + reservationFound!, + ReservationTerminationReason.REPLACE_EXISTING, + ); } this.getConnectorStatus(reservation.connectorId)!.reservation = reservation; await OCPPServiceUtils.sendAndSetConnectorStatus( @@ -1064,18 +1067,17 @@ export class ChargingStation { idTag?: string, connectorId?: number, ): boolean { - const reservation = this.getReservationBy(ReservationFilterKey.RESERVATION_ID, reservationId); - const userReservedAlready = + const reservationExists = !isUndefined( + this.getReservationBy(ReservationFilterKey.RESERVATION_ID, reservationId), + ); + const userReservationExists = !isUndefined(idTag) && isUndefined(this.getReservationBy(ReservationFilterKey.ID_TAG, idTag!)) ? false : true; const notConnectorZero = isUndefined(connectorId) ? true : connectorId! > 0; const freeConnectorsAvailable = this.getNumberOfReservableConnectors() > 0; return ( - isUndefined(reservation) && - !userReservedAlready && - notConnectorZero && - freeConnectorsAvailable + !reservationExists && !userReservationExists && notConnectorZero && freeConnectorsAvailable ); } -- 2.34.1