mts-booking-library 1.1.10 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -52,6 +52,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
52
52
  };
53
53
  Object.defineProperty(exports, "__esModule", { value: true });
54
54
  exports.JourneyBooking = void 0;
55
+ var ErrorResponse_1 = require("../types/ErrorResponse");
55
56
  var booking_1 = require("./booking");
56
57
  var JourneyBooking = /** @class */ (function (_super) {
57
58
  __extends(JourneyBooking, _super);
@@ -90,7 +91,14 @@ var JourneyBooking = /** @class */ (function (_super) {
90
91
  if (cartDate > new Date()) {
91
92
  _this.cart = cart;
92
93
  _this.bookingDueDate = cartDate; // See Booking class
93
- _this.cartStatus = cart.status;
94
+ // Fill the booking process status
95
+ _this.bookingStepsToStatus = new Map(Object.entries(cart.stepsToStatus).map(function (_a) {
96
+ var bookingStep = _a[0], values = _a[1];
97
+ return [
98
+ bookingStep,
99
+ values
100
+ ];
101
+ }));
94
102
  _this.createCartTimer(cartDate);
95
103
  }
96
104
  });
@@ -105,7 +113,7 @@ var JourneyBooking = /** @class */ (function (_super) {
105
113
  setTimeout(function () {
106
114
  localStorage.removeItem("cartId");
107
115
  // Put actions that have to be done when timer expires here
108
- _this.cartStatus = booking_1.Booking.CartStatus.EMPTY;
116
+ _this.bookingStepsToStatus = new Map();
109
117
  _this.cart = undefined;
110
118
  _this.bookingDueDate = undefined; // See Booking class
111
119
  _this.onCartExpiration();
@@ -119,7 +127,13 @@ var JourneyBooking = /** @class */ (function (_super) {
119
127
  var url;
120
128
  return __generator(this, function (_a) {
121
129
  url = "".concat(this.config.API_ENDPOINT, "/booking/cart?").concat(new URLSearchParams({ cartId: cartId.toString() }));
122
- return [2 /*return*/, this.callGetApi(url)];
130
+ return [2 /*return*/, this.callGetApi(url).then(function (response) {
131
+ if ((0, ErrorResponse_1.objectIsMTSErrorResponse)(response)) {
132
+ localStorage.removeItem("cartId");
133
+ throw new Error("Error ".concat(response.httpStatus, " ").concat(response.message));
134
+ }
135
+ return response.cart;
136
+ })];
123
137
  });
124
138
  });
125
139
  };
@@ -132,7 +146,9 @@ var JourneyBooking = /** @class */ (function (_super) {
132
146
  var url;
133
147
  return __generator(this, function (_a) {
134
148
  url = "".concat(this.config.API_ENDPOINT, "/booking/journeys/departures?");
135
- return [2 /*return*/, this.callGetApi(url)];
149
+ return [2 /*return*/, this.callGetApi(url).then(function (response) {
150
+ return (0, ErrorResponse_1.objectIsMTSErrorResponse)(response) ? response : response.stops;
151
+ })];
136
152
  });
137
153
  });
138
154
  };
@@ -147,7 +163,9 @@ var JourneyBooking = /** @class */ (function (_super) {
147
163
  return __generator(this, function (_a) {
148
164
  searchParams = new URLSearchParams({ departureStopName: departureStopName });
149
165
  url = "".concat(this.config.API_ENDPOINT, "/booking/journeys/destinations?").concat(searchParams);
150
- return [2 /*return*/, this.callGetApi(url)];
166
+ return [2 /*return*/, this.callGetApi(url).then(function (response) {
167
+ return (0, ErrorResponse_1.objectIsMTSErrorResponse)(response) ? response : response.stops;
168
+ })];
151
169
  });
152
170
  });
153
171
  };
@@ -155,107 +173,241 @@ var JourneyBooking = /** @class */ (function (_super) {
155
173
  * This method returns the journeys that match the given parameters.
156
174
  * Note that it will always return the journeys for the one-way trip.
157
175
  * If you want to get the journeys for the round trip, you have to call this method twice (make sure to swap departureStopName and destinationStopName)
158
- * @param {JourneySearch} params an object of type {@link JourneySearch} containing the parameters of the search
176
+ * @param {JourneySearch} request an object of type {@link JourneySearch} containing the parameters of the search
159
177
  * @returns {JourneySearchResult[]} The returned journeys that match the given parameters
160
178
  */
161
- JourneyBooking.prototype.getJourneys = function (params) {
179
+ JourneyBooking.prototype.getJourneys = function (request) {
162
180
  return __awaiter(this, void 0, void 0, function () {
163
181
  var searchParams, url;
164
182
  return __generator(this, function (_a) {
165
- if (params.departureStopName === undefined || params.destinationStopName === undefined) {
183
+ if (request.departureStopName === undefined || request.destinationStopName === undefined) {
166
184
  throw Error("Fields departureStopName and destinationStopName are required");
167
185
  }
168
186
  searchParams = new URLSearchParams({
169
- departureStopName: params.departureStopName,
170
- destinationStopName: params.destinationStopName,
171
- passengersNumber: params.passengersNumber.toString(),
172
- date: params.date,
173
- roundTripDate: params.roundTripDate ? params.roundTripDate : "null",
174
- currency: params.currency,
175
- isRoundtrip: params.isRoundtrip.toString()
187
+ departureStopName: request.departureStopName,
188
+ destinationStopName: request.destinationStopName,
189
+ passengersNumber: request.passengersNumber.toString(),
190
+ date: request.date,
191
+ roundTripDate: request.roundTripDate ? request.roundTripDate : "null",
192
+ currency: request.currency,
193
+ isRoundtrip: request.isRoundtrip.toString()
176
194
  });
177
195
  url = "".concat(this.config.API_ENDPOINT, "/booking/journeys?").concat(searchParams);
178
- return [2 /*return*/, this.callGetApi(url)];
196
+ return [2 /*return*/, this.callGetApi(url).then(function (response) {
197
+ return (0, ErrorResponse_1.objectIsMTSErrorResponse)(response) ? response : response.journeys;
198
+ })];
179
199
  });
180
200
  });
181
201
  };
182
- JourneyBooking.prototype.createJourneyCart = function (tripCart) {
202
+ JourneyBooking.prototype.createJourneyCart = function (journeyCart) {
183
203
  return __awaiter(this, void 0, void 0, function () {
184
204
  var url;
185
205
  var _this = this;
186
206
  return __generator(this, function (_a) {
187
207
  url = "".concat(this.config.API_ENDPOINT, "/booking/journeys/cart");
188
- return [2 /*return*/, this.callPostApi(url, tripCart).then(function (cart) {
189
- _this.cart = cart.cart;
190
- _this.cartStatus = cart.cart.status;
191
- _this.bookingDueDate = new Date(cart.cart.bookingDueDate);
192
- _this.createCartTimer(new Date(cart.cart.bookingDueDate));
193
- return cart.cart;
208
+ return [2 /*return*/, this.callPostApi(url, journeyCart).then(function (response) {
209
+ // Check for errors
210
+ if ((0, ErrorResponse_1.objectIsMTSErrorResponse)(response)) {
211
+ return response;
212
+ }
213
+ _this.cart = response.cart;
214
+ // Save the cartId in the localStorage
215
+ localStorage.setItem("cartId", response.cart.id.toString());
216
+ // Fill the booking process status
217
+ _this.bookingStepsToStatus = new Map(Object.entries(response.cart.stepsToStatus).map(function (_a) {
218
+ var bookingStep = _a[0], values = _a[1];
219
+ return [
220
+ bookingStep,
221
+ values
222
+ ];
223
+ }));
224
+ _this.bookingDueDate = new Date(response.cart.bookingDueDate);
225
+ _this.createCartTimer(new Date(response.cart.bookingDueDate));
226
+ return response.cart;
194
227
  })];
195
228
  });
196
229
  });
197
230
  };
198
- JourneyBooking.prototype.getBusMatrix = function (tripId, departureStopId, destinationStopId) {
231
+ /**
232
+ * @description This method shall be called when the user wants to retrieve information about the buyer and the passengers.
233
+ * @returns An object of type {@link GetBuyerPassengersDetailsResponse} containing the buyer and the passengers information,
234
+ * as well as a list of the available tariffs for each trip.
235
+ */
236
+ JourneyBooking.prototype.getBuyerPassengersDetails = function () {
199
237
  return __awaiter(this, void 0, void 0, function () {
200
- var searchParams, url;
238
+ var buyerPassengersDetails, url;
201
239
  return __generator(this, function (_a) {
240
+ // First check that it is possible to call this API
241
+ if (!this.cart) {
242
+ throw Error("Cart is not initialized yet");
243
+ }
244
+ buyerPassengersDetails = this.bookingStepsToStatus.get(booking_1.Booking.BookingSteps.BUYER_PASSENGERS);
245
+ if (!buyerPassengersDetails || !buyerPassengersDetails[0]) {
246
+ throw Error("The status of the cart does not allow to call this API");
247
+ }
248
+ url = "".concat(this.config.API_ENDPOINT, "/booking/cart/").concat(this.cart.id, "/details?");
249
+ return [2 /*return*/, this.callGetApi(url).then(function (response) {
250
+ // Check for errors
251
+ if ((0, ErrorResponse_1.objectIsMTSErrorResponse)(response)) {
252
+ return response;
253
+ }
254
+ // Parse the map for the tariffs
255
+ var tripsToTariffs = new Map(Object.entries(response.tariffs).map(function (_a) {
256
+ var tripId = _a[0], tariff = _a[1];
257
+ return [
258
+ +tripId,
259
+ tariff
260
+ ];
261
+ }));
262
+ return {
263
+ buyer: response.buyer,
264
+ passengers: response.passengers,
265
+ tariffs: tripsToTariffs
266
+ };
267
+ })];
268
+ });
269
+ });
270
+ };
271
+ /**
272
+ * @description This method shall be called when the user wants to retrieve information about an esisting buyer.
273
+ * @param {string} [linkavelCardNumber=undefined] The linkavel card number of the buyer. This parameter is required if linkavelCardPhoneNumber is not set.
274
+ * A linkavelCardNumber is a string of 9 digits.
275
+ * @param {string} [linkavelCardPhoneNumber=undefined] The linkavel card phone number of the buyer. This parameter is required if linkavelCardNumber is not set.
276
+ * @returns An object of type {@link GetBuyerPassengersDetailsResponse} containing the buyer and the passengers information,
277
+ * as well as a list of the available tariffs for each trip.
278
+ */
279
+ JourneyBooking.prototype.getBuyer = function (linkavelCardNumber, linkavelCardPhoneNumber) {
280
+ return __awaiter(this, void 0, void 0, function () {
281
+ var buyerPassengersDetails, searchParams, url;
282
+ return __generator(this, function (_a) {
283
+ // First check that it is possible to call this API
284
+ if (!this.cart) {
285
+ throw Error("Cart is not initialized yet");
286
+ }
287
+ buyerPassengersDetails = this.bookingStepsToStatus.get(booking_1.Booking.BookingSteps.BUYER_PASSENGERS);
288
+ if (!buyerPassengersDetails || !buyerPassengersDetails[0]) {
289
+ throw Error("The status of the cart does not allow to call this API");
290
+ }
291
+ if (!linkavelCardNumber && !linkavelCardPhoneNumber) {
292
+ throw Error("At least one of the parameters linkavelCardNumber and linkavelCardPhoneNumber must be set");
293
+ }
202
294
  searchParams = new URLSearchParams({
203
- departureStopId: departureStopId.toString(),
204
- destinationStopId: destinationStopId.toString()
295
+ linkavelCardNumber: linkavelCardNumber !== null && linkavelCardNumber !== void 0 ? linkavelCardNumber : "",
296
+ linkavelCardPhoneNumber: linkavelCardPhoneNumber !== null && linkavelCardPhoneNumber !== void 0 ? linkavelCardPhoneNumber : "",
205
297
  });
206
- url = "".concat(this.config.API_ENDPOINT, "/booking/journeys/trips/").concat(tripId, "/seats?").concat(searchParams);
207
- return [2 /*return*/, this.callGetApi(url)];
298
+ url = "".concat(this.config.API_ENDPOINT, "/buyers?").concat(searchParams, "}");
299
+ return [2 /*return*/, this.callGetApi(url).then(function (response) {
300
+ // Check for errors
301
+ if ((0, ErrorResponse_1.objectIsMTSErrorResponse)(response)) {
302
+ return response;
303
+ }
304
+ return response.buyers[0];
305
+ })];
208
306
  });
209
307
  });
210
308
  };
211
- // TODO: Fix this
212
- JourneyBooking.prototype.getSeatsStatus = function (params) {
309
+ /**
310
+ * @description This methosd shall be called when the user wants to update the buyer and the passengers information.
311
+ * @param {EditPassengersDetailsRequest} passengersDetails The object containing the buyer and the passengers information.
312
+ * @returns An {@link ErrorResponse} object in case of error, nothing otherwise.
313
+ */
314
+ JourneyBooking.prototype.updateBuyerPassengersDetails = function (passengersDetails) {
213
315
  return __awaiter(this, void 0, void 0, function () {
214
- var searchParams, url;
316
+ var buyerPassengersDetails, url;
215
317
  return __generator(this, function (_a) {
318
+ // First check that it is possible to call this API
216
319
  if (!this.cart) {
217
320
  throw Error("Cart is not initialized yet");
218
321
  }
219
- searchParams = new URLSearchParams({ tripId: params.tripId.toString() });
220
- url = "".concat(this.config.API_ENDPOINT, "/booking/cart/").concat(this.cart.id, "/seats?").concat(searchParams);
221
- return [2 /*return*/, this.callGetApi(url)];
322
+ buyerPassengersDetails = this.bookingStepsToStatus.get(booking_1.Booking.BookingSteps.BUYER_PASSENGERS);
323
+ if (!buyerPassengersDetails || !buyerPassengersDetails[0]) {
324
+ throw Error("The status of the cart does not allow to call this API");
325
+ }
326
+ url = "".concat(this.config.API_ENDPOINT, "/booking/cart/").concat(this.cart.id, "/details");
327
+ return [2 /*return*/, this.callPostApi(url, passengersDetails).then(function (response) {
328
+ return (0, ErrorResponse_1.objectIsMTSErrorResponse)(response) ? response : response;
329
+ })];
222
330
  });
223
331
  });
224
332
  };
225
- // TODO: Fix this
226
- JourneyBooking.prototype.movePassengers = function (params) {
333
+ /**
334
+ * @description This method shall be used when the user wants to retrieve the bus matrix for a trip. The bus matrix shows
335
+ * the seats that are available and the ones that are already taken, as well as how the bus is made.
336
+ * @param {number} tripId The id of the trip for which the bus matrix should be retrieved
337
+ * @param {number} departureStopId The id of the departure stop
338
+ * @param {number} destinationStopId The id of the destination stop
339
+ * @returns An {@link ErrorResponse} object in case of error, a {@link BusMatrix} otherwise.
340
+ */
341
+ JourneyBooking.prototype.getBusMatrix = function (tripId, departureStopId, destinationStopId) {
227
342
  return __awaiter(this, void 0, void 0, function () {
228
- var url;
343
+ var seatSelectionStatus, searchParams, url;
229
344
  return __generator(this, function (_a) {
345
+ // First check that it is possible to call this API
230
346
  if (!this.cart) {
231
347
  throw Error("Cart is not initialized yet");
232
348
  }
233
- url = "".concat(this.config.API_ENDPOINT, "/booking/cart/").concat(this.cart.id, "/seats");
234
- return [2 /*return*/, this.callGetApi(url)];
349
+ seatSelectionStatus = this.bookingStepsToStatus.get(booking_1.Booking.BookingSteps.SEATS_SELECTION);
350
+ if (!seatSelectionStatus || !seatSelectionStatus[0]) {
351
+ throw Error("The status of the cart does not allow to call this API");
352
+ }
353
+ searchParams = new URLSearchParams({
354
+ departureStopId: departureStopId.toString(),
355
+ destinationStopId: destinationStopId.toString()
356
+ });
357
+ url = "".concat(this.config.API_ENDPOINT, "/booking/journeys/trips/").concat(tripId, "/seats?").concat(searchParams);
358
+ return [2 /*return*/, this.callGetApi(url).then(function (response) {
359
+ return (0, ErrorResponse_1.objectIsMTSErrorResponse)(response) ? response : response.busMatrix;
360
+ })];
235
361
  });
236
362
  });
237
363
  };
238
- JourneyBooking.prototype.getPassengersDetails = function () {
364
+ /**
365
+ * @description This method shall be called when the user wants to retrieve which seats were assigned for this booking.
366
+ * @param {number} tripId The id of the trip for which the seats should be retrieved.
367
+ * @returns An {@link ErrorResponse} object in case of error, a list of {@link BusLayoutCell} ids otherwise, representing the assigned seats.
368
+ */
369
+ JourneyBooking.prototype.getAssignedSeats = function (tripId) {
239
370
  return __awaiter(this, void 0, void 0, function () {
240
- var url;
371
+ var seatSelectionStatus, searchParams, url;
241
372
  return __generator(this, function (_a) {
373
+ // First check that it is possible to call this API
242
374
  if (!this.cart) {
243
375
  throw Error("Cart is not initialized yet");
244
376
  }
245
- url = "".concat(this.config.API_ENDPOINT, "/booking/cart/").concat(this.cart.id, "/details?");
246
- return [2 /*return*/, this.callGetApi(url)];
377
+ seatSelectionStatus = this.bookingStepsToStatus.get(booking_1.Booking.BookingSteps.SEATS_SELECTION);
378
+ if (!seatSelectionStatus || !seatSelectionStatus[0]) {
379
+ throw Error("The status of the cart does not allow to call this API");
380
+ }
381
+ searchParams = new URLSearchParams({ tripId: tripId.toString() });
382
+ url = "".concat(this.config.API_ENDPOINT, "/booking/cart/").concat(this.cart.id, "/seats?").concat(searchParams);
383
+ return [2 /*return*/, this.callGetApi(url).then(function (response) {
384
+ return (0, ErrorResponse_1.objectIsMTSErrorResponse)(response) ? response : response.assignedSeats;
385
+ })];
247
386
  });
248
387
  });
249
388
  };
250
- JourneyBooking.prototype.updatePassengersDetails = function (passengersDetails) {
389
+ /**
390
+ * @description This method shall be called when the user wants to change the assigned seats of a trip.
391
+ * @param {number} tripId The id of the trip for which the seats should be changed
392
+ * @param {number[]} newSeatsIds The ids of the new seats, as found the {@link BusMatrix}
393
+ * @returns An {@link ErrorResponse} object in case of error, nothing otherwise.
394
+ */
395
+ JourneyBooking.prototype.changeSeatsOfTrip = function (tripId, newSeatsIds) {
251
396
  return __awaiter(this, void 0, void 0, function () {
252
- var url;
397
+ var seatSelectionStatus, url;
253
398
  return __generator(this, function (_a) {
399
+ // First check that it is possible to call this API
254
400
  if (!this.cart) {
255
401
  throw Error("Cart is not initialized yet");
256
402
  }
257
- url = "".concat(this.config.API_ENDPOINT, "/booking/cart/").concat(this.cart.id, "/details");
258
- return [2 /*return*/, this.callPostApi(url, passengersDetails)];
403
+ seatSelectionStatus = this.bookingStepsToStatus.get(booking_1.Booking.BookingSteps.SEATS_SELECTION);
404
+ if (!seatSelectionStatus || !seatSelectionStatus[0]) {
405
+ throw Error("The status of the cart does not allow to call this API");
406
+ }
407
+ url = "".concat(this.config.API_ENDPOINT, "/booking/cart/").concat(this.cart.id, "/seats");
408
+ return [2 /*return*/, this.callPostApi(url, { tripId: tripId, seatsIds: newSeatsIds }).then(function (response) {
409
+ return (0, ErrorResponse_1.objectIsMTSErrorResponse)(response) ? response : response;
410
+ })];
259
411
  });
260
412
  });
261
413
  };
@@ -1,4 +1,5 @@
1
1
  import { MTSEnvs } from "../config";
2
+ import { ErrorResponse } from "../types/ErrorResponse";
2
3
  import { Service, ServiceTrip } from "../types/services/Service";
3
4
  import { CreateServiceCartRequest, ServiceCart } from "../types/services/ServiceCart";
4
5
  import { Booking } from "./booking";
@@ -25,7 +26,7 @@ export declare class ServiceBooking extends Booking {
25
26
  * If the sellerId is not set or it is 0, it will return the list of cities for all sellers.
26
27
  * @returns {string[]} The list of possible cities
27
28
  */
28
- getServiceCities(): Promise<string[]>;
29
+ getServiceCities(): Promise<ErrorResponse | string[]>;
29
30
  /**
30
31
  * This method returns the tours sold by this seller in the given city.
31
32
  * If the sellerId is not set or it is 0, it will return the tours in the given city for all sellers.
@@ -33,7 +34,7 @@ export declare class ServiceBooking extends Booking {
33
34
  * @param {Booking.Currencies} currency The currency in which the prices should be returned
34
35
  * @returns {Tour[]} The returned tours
35
36
  */
36
- getServices(currency: Booking.Currencies, cityName?: string): Promise<Service[]>;
37
+ getServices(currency: Booking.Currencies, cityName?: string): Promise<ErrorResponse | Service[]>;
37
38
  /**
38
39
  * This method returns the tours available for the given date. This method can be used if the seller wants the user to book a specific date
39
40
  * and/or a specific hour for the tour.
@@ -42,6 +43,6 @@ export declare class ServiceBooking extends Booking {
42
43
  * @param {Date} date The date on which to get the tours
43
44
  * @returns {ServiceTrip[]} The returned information about the tour (tripId and hour)
44
45
  */
45
- getServiceTrips(serviceId: number, date: Date): Promise<ServiceTrip[]>;
46
- createServiceCart(serviceCart: CreateServiceCartRequest): Promise<ServiceCart>;
46
+ getServiceTrips(serviceId: number, date: Date): Promise<ErrorResponse | ServiceTrip[]>;
47
+ createServiceCart(serviceCart: CreateServiceCartRequest): Promise<ErrorResponse | ServiceCart>;
47
48
  }
@@ -63,6 +63,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
63
63
  };
64
64
  Object.defineProperty(exports, "__esModule", { value: true });
65
65
  exports.ServiceBooking = void 0;
66
+ var ErrorResponse_1 = require("../types/ErrorResponse");
66
67
  var booking_1 = require("./booking");
67
68
  var ServiceBooking = /** @class */ (function (_super) {
68
69
  __extends(ServiceBooking, _super);
@@ -101,7 +102,14 @@ var ServiceBooking = /** @class */ (function (_super) {
101
102
  if (cartDate > new Date()) {
102
103
  _this.cart = cart;
103
104
  _this.bookingDueDate = cartDate; // See Booking class
104
- _this.cartStatus = cart.status;
105
+ // Fill the booking process status
106
+ _this.bookingStepsToStatus = new Map(Object.entries(cart.stepsToStatus).map(function (_a) {
107
+ var bookingStep = _a[0], values = _a[1];
108
+ return [
109
+ bookingStep,
110
+ values
111
+ ];
112
+ }));
105
113
  _this.createCartTimer(cartDate);
106
114
  }
107
115
  });
@@ -116,7 +124,6 @@ var ServiceBooking = /** @class */ (function (_super) {
116
124
  setTimeout(function () {
117
125
  localStorage.removeItem("cartId");
118
126
  // Put actions that have to be done when timer expires here
119
- _this.cartStatus = booking_1.Booking.CartStatus.EMPTY;
120
127
  _this.cart = undefined;
121
128
  _this.bookingDueDate = undefined; // See Booking class
122
129
  _this.onCartExpiration();
@@ -130,7 +137,13 @@ var ServiceBooking = /** @class */ (function (_super) {
130
137
  var url;
131
138
  return __generator(this, function (_a) {
132
139
  url = "".concat(this.config.API_ENDPOINT, "/booking/cart?").concat(new URLSearchParams({ cartId: cartId.toString() }));
133
- return [2 /*return*/, this.callGetApi(url)];
140
+ return [2 /*return*/, this.callGetApi(url).then(function (response) {
141
+ if ((0, ErrorResponse_1.objectIsMTSErrorResponse)(response)) {
142
+ localStorage.removeItem("cartId");
143
+ throw new Error("Error ".concat(response.httpStatus, " ").concat(response.message));
144
+ }
145
+ return response.cart;
146
+ })];
134
147
  });
135
148
  });
136
149
  };
@@ -144,7 +157,9 @@ var ServiceBooking = /** @class */ (function (_super) {
144
157
  var url;
145
158
  return __generator(this, function (_a) {
146
159
  url = "".concat(this.config.API_ENDPOINT, "/booking/services/cities?");
147
- return [2 /*return*/, this.callGetApi(url)];
160
+ return [2 /*return*/, this.callGetApi(url).then(function (response) {
161
+ return (0, ErrorResponse_1.objectIsMTSErrorResponse)(response) ? response : response.cities;
162
+ })];
148
163
  });
149
164
  });
150
165
  };
@@ -161,7 +176,9 @@ var ServiceBooking = /** @class */ (function (_super) {
161
176
  return __generator(this, function (_a) {
162
177
  searchParams = new URLSearchParams(__assign(__assign({}, (cityName && { cityName: cityName })), { currency: currency }));
163
178
  url = "".concat(this.config.API_ENDPOINT, "/booking/services?").concat(searchParams);
164
- return [2 /*return*/, this.callGetApi(url)];
179
+ return [2 /*return*/, this.callGetApi(url).then(function (response) {
180
+ return (0, ErrorResponse_1.objectIsMTSErrorResponse)(response) ? response : response.services;
181
+ })];
165
182
  });
166
183
  });
167
184
  };
@@ -179,7 +196,9 @@ var ServiceBooking = /** @class */ (function (_super) {
179
196
  return __generator(this, function (_a) {
180
197
  searchParams = new URLSearchParams({ date: date.toDateString() });
181
198
  url = "".concat(this.config.API_ENDPOINT, "/booking/services/").concat(serviceId, "/trips?").concat(searchParams);
182
- return [2 /*return*/, this.callGetApi(url)];
199
+ return [2 /*return*/, this.callGetApi(url).then(function (response) {
200
+ return (0, ErrorResponse_1.objectIsMTSErrorResponse)(response) ? response : response.trips;
201
+ })];
183
202
  });
184
203
  });
185
204
  };
@@ -189,12 +208,26 @@ var ServiceBooking = /** @class */ (function (_super) {
189
208
  var _this = this;
190
209
  return __generator(this, function (_a) {
191
210
  url = "".concat(this.config.API_ENDPOINT, "/booking/services/cart");
192
- return [2 /*return*/, this.callPostApi(url, serviceCart).then(function (cart) {
193
- _this.cart = cart.cart;
194
- _this.cartStatus = cart.cart.status;
195
- _this.bookingDueDate = new Date(cart.cart.bookingDueDate);
196
- _this.createCartTimer(new Date(cart.cart.bookingDueDate));
197
- return cart.cart;
211
+ return [2 /*return*/, this.callPostApi(url, serviceCart).then(function (response) {
212
+ // Check for errors
213
+ if ((0, ErrorResponse_1.objectIsMTSErrorResponse)(response)) {
214
+ return response;
215
+ }
216
+ // Save the local data
217
+ _this.cart = response.cart;
218
+ // Save the cartId in the localStorage
219
+ localStorage.setItem("cartId", response.cart.id.toString());
220
+ // Fill the booking process status
221
+ _this.bookingStepsToStatus = new Map(Object.entries(response.cart.stepsToStatus).map(function (_a) {
222
+ var bookingStep = _a[0], values = _a[1];
223
+ return [
224
+ bookingStep,
225
+ values
226
+ ];
227
+ }));
228
+ _this.bookingDueDate = new Date(response.cart.bookingDueDate);
229
+ _this.createCartTimer(new Date(response.cart.bookingDueDate));
230
+ return response.cart;
198
231
  })];
199
232
  });
200
233
  });
@@ -1,5 +1,7 @@
1
1
  import { MTSEnvs } from "../config";
2
+ import { ErrorResponse } from "../types/ErrorResponse";
2
3
  import { Cart } from "../types/common/Cart";
4
+ import { EditPassengersDetailsRequest, GetBuyerPassengersDetailsResponse, Person } from "../types/common/Person";
3
5
  import { GetSubscriptionAvailabilityRequest, GetSubscriptionAvailabilityResponse } from "../types/subscriptions/SubscriptionAvailabilities";
4
6
  import { CreateSubscriptionCartRequest, SubscriptionCart } from "../types/subscriptions/SubscriptionCart";
5
7
  import { Subscription } from "../types/subscriptions/Subscriptions";
@@ -26,13 +28,13 @@ export declare class SubscriptionBooking extends Booking {
26
28
  * This method returns the possible departures for all subscriptions sold by this seller.
27
29
  * @returns {string[]} The list of possible departures
28
30
  */
29
- getSubscriptionsDepartures(): Promise<string[]>;
31
+ getSubscriptionsDepartures(): Promise<ErrorResponse | string[]>;
30
32
  /**
31
33
  * This method returns the possible destination for the subscriptions sold by this seller that start at the selected departure.
32
34
  * @param {string} departureStopName The departure stop name (as returned by {@link getSubscriptionsDepartures})
33
35
  * @returns {string[]} The list of possible destinations
34
36
  */
35
- getSubscriptionsDestinations(departureStopName: string): Promise<string[]>;
37
+ getSubscriptionsDestinations(departureStopName: string): Promise<ErrorResponse | string[]>;
36
38
  /**
37
39
  * This method returns the possible validity types for the subscriptions sold by this seller between the selected departure and destination.
38
40
  * The validity type is the duration of the subscription (1 week, 1 month, etc.). See {@link ValidityTypes} for more details.
@@ -40,7 +42,7 @@ export declare class SubscriptionBooking extends Booking {
40
42
  * @param {string} destinationStopName The destination stop name (as returned by {@link getSubscriptionsDstinations})
41
43
  * @returns {ValidityTypes[]} The list of possible validity types
42
44
  */
43
- getSubscriptionsValidityTypes(departureStopName: string, destinationStopName: string): Promise<SubscriptionBooking.ValidityTypes[]>;
45
+ getSubscriptionsValidityTypes(departureStopName: string, destinationStopName: string): Promise<ErrorResponse | SubscriptionBooking.ValidityTypes[]>;
44
46
  /**
45
47
  * This method returns the subscriptions that match the given parameters.
46
48
  * Note that it will always return the subscription for the one-way trip.
@@ -50,20 +52,41 @@ export declare class SubscriptionBooking extends Booking {
50
52
  * @param {string} validityType The validity type (as returned by {@link getSubscriptionsValidityTypes})
51
53
  * @returns {Array<Subscription>} The subscriptions that match the given parameters
52
54
  */
53
- getSubscriptions(departureStopName: string, destinationStopName: string, validityType: string): Promise<Subscription[]>;
55
+ getSubscriptions(departureStopName: string, destinationStopName: string, validityType: string): Promise<ErrorResponse | Subscription[]>;
54
56
  /**
55
57
  * This method returns the availability of the subscription for the given parameters.
56
58
  * Note that, contrary for the previous method, you should make one single request for both the outbound and return trip.
57
59
  * @param {GetSubscriptionAvailabilityRequest} request the request object containing the parameters for the availability check
58
60
  * @returns {GetSubscriptionAvailabilityResponse} The availability of the subscription for the given parameters
59
61
  */
60
- getSubscriptionAvailabilities(request: GetSubscriptionAvailabilityRequest): Promise<GetSubscriptionAvailabilityResponse>;
62
+ getSubscriptionAvailabilities(request: GetSubscriptionAvailabilityRequest): Promise<ErrorResponse | GetSubscriptionAvailabilityResponse>;
61
63
  /**
62
64
  * This method creates a subscription cart for the given parameters.
63
65
  * @param {CreateSubscriptionCartRequest} request the request object containing the parameters for the availability check
64
- * @returns
66
+ * @returns {SubscriptionCart} The created cart
65
67
  */
66
- createSubscriptionCart(request: CreateSubscriptionCartRequest): Promise<SubscriptionCart>;
68
+ createSubscriptionCart(request: CreateSubscriptionCartRequest): Promise<ErrorResponse | SubscriptionCart>;
69
+ /**
70
+ * @description This method shall be called when the user wants to retrieve information about the buyer and the passengers.
71
+ * @returns An object of type {@link GetBuyerPassengersDetailsResponse} containing the buyer and the passengers information,
72
+ * as well as a list of the available tariffs for each trip.
73
+ */
74
+ getBuyerPassengersDetails(): Promise<ErrorResponse | GetBuyerPassengersDetailsResponse>;
75
+ /**
76
+ * @description This method shall be called when the user wants to retrieve information about an esisting buyer.
77
+ * @param {string} [linkavelCardNumber=undefined] The linkavel card number of the buyer. This parameter is required if linkavelCardPhoneNumber is not set.
78
+ * A linkavelCardNumber is a string of 9 digits.
79
+ * @param {string} [linkavelCardPhoneNumber=undefined] The linkavel card phone number of the buyer. This parameter is required if linkavelCardNumber is not set.
80
+ * @returns An object of type {@link GetBuyerPassengersDetailsResponse} containing the buyer and the passengers information,
81
+ * as well as a list of the available tariffs for each trip.
82
+ */
83
+ getBuyer(linkavelCardNumber?: string, linkavelCardPhoneNumber?: string): Promise<ErrorResponse | Person>;
84
+ /**
85
+ * @description This methosd shall be called when the user wants to update the buyer and the passengers information.
86
+ * @param {EditPassengersDetailsRequest} passengersDetails The object containing the buyer and the passengers information.
87
+ * @returns An {@link ErrorResponse} object in case of error, nothing otherwise.
88
+ */
89
+ updateBuyerPassengersDetails(passengersDetails: EditPassengersDetailsRequest): Promise<ErrorResponse | any>;
67
90
  }
68
91
  export declare namespace SubscriptionBooking {
69
92
  enum ValidityTypes {