From d400ecfe9f4c25b8e31e7f717b2d2f50e8cfd76e Mon Sep 17 00:00:00 2001 From: Ghareeb Falazi Date: Sun, 24 Nov 2024 22:50:58 +0100 Subject: [PATCH] Fix flight manager --- .../basic-flight-booking-manager.js | 29 ++++++++++++++----- .../booking-list.js | 4 +++ lib/basic-flight-booking-manager/booking.js | 4 +++ .../client-balance-list.js | 9 +++++- .../client-balance.js | 8 ++++- .../flight-list.js | 4 +++ 6 files changed, 48 insertions(+), 10 deletions(-) diff --git a/lib/basic-flight-booking-manager/basic-flight-booking-manager.js b/lib/basic-flight-booking-manager/basic-flight-booking-manager.js index 285a0fb..12e95fb 100644 --- a/lib/basic-flight-booking-manager/basic-flight-booking-manager.js +++ b/lib/basic-flight-booking-manager/basic-flight-booking-manager.js @@ -20,6 +20,14 @@ class BasicFlightBookingManager extends Contract { return new BasicFlightBookingManagerContext(); } + /** + * + * @param {BasicFlightBookingManagerContext} ctx + */ + async InitLedger(ctx) { + await this.createFlight(ctx, "FLY-1", 200, 500); + } + /** * * @param {BasicFlightBookingManagerContext} ctx @@ -101,8 +109,6 @@ class BasicFlightBookingManager extends Contract { return -1; } - - /** * * @param {BasicFlightBookingManagerContext} ctx @@ -127,6 +133,10 @@ class BasicFlightBookingManager extends Contract { return booking != null && booking != undefined; } + /** + * + * @param {BasicFlightBookingManagerContext} ctx + */ async queryClientBalance(ctx) { let client = ctx.clientIdentity.getID(); let clientBalance = await ctx.clientBalances.getClientBalance(client); @@ -214,16 +224,19 @@ class BasicFlightBookingManager extends Contract { async bookSeat(ctx, flightId, seatNumber) { let clientId = ctx.clientIdentity.getID(); let isSeatAvailable = await this.isSeatAvailable(ctx, flightId, seatNumber); - let hasReservation = await this.hasReservation(ctx, flightId, clientId); let balance = await this.queryClientBalance(ctx); let price = await this.querySeatPrice(ctx, flightId); - if(isSeatAvailable && !hasReservation && balance >= price) { - await this._deductFromClientBalance(ctx, price); - let booking = Booking.createInstance(clientId, flightId, seatNumber); - await ctx.bookingList.addBooking(booking); + if(isSeatAvailable) { + if (balance >= price) { + await this._deductFromClientBalance(ctx, price); + let booking = Booking.createInstance(clientId, flightId, seatNumber); + await ctx.bookingList.addBooking(booking); + } else { + throw new Error('Cannot reserve the seat: insufficient client balance!'); + } } else { - throw new Error('Cannot reserve the seat!'); + throw new Error('Cannot reserve the seat: the seat is unavailable!'); } } diff --git a/lib/basic-flight-booking-manager/booking-list.js b/lib/basic-flight-booking-manager/booking-list.js index 322c83c..27725cf 100644 --- a/lib/basic-flight-booking-manager/booking-list.js +++ b/lib/basic-flight-booking-manager/booking-list.js @@ -4,6 +4,7 @@ 'use strict'; +const crypto = require('crypto'); const StateList = require('../../ledger-api/statelist'); const Booking = require('./booking'); @@ -44,6 +45,9 @@ class BookingList extends StateList { * @returns {Promise} */ async getBooking(clientId, flightId) { + clientId = crypto.createHash('sha256') + .update(clientId) + .digest('hex'); let key = Booking.makeKey([clientId, flightId]); if (this.cache.has(key)) { return this.cache.get(key); diff --git a/lib/basic-flight-booking-manager/booking.js b/lib/basic-flight-booking-manager/booking.js index abd513a..5414de7 100644 --- a/lib/basic-flight-booking-manager/booking.js +++ b/lib/basic-flight-booking-manager/booking.js @@ -5,6 +5,7 @@ 'use strict'; const State = require('../../ledger-api/state'); +const crypto = require('crypto'); class Booking extends State { @@ -71,6 +72,9 @@ class Booking extends State { * @param {number} seatNumber */ static createInstance(clientId, flightId, seatNumber) { + clientId = crypto.createHash('sha256') + .update(clientId) + .digest('hex'); return new Booking({ id: Booking.makeKey([clientId, flightId]), clientId: clientId, flightId: flightId, seatNumber: seatNumber}); } diff --git a/lib/basic-flight-booking-manager/client-balance-list.js b/lib/basic-flight-booking-manager/client-balance-list.js index 2a68868..651165e 100644 --- a/lib/basic-flight-booking-manager/client-balance-list.js +++ b/lib/basic-flight-booking-manager/client-balance-list.js @@ -4,6 +4,7 @@ 'use strict'; +const crypto = require('crypto'); const StateList = require('../../ledger-api/statelist'); const ClientBalance = require('./client-balance'); @@ -43,6 +44,12 @@ class ClientBalanceList extends StateList { * @returns {Promise} */ async getClientBalance(client) { + client = crypto.createHash('sha256') + .update(client) + .digest('hex'); + + console.log("client=" + client); + if (this.cache.has(client)) { return this.cache.get(client); } @@ -50,7 +57,7 @@ class ClientBalanceList extends StateList { let key = ClientBalance.makeKey([client]); let result = await this.getState(key); - return result ? ClientBalance.fromBuffer(result) : null; + return result ? ClientBalance.fromBuffer(result) : ClientBalance.createInstance(client, "2000"); } /** diff --git a/lib/basic-flight-booking-manager/client-balance.js b/lib/basic-flight-booking-manager/client-balance.js index 4729e78..5f447fd 100644 --- a/lib/basic-flight-booking-manager/client-balance.js +++ b/lib/basic-flight-booking-manager/client-balance.js @@ -4,6 +4,9 @@ 'use strict'; + + +const crypto = require('crypto'); const State = require('../../ledger-api/state'); class ClientBalance extends State { @@ -67,7 +70,10 @@ class ClientBalance extends State { * @param {String} balance */ static createInstance(client, balance) { - return new ClientBalance({ client: client, balance: balance }); + const hash = crypto.createHash('sha256') + .update(client) + .digest('hex'); + return new ClientBalance({ client: hash, balance: balance }); } } diff --git a/lib/basic-flight-booking-manager/flight-list.js b/lib/basic-flight-booking-manager/flight-list.js index 3cc4a2e..643783b 100644 --- a/lib/basic-flight-booking-manager/flight-list.js +++ b/lib/basic-flight-booking-manager/flight-list.js @@ -23,6 +23,7 @@ class FlightList extends StateList { * @returns {Promise} */ async addFlight(element) { + console.debug("adding flight with id: " + element.getId()); await this.addState(element); this.cache.set(element.getId(), element); } @@ -43,12 +44,15 @@ class FlightList extends StateList { * @returns {Promise} */ async getFlight(id) { + console.debug("getFlight(" + id + ")"); + if (this.cache.has(id)) { return this.cache.get(id); } let key = Flight.makeKey([id]); let result = await this.getState(key); + console.debug("get Flight result = " + result ? Flight.fromBuffer(result) : "null"); return result ? Flight.fromBuffer(result) : null; }