@delicity/client-cart 1.2.13 → 1.2.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/classes/Cart.d.ts +134 -0
  2. package/classes/Cart.d.ts.map +1 -0
  3. package/classes/Cart.js +508 -0
  4. package/classes/Cart.js.map +1 -0
  5. package/index.d.ts +4 -0
  6. package/index.d.ts.map +1 -0
  7. package/index.js +20 -0
  8. package/index.js.map +1 -0
  9. package/package.json +5 -4
  10. package/service/CartService.d.ts +14 -0
  11. package/service/CartService.d.ts.map +1 -0
  12. package/service/CartService.js +130 -0
  13. package/service/CartService.js.map +1 -0
  14. package/service/CategoryAvailabilityService.d.ts +23 -0
  15. package/service/CategoryAvailabilityService.d.ts.map +1 -0
  16. package/service/CategoryAvailabilityService.js +103 -0
  17. package/service/CategoryAvailabilityService.js.map +1 -0
  18. package/service/DeliveryService.d.ts +4 -0
  19. package/service/DeliveryService.d.ts.map +1 -0
  20. package/service/DeliveryService.js +30 -0
  21. package/service/DeliveryService.js.map +1 -0
  22. package/service/HelperService.d.ts +2 -0
  23. package/service/HelperService.d.ts.map +1 -0
  24. package/service/HelperService.js +14 -0
  25. package/service/HelperService.js.map +1 -0
  26. package/README.md +0 -1
  27. package/coverage/clover.xml +0 -71
  28. package/coverage/coverage-final.json +0 -3
  29. package/coverage/lcov-report/base.css +0 -224
  30. package/coverage/lcov-report/block-navigation.js +0 -87
  31. package/coverage/lcov-report/classes/Cart.ts.html +0 -340
  32. package/coverage/lcov-report/classes/index.html +0 -116
  33. package/coverage/lcov-report/favicon.png +0 -0
  34. package/coverage/lcov-report/index.html +0 -131
  35. package/coverage/lcov-report/prettify.css +0 -1
  36. package/coverage/lcov-report/prettify.js +0 -2
  37. package/coverage/lcov-report/service/CartService.ts.html +0 -238
  38. package/coverage/lcov-report/service/index.html +0 -116
  39. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  40. package/coverage/lcov-report/sorter.js +0 -196
  41. package/coverage/lcov.info +0 -129
  42. package/jest.config.js +0 -8
  43. package/src/classes/Cart.ts +0 -563
  44. package/src/index.ts +0 -3
  45. package/src/service/CartService.ts +0 -139
  46. package/src/service/CategoryAvailabilityService.ts +0 -121
  47. package/src/service/DeliveryService.ts +0 -17
  48. package/src/service/HelperService.ts +0 -9
  49. package/src/test/availability.test.ts +0 -346
  50. package/src/test/class.test.ts +0 -497
  51. package/src/test/data/CartData.json +0 -244
  52. package/src/test/data/menu.json +0 -5875
  53. package/src/test/data/merchant.json +0 -186
  54. package/tsconfig.json +0 -24
