From: Jérôme Benoit Date: Sun, 30 Jul 2023 10:51:19 +0000 (+0200) Subject: refactor: cleanup reservation code X-Git-Tag: v1.2.20~92 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=530e5fbb749e20d337aff18f643dc69a5b5444d6;p=e-mobility-charging-stations-simulator.git refactor: cleanup reservation code Signed-off-by: Jérôme Benoit --- 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 ); }