mts-booking-library 1.2.25 → 1.2.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,12 +21,10 @@ export declare abstract class Booking {
21
21
  bookingStepsToStatus: Map<string, boolean[]>;
22
22
  cartId: number | undefined;
23
23
  bookingDueDate: Date | undefined;
24
- onCartExpiration: () => void;
25
24
  /**
26
25
  * This is the constructor of the Booking class.
27
26
  * @param {string} env The environment in which the app is running. Can be "DEV", "STAGING" or "PROD"
28
27
  * @param {string} sub_key The subscription key for using the APIs
29
- * @param {() => void} onCartExpiration A callback function that will be called when the cart expires
30
28
  * @param {Booking.BookingTypes} bookingType The type of booking. Can be "MLP", "SERVICE" or "MLP_SUBSCRIPTION"
31
29
  * @param {boolean} [debug=false] If true, the app will run in debug mode (meaning that will print requests and responses in the console)
32
30
  * @param {string} [language=Booking.Languages.EN] The language in which the app is running. Can be "it", "en", "fr", "de" or "es"
@@ -34,7 +32,7 @@ export declare abstract class Booking {
34
32
  * @param {number} [sellerId=undefined] The id of the seller. If not set, it will return the results for all sellers
35
33
  * @param {number} [resellerId=undefined] The id of the reseller.
36
34
  */
37
- constructor(env: MTSEnvs, sub_key: string, onCartExpiration: () => void, bookingType: Booking.BookingTypes, debug?: boolean, language?: string, access_token?: string, sellerId?: number, resellerId?: number);
35
+ constructor(env: MTSEnvs, sub_key: string, bookingType: Booking.BookingTypes, debug?: boolean, language?: string, access_token?: string, sellerId?: number, resellerId?: number);
38
36
  /**
39
37
  * This method allows to update the sellerId. It is mostly important for Linkavel, as we start with sellerId = 0
40
38
  * but then it changes to the actual sellerId once the cart is created.
@@ -60,6 +58,7 @@ export declare abstract class Booking {
60
58
  */
61
59
  markBookingStepCompleted(bookingStep: Booking.BookingSteps): Promise<ErrorResponse | boolean>;
62
60
  abstract deleteCart(): Promise<ErrorResponse | any>;
61
+ abstract resetBooking(): void;
63
62
  abstract getBuyerPassengersDetails(): Promise<ErrorResponse | GetBuyerPassengersDetailsResponse>;
64
63
  abstract getBuyerFromLinkavelCard(linkavelCardNumber?: string, linkavelCardPhoneNumber?: string): Promise<ErrorResponse | Person>;
65
64
  abstract updateBuyerPassengersDetails(request: any): Promise<ErrorResponse | boolean>;
@@ -58,7 +58,6 @@ var Booking = /** @class */ (function () {
58
58
  * This is the constructor of the Booking class.
59
59
  * @param {string} env The environment in which the app is running. Can be "DEV", "STAGING" or "PROD"
60
60
  * @param {string} sub_key The subscription key for using the APIs
61
- * @param {() => void} onCartExpiration A callback function that will be called when the cart expires
62
61
  * @param {Booking.BookingTypes} bookingType The type of booking. Can be "MLP", "SERVICE" or "MLP_SUBSCRIPTION"
63
62
  * @param {boolean} [debug=false] If true, the app will run in debug mode (meaning that will print requests and responses in the console)
64
63
  * @param {string} [language=Booking.Languages.EN] The language in which the app is running. Can be "it", "en", "fr", "de" or "es"
@@ -66,7 +65,7 @@ var Booking = /** @class */ (function () {
66
65
  * @param {number} [sellerId=undefined] The id of the seller. If not set, it will return the results for all sellers
67
66
  * @param {number} [resellerId=undefined] The id of the reseller.
68
67
  */
69
- function Booking(env, sub_key, onCartExpiration, bookingType, debug, language, access_token, sellerId, resellerId) {
68
+ function Booking(env, sub_key, bookingType, debug, language, access_token, sellerId, resellerId) {
70
69
  if (debug === void 0) { debug = false; }
71
70
  if (language === void 0) { language = Booking.Languages.EN; }
72
71
  this.language = Booking.Languages.EN;
@@ -84,7 +83,6 @@ var Booking = /** @class */ (function () {
84
83
  this.resellerId = resellerId || undefined;
85
84
  this.config = (0, config_1.setConfig)(env, sub_key, debug, access_token);
86
85
  this.bookingType = bookingType;
87
- this.onCartExpiration = onCartExpiration;
88
86
  this.changeLanguage(language);
89
87
  }
90
88
  /**
@@ -329,7 +327,7 @@ var Booking = /** @class */ (function () {
329
327
  return [3 /*break*/, 3];
330
328
  case 6:
331
329
  // Check again if the issue step is accessible
332
- issueStep = this.bookingStepsToStatus.get(Booking.BookingSteps.DISCOUNTS);
330
+ issueStep = this.bookingStepsToStatus.get(Booking.BookingSteps.ISSUE);
333
331
  if (!issueStep || !issueStep[0]) {
334
332
  throw Error("The status of the cart does not allow to call this API");
335
333
  }
@@ -3,7 +3,7 @@ import { ErrorResponse } from "../types/ErrorResponse";
3
3
  import { EditPassengersDetailsRequest, GetBuyerPassengersDetailsResponse, Person } from "../types/common/Person";
4
4
  import { AddReductionRequest } from "../types/common/Reduction";
5
5
  import { BusMatrix } from "../types/journeys/BusMatrix";
6
- import { CreateJourneyCartRequest, JourneyCart } from "../types/journeys/JourneyCart";
6
+ import { CreateJourneyCartRequest, GetJourneyCartResponse, JourneyCart } from "../types/journeys/JourneyCart";
7
7
  import { JourneySearchRequest, JourneySearchResult } from "../types/journeys/JourneySearch";
8
8
  import { ReductionTrip } from "../types/journeys/Trip";
9
9
  import { Booking } from "./booking";
@@ -13,19 +13,17 @@ export declare class JourneyBooking extends Booking {
13
13
  * This is the constructor of the ServiceBooking class.
14
14
  * @param {MTSEnvs} env The environment in which the app is running. Can be "DEV", "STAGING" or "PROD"
15
15
  * @param {string} sub_key The subscription key for using the APIs
16
- * @param {() => void} onCartExpiration A callback function that will be called when the cart expires
17
- * @param {Booking.BookingTypes} bookingType The type of booking. Can be "MLP", "SERVICE" or "MLP_SUBSCRIPTION"
18
16
  * @param {boolean} [debug=false] If true, the app will run in debug mode (meaning that will print requests and responses in the console)
19
17
  * @param {string} [language=Booking.Languages.EN] The language in which the app is running. Can be "it", "en", "fr", "de" or "es"
20
18
  * @param {string} [access_token=undefined] The access token for calling MTS APIs
21
19
  * @param {number} [sellerId=undefined] The id of the seller. If not set, it will return the results for all sellers
22
20
  * @param {number} [resellerId=undefined] The id of the reseller.
23
21
  */
24
- constructor(env: MTSEnvs, sub_key: string, onCartExpiration: () => void, debug?: boolean, language?: string, access_token?: string, sellerId?: number, resellerId?: number);
22
+ constructor(env: MTSEnvs, sub_key: string, debug?: boolean, language?: string, access_token?: string, sellerId?: number, resellerId?: number);
25
23
  getCart(): JourneyCart | undefined;
24
+ resetBooking(): void;
26
25
  fetchAndSetCart(cartId: number): Promise<void>;
27
- private createCartTimer;
28
- fetchCart(cartId: number): Promise<JourneyCart>;
26
+ fetchCart(cartId: number): Promise<GetJourneyCartResponse>;
29
27
  /**
30
28
  * This method allows to delete a cart, thus allowing the user to exit from the booking process.
31
29
  * @returns An {@link ErrorResponse} object in case of error, nothing otherwise. Note that also in case of error, all
@@ -138,5 +136,5 @@ export declare class JourneyBooking extends Booking {
138
136
  useVoucher(voucherCode: string): Promise<ErrorResponse | boolean>;
139
137
  }
140
138
  export declare namespace JourneyBooking {
141
- const createBooking: (env: MTSEnvs, sub_key: string, onCartExpiration: () => void, debug: boolean, language: string, access_token?: string, sellerId?: number, resellerId?: number) => Promise<JourneyBooking>;
139
+ const createBooking: (env: MTSEnvs, sub_key: string, debug: boolean, language: string, access_token?: string, sellerId?: number, resellerId?: number) => Promise<JourneyBooking>;
142
140
  }
@@ -82,19 +82,17 @@ var JourneyBooking = /** @class */ (function (_super) {
82
82
  * This is the constructor of the ServiceBooking class.
83
83
  * @param {MTSEnvs} env The environment in which the app is running. Can be "DEV", "STAGING" or "PROD"
84
84
  * @param {string} sub_key The subscription key for using the APIs
85
- * @param {() => void} onCartExpiration A callback function that will be called when the cart expires
86
- * @param {Booking.BookingTypes} bookingType The type of booking. Can be "MLP", "SERVICE" or "MLP_SUBSCRIPTION"
87
85
  * @param {boolean} [debug=false] If true, the app will run in debug mode (meaning that will print requests and responses in the console)
88
86
  * @param {string} [language=Booking.Languages.EN] The language in which the app is running. Can be "it", "en", "fr", "de" or "es"
89
87
  * @param {string} [access_token=undefined] The access token for calling MTS APIs
90
88
  * @param {number} [sellerId=undefined] The id of the seller. If not set, it will return the results for all sellers
91
89
  * @param {number} [resellerId=undefined] The id of the reseller.
92
90
  */
93
- function JourneyBooking(env, sub_key, onCartExpiration, debug, language, access_token, sellerId, resellerId) {
91
+ function JourneyBooking(env, sub_key, debug, language, access_token, sellerId, resellerId) {
94
92
  if (debug === void 0) { debug = false; }
95
93
  if (language === void 0) { language = booking_1.Booking.Languages.EN; }
96
94
  // Call Booking constructor
97
- var _this = _super.call(this, env, sub_key, onCartExpiration, booking_1.Booking.BookingTypes.MLP, debug, language, access_token, sellerId, resellerId) || this;
95
+ var _this = _super.call(this, env, sub_key, booking_1.Booking.BookingTypes.MLP, debug, language, access_token, sellerId, resellerId) || this;
98
96
  // Set cartId
99
97
  var cartId = localStorage.getItem("cartId");
100
98
  if (cartId) {
@@ -105,20 +103,32 @@ var JourneyBooking = /** @class */ (function (_super) {
105
103
  JourneyBooking.prototype.getCart = function () {
106
104
  return this.cart;
107
105
  };
106
+ JourneyBooking.prototype.resetBooking = function () {
107
+ this.cart = undefined;
108
+ this.cartId = undefined;
109
+ this.bookingStepsToStatus = new Map();
110
+ this.bookingDueDate = undefined;
111
+ localStorage.removeItem("cartId");
112
+ };
108
113
  JourneyBooking.prototype.fetchAndSetCart = function (cartId) {
109
114
  return __awaiter(this, void 0, void 0, function () {
110
115
  var _this = this;
111
116
  return __generator(this, function (_a) {
112
117
  switch (_a.label) {
113
- case 0: return [4 /*yield*/, this.fetchCart(cartId).then(function (cart) {
114
- var cartDate = new Date(cart.bookingDueDate);
115
- if (cartDate > new Date()) {
116
- _this.cart = cart;
117
- _this.cartId = cart.id;
118
- _this.bookingDueDate = cartDate; // See Booking class
119
- // Fill the booking process status
120
- _this.bookingStepsToStatus = (0, processBookingSteps_1.processBookingSteps)(cart.stepsToStatus);
121
- _this.createCartTimer(cartDate);
118
+ case 0: return [4 /*yield*/, this.fetchCart(cartId).then(function (response) {
119
+ if (response.cart) {
120
+ var cartDate = new Date(response.cart.bookingDueDate);
121
+ if (cartDate > new Date()) {
122
+ _this.cart = response.cart;
123
+ _this.cartId = response.cart.id;
124
+ _this.bookingDueDate = cartDate; // See Booking class
125
+ // Fill the booking process status
126
+ _this.bookingStepsToStatus = (0, processBookingSteps_1.processBookingSteps)(response.cart.stepsToStatus);
127
+ }
128
+ else {
129
+ // This should never happen, but just in case
130
+ _this.resetBooking();
131
+ }
122
132
  }
123
133
  })];
124
134
  case 1:
@@ -128,34 +138,22 @@ var JourneyBooking = /** @class */ (function (_super) {
128
138
  });
129
139
  });
130
140
  };
131
- JourneyBooking.prototype.createCartTimer = function (expiringDate) {
132
- return __awaiter(this, void 0, void 0, function () {
133
- var _this = this;
134
- return __generator(this, function (_a) {
135
- setTimeout(function () {
136
- localStorage.removeItem("cartId");
137
- // Put actions that have to be done when timer expires here
138
- _this.bookingStepsToStatus = new Map();
139
- _this.cart = undefined;
140
- _this.cartId = undefined;
141
- _this.bookingDueDate = undefined; // See Booking class
142
- _this.onCartExpiration();
143
- }, expiringDate.valueOf() - new Date().valueOf());
144
- return [2 /*return*/];
145
- });
146
- });
147
- };
148
141
  JourneyBooking.prototype.fetchCart = function (cartId) {
149
142
  return __awaiter(this, void 0, void 0, function () {
150
143
  var url;
144
+ var _this = this;
151
145
  return __generator(this, function (_a) {
152
146
  url = "".concat(this.config.API_ENDPOINT, "/booking/cart?").concat(new URLSearchParams({ cartId: cartId.toString() }));
153
147
  return [2 /*return*/, this.callGetApi(url).then(function (response) {
154
148
  if ((0, ErrorResponse_1.objectIsMTSErrorResponse)(response)) {
155
- localStorage.removeItem("cartId");
149
+ _this.resetBooking();
156
150
  throw new Error(response);
157
151
  }
158
- return response.cart;
152
+ // Check that the cart doe not have issued tickets
153
+ if (response.hasIssuedTickets) {
154
+ _this.resetBooking();
155
+ }
156
+ return response;
159
157
  })];
160
158
  });
161
159
  });
@@ -171,17 +169,13 @@ var JourneyBooking = /** @class */ (function (_super) {
171
169
  var _this = this;
172
170
  return __generator(this, function (_a) {
173
171
  // First check that it is possible to call this API
174
- if (!this.cart) {
172
+ if (!this.cart || !this.cartId) {
175
173
  throw Error("Cart is not initialized yet");
176
174
  }
177
- url = "".concat(this.config.API_ENDPOINT, "/removeBookingFromCart");
178
- return [2 /*return*/, this.callPostApi(url, { cartId: this.cart.id }).then(function (response) {
175
+ url = "".concat(this.config.API_ENDPOINT, "/booking/carts?").concat(new URLSearchParams({ cartId: this.cartId.toString() }));
176
+ return [2 /*return*/, this.callDeleteApi(url).then(function (response) {
179
177
  // Clear all data
180
- localStorage.removeItem("cartId");
181
- _this.bookingStepsToStatus = new Map();
182
- _this.cart = undefined;
183
- _this.cartId = undefined;
184
- _this.bookingDueDate = undefined; // See Booking class
178
+ _this.resetBooking();
185
179
  return (0, ErrorResponse_1.objectIsMTSErrorResponse)(response) ? response : response;
186
180
  })];
187
181
  });
@@ -252,8 +246,7 @@ var JourneyBooking = /** @class */ (function (_super) {
252
246
  // Check for errors
253
247
  if ((0, ErrorResponse_1.objectIsMTSErrorResponse)(response)) {
254
248
  // If there was an error, reset cartId and remove it from the localStorage
255
- _this.cartId = undefined;
256
- localStorage.removeItem("cartId");
249
+ _this.resetBooking();
257
250
  return response;
258
251
  }
259
252
  _this.cart = response.cart;
@@ -263,7 +256,6 @@ var JourneyBooking = /** @class */ (function (_super) {
263
256
  // Fill the booking process status
264
257
  _this.bookingStepsToStatus = (0, processBookingSteps_1.processBookingSteps)(response.cart.stepsToStatus);
265
258
  _this.bookingDueDate = new Date(response.cart.bookingDueDate);
266
- _this.createCartTimer(new Date(response.cart.bookingDueDate));
267
259
  return response.cart;
268
260
  })];
269
261
  });
@@ -774,12 +766,12 @@ var JourneyBooking = /** @class */ (function (_super) {
774
766
  exports.JourneyBooking = JourneyBooking;
775
767
  (function (JourneyBooking) {
776
768
  var _this = this;
777
- JourneyBooking.createBooking = function (env, sub_key, onCartExpiration, debug, language, access_token, sellerId, resellerId) { return __awaiter(_this, void 0, void 0, function () {
769
+ JourneyBooking.createBooking = function (env, sub_key, debug, language, access_token, sellerId, resellerId) { return __awaiter(_this, void 0, void 0, function () {
778
770
  var booking, error_1;
779
771
  return __generator(this, function (_a) {
780
772
  switch (_a.label) {
781
773
  case 0:
782
- booking = new JourneyBooking(env, sub_key, onCartExpiration, debug, language, access_token, sellerId, resellerId);
774
+ booking = new JourneyBooking(env, sub_key, debug, language, access_token, sellerId, resellerId);
783
775
  _a.label = 1;
784
776
  case 1:
785
777
  _a.trys.push([1, 4, , 5]);
@@ -3,7 +3,7 @@ import { ErrorResponse } from "../types/ErrorResponse";
3
3
  import { GetBuyerPassengersDetailsResponse, Person } from "../types/common/Person";
4
4
  import { AddReductionRequest } from "../types/common/Reduction";
5
5
  import { Service, ServiceTrip } from "../types/services/Service";
6
- import { CreateServiceCartRequest, ServiceCart } from "../types/services/ServiceCart";
6
+ import { CreateServiceCartRequest, GetServiceCartResponse, ServiceCart } from "../types/services/ServiceCart";
7
7
  import { Booking } from "./booking";
8
8
  export declare class ServiceBooking extends Booking {
9
9
  private cart?;
@@ -11,19 +11,17 @@ export declare class ServiceBooking extends Booking {
11
11
  * This is the constructor of the ServiceBooking class.
12
12
  * @param {MTSEnvs} env The environment in which the app is running. Can be "DEV", "STAGING" or "PROD"
13
13
  * @param {string} sub_key The subscription key for using the APIs
14
- * @param {() => void} onCartExpiration A callback function that will be called when the cart expires
15
- * @param {Booking.BookingTypes} bookingType The type of booking. Can be "MLP", "SERVICE" or "MLP_SUBSCRIPTION"
16
14
  * @param {boolean} [debug=false] If true, the app will run in debug mode (meaning that will print requests and responses in the console)
17
15
  * @param {string} [language=Booking.Languages.EN] The language in which the app is running. Can be "it", "en", "fr", "de" or "es"
18
16
  * @param {string} [access_token=undefined] The access token for calling MTS APIs
19
17
  * @param {number} [sellerId=undefined] The id of the seller. If not set, it will return the results for all sellers
20
18
  * @param {number} [resellerId=undefined] The id of the reseller.
21
19
  */
22
- constructor(env: MTSEnvs, sub_key: string, onCartExpiration: () => void, debug?: boolean, language?: string, access_token?: string, sellerId?: number, resellerId?: number);
20
+ constructor(env: MTSEnvs, sub_key: string, debug?: boolean, language?: string, access_token?: string, sellerId?: number, resellerId?: number);
23
21
  getCart(): ServiceCart | undefined;
22
+ resetBooking(): void;
24
23
  fetchAndSetCart(cartId: number): Promise<void>;
25
- private createCartTimer;
26
- fetchCart(cartId: number): Promise<ServiceCart>;
24
+ fetchCart(cartId: number): Promise<GetServiceCartResponse>;
27
25
  /**
28
26
  * This method allows to delete a cart, thus allowing the user to exit from the booking process.
29
27
  * @returns An {@link ErrorResponse} object in case of error, nothing otherwise. Note that also in case of error, all
@@ -112,5 +110,5 @@ export declare class ServiceBooking extends Booking {
112
110
  useVoucher(voucherCode: string): Promise<ErrorResponse | boolean>;
113
111
  }
114
112
  export declare namespace ServiceBooking {
115
- const createBooking: (env: MTSEnvs, sub_key: string, onCartExpiration: () => void, debug: boolean, language: string, access_token?: string, sellerId?: number, resellerId?: number) => Promise<ServiceBooking>;
113
+ const createBooking: (env: MTSEnvs, sub_key: string, debug: boolean, language: string, access_token?: string, sellerId?: number, resellerId?: number) => Promise<ServiceBooking>;
116
114
  }
@@ -82,19 +82,17 @@ var ServiceBooking = /** @class */ (function (_super) {
82
82
  * This is the constructor of the ServiceBooking class.
83
83
  * @param {MTSEnvs} env The environment in which the app is running. Can be "DEV", "STAGING" or "PROD"
84
84
  * @param {string} sub_key The subscription key for using the APIs
85
- * @param {() => void} onCartExpiration A callback function that will be called when the cart expires
86
- * @param {Booking.BookingTypes} bookingType The type of booking. Can be "MLP", "SERVICE" or "MLP_SUBSCRIPTION"
87
85
  * @param {boolean} [debug=false] If true, the app will run in debug mode (meaning that will print requests and responses in the console)
88
86
  * @param {string} [language=Booking.Languages.EN] The language in which the app is running. Can be "it", "en", "fr", "de" or "es"
89
87
  * @param {string} [access_token=undefined] The access token for calling MTS APIs
90
88
  * @param {number} [sellerId=undefined] The id of the seller. If not set, it will return the results for all sellers
91
89
  * @param {number} [resellerId=undefined] The id of the reseller.
92
90
  */
93
- function ServiceBooking(env, sub_key, onCartExpiration, debug, language, access_token, sellerId, resellerId) {
91
+ function ServiceBooking(env, sub_key, debug, language, access_token, sellerId, resellerId) {
94
92
  if (debug === void 0) { debug = false; }
95
93
  if (language === void 0) { language = booking_1.Booking.Languages.EN; }
96
94
  // Call Booking constructor
97
- var _this = _super.call(this, env, sub_key, onCartExpiration, booking_1.Booking.BookingTypes.SERVICE, debug, language, access_token, sellerId, resellerId) || this;
95
+ var _this = _super.call(this, env, sub_key, booking_1.Booking.BookingTypes.SERVICE, debug, language, access_token, sellerId, resellerId) || this;
98
96
  // Set cartId
99
97
  var cartId = localStorage.getItem("cartId");
100
98
  if (cartId) {
@@ -105,20 +103,33 @@ var ServiceBooking = /** @class */ (function (_super) {
105
103
  ServiceBooking.prototype.getCart = function () {
106
104
  return this.cart;
107
105
  };
106
+ ServiceBooking.prototype.resetBooking = function () {
107
+ this.cart = undefined;
108
+ this.cartId = undefined;
109
+ this.bookingStepsToStatus = new Map();
110
+ this.bookingDueDate = undefined;
111
+ localStorage.removeItem("cartId");
112
+ };
108
113
  ServiceBooking.prototype.fetchAndSetCart = function (cartId) {
109
114
  return __awaiter(this, void 0, void 0, function () {
110
115
  var _this = this;
111
116
  return __generator(this, function (_a) {
112
117
  switch (_a.label) {
113
- case 0: return [4 /*yield*/, this.fetchCart(cartId).then(function (cart) {
114
- var cartDate = new Date(cart.bookingDueDate);
115
- if (cartDate > new Date()) {
116
- _this.cart = cart;
117
- _this.cartId = cart.id;
118
- _this.bookingDueDate = cartDate; // See Booking class
119
- // Fill the booking process status
120
- _this.bookingStepsToStatus = (0, processBookingSteps_1.processBookingSteps)(cart.stepsToStatus);
121
- _this.createCartTimer(cartDate);
118
+ case 0: return [4 /*yield*/, this.fetchCart(cartId).then(function (response) {
119
+ console.log(response);
120
+ if (response.cart) {
121
+ var cartDate = new Date(response.cart.bookingDueDate);
122
+ if (cartDate > new Date()) {
123
+ _this.cart = response.cart;
124
+ _this.cartId = response.cart.id;
125
+ _this.bookingDueDate = cartDate; // See Booking class
126
+ // Fill the booking process status
127
+ _this.bookingStepsToStatus = (0, processBookingSteps_1.processBookingSteps)(response.cart.stepsToStatus);
128
+ }
129
+ else {
130
+ // This should never happen, but just in case
131
+ _this.resetBooking();
132
+ }
122
133
  }
123
134
  })];
124
135
  case 1:
@@ -128,25 +139,10 @@ var ServiceBooking = /** @class */ (function (_super) {
128
139
  });
129
140
  });
130
141
  };
131
- ServiceBooking.prototype.createCartTimer = function (expiringDate) {
132
- return __awaiter(this, void 0, void 0, function () {
133
- var _this = this;
134
- return __generator(this, function (_a) {
135
- setTimeout(function () {
136
- localStorage.removeItem("cartId");
137
- // Put actions that have to be done when timer expires here
138
- _this.cart = undefined;
139
- _this.cartId = undefined;
140
- _this.bookingDueDate = undefined; // See Booking class
141
- _this.onCartExpiration();
142
- }, expiringDate.valueOf() - new Date().valueOf());
143
- return [2 /*return*/];
144
- });
145
- });
146
- };
147
142
  ServiceBooking.prototype.fetchCart = function (cartId) {
148
143
  return __awaiter(this, void 0, void 0, function () {
149
144
  var url;
145
+ var _this = this;
150
146
  return __generator(this, function (_a) {
151
147
  url = "".concat(this.config.API_ENDPOINT, "/booking/cart?").concat(new URLSearchParams({ cartId: cartId.toString() }));
152
148
  return [2 /*return*/, this.callGetApi(url).then(function (response) {
@@ -154,7 +150,11 @@ var ServiceBooking = /** @class */ (function (_super) {
154
150
  localStorage.removeItem("cartId");
155
151
  throw new Error(response);
156
152
  }
157
- return response.cart;
153
+ // Check that the cart doe not have issued tickets
154
+ if (response.hasIssuedTickets) {
155
+ _this.resetBooking();
156
+ }
157
+ return response;
158
158
  })];
159
159
  });
160
160
  });
@@ -170,17 +170,13 @@ var ServiceBooking = /** @class */ (function (_super) {
170
170
  var _this = this;
171
171
  return __generator(this, function (_a) {
172
172
  // First check that it is possible to call this API
173
- if (!this.cart) {
173
+ if (!this.cart || !this.cartId) {
174
174
  throw Error("Cart is not initialized yet");
175
175
  }
176
- url = "".concat(this.config.API_ENDPOINT, "/removeBookingFromCart");
177
- return [2 /*return*/, this.callPostApi(url, { cartId: this.cart.id }).then(function (response) {
176
+ url = "".concat(this.config.API_ENDPOINT, "/booking/carts?").concat(new URLSearchParams({ cartId: this.cartId.toString() }));
177
+ return [2 /*return*/, this.callDeleteApi(url).then(function (response) {
178
178
  // Clear all data
179
- localStorage.removeItem("cartId");
180
- _this.bookingStepsToStatus = new Map();
181
- _this.cart = undefined;
182
- _this.cartId = undefined;
183
- _this.bookingDueDate = undefined; // See Booking class
179
+ _this.resetBooking();
184
180
  return (0, ErrorResponse_1.objectIsMTSErrorResponse)(response) ? response : response;
185
181
  })];
186
182
  });
@@ -251,8 +247,7 @@ var ServiceBooking = /** @class */ (function (_super) {
251
247
  // Check for errors
252
248
  if ((0, ErrorResponse_1.objectIsMTSErrorResponse)(response)) {
253
249
  // If there was an error, reset cartId and remove it from the localStorage
254
- _this.cartId = undefined;
255
- localStorage.removeItem("cartId");
250
+ _this.resetBooking();
256
251
  return response;
257
252
  }
258
253
  // Save the local data
@@ -263,7 +258,6 @@ var ServiceBooking = /** @class */ (function (_super) {
263
258
  // Fill the booking process status
264
259
  _this.bookingStepsToStatus = (0, processBookingSteps_1.processBookingSteps)(response.cart.stepsToStatus);
265
260
  _this.bookingDueDate = new Date(response.cart.bookingDueDate);
266
- _this.createCartTimer(new Date(response.cart.bookingDueDate));
267
261
  return response.cart;
268
262
  })];
269
263
  });
@@ -645,12 +639,12 @@ var ServiceBooking = /** @class */ (function (_super) {
645
639
  exports.ServiceBooking = ServiceBooking;
646
640
  (function (ServiceBooking) {
647
641
  var _this = this;
648
- ServiceBooking.createBooking = function (env, sub_key, onCartExpiration, debug, language, access_token, sellerId, resellerId) { return __awaiter(_this, void 0, void 0, function () {
642
+ ServiceBooking.createBooking = function (env, sub_key, debug, language, access_token, sellerId, resellerId) { return __awaiter(_this, void 0, void 0, function () {
649
643
  var booking, error_1;
650
644
  return __generator(this, function (_a) {
651
645
  switch (_a.label) {
652
646
  case 0:
653
- booking = new ServiceBooking(env, sub_key, onCartExpiration, debug, language, access_token, sellerId, resellerId);
647
+ booking = new ServiceBooking(env, sub_key, debug, language, access_token, sellerId, resellerId);
654
648
  _a.label = 1;
655
649
  case 1:
656
650
  _a.trys.push([1, 4, , 5]);
@@ -3,7 +3,7 @@ import { ErrorResponse } from "../types/ErrorResponse";
3
3
  import { EditPassengersDetailsRequest, GetBuyerPassengersDetailsResponse, Person } from "../types/common/Person";
4
4
  import { AddReductionRequest } from "../types/common/Reduction";
5
5
  import { GetSubscriptionAvailabilityRequest, GetSubscriptionAvailabilityResponse } from "../types/subscriptions/SubscriptionAvailabilities";
6
- import { CreateSubscriptionCartRequest, SubscriptionCart } from "../types/subscriptions/SubscriptionCart";
6
+ import { CreateSubscriptionCartRequest, GetSubscriptionCartResponse, SubscriptionCart } from "../types/subscriptions/SubscriptionCart";
7
7
  import { Subscription } from "../types/subscriptions/Subscriptions";
8
8
  import { Booking } from "./booking";
9
9
  export declare class SubscriptionBooking extends Booking {
@@ -12,19 +12,17 @@ export declare class SubscriptionBooking extends Booking {
12
12
  * This is the constructor of the ServiceBooking class.
13
13
  * @param {MTSEnvs} env The environment in which the app is running. Can be "DEV", "STAGING" or "PROD"
14
14
  * @param {string} sub_key The subscription key for using the APIs
15
- * @param {() => void} onCartExpiration A callback function that will be called when the cart expires
16
- * @param {Booking.BookingTypes} bookingType The type of booking. Can be "MLP", "SERVICE" or "MLP_SUBSCRIPTION"
17
15
  * @param {boolean} [debug=false] If true, the app will run in debug mode (meaning that will print requests and responses in the console)
18
16
  * @param {string} [language=Booking.Languages.EN] The language in which the app is running. Can be "it", "en", "fr", "de" or "es"
19
17
  * @param {string} [access_token=undefined] The access token for calling MTS APIs
20
18
  * @param {number} [sellerId=undefined] The id of the seller. If not set, it will return the results for all sellers
21
19
  * @param {number} [resellerId=undefined] The id of the reseller.
22
20
  */
23
- constructor(env: MTSEnvs, sub_key: string, onCartExpiration: () => void, debug?: boolean, language?: string, access_token?: string, sellerId?: number, resellerId?: number);
21
+ constructor(env: MTSEnvs, sub_key: string, debug?: boolean, language?: string, access_token?: string, sellerId?: number, resellerId?: number);
24
22
  getCart(): SubscriptionCart | undefined;
23
+ resetBooking(): void;
25
24
  fetchAndSetCart(cartId: number): Promise<void>;
26
- private createCartTimer;
27
- fetchCart(cartId: number): Promise<SubscriptionCart>;
25
+ fetchCart(cartId: number): Promise<GetSubscriptionCartResponse>;
28
26
  /**
29
27
  * This method allows to delete a cart, thus allowing the user to exit from the booking process.
30
28
  * @returns An {@link ErrorResponse} object in case of error, nothing otherwise. Note that also in case of error, all
@@ -130,7 +128,7 @@ export declare class SubscriptionBooking extends Booking {
130
128
  useVoucher(voucherCode: string): Promise<ErrorResponse | boolean>;
131
129
  }
132
130
  export declare namespace SubscriptionBooking {
133
- const createBooking: (env: MTSEnvs, sub_key: string, onCartExpiration: () => void, debug: boolean, language: string, access_token?: string, sellerId?: number, resellerId?: number) => Promise<SubscriptionBooking>;
131
+ const createBooking: (env: MTSEnvs, sub_key: string, debug: boolean, language: string, access_token?: string, sellerId?: number, resellerId?: number) => Promise<SubscriptionBooking>;
134
132
  enum ValidityTypes {
135
133
  WORKING_WEEK = "WORKING_WEEK",
136
134
  WEEKLY = "WEEKLY",
@@ -82,19 +82,17 @@ var SubscriptionBooking = /** @class */ (function (_super) {
82
82
  * This is the constructor of the ServiceBooking class.
83
83
  * @param {MTSEnvs} env The environment in which the app is running. Can be "DEV", "STAGING" or "PROD"
84
84
  * @param {string} sub_key The subscription key for using the APIs
85
- * @param {() => void} onCartExpiration A callback function that will be called when the cart expires
86
- * @param {Booking.BookingTypes} bookingType The type of booking. Can be "MLP", "SERVICE" or "MLP_SUBSCRIPTION"
87
85
  * @param {boolean} [debug=false] If true, the app will run in debug mode (meaning that will print requests and responses in the console)
88
86
  * @param {string} [language=Booking.Languages.EN] The language in which the app is running. Can be "it", "en", "fr", "de" or "es"
89
87
  * @param {string} [access_token=undefined] The access token for calling MTS APIs
90
88
  * @param {number} [sellerId=undefined] The id of the seller. If not set, it will return the results for all sellers
91
89
  * @param {number} [resellerId=undefined] The id of the reseller.
92
90
  */
93
- function SubscriptionBooking(env, sub_key, onCartExpiration, debug, language, access_token, sellerId, resellerId) {
91
+ function SubscriptionBooking(env, sub_key, debug, language, access_token, sellerId, resellerId) {
94
92
  if (debug === void 0) { debug = false; }
95
93
  if (language === void 0) { language = booking_1.Booking.Languages.EN; }
96
94
  // Call Booking constructor
97
- var _this = _super.call(this, env, sub_key, onCartExpiration, booking_1.Booking.BookingTypes.MLP_SUBSCRIPTION, debug, language, access_token, sellerId, resellerId) || this;
95
+ var _this = _super.call(this, env, sub_key, booking_1.Booking.BookingTypes.MLP_SUBSCRIPTION, debug, language, access_token, sellerId, resellerId) || this;
98
96
  // Set cartId
99
97
  var cartId = localStorage.getItem("cartId");
100
98
  if (cartId) {
@@ -105,26 +103,32 @@ var SubscriptionBooking = /** @class */ (function (_super) {
105
103
  SubscriptionBooking.prototype.getCart = function () {
106
104
  return this.cart;
107
105
  };
106
+ SubscriptionBooking.prototype.resetBooking = function () {
107
+ this.cart = undefined;
108
+ this.cartId = undefined;
109
+ this.bookingStepsToStatus = new Map();
110
+ this.bookingDueDate = undefined;
111
+ localStorage.removeItem("cartId");
112
+ };
108
113
  SubscriptionBooking.prototype.fetchAndSetCart = function (cartId) {
109
114
  return __awaiter(this, void 0, void 0, function () {
110
115
  var _this = this;
111
116
  return __generator(this, function (_a) {
112
117
  switch (_a.label) {
113
- case 0: return [4 /*yield*/, this.fetchCart(cartId).then(function (cart) {
114
- var cartDate = new Date(cart.bookingDueDate);
115
- if (cartDate > new Date()) {
116
- _this.cart = cart;
117
- _this.cartId = cart.id;
118
- _this.bookingDueDate = cartDate; // See Booking class
119
- // Fill the booking process status
120
- _this.bookingStepsToStatus = new Map(Object.entries(cart.stepsToStatus).map(function (_a) {
121
- var bookingStep = _a[0], values = _a[1];
122
- return [
123
- bookingStep,
124
- values
125
- ];
126
- }));
127
- _this.createCartTimer(cartDate);
118
+ case 0: return [4 /*yield*/, this.fetchCart(cartId).then(function (response) {
119
+ if (response.cart) {
120
+ var cartDate = new Date(response.cart.bookingDueDate);
121
+ if (cartDate > new Date()) {
122
+ _this.cart = response.cart;
123
+ _this.cartId = response.cart.id;
124
+ _this.bookingDueDate = cartDate; // See Booking class
125
+ // Fill the booking process status
126
+ _this.bookingStepsToStatus = (0, processBookingSteps_1.processBookingSteps)(response.cart.stepsToStatus);
127
+ }
128
+ else {
129
+ // This should never happen, but just in case
130
+ _this.resetBooking();
131
+ }
128
132
  }
129
133
  })];
130
134
  case 1:
@@ -134,34 +138,22 @@ var SubscriptionBooking = /** @class */ (function (_super) {
134
138
  });
135
139
  });
136
140
  };
137
- SubscriptionBooking.prototype.createCartTimer = function (expiringDate) {
138
- return __awaiter(this, void 0, void 0, function () {
139
- var _this = this;
140
- return __generator(this, function (_a) {
141
- setTimeout(function () {
142
- localStorage.removeItem("cartId");
143
- // Put actions that have to be done when timer expires here
144
- _this.bookingStepsToStatus = new Map();
145
- _this.cart = undefined;
146
- _this.cartId = undefined;
147
- _this.bookingDueDate = undefined; // See Booking class
148
- _this.onCartExpiration();
149
- }, expiringDate.valueOf() - new Date().valueOf());
150
- return [2 /*return*/];
151
- });
152
- });
153
- };
154
141
  SubscriptionBooking.prototype.fetchCart = function (cartId) {
155
142
  return __awaiter(this, void 0, void 0, function () {
156
143
  var url;
144
+ var _this = this;
157
145
  return __generator(this, function (_a) {
158
146
  url = "".concat(this.config.API_ENDPOINT, "/booking/cart?").concat(new URLSearchParams({ cartId: cartId.toString() }));
159
147
  return [2 /*return*/, this.callGetApi(url).then(function (response) {
160
148
  if ((0, ErrorResponse_1.objectIsMTSErrorResponse)(response)) {
161
- localStorage.removeItem("cartId");
149
+ _this.resetBooking();
162
150
  throw new Error(response);
163
151
  }
164
- return response.cart;
152
+ // Check that the cart doe not have issued tickets
153
+ if (response.hasIssuedTickets) {
154
+ _this.resetBooking();
155
+ }
156
+ return response;
165
157
  })];
166
158
  });
167
159
  });
@@ -177,17 +169,12 @@ var SubscriptionBooking = /** @class */ (function (_super) {
177
169
  var _this = this;
178
170
  return __generator(this, function (_a) {
179
171
  // First check that it is possible to call this API
180
- if (!this.cart) {
172
+ if (!this.cart || !this.cartId) {
181
173
  throw Error("Cart is not initialized yet");
182
174
  }
183
- url = "".concat(this.config.API_ENDPOINT, "/removeBookingFromCart");
184
- return [2 /*return*/, this.callPostApi(url, { cartId: this.cart.id }).then(function (response) {
185
- // Clear all data
186
- localStorage.removeItem("cartId");
187
- _this.bookingStepsToStatus = new Map();
188
- _this.cart = undefined;
189
- _this.cartId = undefined;
190
- _this.bookingDueDate = undefined; // See Booking class
175
+ url = "".concat(this.config.API_ENDPOINT, "/booking/carts?").concat(new URLSearchParams({ cartId: this.cartId.toString() }));
176
+ return [2 /*return*/, this.callDeleteApi(url).then(function (response) {
177
+ _this.resetBooking();
191
178
  return (0, ErrorResponse_1.objectIsMTSErrorResponse)(response) ? response : response;
192
179
  })];
193
180
  });
@@ -314,8 +301,7 @@ var SubscriptionBooking = /** @class */ (function (_super) {
314
301
  // Check for errors
315
302
  if ((0, ErrorResponse_1.objectIsMTSErrorResponse)(response)) {
316
303
  // If there was an error, reset cartId and remove it from the localStorage
317
- _this.cartId = undefined;
318
- localStorage.removeItem("cartId");
304
+ _this.resetBooking();
319
305
  return response;
320
306
  }
321
307
  _this.cart = response.cart;
@@ -325,7 +311,6 @@ var SubscriptionBooking = /** @class */ (function (_super) {
325
311
  // Fill the booking process status
326
312
  _this.bookingStepsToStatus = (0, processBookingSteps_1.processBookingSteps)(response.cart.stepsToStatus);
327
313
  _this.bookingDueDate = new Date(response.cart.bookingDueDate);
328
- _this.createCartTimer(new Date(response.cart.bookingDueDate));
329
314
  return response.cart;
330
315
  })];
331
316
  });
@@ -728,12 +713,12 @@ var SubscriptionBooking = /** @class */ (function (_super) {
728
713
  exports.SubscriptionBooking = SubscriptionBooking;
729
714
  (function (SubscriptionBooking) {
730
715
  var _this = this;
731
- SubscriptionBooking.createBooking = function (env, sub_key, onCartExpiration, debug, language, access_token, sellerId, resellerId) { return __awaiter(_this, void 0, void 0, function () {
716
+ SubscriptionBooking.createBooking = function (env, sub_key, debug, language, access_token, sellerId, resellerId) { return __awaiter(_this, void 0, void 0, function () {
732
717
  var booking, error_1;
733
718
  return __generator(this, function (_a) {
734
719
  switch (_a.label) {
735
720
  case 0:
736
- booking = new SubscriptionBooking(env, sub_key, onCartExpiration, debug, language, access_token, sellerId, resellerId);
721
+ booking = new SubscriptionBooking(env, sub_key, debug, language, access_token, sellerId, resellerId);
737
722
  _a.label = 1;
738
723
  case 1:
739
724
  _a.trys.push([1, 4, , 5]);
package/lib/index.d.ts CHANGED
@@ -10,15 +10,15 @@ export { Reduction, AddReductionRequest } from "./types/common/Reduction";
10
10
  export { Gateway, GetSellerGatewaysResponse, GetPaymentInformationFromGatewayResponse, IssueTicketsResponse, GatewayTypes, PaymentMethods } from "./types/common/Payment";
11
11
  export { ErrorResponse, objectIsMTSErrorResponse } from "./types/ErrorResponse";
12
12
  export { BusLayoutCell, BusMatrix, BusCellTypes, SeatStatus } from "./types/journeys/BusMatrix";
13
- export { JourneyCart, JourneyBookingType, CreateJourneyCartRequest, TripBookingInfo, DEFAULT_CREATE_JOURNEY_CART } from "./types/journeys/JourneyCart";
13
+ export { JourneyCart, GetJourneyCartResponse, JourneyBookingType, CreateJourneyCartRequest, TripBookingInfo, DEFAULT_CREATE_JOURNEY_CART } from "./types/journeys/JourneyCart";
14
14
  export { JourneyInfo } from "./types/journeys/JourneyInfo";
15
15
  export { JourneySearchRequest, JourneySearchResult, DEFAULT_JOURNEY_SEARCH } from "./types/journeys/JourneySearch";
16
16
  export { Stop } from "./types/journeys/Stop";
17
17
  export { Trip, CartTrip, ReductionTrip } from "./types/journeys/Trip";
18
18
  export { Line } from "./types/services/Line";
19
19
  export { Service, ServiceTrip } from "./types/services/Service";
20
- export { ServiceCart, ServiceBookingType, CreateServiceCartRequest } from "./types/services/ServiceCart";
20
+ export { ServiceCart, GetServiceCartResponse, ServiceBookingType, CreateServiceCartRequest } from "./types/services/ServiceCart";
21
21
  export { ServiceInfo } from "./types/services/ServiceInfo";
22
22
  export { Subscription } from "./types/subscriptions/Subscriptions";
23
23
  export { GetSubscriptionAvailabilityRequest, GetSubscriptionAvailabilityResponse, SubscriptionCalendarDayPeriodInfo } from "./types/subscriptions/SubscriptionAvailabilities";
24
- export { SubscriptionCart, SubscriptionBookingType, CreateSubscriptionCartRequest, SubscriptionPeriod } from "./types/subscriptions/SubscriptionCart";
24
+ export { SubscriptionCart, GetSubscriptionCartResponse, SubscriptionBookingType, CreateSubscriptionCartRequest, SubscriptionPeriod } from "./types/subscriptions/SubscriptionCart";
@@ -9,6 +9,7 @@ import { Extra } from "./Extra";
9
9
  * - SubscriptionCart: {@link SubscriptionCart}
10
10
  * @property {number} id The id of the cart
11
11
  * @property {string} cartCode The code of the cart
12
+ * @property {number} sellerId The id of the seller of the tickets in the cart
12
13
  * @property {Map<string, boolean[]>} stepsToStatus This data structure describes the steps of the booking process.
13
14
  * The keys of this dictionary represent the booking steps. See ... for the list of possible steps. If the
14
15
  * key is present, then the booking step shall be displayed. The value is a list of 3 booleans:
@@ -17,10 +18,10 @@ import { Extra } from "./Extra";
17
18
  * - The third tells whether the section is required.
18
19
  * @property {Booking.Currencies} currency The currency of the cart. See {@link Booking.Currencies}
19
20
  * @property {string} bookingDueDate The date and time when the cart expires
20
- * @property {number} buyerId The id of the buyer
21
21
  */
22
22
  export type Cart = {
23
23
  id: number;
24
+ sellerId: number;
24
25
  cartCode: string;
25
26
  stepsToStatus: Map<string, boolean[]>;
26
27
  currency: Booking.Currencies;
@@ -35,6 +35,16 @@ export type CreateJourneyCartRequest = {
35
35
  returnJourney: TripBookingInfo[] | null;
36
36
  };
37
37
  export declare const DEFAULT_CREATE_JOURNEY_CART: CreateJourneyCartRequest;
38
+ /**
39
+ * This type represents the response of the {@link JourneyBooking.fetchCart} API.
40
+ * @property {JourneyCart} cart - The {@link JourneyCart} object representing the cart.
41
+ * @property {boolean} hasIssuedTickets - A boolean telling whether the cart has issued tickets. Note that if this is the case,
42
+ * the cart will be null because it is not possible to add tickets to an issued cart. Thus, all data should be reset.
43
+ */
44
+ export type GetJourneyCartResponse = {
45
+ cart: JourneyCart | null;
46
+ hasIssuedTickets: boolean;
47
+ };
38
48
  /**
39
49
  * @description Represents a `JourneyCart`, which extends the {@link Cart} type by including additional
40
50
  * information about the bookings
@@ -31,6 +31,16 @@ export type CreateServiceCartRequest = {
31
31
  export type ServiceCart = Cart & {
32
32
  bookings: ServiceBookingType[];
33
33
  };
34
+ /**
35
+ * This type represents the response of the {@link ServiceBooking.fetchCart} API.
36
+ * @property {JourneyCart} cart - The {@link ServiceCart} object representing the cart.
37
+ * @property {boolean} hasIssuedTickets - A boolean telling whether the cart has issued tickets. Note that if this is the case,
38
+ * the cart will be null because it is not possible to add tickets to an issued cart. Thus, all data should be reset.
39
+ */
40
+ export type GetServiceCartResponse = {
41
+ cart: ServiceCart | null;
42
+ hasIssuedTickets: boolean;
43
+ };
34
44
  /**
35
45
  * @description Represents a `ServiceBooking`, which contains information about a service booking.
36
46
  *
@@ -48,6 +48,16 @@ export type SubscriptionPeriod = {
48
48
  export type SubscriptionCart = Cart & {
49
49
  bookings: SubscriptionBookingType[];
50
50
  };
51
+ /**
52
+ * This type represents the response of the {@link SubscriptionBooking.fetchCart} API.
53
+ * @property {JourneyCart} cart - The {@link SubscriptionCart} object representing the cart.
54
+ * @property {boolean} hasIssuedTickets - A boolean telling whether the cart has issued tickets. Note that if this is the case,
55
+ * the cart will be null because it is not possible to add tickets to an issued cart. Thus, all data should be reset.
56
+ */
57
+ export type GetSubscriptionCartResponse = {
58
+ cart: SubscriptionCart | null;
59
+ hasIssuedTickets: boolean;
60
+ };
51
61
  /**
52
62
  * @description Represents a `SubscriptionBooking`, which contains information about a subscription booking.
53
63
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mts-booking-library",
3
- "version": "1.2.25",
3
+ "version": "1.2.26",
4
4
  "description": "Library for use MyTicketSolution Booking API",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",