@@ -0,0 +1,134 @@
1
+ import { IAddress, ICart, ICartDelivery, ICartDiscount, ICartItem, ICartModifierGroup, ICartPaymentMethod, ICartPrice, IClientConfig, Menu, MenuItem, Merchant, OrderModesEnum } from "@delicity/client-types";
2
+ export interface CartClass {
3
+ setMenu(menu: Menu): Cart;
4
+ setCart(cart: ICart): Cart;
5
+ setWhen(when: string | null): Cart;
6
+ setOrderMode(mode: OrderModesEnum): Cart;
7
+ setAddress(address: IAddress): Promise<Cart>;
8
+ setAddressComment(comment: string | null): Cart;
9
+ setCartComment(comment: string | null): Cart;
10
+ setAcceptCondition(acceptCondition: boolean): Cart;
11
+ setPaymentMethods(paymentMethod: ICartPaymentMethod[]): Cart;
12
+ pushError(field: string, code: string, vars: any): Cart;
13
+ setErrors(errors: {
14
+ field: string;
15
+ code: string;
16
+ vars: any;
17
+ }[]): Cart;
18
+ getCart(): ICart;
19
+ getItems(): ICartItem[];
20
+ getTotalItems(): number;
21
+ getTotalItemPrice(): number;
22
+ getMerchantId(): number;
23
+ getCartUUID(): string | undefined;
24
+ getOrderMode(): OrderModesEnum;
25
+ getCartComment(): string | null;
26
+ getDeliveryInfos(): ICartDelivery | undefined;
27
+ getPrices(): null | ICartPrice;
28
+ getWhen(): null | string;
29
+ getAcceptCondition(): boolean;
30
+ getPreparationDurationBetween(): number[] | null;
31
+ getPaymentMethods(): ICartPaymentMethod[];
32
+ getErrors(): {
33
+ field: string;
34
+ code: string;
35
+ vars: any;
36
+ }[];
37
+ getDiscounts(): ICartDiscount[];
38
+ getCoupons(): ICartDiscount[];
39
+ canBePayOnsite(): boolean;
40
+ getRemoteErrors(): any;
41
+ synchronize(): Promise<Cart>;
42
+ addToCart(item: MenuItem, modifierGroup: ICartModifierGroup[], quantity: number, comment?: string): Cart;
43
+ updateCart(cartItem: ICartItem, modifierGroup: ICartModifierGroup[], quantity: number, comment?: string): Cart;
44
+ removeFromCart(cartItem: ICartItem): Cart;
45
+ paymentMethodCard(type: 'card_token' | 'payment_method_id', token: string, brand: string, last4: string, expiration: string, amount?: number): Cart;
46
+ paymentMethodWallet(type: 'apple_pay' | 'google_pay', amount?: number): Cart;
47
+ paymentMethodOnsite(amount?: number): Cart;
48
+ validateCart(): Cart;
49
+ validateMerchantSchedule(): Cart;
50
+ validateCartIfError(): Cart;
51
+ resetErrors(): Cart;
52
+ cartIntegrity(): void;
53
+ }
54
+ export declare class Cart implements CartClass {
55
+ private cart;
56
+ private merchant;
57
+ private menu;
58
+ private config;
59
+ private acceptCondition;
60
+ private errors;
61
+ private listeners;
62
+ private isPriceLoading;
63
+ private isDeliveryLoading;
64
+ private merchantSchedule;
65
+ private remoteErrors;
66
+ private voucherBalance;
67
+ constructor(config: IClientConfig, merchant: Merchant, menu: Menu, OrderMode: OrderModesEnum, when: string | null);
68
+ on(event: string, callback: any): void;
69
+ off(event: string, callback: any): void;
70
+ trigger(event: string, ...args: any): void;
71
+ setMenu(menu: Menu): Cart;
72
+ setCart(cart: ICart): Cart;
73
+ getPrices(): null | ICartPrice;
74
+ getAcceptCondition(): boolean;
75
+ setAcceptCondition(acceptCondition: boolean): Cart;
76
+ getPreparationDurationBetween(): number[] | null;
77
+ getIsDeliveryLoading(): boolean;
78
+ getIsPriceLoading(): boolean;
79
+ getRemoteErrors(): any;
80
+ setAddress(address: IAddress): Promise<Cart>;
81
+ getDeliveryInfos(): ICartDelivery | undefined;
82
+ setCartComment(comment: string | null): Cart;
83
+ getCartComment(): string | null;
84
+ updateCartProductPrices(): void;
85
+ setOrderMode(mode: OrderModesEnum): Cart;
86
+ setWhen(when: string | null): Cart;
87
+ setAddressComment(comment: string | null): Cart;
88
+ getMerchantId(): number;
89
+ getCartUUID(): string | undefined;
90
+ getOrderMode(): OrderModesEnum;
91
+ getWhen(): null | string;
92
+ getPaymentMethods(): ICartPaymentMethod[];
93
+ setPaymentMethods(paymentMethods: ICartPaymentMethod[]): Cart;
94
+ getPaymentMethodTotalAmount(): number;
95
+ isVoucherEligible(): boolean;
96
+ enablePaymentMethodVoucher(type: "edenred", availableVoucherAmount: number): void;
97
+ disablePaymentMethodVoucher(): void;
98
+ paymentMethodCard(type: 'card_token' | 'payment_method_id', token: string, brand: string, last4: string, expiration: string): Cart;
99
+ paymentMethodWallet(type: 'apple_pay' | 'google_pay'): Cart;
100
+ paymentMethodOnsite(): Cart;
101
+ updatePaymentMethodAmount(): Cart;
102
+ addToCart(item: MenuItem, modifierGroups: ICartModifierGroup[], quantity: number, comment?: string, replace?: ICartItem): Cart;
103
+ removeFromCart(cartItem: ICartItem): Cart;
104
+ updateCart(cartItem: ICartItem, modifierGroup: ICartModifierGroup[], quantity: number, comment?: string): Cart;
105
+ getCart(): ICart;
106
+ getItems(): ICartItem[];
107
+ getTotalItems(): number;
108
+ getTotalItemPrice(): number;
109
+ getTotalPrice(): number;
110
+ canBePayOnsite(): boolean;
111
+ synchronize(): Promise<Cart>;
112
+ validateMerchantSchedule(): Cart;
113
+ validateCart(): Cart;
114
+ validateCartIfError(): Cart;
115
+ resetErrors(): Cart;
116
+ getErrors(): {
117
+ field: string;
118
+ code: string;
119
+ vars: any;
120
+ }[];
121
+ pushError(field: string, code: string, vars: any): Cart;
122
+ setErrors(errors: {
123
+ field: string;
124
+ code: string;
125
+ vars: any;
126
+ }[]): Cart;
127
+ setVoucherBalance(type: string, amount: number): void;
128
+ addDiscountCoupon(code: string): Promise<Cart>;
129
+ getDiscounts(): ICartDiscount[];
130
+ getCoupons(): ICartDiscount[];
131
+ removeDiscount(discount: number): Promise<Cart>;
132
+ cartIntegrity(event?: boolean): void;
133
+ }
134
+ //# sourceMappingURL=Cart.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Cart.d.ts","sourceRoot":"","sources":["../../src/classes/Cart.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAE,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,SAAS,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,UAAU,EAAE,aAAa,EAAE,IAAI,EAAE,QAAQ,EAAqB,QAAQ,EAAE,cAAc,EAAC,MAAM,wBAAwB,CAAC;AAUhO,MAAM,WAAW,SAAS;IAEtB,OAAO,CAAC,IAAI,EAAC,IAAI,GAAG,IAAI,CAAA;IACxB,OAAO,CAAC,IAAI,EAAC,KAAK,GAAG,IAAI,CAAA;IACzB,OAAO,CAAC,IAAI,EAAC,MAAM,GAAC,IAAI,GAAG,IAAI,CAAA;IAC/B,YAAY,CAAC,IAAI,EAAC,cAAc,GAAG,IAAI,CAAA;IACvC,UAAU,CAAC,OAAO,EAAC,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC3C,iBAAiB,CAAC,OAAO,EAAE,MAAM,GAAC,IAAI,GAAG,IAAI,CAAA;IAC7C,cAAc,CAAC,OAAO,EAAC,MAAM,GAAC,IAAI,GAAG,IAAI,CAAA;IACzC,kBAAkB,CAAC,eAAe,EAAC,OAAO,GAAE,IAAI,CAAA;IAChD,iBAAiB,CAAC,aAAa,EAAC,kBAAkB,EAAE,GAAE,IAAI,CAAA;IAC1D,SAAS,CAAC,KAAK,EAAC,MAAM,EAAE,IAAI,EAAC,MAAM,EAAE,IAAI,EAAC,GAAG,GAAG,IAAI,CAAA;IACpD,SAAS,CAAC,MAAM,EAAC;QAAC,KAAK,EAAC,MAAM,CAAC;QAAC,IAAI,EAAC,MAAM,CAAC;QAAC,IAAI,EAAC,GAA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AAC,IAAI,EAAC,MAAM,EAAE,MAAM,EAAE,MAAM;IAKvC,iBAAiB,CAAC,IAAI,EAAC,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAKnD,YAAY,IAAI,aAAa,EAAE;IAG/B,UAAU,IAAI,aAAa,EAAE;IAGvB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAOrD,aAAa,CAAC,KAAK,GAAC,OAAa,GAAG,IAAI;CAmD3C"}
@@ -0,0 +1,508 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.Cart = void 0;
16
+ const client_types_1 = require("@delicity/client-types");
17
+ const luxon_1 = require("luxon");
18
+ const uuid_1 = require("uuid");
19
+ const CartService_1 = require("../service/CartService");
20
+ const DeliveryService_1 = require("../service/DeliveryService");
21
+ const HelperService_1 = require("../service/HelperService");
22
+ // @ts-ignore
23
+ const backend_schedule_management_1 = __importDefault(require("@delicity/backend-schedule-management"));
24
+ const CategoryAvailabilityService_1 = require("../service/CategoryAvailabilityService");
25
+ class Cart {
26
+ constructor(config, merchant, menu, OrderMode, when) {
27
+ this.acceptCondition = false;
28
+ this.errors = [];
29
+ this.listeners = {};
30
+ this.isPriceLoading = false;
31
+ this.isDeliveryLoading = false;
32
+ this.voucherBalance = {
33
+ edenred: 0
34
+ };
35
+ this.merchant = merchant;
36
+ this.menu = JSON.parse(JSON.stringify(menu));
37
+ this.config = config;
38
+ this.remoteErrors = {};
39
+ if (this.merchant.schedule) {
40
+ this.merchantSchedule = new backend_schedule_management_1.default(this.merchant.schedule.slots, this.merchant.schedule.closes, this.merchant.schedule.options);
41
+ }
42
+ this.cart = {
43
+ uuid: (0, uuid_1.v4)(),
44
+ createdAt: luxon_1.DateTime.now().toISO(),
45
+ merchantId: merchant.id,
46
+ preparationDurationBetween: null,
47
+ items: [],
48
+ totalItems: 0,
49
+ totalItemPrice: 0,
50
+ paymentMethods: [],
51
+ when: when,
52
+ comment: null,
53
+ type: OrderMode,
54
+ };
55
+ }
56
+ on(event, callback) {
57
+ if (!this.listeners[event]) {
58
+ this.listeners[event] = [];
59
+ }
60
+ this.listeners[event].push(callback);
61
+ }
62
+ off(event, callback) {
63
+ if (!this.listeners[event])
64
+ return;
65
+ this.listeners[event] = [];
66
+ }
67
+ trigger(event, ...args) {
68
+ if (this.listeners[event]) {
69
+ this.listeners[event].forEach((callback) => {
70
+ // @ts-ignore
71
+ callback(...args);
72
+ });
73
+ }
74
+ }
75
+ setMenu(menu) {
76
+ this.menu = JSON.parse(JSON.stringify(menu));
77
+ this.cartIntegrity();
78
+ this.updateCartProductPrices();
79
+ this.trigger('update-cart');
80
+ return this;
81
+ }
82
+ setCart(cart) {
83
+ if (!cart.paymentMethods)
84
+ cart.paymentMethods = [];
85
+ this.cart = cart;
86
+ this.cartIntegrity();
87
+ this.trigger('update-cart');
88
+ return this;
89
+ }
90
+ getPrices() {
91
+ return this.cart.prices || null;
92
+ }
93
+ getAcceptCondition() {
94
+ return this.acceptCondition;
95
+ }
96
+ setAcceptCondition(acceptCondition) {
97
+ this.acceptCondition = acceptCondition;
98
+ return this;
99
+ }
100
+ getPreparationDurationBetween() {
101
+ return this.cart.preparationDurationBetween;
102
+ }
103
+ getIsDeliveryLoading() {
104
+ return this.isDeliveryLoading;
105
+ }
106
+ getIsPriceLoading() {
107
+ return this.isPriceLoading;
108
+ }
109
+ getRemoteErrors() {
110
+ return this.remoteErrors;
111
+ }
112
+ setAddress(address) {
113
+ return __awaiter(this, void 0, void 0, function* () {
114
+ if (this.cart.type !== client_types_1.OrderModesEnum.DELIVERY)
115
+ throw new Error('invalid_order_mode');
116
+ this.isDeliveryLoading = true;
117
+ const delivery = yield (0, DeliveryService_1.getDeliveryDetails)(this.config, this.merchant.id, address.position);
118
+ this.cart.delivery = Object.assign({ address }, delivery);
119
+ this.isDeliveryLoading = false;
120
+ this.trigger('update-cart');
121
+ return this;
122
+ });
123
+ }
124
+ getDeliveryInfos() {
125
+ return this.cart.delivery;
126
+ }
127
+ setCartComment(comment) {
128
+ this.cart.comment = comment;
129
+ return this;
130
+ }
131
+ getCartComment() {
132
+ return this.cart.comment;
133
+ }
134
+ updateCartProductPrices() {
135
+ for (let index in this.cart.items) {
136
+ const menuItem = this.menu.items.find(m => m.id === this.cart.items[index].item.id);
137
+ if (!menuItem)
138
+ this.cart.items.splice(parseInt(index), 1);
139
+ const modifierGroups = this.cart.items[index].modifierGroups;
140
+ const quantity = this.cart.items[index].quantity;
141
+ this.cart.items[index].item = menuItem;
142
+ this.cart.items[index].unitPrice = (0, CartService_1.getProductPrice)(menuItem, this.cart.type) + (0, CartService_1.getTotalModifiersUnitPrice)(modifierGroups);
143
+ this.cart.items[index].totalPrice = ((0, CartService_1.getProductPrice)(menuItem, this.cart.type) + (0, CartService_1.getTotalModifiersUnitPrice)(modifierGroups)) * quantity;
144
+ }
145
+ this.cart.totalItems = (0, CartService_1.getProductCount)(this.cart);
146
+ this.cart.totalItemPrice = (0, CartService_1.getProductPriceSum)(this.cart);
147
+ if (this.cart.paymentMethods && this.cart.paymentMethods.length > 0) {
148
+ this.updatePaymentMethodAmount();
149
+ }
150
+ this.validateCartIfError();
151
+ }
152
+ setOrderMode(mode) {
153
+ this.cart.type = mode;
154
+ if (!this.canBePayOnsite() && this.getPaymentMethods().some(e => e.type === 'cash')) {
155
+ this.cart.paymentMethods = [];
156
+ }
157
+ this.updateCartProductPrices();
158
+ this.trigger('update-cart');
159
+ return this;
160
+ }
161
+ setWhen(when) {
162
+ this.cart.when = when;
163
+ this.trigger('update-cart');
164
+ this.cartIntegrity(true);
165
+ this.validateCartIfError();
166
+ return this;
167
+ }
168
+ setAddressComment(comment) {
169
+ var _a;
170
+ if ((_a = this.cart.delivery) === null || _a === void 0 ? void 0 : _a.address) {
171
+ this.cart.delivery.address.additional_informations = comment;
172
+ }
173
+ return this;
174
+ }
175
+ getMerchantId() {
176
+ return this.merchant.id;
177
+ }
178
+ getCartUUID() {
179
+ return this.cart.uuid;
180
+ }
181
+ getOrderMode() {
182
+ return this.cart.type;
183
+ }
184
+ getWhen() {
185
+ return this.cart.when;
186
+ }
187
+ getPaymentMethods() {
188
+ return this.cart.paymentMethods || [];
189
+ }
190
+ setPaymentMethods(paymentMethods) {
191
+ this.cart.paymentMethods = paymentMethods;
192
+ return this;
193
+ }
194
+ getPaymentMethodTotalAmount() {
195
+ return this.cart.paymentMethods.reduce((a, value) => a + value.amount, 0);
196
+ }
197
+ isVoucherEligible() {
198
+ return true;
199
+ }
200
+ enablePaymentMethodVoucher(type, availableVoucherAmount) {
201
+ if (!this.isVoucherEligible())
202
+ throw new Error("voucher_not_available_in_this_merchant");
203
+ // Set current voucher balance
204
+ this.setVoucherBalance(type, availableVoucherAmount);
205
+ // Keep Only Existing payment method
206
+ this.cart.paymentMethods = this.cart.paymentMethods.filter(p => p.type === 'card');
207
+ // Get Amount to pay with voucher (Only product price)
208
+ const totalToPayVoucher = Math.min(this.voucherBalance[type], this.getTotalItemPrice());
209
+ // Set Meal Voucher
210
+ this.cart.paymentMethods.push({
211
+ type: "meal-voucher",
212
+ voucher: { type },
213
+ amount: totalToPayVoucher
214
+ });
215
+ // Update current card amount if defined
216
+ const leftToPayWithCard = this.getTotalPrice() - totalToPayVoucher;
217
+ if (leftToPayWithCard > 0 && this.cart.paymentMethods.some(p => p.type === 'card')) {
218
+ const cardIndex = this.cart.paymentMethods.findIndex(p => p.type === 'card');
219
+ this.cart.paymentMethods[cardIndex].amount = leftToPayWithCard;
220
+ }
221
+ this.updatePaymentMethodAmount();
222
+ }
223
+ disablePaymentMethodVoucher() {
224
+ // Keep Only Existing payment method
225
+ this.cart.paymentMethods = this.cart.paymentMethods.filter(p => p.type === 'card');
226
+ this.updatePaymentMethodAmount();
227
+ }
228
+ paymentMethodCard(type, token, brand, last4, expiration) {
229
+ // Keep only meal-voucher
230
+ this.cart.paymentMethods = this.cart.paymentMethods.filter(p => p.type === 'meal-voucher');
231
+ // Get left to pay with Card
232
+ let leftToPay = Math.max(this.getTotalPrice() - this.getPaymentMethodTotalAmount(), 0);
233
+ // Add Card
234
+ this.cart.paymentMethods.push({
235
+ type: 'card',
236
+ amount: leftToPay,
237
+ card: { type, token, brand, last4, expiration }
238
+ });
239
+ this.updatePaymentMethodAmount();
240
+ return this;
241
+ }
242
+ paymentMethodWallet(type) {
243
+ // Keep only meal-voucher
244
+ this.cart.paymentMethods = this.cart.paymentMethods.filter(p => p.type === 'meal-voucher');
245
+ // Get left to pay with Card
246
+ let leftToPay = Math.max(this.getTotalPrice() - this.getPaymentMethodTotalAmount(), 0);
247
+ // Add Wallet
248
+ this.cart.paymentMethods.push({
249
+ type: "wallet",
250
+ wallet: { type },
251
+ amount: leftToPay,
252
+ });
253
+ return this;
254
+ }
255
+ // Set All order is paid onsite
256
+ paymentMethodOnsite() {
257
+ this.cart.paymentMethods = [];
258
+ this.cart.paymentMethods.push({
259
+ type: 'cash',
260
+ amount: this.getTotalPrice(),
261
+ });
262
+ return this;
263
+ }
264
+ updatePaymentMethodAmount() {
265
+ var _a, _b, _c;
266
+ if (((_a = this.cart.paymentMethods) === null || _a === void 0 ? void 0 : _a.length) === 1) {
267
+ if (this.cart.paymentMethods[0].type !== 'meal-voucher') {
268
+ this.cart.paymentMethods[0].amount = this.getTotalPrice(); // Card Pay all
269
+ }
270
+ else if (this.cart.paymentMethods[0].voucher) {
271
+ // Voucher Can pay only product
272
+ this.cart.paymentMethods[0].amount = Math.min(this.voucherBalance[this.cart.paymentMethods[0].voucher.type], this.getTotalItemPrice());
273
+ }
274
+ }
275
+ else if (((_b = this.cart.paymentMethods) === null || _b === void 0 ? void 0 : _b.length) === 2 && ((_c = this.cart.paymentMethods) === null || _c === void 0 ? void 0 : _c.some(p => p.type === 'meal-voucher'))) {
276
+ const voucherIndex = this.cart.paymentMethods.findIndex(p => p.type === 'meal-voucher' && p.voucher);
277
+ const cardIndex = this.cart.paymentMethods.findIndex(p => p.type === 'card');
278
+ // Voucher Can pay only product
279
+ const voucherType = this.cart.paymentMethods[voucherIndex];
280
+ if (voucherType.voucher) {
281
+ this.cart.paymentMethods[voucherIndex].amount = Math.min(this.voucherBalance[voucherType.voucher.type], this.getTotalItemPrice());
282
+ }
283
+ // Card pay the rest : Delivery / Delicity Fees...
284
+ this.cart.paymentMethods[cardIndex].amount = this.getTotalPrice() - this.cart.paymentMethods[voucherIndex].amount;
285
+ }
286
+ else {
287
+ // If multiple payment methods, split amount
288
+ // Not dev yet...
289
+ }
290
+ return this;
291
+ }
292
+ addToCart(item, modifierGroups, quantity, comment, replace) {
293
+ // Check if item have mandatory modifiers
294
+ if (!(0, CartService_1.mandatoryModifierCheck)(item, modifierGroups, this.menu)) {
295
+ throw new Error('mandatory_field_missing');
296
+ }
297
+ // Check if item is available
298
+ if (!(0, CartService_1.isItemAvailable)(item, this.menu)) {
299
+ throw new Error('item_not_available');
300
+ }
301
+ const index = (0, CartService_1.isItemAlreadyInCart)(item, modifierGroups, this.cart, comment);
302
+ if (index !== -1 && !replace) {
303
+ this.cart.items[index].quantity += quantity;
304
+ this.cart.items[index].totalPrice = ((0, CartService_1.getProductPrice)(item, this.cart.type) + (0, CartService_1.getTotalModifiersUnitPrice)(modifierGroups)) * this.cart.items[index].quantity;
305
+ }
306
+ else {
307
+ const cartItem = {
308
+ uuid: (0, uuid_1.v4)(),
309
+ item,
310
+ modifierGroups,
311
+ quantity,
312
+ comment: comment || null,
313
+ unitPrice: (0, CartService_1.getProductPrice)(item, this.cart.type) + (0, CartService_1.getTotalModifiersUnitPrice)(modifierGroups),
314
+ totalPrice: ((0, CartService_1.getProductPrice)(item, this.cart.type) + (0, CartService_1.getTotalModifiersUnitPrice)(modifierGroups)) * quantity
315
+ };
316
+ if (replace) {
317
+ const index = this.cart.items.findIndex(i => i.uuid === replace.uuid);
318
+ this.cart.items.splice(index, 1, cartItem);
319
+ }
320
+ else {
321
+ this.cart.items.push(cartItem);
322
+ }
323
+ }
324
+ this.cart.totalItems = (0, CartService_1.getProductCount)(this.cart);
325
+ this.cart.totalItemPrice = (0, CartService_1.getProductPriceSum)(this.cart);
326
+ if (this.cart.paymentMethods && this.cart.paymentMethods.length > 0) {
327
+ this.updatePaymentMethodAmount();
328
+ }
329
+ this.validateCartIfError();
330
+ this.cartIntegrity(true);
331
+ this.trigger('update-cart');
332
+ return this;
333
+ }
334
+ removeFromCart(cartItem) {
335
+ this.cart.items = this.cart.items.filter((item) => item.uuid !== cartItem.uuid);
336
+ this.cart.totalItems = (0, CartService_1.getProductCount)(this.cart);
337
+ this.cart.totalItemPrice = (0, CartService_1.getProductPriceSum)(this.cart);
338
+ if (this.cart.paymentMethods) {
339
+ this.updatePaymentMethodAmount();
340
+ }
341
+ this.cartIntegrity(true);
342
+ this.trigger('update-cart');
343
+ return this;
344
+ }
345
+ updateCart(cartItem, modifierGroup, quantity, comment) {
346
+ return this.addToCart(cartItem.item, modifierGroup, quantity, comment, cartItem);
347
+ }
348
+ getCart() {
349
+ return this.cart;
350
+ }
351
+ getItems() {
352
+ return this.cart.items;
353
+ }
354
+ getTotalItems() {
355
+ return this.cart.totalItems;
356
+ }
357
+ getTotalItemPrice() {
358
+ return this.cart.totalItemPrice;
359
+ }
360
+ getTotalPrice() {
361
+ var _a;
362
+ return ((_a = this.cart.prices) === null || _a === void 0 ? void 0 : _a.total) || this.cart.totalItemPrice;
363
+ }
364
+ canBePayOnsite() {
365
+ return [client_types_1.OrderModesEnum.PICKUP, client_types_1.OrderModesEnum.ON_SITE_COUNTER].includes(this.cart.type) && this.merchant.features.onsitePayment;
366
+ }
367
+ synchronize() {
368
+ var _a;
369
+ return __awaiter(this, void 0, void 0, function* () {
370
+ if (this.cart.type === client_types_1.OrderModesEnum.DELIVERY && !this.cart.delivery) {
371
+ throw new Error('no_delivery_address');
372
+ }
373
+ this.isPriceLoading = true;
374
+ const request = yield (0, CartService_1.synchronizeCart)(this.config, this.cart);
375
+ if (this.cart.delivery && request.delivery) {
376
+ this.cart.delivery = Object.assign({ address: (_a = this.cart.delivery) === null || _a === void 0 ? void 0 : _a.address }, request.delivery);
377
+ }
378
+ this.cart.preparationDurationBetween = request.preparationDurationBetween;
379
+ this.cart.discounts = request.discounts;
380
+ this.cart.prices = request.prices;
381
+ this.remoteErrors = request.errors;
382
+ if (this.cart.paymentMethods && this.cart.paymentMethods.length > 0) {
383
+ this.updatePaymentMethodAmount();
384
+ }
385
+ this.isPriceLoading = false;
386
+ return this;
387
+ });
388
+ }
389
+ validateMerchantSchedule() {
390
+ this.errors = this.errors.filter(e => e.field !== 'schedule');
391
+ if (this.merchantSchedule) {
392
+ const when = this.getWhen() !== null ? luxon_1.DateTime.fromISO(this.getWhen()) : luxon_1.DateTime.now();
393
+ if (!this.merchantSchedule.isOpenAt(when))
394
+ this.errors.push({ field: 'schedule', code: 'merchant_currently_close', vars: {} });
395
+ }
396
+ return this;
397
+ }
398
+ validateCart() {
399
+ var _a;
400
+ this.errors = [];
401
+ if (this.cart.type === client_types_1.OrderModesEnum.DELIVERY && !this.cart.delivery)
402
+ this.errors.push({ field: 'address', code: 'no_address_defined', vars: {} });
403
+ if (this.cart.type === client_types_1.OrderModesEnum.DELIVERY && this.cart.delivery && !this.cart.delivery.available)
404
+ this.errors.push({ field: 'address', code: 'address_not_available', vars: {} });
405
+ if (!this.cart.paymentMethods || ((_a = this.cart.paymentMethods) === null || _a === void 0 ? void 0 : _a.length) === 0)
406
+ this.errors.push({ field: 'payment_method', code: 'missing_payment_method', vars: {} });
407
+ if (!this.acceptCondition)
408
+ this.errors.push({ field: 'conditions', code: 'condition_not_accepted', vars: {} });
409
+ if (this.cart.type === client_types_1.OrderModesEnum.DELIVERY && this.merchant.settings.minDeliveryOrderPrice > this.cart.totalItemPrice)
410
+ this.errors.push({ field: 'prices', code: 'minium_delivery_product_price', vars: { min: (0, HelperService_1.formatPrice)(this.merchant.settings.minDeliveryOrderPrice) } });
411
+ this.validateMerchantSchedule();
412
+ return this;
413
+ }
414
+ validateCartIfError() {
415
+ if (this.errors.length > 0)
416
+ this.validateCart();
417
+ return this;
418
+ }
419
+ resetErrors() {
420
+ this.errors = [];
421
+ return this;
422
+ }
423
+ getErrors() {
424
+ return this.errors;
425
+ }
426
+ pushError(field, code, vars) {
427
+ this.errors.push({ field, code, vars });
428
+ return this;
429
+ }
430
+ setErrors(errors) {
431
+ this.errors = errors;
432
+ return this;
433
+ }
434
+ setVoucherBalance(type, amount) {
435
+ this.voucherBalance[type] = amount;
436
+ }
437
+ addDiscountCoupon(code) {
438
+ return __awaiter(this, void 0, void 0, function* () {
439
+ yield (0, CartService_1.addDiscountCode)(this.config, this.cart, code);
440
+ this.trigger('update-cart');
441
+ return this;
442
+ });
443
+ }
444
+ getDiscounts() {
445
+ var _a;
446
+ return (_a = this.cart.discounts) !== null && _a !== void 0 ? _a : [];
447
+ }
448
+ getCoupons() {
449
+ var _a, _b;
450
+ return (_b = (_a = this.cart.discounts) === null || _a === void 0 ? void 0 : _a.filter(d => d.type === 'coupon')) !== null && _b !== void 0 ? _b : [];
451
+ }
452
+ removeDiscount(discount) {
453
+ return __awaiter(this, void 0, void 0, function* () {
454
+ yield (0, CartService_1.removeDiscountCode)(this.config, this.cart, discount);
455
+ this.trigger('update-cart');
456
+ return this;
457
+ });
458
+ }
459
+ cartIntegrity(event = false) {
460
+ // Check if cart products are still available in menu
461
+ if (this.cart.items.length > 0) {
462
+ const merchantSchedule = new backend_schedule_management_1.default(this.merchant.schedule.slots, this.merchant.schedule.closes, this.merchant.schedule.options);
463
+ const toRemove = [];
464
+ for (const i in this.cart.items) {
465
+ // Remove from cart if item not in menu anymore
466
+ const menuItem = this.menu.items.find(m => m.id === this.cart.items[i].item.id);
467
+ const category = this.menu.categories.find(c => { var _a; return (_a = c.itemIds) === null || _a === void 0 ? void 0 : _a.includes(this.cart.items[i].item.uid); });
468
+ if (!menuItem) {
469
+ toRemove.push(this.cart.items[i]);
470
+ continue;
471
+ }
472
+ // Remove from cart if item not available
473
+ if (menuItem.unavailable) {
474
+ toRemove.push(this.cart.items[i]);
475
+ continue;
476
+ }
477
+ // Check if product is available
478
+ if (category === null || category === void 0 ? void 0 : category.availability) {
479
+ const atDate = this.cart.when ? luxon_1.DateTime.fromISO(this.cart.when) : luxon_1.DateTime.now();
480
+ // @ts-ignore
481
+ const categoryAvailability = (0, CategoryAvailabilityService_1.categoryAvailable)(category.availability, atDate, merchantSchedule);
482
+ if (!categoryAvailability.isAvailable) {
483
+ toRemove.push(this.cart.items[i]);
484
+ if (event) {
485
+ // Used when cart update check.
486
+ this.trigger('product-removed:not-available-at-date', this.cart.items[i]);
487
+ }
488
+ continue;
489
+ }
490
+ }
491
+ // Update from menu
492
+ this.cart.items[i].item = menuItem;
493
+ }
494
+ // Remove item from cart when necessary
495
+ toRemove.forEach((cartItem) => {
496
+ this.cart.items = this.cart.items.filter((item) => item.uuid !== cartItem.uuid);
497
+ });
498
+ // Update cart
499
+ this.cart.totalItems = (0, CartService_1.getProductCount)(this.cart);
500
+ this.cart.totalItemPrice = (0, CartService_1.getProductPriceSum)(this.cart);
501
+ if (this.cart.paymentMethods && this.cart.paymentMethods.length > 0) {
502
+ this.updatePaymentMethodAmount();
503
+ }
504
+ }
505
+ }
506
+ }
507
+ exports.Cart = Cart;
508
+ //# sourceMappingURL=Cart.js.map