mts-booking-library 1.2.10 → 1.2.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -61,9 +61,20 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
61
61
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
62
62
  }
63
63
  };
64
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
65
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
66
+ if (ar || !(i in from)) {
67
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
68
+ ar[i] = from[i];
69
+ }
70
+ }
71
+ return to.concat(ar || Array.prototype.slice.call(from));
72
+ };
64
73
  Object.defineProperty(exports, "__esModule", { value: true });
65
74
  exports.ServiceBooking = void 0;
66
75
  var ErrorResponse_1 = require("../types/ErrorResponse");
76
+ var Reduction_1 = require("../types/common/Reduction");
77
+ var processBookingSteps_1 = require("../utils/processBookingSteps");
67
78
  var booking_1 = require("./booking");
68
79
  var ServiceBooking = /** @class */ (function (_super) {
69
80
  __extends(ServiceBooking, _super);
@@ -106,13 +117,7 @@ var ServiceBooking = /** @class */ (function (_super) {
106
117
  _this.cartId = cart.id;
107
118
  _this.bookingDueDate = cartDate; // See Booking class
108
119
  // Fill the booking process status
109
- _this.bookingStepsToStatus = new Map(Object.entries(cart.stepsToStatus).map(function (_a) {
110
- var bookingStep = _a[0], values = _a[1];
111
- return [
112
- bookingStep,
113
- values
114
- ];
115
- }));
120
+ _this.bookingStepsToStatus = (0, processBookingSteps_1.processBookingSteps)(cart.stepsToStatus);
116
121
  _this.createCartTimer(cartDate);
117
122
  }
118
123
  })];
