mts-booking-library 1.2.11 → 1.2.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,7 @@
1
1
  import { MTSEnvs } from "../config";
2
2
  import { ErrorResponse } from "../types/ErrorResponse";
3
3
  import { GetBuyerPassengersDetailsResponse, Person } from "../types/common/Person";
4
+ import { AddReductionRequest } from "../types/common/Reduction";
4
5
  import { Service, ServiceTrip } from "../types/services/Service";
5
6
  import { CreateServiceCartRequest, ServiceCart } from "../types/services/ServiceCart";
6
7
  import { Booking } from "./booking";
@@ -74,6 +75,40 @@ export declare class ServiceBooking extends Booking {
74
75
  * @returns An {@link ErrorResponse} object in case of error, true otherwise.
75
76
  */
76
77
  updateBuyerPassengersDetails(buyerDetails: Person): Promise<ErrorResponse | boolean>;
78
+ /**
79
+ * @description This method allows to add a reduction to the whole cart or to a single trip in the cart.
80
+ * @param {AddReductionRequest} request The information about the reduction to add
81
+ * @returns An {@link ErrorResponse} object in case of error, true otherwise. Also, the cart is updated.
82
+ */
83
+ addReduction(request: AddReductionRequest): Promise<ErrorResponse | boolean>;
84
+ /**
85
+ * @description This method allows to remove a reduction from the whole cart or from a single trip in the cart.
86
+ * @param tripId The id of the trip from which the reduction should be removed. If the tripId is 0, then the reduction
87
+ * will be removed from the whole cart.
88
+ * @returns An {@link ErrorResponse} object in case of error, true otherwise. Also, the cart is updated.
89
+ */
90
+ removeReduction(tripId: number): Promise<ErrorResponse | boolean>;
91
+ /**
92
+ * @description This method allows the user to use the balance in his wallet to pay the cart. If the balance of
93
+ * the wallet is enough to pay the cart, the cart will be paid fully. Otherwise, the user will be asked to pay the
94
+ * remaining amount with another payment method.
95
+ * @returns An {@link ErrorResponse} object in case of error, true otherwise.
96
+ */
97
+ useWallet(): Promise<ErrorResponse | boolean>;
98
+ /**
99
+ * @description This method allows to remove a waller reduction from the cart.
100
+ *
101
+ * @returns An {@link ErrorResponse} object in case of error, true otherwise. Also, the cart is updated.
102
+ */
103
+ removeWalletReduction(): Promise<ErrorResponse | boolean>;
104
+ /**
105
+ * @description This method allows the user to use a voucher to pay the cart.
106
+ * If the balance of the value of the voucher is enough to pay the cart, the cart will be paid fully.
107
+ * Otherwise, the user will be asked to pay the remaining amount with another payment method.
108
+ * @param {string} voucherCode The code of the voucher to use
109
+ * @returns An {@link ErrorResponse} object in case of error, true otherwise.
110
+ */
111
+ useVoucher(voucherCode: string): Promise<ErrorResponse | boolean>;
77
112
  }
78
113
  export declare namespace ServiceBooking {
79
114
  const createBooking: (env: MTSEnvs, sub_key: string, onCartExpiration: () => void, debug: boolean, language: string, access_token?: string, sellerId?: number, resellerId?: number) => Promise<ServiceBooking>;
@@ -61,9 +61,19 @@ 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");
67
77
  var processBookingSteps_1 = require("../utils/processBookingSteps");
68
78
  var booking_1 = require("./booking");
69
79
  var ServiceBooking = /** @class */ (function (_super) {
@@ -361,6 +371,274 @@ var ServiceBooking = /** @class */ (function (_super) {
361
371
  });
362
372
  });
363
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;
571
+ })];
572
+ });
573
+ });
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
+ };
364
642
  return ServiceBooking;
365
643
  }(booking_1.Booking));
366
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";
@@ -93,6 +94,40 @@ export declare class SubscriptionBooking extends Booking {
93
94
  * @returns An {@link ErrorResponse} object in case of error, true otherwise.
94
95
  */
95
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>;