mts-booking-library 1.0.5 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.Booking = void 0;
40
+ var config_1 = require("../config");
41
+ var Booking = /** @class */ (function () {
42
+ /**
43
+ * This is the constructor of the Booking class.
44
+ * @param {string} env The environment in which the app is running. Can be "dev", "stag" or "prod"
45
+ * @param {string} sub_key The subscription key for using the APIs
46
+ * @param {() => void} onCartExpiration A callback function that will be called when the cart expires
47
+ * @param {string} [access_token=undefined] The access token for calling MTS APIs
48
+ * @param {number} [sellerId=undefined] The id of the seller. If not set, it will return the results for all sellers
49
+ */
50
+ function Booking(env, sub_key, onCartExpiration, debug, access_token, sellerId) {
51
+ if (debug === void 0) { debug = false; }
52
+ this.cartStatus = Booking.CartStatus.EMPTY;
53
+ this.sellerId = sellerId || undefined;
54
+ this.config = (0, config_1.setConfig)(env, sub_key, debug, access_token);
55
+ this.onCartExpiration = onCartExpiration;
56
+ }
57
+ /**
58
+ * This method allows to renew the access token for calling MTS APIs
59
+ * @param {string} access_token The new access token
60
+ */
61
+ Booking.prototype.renewAccessToken = function (access_token) {
62
+ (0, config_1.setConfig)(this.config.ENV, this.config.OCP_SUBSCRIPTION_KEY, this.config.DEBUG, access_token);
63
+ };
64
+ Booking.prototype.getCartStatus = function () {
65
+ return this.cartStatus;
66
+ };
67
+ Booking.prototype.getCartExpirationTimeInMs = function () {
68
+ return __awaiter(this, void 0, void 0, function () {
69
+ return __generator(this, function (_a) {
70
+ // If cart is initialized, return the time in ms until the cart expires.
71
+ // Note that bookingDueDate is initialized only when the cart is created.
72
+ if (this.bookingDueDate) {
73
+ return [2 /*return*/, (new Date(this.bookingDueDate).valueOf() - new Date().valueOf())];
74
+ }
75
+ // Cart is not initialized
76
+ throw Error("Cart is not initialized yet");
77
+ });
78
+ });
79
+ };
80
+ return Booking;
81
+ }());
82
+ exports.Booking = Booking;
83
+ (function (Booking) {
84
+ var Currencies;
85
+ (function (Currencies) {
86
+ Currencies["EUR"] = "EUR";
87
+ Currencies["USD"] = "USD";
88
+ Currencies["CHF"] = "CHF";
89
+ })(Currencies = Booking.Currencies || (Booking.Currencies = {}));
90
+ var CartStatus;
91
+ (function (CartStatus) {
92
+ CartStatus["EMPTY"] = "EMPTY";
93
+ CartStatus["CREATION"] = "CREATION";
94
+ CartStatus["PASSENGERS"] = "PASSENGERS";
95
+ CartStatus["SEATS"] = "SEATS";
96
+ CartStatus["EXTRAS"] = "EXTRAS";
97
+ CartStatus["PAYMENT"] = "PAYMENT";
98
+ })(CartStatus = Booking.CartStatus || (Booking.CartStatus = {}));
99
+ var BookingTypes;
100
+ (function (BookingTypes) {
101
+ BookingTypes["MLP"] = "MLP";
102
+ BookingTypes["SERVICE"] = "SERVICE";
103
+ BookingTypes["MLP_SUBSCRIPTIONS"] = "MLP_SUBSCRIPTIONS";
104
+ })(BookingTypes = Booking.BookingTypes || (Booking.BookingTypes = {}));
105
+ })(Booking || (exports.Booking = Booking = {}));
@@ -0,0 +1,246 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
28
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
29
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
30
+ return new (P || (P = Promise))(function (resolve, reject) {
31
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
32
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
33
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
34
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
35
+ });
36
+ };
37
+ var __generator = (this && this.__generator) || function (thisArg, body) {
38
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
39
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
40
+ function verb(n) { return function (v) { return step([n, v]); }; }
41
+ function step(op) {
42
+ if (f) throw new TypeError("Generator is already executing.");
43
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
44
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
45
+ if (y = 0, t) op = [op[0] & 2, t.value];
46
+ switch (op[0]) {
47
+ case 0: case 1: t = op; break;
48
+ case 4: _.label++; return { value: op[1], done: false };
49
+ case 5: _.label++; y = op[1]; op = [0]; continue;
50
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
51
+ default:
52
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
53
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
54
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
55
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
56
+ if (t[2]) _.ops.pop();
57
+ _.trys.pop(); continue;
58
+ }
59
+ op = body.call(thisArg, _);
60
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
61
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
62
+ }
63
+ };
64
+ Object.defineProperty(exports, "__esModule", { value: true });
65
+ exports.JourneyBooking = void 0;
66
+ var apiCall_1 = require("../utils/apiCall");
67
+ var booking_1 = require("./booking");
68
+ var JourneyBooking = /** @class */ (function (_super) {
69
+ __extends(JourneyBooking, _super);
70
+ function JourneyBooking(env, sub_key, onCartExpiration, debug, access_token, sellerId) {
71
+ if (debug === void 0) { debug = false; }
72
+ var _this =
73
+ // Call Booking constructor
74
+ _super.call(this, env, sub_key, onCartExpiration, debug, access_token, sellerId) || this;
75
+ var cartId = localStorage.getItem("cartId");
76
+ if (cartId) {
77
+ _this.fetchAndSetCart(parseInt(cartId));
78
+ }
79
+ return _this;
80
+ }
81
+ JourneyBooking.prototype.getCart = function () {
82
+ return this.cart;
83
+ };
84
+ JourneyBooking.prototype.fetchAndSetCart = function (cartId) {
85
+ return __awaiter(this, void 0, void 0, function () {
86
+ var _this = this;
87
+ return __generator(this, function (_a) {
88
+ this.fetchCart(cartId).then(function (cart) {
89
+ var cartDate = new Date(cart.bookingDueDate);
90
+ if (cartDate > new Date()) {
91
+ _this.cart = cart;
92
+ _this.bookingDueDate = cartDate; // See Booking class
93
+ _this.cartStatus = cart.status;
94
+ _this.createCartTimer(cartDate);
95
+ }
96
+ });
97
+ return [2 /*return*/];
98
+ });
99
+ });
100
+ };
101
+ JourneyBooking.prototype.createCartTimer = function (expiringDate) {
102
+ return __awaiter(this, void 0, void 0, function () {
103
+ var _this = this;
104
+ return __generator(this, function (_a) {
105
+ setTimeout(function () {
106
+ localStorage.removeItem("cartId");
107
+ // Put actions that have to be done when timer expires here
108
+ _this.cartStatus = booking_1.Booking.CartStatus.EMPTY;
109
+ _this.cart = undefined;
110
+ _this.bookingDueDate = undefined; // See Booking class
111
+ _this.onCartExpiration();
112
+ }, expiringDate.valueOf() - new Date().valueOf());
113
+ return [2 /*return*/];
114
+ });
115
+ });
116
+ };
117
+ JourneyBooking.prototype.fetchCart = function (cartId) {
118
+ return __awaiter(this, void 0, void 0, function () {
119
+ var url;
120
+ return __generator(this, function (_a) {
121
+ url = "".concat(this.config.API_ENDPOINT, "/booking/cart/\n ").concat(new URLSearchParams({ cartId: cartId.toString() }));
122
+ return [2 /*return*/, (0, apiCall_1.makeGet)(url)];
123
+ });
124
+ });
125
+ };
126
+ /**
127
+ * This method returns the possible departures for all journeys (MLP) sold by this seller.
128
+ * @returns {string[]} The list of possible departures
129
+ */
130
+ JourneyBooking.prototype.getJourneysDepartures = function () {
131
+ return __awaiter(this, void 0, void 0, function () {
132
+ var url;
133
+ return __generator(this, function (_a) {
134
+ url = "".concat(this.config.API_ENDPOINT, "/booking/journeys/departures?").concat(new URLSearchParams(__assign({}, (this.sellerId && { sellerId: this.sellerId.toString() }))));
135
+ return [2 /*return*/, (0, apiCall_1.makeGet)(url)];
136
+ });
137
+ });
138
+ };
139
+ /**
140
+ * This method returns the possible destination for the journeys sold by this seller that start at the selected departure.
141
+ * @param {string} departureStopName The departure stop name (as returned by {@link getJourneysDepartures})
142
+ * @returns {string[]} The list of possible destinations
143
+ */
144
+ JourneyBooking.prototype.getJourneysDestinations = function (departureStopName) {
145
+ return __awaiter(this, void 0, void 0, function () {
146
+ var url;
147
+ return __generator(this, function (_a) {
148
+ url = "".concat(this.config.API_ENDPOINT, "/booking/journeys/destinations?").concat(new URLSearchParams(__assign(__assign({}, (this.sellerId && { sellerId: this.sellerId.toString() })), { departureStopName: departureStopName })));
149
+ return [2 /*return*/, (0, apiCall_1.makeGet)(url)];
150
+ });
151
+ });
152
+ };
153
+ /**
154
+ * This method returns the journeys that match the given parameters.
155
+ * Note that it will always return the journeys for the one-way trip.
156
+ * 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)
157
+ * @param {JourneySearch} params an object of type {@link JourneySearch} containing the parameters of the search
158
+ * @returns {JourneySearchResult[]} The returned journeys that match the given parameters
159
+ */
160
+ JourneyBooking.prototype.getJourneys = function (params) {
161
+ return __awaiter(this, void 0, void 0, function () {
162
+ var url;
163
+ return __generator(this, function (_a) {
164
+ if (params.departureStopName === undefined || params.destinationStopName === undefined) {
165
+ throw Error("Fields departureStopName and destinationStopName are required");
166
+ }
167
+ url = "".concat(this.config.API_ENDPOINT, "/booking/journeys?").concat(new URLSearchParams(__assign(__assign({}, (this.sellerId && { sellerId: this.sellerId.toString() })), { departureStopName: params.departureStopName, destinationStopName: params.destinationStopName, passengersNumber: params.passengersNumber.toString(), date: params.date, roundTripDate: params.roundTripDate ? params.roundTripDate : "null", currency: params.currency, isRoundtrip: params.isRoundtrip.toString() })));
168
+ return [2 /*return*/, (0, apiCall_1.makeGet)(url)];
169
+ });
170
+ });
171
+ };
172
+ JourneyBooking.prototype.createJourneyCart = function (tripCart) {
173
+ return __awaiter(this, void 0, void 0, function () {
174
+ var url;
175
+ var _this = this;
176
+ return __generator(this, function (_a) {
177
+ url = "".concat(this.config.API_ENDPOINT, "/booking/journeys/cart");
178
+ return [2 /*return*/, (0, apiCall_1.makePost)(url, tripCart).then(function (cart) {
179
+ _this.cart = cart.cart;
180
+ _this.cartStatus = cart.cart.status;
181
+ _this.createCartTimer(new Date(cart.cart.bookingDueDate));
182
+ return cart.cart;
183
+ })];
184
+ });
185
+ });
186
+ };
187
+ JourneyBooking.prototype.getBusMatrix = function (params) {
188
+ return __awaiter(this, void 0, void 0, function () {
189
+ var url;
190
+ return __generator(this, function (_a) {
191
+ url = "".concat(this.config.API_ENDPOINT, "/booking/journeys/trips/").concat(params.tripId, "/seats?").concat(new URLSearchParams(__assign(__assign({}, (this.sellerId && { sellerId: this.sellerId.toString() })), { departureStopId: params.departureStopId.toString(), destinationStopId: params.destinationStopId.toString() })));
192
+ return [2 /*return*/, (0, apiCall_1.makeGet)(url)];
193
+ });
194
+ });
195
+ };
196
+ JourneyBooking.prototype.getSeatsStatus = function (params) {
197
+ return __awaiter(this, void 0, void 0, function () {
198
+ var url;
199
+ return __generator(this, function (_a) {
200
+ if (!this.cart) {
201
+ throw Error("Cart is not initialized yet");
202
+ }
203
+ url = "".concat(this.config.API_ENDPOINT, "/booking/cart/").concat(this.cart.id, "/seats?").concat(new URLSearchParams(__assign(__assign({}, (this.sellerId && { sellerId: this.sellerId.toString() })), { tripId: params.tripId.toString() })));
204
+ return [2 /*return*/, (0, apiCall_1.makeGet)(url)];
205
+ });
206
+ });
207
+ };
208
+ JourneyBooking.prototype.movePassengers = function (params) {
209
+ return __awaiter(this, void 0, void 0, function () {
210
+ var url;
211
+ return __generator(this, function (_a) {
212
+ if (!this.cart) {
213
+ throw Error("Cart is not initialized yet");
214
+ }
215
+ url = "".concat(this.config.API_ENDPOINT, "/booking/cart/").concat(this.cart.id, "/seats");
216
+ return [2 /*return*/, (0, apiCall_1.makePost)(url, params)];
217
+ });
218
+ });
219
+ };
220
+ JourneyBooking.prototype.getPassengersDetails = function () {
221
+ return __awaiter(this, void 0, void 0, function () {
222
+ var url;
223
+ return __generator(this, function (_a) {
224
+ if (!this.cart) {
225
+ throw Error("Cart is not initialized yet");
226
+ }
227
+ url = "".concat(this.config.API_ENDPOINT, "/booking/cart/").concat(this.cart.id, "/details?").concat(new URLSearchParams(__assign({}, (this.sellerId && { sellerId: this.sellerId.toString() }))));
228
+ return [2 /*return*/, (0, apiCall_1.makeGet)(url)];
229
+ });
230
+ });
231
+ };
232
+ JourneyBooking.prototype.updatePassengersDetails = function (passengersDetails) {
233
+ return __awaiter(this, void 0, void 0, function () {
234
+ var url;
235
+ return __generator(this, function (_a) {
236
+ if (!this.cart) {
237
+ throw Error("Cart is not initialized yet");
238
+ }
239
+ url = "".concat(this.config.API_ENDPOINT, "/booking/cart/").concat(this.cart.id, "/details");
240
+ return [2 /*return*/, (0, apiCall_1.makePost)(url, passengersDetails)];
241
+ });
242
+ });
243
+ };
244
+ return JourneyBooking;
245
+ }(booking_1.Booking));
246
+ exports.JourneyBooking = JourneyBooking;
@@ -0,0 +1,190 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
28
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
29
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
30
+ return new (P || (P = Promise))(function (resolve, reject) {
31
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
32
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
33
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
34
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
35
+ });
36
+ };
37
+ var __generator = (this && this.__generator) || function (thisArg, body) {
38
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
39
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
40
+ function verb(n) { return function (v) { return step([n, v]); }; }
41
+ function step(op) {
42
+ if (f) throw new TypeError("Generator is already executing.");
43
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
44
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
45
+ if (y = 0, t) op = [op[0] & 2, t.value];
46
+ switch (op[0]) {
47
+ case 0: case 1: t = op; break;
48
+ case 4: _.label++; return { value: op[1], done: false };
49
+ case 5: _.label++; y = op[1]; op = [0]; continue;
50
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
51
+ default:
52
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
53
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
54
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
55
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
56
+ if (t[2]) _.ops.pop();
57
+ _.trys.pop(); continue;
58
+ }
59
+ op = body.call(thisArg, _);
60
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
61
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
62
+ }
63
+ };
64
+ Object.defineProperty(exports, "__esModule", { value: true });
65
+ exports.ServiceBooking = void 0;
66
+ var apiCall_1 = require("../utils/apiCall");
67
+ var booking_1 = require("./booking");
68
+ var ServiceBooking = /** @class */ (function (_super) {
69
+ __extends(ServiceBooking, _super);
70
+ function ServiceBooking(env, sub_key, onCartExpiration, debug, access_token, sellerId) {
71
+ if (debug === void 0) { debug = false; }
72
+ var _this =
73
+ // Call Booking constructor
74
+ _super.call(this, env, sub_key, onCartExpiration, debug, access_token, sellerId) || this;
75
+ var cartId = localStorage.getItem("cartId");
76
+ if (cartId) {
77
+ _this.fetchAndSetCart(parseInt(cartId));
78
+ }
79
+ return _this;
80
+ }
81
+ ServiceBooking.prototype.getCart = function () {
82
+ return this.cart;
83
+ };
84
+ ServiceBooking.prototype.fetchAndSetCart = function (cartId) {
85
+ return __awaiter(this, void 0, void 0, function () {
86
+ var _this = this;
87
+ return __generator(this, function (_a) {
88
+ this.fetchCart(cartId).then(function (cart) {
89
+ var cartDate = new Date(cart.bookingDueDate);
90
+ if (cartDate > new Date()) {
91
+ _this.cart = cart;
92
+ _this.bookingDueDate = cartDate; // See Booking class
93
+ _this.cartStatus = cart.status;
94
+ _this.createCartTimer(cartDate);
95
+ }
96
+ });
97
+ return [2 /*return*/];
98
+ });
99
+ });
100
+ };
101
+ ServiceBooking.prototype.createCartTimer = function (expiringDate) {
102
+ return __awaiter(this, void 0, void 0, function () {
103
+ var _this = this;
104
+ return __generator(this, function (_a) {
105
+ setTimeout(function () {
106
+ localStorage.removeItem("cartId");
107
+ // Put actions that have to be done when timer expires here
108
+ _this.cartStatus = booking_1.Booking.CartStatus.EMPTY;
109
+ _this.cart = undefined;
110
+ _this.bookingDueDate = undefined; // See Booking class
111
+ _this.onCartExpiration();
112
+ }, expiringDate.valueOf() - new Date().valueOf());
113
+ return [2 /*return*/];
114
+ });
115
+ });
116
+ };
117
+ ServiceBooking.prototype.fetchCart = function (cartId) {
118
+ return __awaiter(this, void 0, void 0, function () {
119
+ var url;
120
+ return __generator(this, function (_a) {
121
+ url = "".concat(this.config.API_ENDPOINT, "/booking/cart/\n ").concat(new URLSearchParams({ cartId: cartId.toString() }));
122
+ return [2 /*return*/, (0, apiCall_1.makeGet)(url)];
123
+ });
124
+ });
125
+ };
126
+ /**
127
+ * This method returns the list of cities in which the seller offers tours.
128
+ * If the sellerId is not set or it is 0, it will return the list of cities for all sellers.
129
+ * @returns {string[]} The list of possible cities
130
+ */
131
+ ServiceBooking.prototype.getServiceCities = function () {
132
+ return __awaiter(this, void 0, void 0, function () {
133
+ var url;
134
+ return __generator(this, function (_a) {
135
+ url = "".concat(this.config.API_ENDPOINT, "/booking/services/cities?").concat(new URLSearchParams(__assign({}, (this.sellerId && { sellerId: this.sellerId.toString() }))));
136
+ return [2 /*return*/, (0, apiCall_1.makeGet)(url)];
137
+ });
138
+ });
139
+ };
140
+ /**
141
+ * This method returns the tours sold by this seller in the given city.
142
+ * If the sellerId is not set or it is 0, it will return the tours in the given city for all sellers.
143
+ * @param {string} [cityName=undefined] The name of the selected city (as returned by {@link getTourCities})
144
+ * @param {Booking.Currencies} currency The currency in which the prices should be returned
145
+ * @returns {Tour[]} The returned tours
146
+ */
147
+ ServiceBooking.prototype.getServices = function (currency, cityName) {
148
+ return __awaiter(this, void 0, void 0, function () {
149
+ var url;
150
+ return __generator(this, function (_a) {
151
+ url = "".concat(this.config.API_ENDPOINT, "/booking/tours?").concat(new URLSearchParams(__assign(__assign(__assign({}, (this.sellerId && { sellerId: this.sellerId.toString() })), (cityName && { cityName: cityName })), { currency: currency })));
152
+ return [2 /*return*/, (0, apiCall_1.makeGet)(url)];
153
+ });
154
+ });
155
+ };
156
+ /**
157
+ * 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
158
+ * and/or a specific hour for the tour.
159
+ * You should call this method only if {@link Service.isDateOptional} or {@link Service.isDateRequired} is true for the selected tour.
160
+ * @param {number} serviceId The id of the selected tour (can be retrieved in the object {@link Service} returned by {@link getTours})
161
+ * @param {Date} date The date on which to get the tours
162
+ * @returns {ServiceTrip[]} The returned information about the tour (tripId and hour)
163
+ */
164
+ ServiceBooking.prototype.getServiceTrips = function (serviceId, date) {
165
+ return __awaiter(this, void 0, void 0, function () {
166
+ var url;
167
+ return __generator(this, function (_a) {
168
+ url = "".concat(this.config.API_ENDPOINT, "/booking/services/").concat(serviceId, "/trips?").concat(new URLSearchParams(__assign(__assign({}, (this.sellerId && { sellerId: this.sellerId.toString() })), { date: date.toDateString() })));
169
+ return [2 /*return*/, (0, apiCall_1.makeGet)(url)];
170
+ });
171
+ });
172
+ };
173
+ ServiceBooking.prototype.createServiceCart = function (serviceCart) {
174
+ return __awaiter(this, void 0, void 0, function () {
175
+ var url;
176
+ var _this = this;
177
+ return __generator(this, function (_a) {
178
+ url = "".concat(this.config.API_ENDPOINT, "/booking/services/cart");
179
+ return [2 /*return*/, (0, apiCall_1.makePost)(url, serviceCart).then(function (cart) {
180
+ _this.cart = cart.cart;
181
+ _this.cartStatus = cart.cart.status;
182
+ _this.createCartTimer(new Date(cart.cart.bookingDueDate));
183
+ return cart.cart;
184
+ })];
185
+ });
186
+ });
187
+ };
188
+ return ServiceBooking;
189
+ }(booking_1.Booking));
190
+ exports.ServiceBooking = ServiceBooking;