@@ -253,13 +258,7 @@ var ServiceBooking = /** @class */ (function (_super) {
253
258
  // Save the cartId in the localStorage
254
259
  localStorage.setItem("cartId", response.cart.id.toString());
255
260
  // Fill the booking process status
256
- _this.bookingStepsToStatus = new Map(Object.entries(response.cart.stepsToStatus).map(function (_a) {
257
- var bookingStep = _a[0], values = _a[1];
258
- return [
259
- bookingStep,
260
- values
261
- ];
262
- }));
261
+ _this.bookingStepsToStatus = (0, processBookingSteps_1.processBookingSteps)(response.cart.stepsToStatus);
263
262
  _this.bookingDueDate = new Date(response.cart.bookingDueDate);
264
263
  _this.createCartTimer(new Date(response.cart.bookingDueDate));
265
264
  return response.cart;
@@ -341,11 +340,12 @@ var ServiceBooking = /** @class */ (function (_super) {
341
340
  /**
342
341
  * @description This method shall be called when the user wants to update the buyer information.
343
342
  * @param {Person} buyerDetails The object containing the buyer information.
344
- * @returns An {@link ErrorResponse} object in case of error, nothing otherwise.
343
+ * @returns An {@link ErrorResponse} object in case of error, true otherwise.
345
344
  */
346
345
  ServiceBooking.prototype.updateBuyerPassengersDetails = function (buyerDetails) {
347
346
  return __awaiter(this, void 0, void 0, function () {
348
347
  var buyerPassengersDetails, request, url;
348
+ var _this = this;
349
349
  return __generator(this, function (_a) {
350
350
  // First check that it is possible to call this API
351
351
  if (!this.cart) {
@@ -361,11 +361,284 @@ var ServiceBooking = /** @class */ (function (_super) {
361
361
  };
362
362
  url = "".concat(this.config.API_ENDPOINT, "/booking/cart/").concat(this.cart.id, "/details");
363
363
  return [2 /*return*/, this.callPostApi(url, request).then(function (response) {
364
- return (0, ErrorResponse_1.objectIsMTSErrorResponse)(response) ? response : response;
364
+ if ((0, ErrorResponse_1.objectIsMTSErrorResponse)(response)) {
365
+ return response;
366
+ }
367
+ // Update the booking process status
368
+ _this.bookingStepsToStatus = (0, processBookingSteps_1.processBookingSteps)(response.stepsToStatus);
369
+ return true;
370
+ })];
371
+ });
372
+ });
373
+ };
374
+ //#endregion
375
+ //#region Reductions APIs
376
+ /**
377
+ * @description This method allows to add a reduction to the whole cart or to a single trip in the cart.
378
+ * @param {AddReductionRequest} request The information about the reduction to add
379
+ * @returns An {@link ErrorResponse} object in case of error, true otherwise. Also, the cart is updated.
380
+ */
381
+ ServiceBooking.prototype.addReduction = function (request) {
382
+ var _a;
383
+ return __awaiter(this, void 0, void 0, function () {
384
+ var discountsStep, _b, _c, _d, _i, step, url;
385
+ var _this = this;
386
+ return __generator(this, function (_e) {
387
+ switch (_e.label) {
388
+ case 0:
389
+ // First check that it is possible to call this API
390
+ if (!this.cartId || this.cartId === 0) {
391
+ throw Error("Cart is not initialized yet");
392
+ }
393
+ discountsStep = this.bookingStepsToStatus.get(booking_1.Booking.BookingSteps.DISCOUNTS);
394
+ if (!discountsStep)
395
+ throw Error("Booking step not found!");
396
+ if (!!discountsStep[0]) return [3 /*break*/, 5];
397
+ _b = [booking_1.Booking.BookingSteps.SEATS_SELECTION, booking_1.Booking.BookingSteps.EXTRAS];
398
+ _c = [];
399
+ for (_d in _b)
400
+ _c.push(_d);
401
+ _i = 0;
402
+ _e.label = 1;
403
+ case 1:
404
+ if (!(_i < _c.length)) return [3 /*break*/, 4];
405
+ _d = _c[_i];
406
+ if (!(_d in _b)) return [3 /*break*/, 3];
407
+ step = _d;
408
+ if (!!((_a = this.bookingStepsToStatus.get(step)) !== null && _a !== void 0 ? _a : [false])[0]) return [3 /*break*/, 3];
409
+ return [4 /*yield*/, this.markBookingStepCompleted(step)];
410
+ case 2:
411
+ _e.sent();
412
+ _e.label = 3;
413
+ case 3:
414
+ _i++;
415
+ return [3 /*break*/, 1];
416
+ case 4:
417
+ // Check again if the discounts step is accessible
418
+ discountsStep = this.bookingStepsToStatus.get(booking_1.Booking.BookingSteps.DISCOUNTS);
419
+ if (!discountsStep || !discountsStep[0]) {
420
+ throw Error("The status of the cart does not allow to call this API");
421
+ }
422
+ _e.label = 5;
423
+ case 5:
424
+ url = "".concat(this.config.API_ENDPOINT, "/booking/cart/").concat(this.cartId, "/payment/reduction");
425
+ return [2 /*return*/, this.callPostApi(url, request).then(function (response) {
426
+ var _a, _b;
427
+ if ((0, ErrorResponse_1.objectIsMTSErrorResponse)(response)) {
428
+ return response;
429
+ }
430
+ var reductionResponse = response;
431
+ // Update the booking process status
432
+ _this.bookingStepsToStatus = (0, processBookingSteps_1.processBookingSteps)(reductionResponse.stepsToStatus);
433
+ // Update the cart
434
+ _this.cart = __assign(__assign({}, _this.cart), { totalAmountToPay: reductionResponse.totalAmountToPay, reductions: __spreadArray(__spreadArray([], ((_b = (_a = _this.cart) === null || _a === void 0 ? void 0 : _a.reductions) !== null && _b !== void 0 ? _b : []), true), [reductionResponse.reduction], false) });
435
+ return true;
436
+ })];
437
+ }
438
+ });
439
+ });
440
+ };
441
+ /**
442
+ * @description This method allows to remove a reduction from the whole cart or from a single trip in the cart.
443
+ * @param tripId The id of the trip from which the reduction should be removed. If the tripId is 0, then the reduction
444
+ * will be removed from the whole cart.
445
+ * @returns An {@link ErrorResponse} object in case of error, true otherwise. Also, the cart is updated.
446
+ */
447
+ ServiceBooking.prototype.removeReduction = function (tripId) {
448
+ return __awaiter(this, void 0, void 0, function () {
449
+ var discountsStep, queryParams, url;
450
+ var _this = this;
451
+ return __generator(this, function (_a) {
452
+ // First check that it is possible to call this API
453
+ if (!this.cartId || this.cartId === 0) {
454
+ throw Error("Cart is not initialized yet");
455
+ }
456
+ discountsStep = this.bookingStepsToStatus.get(booking_1.Booking.BookingSteps.DISCOUNTS);
457
+ if (!discountsStep || !discountsStep[0]) {
458
+ throw Error("The status of the cart does not allow to call this API");
459
+ }
460
+ queryParams = new URLSearchParams({ tripId: tripId.toString() });
461
+ url = "".concat(this.config.API_ENDPOINT, "/booking/cart/").concat(this.cartId, "/payment/reduction?").concat(queryParams);
462
+ return [2 /*return*/, this.callDeleteApi(url).then(function (response) {
463
+ var _a, _b, _c;
464
+ if ((0, ErrorResponse_1.objectIsMTSErrorResponse)(response)) {
465
+ return response;
466
+ }
467
+ var reductionResponse = response;
468
+ // Update the booking process status
469
+ _this.bookingStepsToStatus = (0, processBookingSteps_1.processBookingSteps)(reductionResponse.stepsToStatus);
470
+ // Update the cart
471
+ _this.cart = __assign(__assign({}, _this.cart), { totalAmountToPay: reductionResponse.totalAmountToPay, reductions: (_c = (_b = (_a = _this.cart) === null || _a === void 0 ? void 0 : _a.reductions) === null || _b === void 0 ? void 0 : _b.filter(function (reduction) { return reduction.type === Reduction_1.ReductionType.WALLET ||
472
+ reduction.type === Reduction_1.ReductionType.VOUCHER ||
473
+ (reduction.type === Reduction_1.ReductionType.CASH && reduction.tripId !== tripId); })) !== null && _c !== void 0 ? _c : [] });
474
+ return true;
475
+ })];
476
+ });
477
+ });
478
+ };
479
+ /**
480
+ * @description This method allows the user to use the balance in his wallet to pay the cart. If the balance of
481
+ * the wallet is enough to pay the cart, the cart will be paid fully. Otherwise, the user will be asked to pay the
482
+ * remaining amount with another payment method.
483
+ * @returns An {@link ErrorResponse} object in case of error, true otherwise.
484
+ */
485
+ ServiceBooking.prototype.useWallet = function () {
486
+ var _a;
487
+ return __awaiter(this, void 0, void 0, function () {
488
+ var discountsStep, _b, _c, _d, _i, step, url;
489
+ var _this = this;
490
+ return __generator(this, function (_e) {
491
+ switch (_e.label) {
492
+ case 0:
493
+ // First check that it is possible to call this API
494
+ if (!this.cartId || this.cartId === 0) {
495
+ throw Error("Cart is not initialized yet");
496
+ }
497
+ discountsStep = this.bookingStepsToStatus.get(booking_1.Booking.BookingSteps.DISCOUNTS);
498
+ if (!discountsStep)
499
+ throw Error("Booking step not found!");
500
+ if (!!discountsStep[0]) return [3 /*break*/, 5];
501
+ _b = [booking_1.Booking.BookingSteps.SEATS_SELECTION, booking_1.Booking.BookingSteps.EXTRAS];
502
+ _c = [];
503
+ for (_d in _b)
504
+ _c.push(_d);
505
+ _i = 0;
506
+ _e.label = 1;
507
+ case 1:
508
+ if (!(_i < _c.length)) return [3 /*break*/, 4];
509
+ _d = _c[_i];
510
+ if (!(_d in _b)) return [3 /*break*/, 3];
511
+ step = _d;
512
+ if (!!((_a = this.bookingStepsToStatus.get(step)) !== null && _a !== void 0 ? _a : [false])[0]) return [3 /*break*/, 3];
513
+ return [4 /*yield*/, this.markBookingStepCompleted(step)];
514
+ case 2:
515
+ _e.sent();
516
+ _e.label = 3;
517
+ case 3:
518
+ _i++;
519
+ return [3 /*break*/, 1];
520
+ case 4:
521
+ // Check again if the discounts step is accessible
522
+ discountsStep = this.bookingStepsToStatus.get(booking_1.Booking.BookingSteps.DISCOUNTS);
523
+ if (!discountsStep || !discountsStep[0]) {
524
+ throw Error("The status of the cart does not allow to call this API");
525
+ }
526
+ _e.label = 5;
527
+ case 5:
528
+ url = "".concat(this.config.API_ENDPOINT, "/booking/cart/").concat(this.cartId, "/payment/wallet");
529
+ return [2 /*return*/, this.callPostApi(url, {}).then(function (response) {
530
+ var _a, _b;
531
+ if ((0, ErrorResponse_1.objectIsMTSErrorResponse)(response)) {
532
+ return response;
533
+ }
534
+ var reductionResponse = response;
535
+ // Update the booking process status
536
+ _this.bookingStepsToStatus = (0, processBookingSteps_1.processBookingSteps)(reductionResponse.stepsToStatus);
537
+ // Update the cart
538
+ _this.cart = __assign(__assign({}, _this.cart), { totalAmountToPay: reductionResponse.totalAmountToPay, reductions: __spreadArray(__spreadArray([], ((_b = (_a = _this.cart) === null || _a === void 0 ? void 0 : _a.reductions) !== null && _b !== void 0 ? _b : []), true), [reductionResponse.reduction], false) });
539
+ return true;
540
+ })];
541
+ }
542
+ });
543
+ });
544
+ };
545
+ /**
546
+ * @description This method allows to remove a waller reduction from the cart.
547
+ *
548
+ * @returns An {@link ErrorResponse} object in case of error, true otherwise. Also, the cart is updated.
549
+ */
550
+ ServiceBooking.prototype.removeWalletReduction = function () {
551
+ return __awaiter(this, void 0, void 0, function () {
552
+ var url;
553
+ var _this = this;
554
+ return __generator(this, function (_a) {
555
+ // First check that it is possible to call this API
556
+ if (!this.cartId || this.cartId === 0) {
557
+ throw Error("Cart is not initialized yet");
558
+ }
559
+ url = "".concat(this.config.API_ENDPOINT, "/booking/cart/").concat(this.cartId, "/payment/wallet");
560
+ return [2 /*return*/, this.callDeleteApi(url).then(function (response) {
561
+ var _a, _b, _c;
562
+ if ((0, ErrorResponse_1.objectIsMTSErrorResponse)(response)) {
563
+ return response;
564
+ }
565
+ var reductionResponse = response;
566
+ // Update the booking process status
567
+ _this.bookingStepsToStatus = (0, processBookingSteps_1.processBookingSteps)(reductionResponse.stepsToStatus);
568
+ // Update the cart
569
+ _this.cart = __assign(__assign({}, _this.cart), { totalAmountToPay: reductionResponse.totalAmountToPay, reductions: (_c = (_b = (_a = _this.cart) === null || _a === void 0 ? void 0 : _a.reductions) === null || _b === void 0 ? void 0 : _b.filter(function (reduction) { return reduction.type !== Reduction_1.ReductionType.WALLET; })) !== null && _c !== void 0 ? _c : [] });
570
+ return true;
365
571
  })];
366
572
  });
367
573
  });
368
574
  };
575
+ /**
576
+ * @description This method allows the user to use a voucher to pay the cart.
577
+ * If the balance of the value of the voucher is enough to pay the cart, the cart will be paid fully.
578
+ * Otherwise, the user will be asked to pay the remaining amount with another payment method.
579
+ * @param {string} voucherCode The code of the voucher to use
580
+ * @returns An {@link ErrorResponse} object in case of error, true otherwise.
581
+ */
582
+ ServiceBooking.prototype.useVoucher = function (voucherCode) {
583
+ var _a;
584
+ return __awaiter(this, void 0, void 0, function () {
585
+ var discountsStep, _b, _c, _d, _i, step, url;
586
+ var _this = this;
587
+ return __generator(this, function (_e) {
588
+ switch (_e.label) {
589
+ case 0:
590
+ // First check that it is possible to call this API
591
+ if (!this.cartId || this.cartId === 0) {
592
+ throw Error("Cart is not initialized yet");
593
+ }
594
+ discountsStep = this.bookingStepsToStatus.get(booking_1.Booking.BookingSteps.DISCOUNTS);
595
+ if (!discountsStep)
596
+ throw Error("Booking step not found!");
597
+ if (!!discountsStep[0]) return [3 /*break*/, 5];
598
+ _b = [booking_1.Booking.BookingSteps.SEATS_SELECTION, booking_1.Booking.BookingSteps.EXTRAS];
599
+ _c = [];
600
+ for (_d in _b)
601
+ _c.push(_d);
602
+ _i = 0;
603
+ _e.label = 1;
604
+ case 1:
605
+ if (!(_i < _c.length)) return [3 /*break*/, 4];
606
+ _d = _c[_i];
607
+ if (!(_d in _b)) return [3 /*break*/, 3];
608
+ step = _d;
609
+ if (!!((_a = this.bookingStepsToStatus.get(step)) !== null && _a !== void 0 ? _a : [false])[0]) return [3 /*break*/, 3];
610
+ return [4 /*yield*/, this.markBookingStepCompleted(step)];
611
+ case 2:
612
+ _e.sent();
613
+ _e.label = 3;
614
+ case 3:
615
+ _i++;
616
+ return [3 /*break*/, 1];
617
+ case 4:
618
+ // Check again if the discounts step is accessible
619
+ discountsStep = this.bookingStepsToStatus.get(booking_1.Booking.BookingSteps.DISCOUNTS);
620
+ if (!discountsStep || !discountsStep[0]) {
621
+ throw Error("The status of the cart does not allow to call this API");
622
+ }
623
+ _e.label = 5;
624
+ case 5:
625
+ url = "".concat(this.config.API_ENDPOINT, "/booking/cart/").concat(this.cartId, "/payment/voucher");
626
+ return [2 /*return*/, this.callPostApi(url, { voucherCode: voucherCode }).then(function (response) {
627
+ var _a, _b;
628
+ if ((0, ErrorResponse_1.objectIsMTSErrorResponse)(response)) {
629
+ return response;
630
+ }
631
+ var reductionResponse = response;
632
+ // Update the booking process status
633
+ _this.bookingStepsToStatus = (0, processBookingSteps_1.processBookingSteps)(reductionResponse.stepsToStatus);
634
+ // Update the cart
635
+ _this.cart = __assign(__assign({}, _this.cart), { totalAmountToPay: reductionResponse.totalAmountToPay, reductions: __spreadArray(__spreadArray([], ((_b = (_a = _this.cart) === null || _a === void 0 ? void 0 : _a.reductions) !== null && _b !== void 0 ? _b : []), true), [reductionResponse.reduction], false) });
636
+ return true;
637
+ })];
638
+ }
639
+ });
640
+ });
641
+ };
369
642
  return ServiceBooking;
370
643
  }(booking_1.Booking));
371
644
  exports.ServiceBooking = ServiceBooking;
@@ -1,6 +1,7 @@
1
1
  import { MTSEnvs } from "../config";
2
2
  import { ErrorResponse } from "../types/ErrorResponse";
3
3
  import { EditPassengersDetailsRequest, GetBuyerPassengersDetailsResponse, Person } from "../types/common/Person";
4
+ import { AddReductionRequest } from "../types/common/Reduction";
4
5
  import { GetSubscriptionAvailabilityRequest, GetSubscriptionAvailabilityResponse } from "../types/subscriptions/SubscriptionAvailabilities";
5
6
  import { CreateSubscriptionCartRequest, SubscriptionCart } from "../types/subscriptions/SubscriptionCart";
6
7
  import { Subscription } from "../types/subscriptions/Subscriptions";
@@ -90,9 +91,43 @@ export declare class SubscriptionBooking extends Booking {
90
91
  /**
91
92
  * @description This methosd shall be called when the user wants to update the buyer and the passengers information.
92
93
  * @param {EditPassengersDetailsRequest} passengersDetails The object containing the buyer and the passengers information.
93
- * @returns An {@link ErrorResponse} object in case of error, nothing otherwise.
94
+ * @returns An {@link ErrorResponse} object in case of error, true otherwise.
94
95
  */
95
- updateBuyerPassengersDetails(passengersDetails: EditPassengersDetailsRequest): Promise<ErrorResponse | any>;
96
+ updateBuyerPassengersDetails(passengersDetails: EditPassengersDetailsRequest): Promise<ErrorResponse | boolean>;
97
+ /**
98
+ * @description This method allows to add a reduction to the whole cart or to a single trip in the cart.
99
+ * @param {AddReductionRequest} request The information about the reduction to add
100
+ * @returns An {@link ErrorResponse} object in case of error, true otherwise. Also, the cart is updated.
101
+ */
102
+ addReduction(request: AddReductionRequest): Promise<ErrorResponse | boolean>;
103
+ /**
104
+ * @description This method allows to remove a reduction from the whole cart or from a single trip in the cart.
105
+ * @param tripId The id of the trip from which the reduction should be removed. If the tripId is 0, then the reduction
106
+ * will be removed from the whole cart.
107
+ * @returns An {@link ErrorResponse} object in case of error, true otherwise. Also, the cart is updated.
108
+ */
109
+ removeReduction(tripId: number): Promise<ErrorResponse | boolean>;
110
+ /**
111
+ * @description This method allows the user to use the balance in his wallet to pay the cart. If the balance of
112
+ * the wallet is enough to pay the cart, the cart will be paid fully. Otherwise, the user will be asked to pay the
113
+ * remaining amount with another payment method.
114
+ * @returns An {@link ErrorResponse} object in case of error, true otherwise.
115
+ */
116
+ useWallet(): Promise<ErrorResponse | boolean>;
117
+ /**
118
+ * @description This method allows to remove a waller reduction from the cart.
119
+ *
120
+ * @returns An {@link ErrorResponse} object in case of error, true otherwise. Also, the cart is updated.
121
+ */
122
+ removeWalletReduction(): Promise<ErrorResponse | boolean>;
123
+ /**
124
+ * @description This method allows the user to use a voucher to pay the cart.
125
+ * If the balance of the value of the voucher is enough to pay the cart, the cart will be paid fully.
126
+ * Otherwise, the user will be asked to pay the remaining amount with another payment method.
127
+ * @param {string} voucherCode The code of the voucher to use
128
+ * @returns An {@link ErrorResponse} object in case of error, true otherwise.
129
+ */
130
+ useVoucher(voucherCode: string): Promise<ErrorResponse | boolean>;
96
131
  }
97
132
  export declare namespace SubscriptionBooking {
98
133
  const createBooking: (env: MTSEnvs, sub_key: string, onCartExpiration: () => void, debug: boolean, language: string, access_token?: string, sellerId?: number, resellerId?: number) => Promise<SubscriptionBooking>;