orderiom-api-package 0.4.6 → 0.4.8

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orderiom-api-package",
3
- "version": "0.4.6",
3
+ "version": "0.4.8",
4
4
  "description": "This package will install all necessary API calls for every orderiom restaurant",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/src/common.js CHANGED
@@ -82,6 +82,12 @@ axios.defaults.baseURL = process.env.VUE_APP_BASE_API_URL || window.dynamicData.
82
82
  axios.interceptors.request.use(config => {
83
83
  const token = localStorage.getItem("privateToken") || localStorage.getItem("publicToken");
84
84
  if (token) config.headers["Authorization"] = `Bearer ${token}`;
85
+
86
+ const key = config.method === 'get' ? 'params' : 'data';
87
+ const data = config[key] || {};
88
+ if(restaurantIdEnv && !data.restaurantId) data.restaurantId = restaurantIdEnv;
89
+ config[key] = data;
90
+
85
91
  return config;
86
92
  }, error => {
87
93
  return Promise.reject(error);
@@ -1,4 +1,4 @@
1
- import { commonErrorCallback, $http } from '../common';
1
+ import {commonErrorCallback, $http, calculateBasketIdParameter, restaurantIdEnv} from '../common';
2
2
 
3
3
  const state = {
4
4
  publicToken: null,
@@ -92,18 +92,17 @@ const actions = {
92
92
  });
93
93
  dispatch("fetchUser");
94
94
  const basketData = JSON.parse(window.localStorage.getItem("basket"));
95
- let hasTarget = false;
96
- if (basketData && basketData.length) {
97
- basketData.map((m) => {
98
- if (m.restaurantId == authData.restaurantId) {
99
- hasTarget = true;
95
+
96
+ const restaurantId = authData.restaurantId || restaurantIdEnv;
97
+ if(Array.isArray(basketData) && restaurantId){
98
+ for(let i = 0; i < basketData.length; i++){
99
+ if (basketData[i].restaurantId == restaurantId) {
100
+ dispatch('assignBasket', restaurantId);
101
+ break;
100
102
  }
101
- });
102
- }
103
- if (authData.restaurantId &&
104
- basketData && basketData.length && hasTarget) {
105
- dispatch('assignBasket', authData.restaurantId)
103
+ }
106
104
  }
105
+
107
106
  return {
108
107
  type: 'success',
109
108
  msg: 'ok'
@@ -126,12 +125,12 @@ const actions = {
126
125
  }
127
126
  )
128
127
  .then((res) => {
129
- const restaurantId = authData.restaurantId ? { restaurantId: authData.restaurantId } : {}
128
+ const restaurantId = authData.restaurantId || restaurantIdEnv;
130
129
  const ordermood = authData.ordermood ? { ordermood: authData.ordermood } : {}
131
130
  dispatch("login", {
132
131
  email: authData.email,
133
132
  password: authData.password,
134
- ...restaurantId,
133
+ restaurantId: restaurantId ? restaurantId : undefined,
135
134
  ...ordermood
136
135
  });
137
136
  return {
@@ -160,21 +159,17 @@ const actions = {
160
159
  commit("authUser", { privateToken, expires_at });
161
160
  dispatch('fetchUser')
162
161
  },
163
- logout({ commit, state, dispatch }, data) {
164
- const restaurantId = data ? { restaurantId: data } : {}
165
-
166
- if (!state.privateToken) {
167
- return;
168
- }
162
+ logout({ commit, state, dispatch }, restaurantId) {
163
+ if (!state.privateToken) return;
169
164
  return $http
170
165
  .get("api/auth/logout", {
171
166
  params: {
172
- ...restaurantId
167
+ restaurantId
173
168
  }
174
169
  })
175
170
  .then(() => {
176
171
  commit("clearAuthData");
177
- dispatch('unassignBasket', data)
172
+ dispatch('unassignBasket', restaurantId)
178
173
  // commit("product/SetShoppingCart", [], { root: true });
179
174
  // commit("product/SetSubtotalPrice", null, { root: true });
180
175
  return true
@@ -201,42 +196,29 @@ const actions = {
201
196
  },
202
197
  assignBasket({ dispatch }, restaurantId) {
203
198
  const basket = JSON.parse(localStorage.getItem("basket"));
204
- const basketId = basket.length ?
205
- basket.find((basket) => basket.restaurantId == restaurantId).basketId
206
- : ''
207
- if (basketId)
208
- $http
209
- .post(`api/basket/assign-basket`, null, { params: { basketId: basketId, restaurantId: restaurantId } })
210
- .then(() => {
211
- dispatch('product/getBasket', restaurantId, { root: true })
212
- })
213
- .catch(
214
- commonErrorCallback()
215
- );
199
+ if(!basket || !Array.isArray(basket)) return;
200
+ const basketId = basket.find(item => Number(item.restaurantId) === Number(restaurantId || restaurantIdEnv))?.basketId
201
+
202
+ basketId && $http
203
+ .post(`api/basket/assign-basket`, null, { params: { basketId: basketId, restaurantId: restaurantId } })
204
+ .then(() => {
205
+ dispatch('product/getBasket', restaurantId, { root: true })
206
+ })
207
+ .catch(
208
+ commonErrorCallback()
209
+ );
216
210
  },
217
211
  unassignBasket({ }, restaurantId) {
218
- if (restaurantId) {
219
- const basket = JSON.parse(localStorage.getItem("basket"));
220
- const basketId = basket.length ?
221
- basket.find((basket) => basket.restaurantId == restaurantId).basketId
222
- : ''
223
- if (basketId)
224
- $http
225
- .post(`api/basket/unassigned-basket`, null, { params: { basketId: basketId } })
226
- .then(() => {
227
- return {
228
- type: 'success',
229
- msg: 'ok'
230
- };
231
- })
232
- .catch(
233
- commonErrorCallback()
234
- );
212
+ const basket = JSON.parse(localStorage.getItem("basket"));
213
+ if(!basket || !Array.isArray(basket)) return;
214
+ let baskets = [];
215
+
216
+ if (restaurantId || restaurantIdEnv) {
217
+ const basketId = basket.find(item => Number(item.restaurantId) === Number(restaurantId || restaurantIdEnv))?.basketId
218
+ if(basketId) baskets.push(basketId);
219
+ } else {
220
+ baskets = basket.map(m => m.basketId)
235
221
  }
236
- else {
237
- let basket = JSON.parse(localStorage.getItem("basket"));
238
- if (!basket.length) return
239
- let baskets = basket.map(m => m.basketId)
240
222
  for (let i = 0; i < baskets.length; i++) {
241
223
  $http
242
224
  .post(`api/basket/unassigned-basket`, null, { params: { basketId: baskets[i] } })
@@ -250,7 +232,6 @@ const actions = {
250
232
  commonErrorCallback()
251
233
  );
252
234
  }
253
- }
254
235
  },
255
236
  restaurantInfo({ commit }, restaurantId) {
256
237
  return $http
@@ -136,14 +136,17 @@ const actions = {
136
136
  .catch(commonErrorCallback("There was an error in creating basket"));
137
137
  },
138
138
  setTable({ rootState }, data) {
139
- const basket = JSON.parse(localStorage.getItem("basket"));
140
- const basketId = !rootState.orderiomApiPackage.auth.privateToken
141
- ? {
142
- basketId: basket.find(
143
- (basket) => basket.restaurantId == data.restaurantId
144
- ).basketId,
145
- }
146
- : {};
139
+ let basketId = undefined;
140
+ try {
141
+ basketId = calculateBasketIdParameter(
142
+ !!rootState.orderiomApiPackage.auth.privateToken,
143
+ data.restaurantId
144
+ );
145
+ } catch (e) {
146
+ console.error(e);
147
+ return { type: "error", msg: "Basket not found" };
148
+ }
149
+
147
150
  let table = null;
148
151
  if (data.tableId) {
149
152
  table = { tableId: data.tableId };
@@ -156,7 +159,7 @@ const actions = {
156
159
  return $http
157
160
  .post("api/basket/set-table", {
158
161
  restaurantId: data.restaurantId,
159
- ...basketId,
162
+ basketId,
160
163
  ...table,
161
164
  qrcode: data.qrcode,
162
165
  })
@@ -173,14 +176,16 @@ const actions = {
173
176
  ? null
174
177
  : rootState.orderiomApiPackage.product.ShoppingCart.find(f => f.basketProductId === data.basketProductId);
175
178
 
176
- const basket = JSON.parse(localStorage.getItem("basket"));
177
- const basketId = !rootState.orderiomApiPackage.auth.privateToken
178
- ? {
179
- basketId: basket.find(
180
- (basket) => basket.restaurantId == data.restaurantId
181
- ).basketId,
182
- }
183
- : {};
179
+ let basketId = undefined;
180
+ try {
181
+ basketId = calculateBasketIdParameter(
182
+ !!rootState.orderiomApiPackage.auth.privateToken,
183
+ data.restaurantId
184
+ );
185
+ } catch (e) {
186
+ console.error(e);
187
+ return { type: "error", msg: "Basket not found" };
188
+ }
184
189
 
185
190
  if (basketProduct) basketProduct.changingQuantity = true;
186
191
 
@@ -188,7 +193,7 @@ const actions = {
188
193
 
189
194
  $http
190
195
  .post("api/basket/add", {
191
- ...basketId,
196
+ basketId,
192
197
  productId: data.productId,
193
198
  quantity: !data.quantity || data.quantity < 1 ? 1 : data.quantity,
194
199
  attributeItems: data.attributeItems.length
@@ -220,20 +225,22 @@ const actions = {
220
225
  };
221
226
  }
222
227
 
223
- const basket = JSON.parse(localStorage.getItem("basket"));
224
- const basketId = !rootState.orderiomApiPackage.auth.privateToken
225
- ? {
226
- basketId: basket.find(
227
- (basket) => basket.restaurantId == data.restaurantId
228
- ).basketId,
229
- }
230
- : {};
228
+ let basketId = undefined;
229
+ try {
230
+ basketId = calculateBasketIdParameter(
231
+ !!rootState.orderiomApiPackage.auth.privateToken,
232
+ data.restaurantId
233
+ );
234
+ } catch (e) {
235
+ console.error(e);
236
+ return { type: "error", msg: "Basket not found" };
237
+ }
231
238
 
232
239
  basketProduct.changingQuantity = true;
233
240
  $http
234
241
  .get("api/basket/remove", {
235
242
  params: {
236
- ...basketId,
243
+ basketId,
237
244
  basketProductId: basketProduct.basketProductId,
238
245
  restaurantId: data.restaurantId,
239
246
  },
@@ -262,20 +269,22 @@ const actions = {
262
269
  };
263
270
  }
264
271
 
265
- const basket = JSON.parse(localStorage.getItem("basket"));
266
- const basketId = !rootState.orderiomApiPackage.auth.privateToken
267
- ? {
268
- basketId: basket.find(
269
- (basket) => basket.restaurantId == data.restaurantId
270
- ).basketId,
271
- }
272
- : {};
272
+ let basketId = undefined;
273
+ try {
274
+ basketId = calculateBasketIdParameter(
275
+ !!rootState.orderiomApiPackage.auth.privateToken,
276
+ data.restaurantId
277
+ );
278
+ } catch (e) {
279
+ console.error(e);
280
+ return { type: "error", msg: "Basket not found" };
281
+ }
273
282
 
274
283
  basketProduct.changingQuantity = true;
275
284
  $http
276
285
  .get("api/basket/remove", {
277
286
  params: {
278
- ...basketId,
287
+ basketId,
279
288
  basketProductId: data.basketProductId,
280
289
  restaurantId: data.restaurantId,
281
290
  },
@@ -303,20 +312,22 @@ const actions = {
303
312
  };
304
313
  }
305
314
 
306
- const basket = JSON.parse(localStorage.getItem("basket"));
307
- const basketId = !rootState.orderiomApiPackage.auth.privateToken
308
- ? {
309
- basketId: basket.find(
310
- (basket) => basket.restaurantId == data.restaurantId
311
- ).basketId,
312
- }
313
- : {};
315
+ let basketId = undefined;
316
+ try {
317
+ basketId = calculateBasketIdParameter(
318
+ !!rootState.orderiomApiPackage.auth.privateToken,
319
+ data.restaurantId
320
+ );
321
+ } catch (e) {
322
+ console.error(e);
323
+ return { type: "error", msg: "Basket not found" };
324
+ }
314
325
 
315
326
  basketProduct.deleting = true;
316
327
  return $http
317
328
  .get("api/basket/delete-basket-product", {
318
329
  params: {
319
- ...basketId,
330
+ basketId,
320
331
  basketProductId: basketProduct.basketProductId,
321
332
  restaurantId: data.restaurantId,
322
333
  },
@@ -345,20 +356,22 @@ const actions = {
345
356
  };
346
357
  }
347
358
 
348
- const basket = JSON.parse(localStorage.getItem("basket"));
349
- const basketId = !rootState.orderiomApiPackage.auth.privateToken
350
- ? {
351
- basketId: basket.find(
352
- (basket) => basket.restaurantId == data.restaurantId
353
- ).basketId,
354
- }
355
- : {};
359
+ let basketId = undefined;
360
+ try {
361
+ basketId = calculateBasketIdParameter(
362
+ !!rootState.orderiomApiPackage.auth.privateToken,
363
+ data.restaurantId
364
+ );
365
+ } catch (e) {
366
+ console.error(e);
367
+ return { type: "error", msg: "Basket not found" };
368
+ }
356
369
 
357
370
  basketProduct.deleting = true;
358
371
  $http
359
372
  .get("api/basket/delete-basket-product", {
360
373
  params: {
361
- ...basketId,
374
+ basketId,
362
375
  basketProductId: data.basketProductId,
363
376
  restaurantId: data.restaurantId,
364
377
  },
@@ -378,21 +391,23 @@ const actions = {
378
391
  ? null
379
392
  : rootState.orderiomApiPackage.product.ShoppingCart.find(f => f.basketProductId === Number(data.basketProductId));
380
393
 
381
- const basket = JSON.parse(localStorage.getItem("basket"));
382
- const basketId = !rootState.orderiomApiPackage.auth.privateToken
383
- ? {
384
- basketId: basket.find(
385
- (basket) => basket.restaurantId == data.restaurantId
386
- ).basketId,
387
- }
388
- : {};
394
+ let basketId = undefined;
395
+ try {
396
+ basketId = calculateBasketIdParameter(
397
+ !!rootState.orderiomApiPackage.auth.privateToken,
398
+ data.restaurantId
399
+ );
400
+ } catch (e) {
401
+ console.error(e);
402
+ return { type: "error", msg: "Basket not found" };
403
+ }
389
404
 
390
405
  data.quantity = parseInt(data.quantity);
391
406
 
392
407
  if (basketProduct) basketProduct.changingQuantity = true;
393
408
  return $http
394
409
  .post("api/basket/add-product-package-items", {
395
- ...basketId,
410
+ basketId,
396
411
  ...data,
397
412
  quantity: !data.quantity || data.quantity < 1 ? 1 : data.quantity,
398
413
  })
@@ -411,14 +426,16 @@ const actions = {
411
426
  ? null
412
427
  : rootState.orderiomApiPackage.product.ShoppingCart.find(f => f.basketProductId === data.basketProductId);
413
428
 
414
- const basket = JSON.parse(localStorage.getItem("basket"));
415
- const basketId = !rootState.orderiomApiPackage.auth.privateToken
416
- ? {
417
- basketId: basket.find(
418
- (basket) => basket.restaurantId == data.restaurantId
419
- ).basketId,
420
- }
421
- : {};
429
+ let basketId = undefined;
430
+ try {
431
+ basketId = calculateBasketIdParameter(
432
+ !!rootState.orderiomApiPackage.auth.privateToken,
433
+ data.restaurantId
434
+ );
435
+ } catch (e) {
436
+ console.error(e);
437
+ return { type: "error", msg: "Basket not found" };
438
+ }
422
439
 
423
440
  if (basketProduct) basketProduct.changingQuantity = true;
424
441
 
@@ -427,7 +444,7 @@ const actions = {
427
444
  $http
428
445
  .post("api/basket/add-attribute-items", {
429
446
  restaurantId: data.restaurantId,
430
- ...basketId,
447
+ basketId,
431
448
  basketProductId: data.basketProductId,
432
449
  attributeItems: data.attributeItems.length
433
450
  ? data.attributeItems
@@ -451,14 +468,16 @@ const actions = {
451
468
  ? null
452
469
  : rootState.orderiomApiPackage.product.ShoppingCart.find(f => f.basketProductId === data.basketProductId);
453
470
 
454
- const basket = JSON.parse(localStorage.getItem("basket"));
455
- const basketId = !rootState.orderiomApiPackage.auth.privateToken
456
- ? {
457
- basketId: basket.find(
458
- (basket) => basket.restaurantId == data.restaurantId
459
- ).basketId,
460
- }
461
- : {};
471
+ let basketId = undefined;
472
+ try {
473
+ basketId = calculateBasketIdParameter(
474
+ !!rootState.orderiomApiPackage.auth.privateToken,
475
+ data.restaurantId
476
+ );
477
+ } catch (e) {
478
+ console.error(e);
479
+ return { type: "error", msg: "Basket not found" };
480
+ }
462
481
 
463
482
  if (basketProduct) basketProduct.changingQuantity = true;
464
483
 
@@ -467,7 +486,7 @@ const actions = {
467
486
  $http
468
487
  .post("api/basket/remove-attribute-items", {
469
488
  restaurantId: data.restaurantId,
470
- ...basketId,
489
+ basketId,
471
490
  basketProductId: data.basketProductId,
472
491
  attributeItems: data.attributeItems.length
473
492
  ? data.attributeItems
@@ -487,17 +506,20 @@ const actions = {
487
506
  });
488
507
  },
489
508
  addAddress({ rootState }, data) {
490
- const basket = JSON.parse(localStorage.getItem("basket"));
491
- const basketId = !rootState.orderiomApiPackage.auth.privateToken
492
- ? {
493
- basketId: basket.find(
494
- (basket) => basket.restaurantId == data.restaurantId
495
- ).basketId,
496
- }
497
- : {};
509
+ let basketId = undefined;
510
+ try {
511
+ basketId = calculateBasketIdParameter(
512
+ !!rootState.orderiomApiPackage.auth.privateToken,
513
+ data.restaurantId
514
+ );
515
+ } catch (e) {
516
+ console.error(e);
517
+ return { type: "error", msg: "Basket not found" };
518
+ }
519
+
498
520
  return $http
499
521
  .post("api/basket/add-address", {
500
- ...basketId,
522
+ basketId,
501
523
  restaurantId: data.restaurantId,
502
524
  userInfo: data.userInfo,
503
525
  })
@@ -507,18 +529,21 @@ const actions = {
507
529
  .catch(commonErrorCallback());
508
530
  },
509
531
  checkAddress({ rootState }, data) {
510
- const basket = JSON.parse(localStorage.getItem("basket"));
511
- const basketId = !rootState.orderiomApiPackage.auth.privateToken
512
- ? {
513
- basketId: basket.find(
514
- (basket) => basket.restaurantId == data.restaurantId
515
- ).basketId,
516
- }
517
- : {};
532
+ let basketId = undefined;
533
+ try {
534
+ basketId = calculateBasketIdParameter(
535
+ !!rootState.orderiomApiPackage.auth.privateToken,
536
+ data.restaurantId
537
+ );
538
+ } catch (e) {
539
+ console.error(e);
540
+ return { type: "error", msg: "Basket not found" };
541
+ }
542
+
518
543
  return $http
519
544
  .get("api/order/check-address", {
520
545
  params: {
521
- ...basketId,
546
+ basketId,
522
547
  restaurantId: data.restaurantId,
523
548
  postalCode: data.postalCode,
524
549
  deliveryType: data.deliveryType,
@@ -540,80 +565,81 @@ const actions = {
540
565
  }
541
566
  },
542
567
  createOrder({ commit, rootState }, data) {
543
- const basketData = JSON.parse(localStorage.getItem("basket"));
544
- const basketId = !rootState.orderiomApiPackage.auth.privateToken
545
- ? {
546
- basketId: data.basketId,
547
- }
548
- : {};
549
- const restaurantID = data.restaurantId
550
- ? { restaurantId: data.restaurantId }
551
- : {};
552
- return $http
553
- .post("api/order/create-order", {
554
- ...basketId,
555
- ...restaurantID,
556
- paymentType: data.paymentType,
557
- ...(data.paymentType === "payFix" && {
558
- qrCode: data.payfix_qrcode,
559
- pin: data.payfix_pin,
560
- }),
561
- tablePaymentId: data.tablePaymentId ? data.tablePaymentId : null,
562
- externalBasketItems: data.externalBasketItems ? data.externalBasketItems: null
563
- })
564
- .then((res) => {
565
- // localStorage.setItem("orderId", res.data.data.order.id);
566
- // commit('setproductOrder', res.data.data.product)
567
- // commit('settotalPriceOrder', res.data.data.order.total_price)
568
- // commit('setorder', res.data.data.order)
569
- // commit('setuserOrder', JSON.parse(res.data.data.order.user_details));
570
- // commit('setshippigPriceOrder', res.data.data.order.shipping_price);
568
+ let basketId = undefined;
569
+ try {
570
+ basketId = calculateBasketIdParameter(
571
+ !!rootState.orderiomApiPackage.auth.privateToken,
572
+ data.restaurantId
573
+ );
574
+ } catch (e) {
575
+ console.error(e);
576
+ return { type: "error", msg: "Basket not found" };
577
+ }
571
578
 
572
- if (
573
- (res.data.data.paymentLink.includes("/paymentstatus/?") ||
574
- res.data.data.paymentLink.includes("/paymentstatus?")) &&
575
- data.$router &&
576
- data.routeName
577
- ) {
578
- data.$router.push({
579
- name: data.routeName,
580
- query: res.data.data.paymentLink
581
- .split("?")[1]
582
- .split("&")
583
- .map((query) => query.split("="))
584
- .reduce((result, element) => {
585
- if (!element) return result;
586
- result[element[0]] = element[1];
587
- return result;
588
- }, {}),
589
- });
590
- } else {
591
- window.location = res.data.data.paymentLink;
592
- }
579
+ return $http.post("api/order/create-order", {
580
+ basketId,
581
+ restaurantId: data.restaurantId || undefined,
582
+ paymentType: data.paymentType,
583
+ ...(data.paymentType === "payFix" && {
584
+ qrCode: data.payfix_qrcode,
585
+ pin: data.payfix_pin,
586
+ }),
587
+ tablePaymentId: data.tablePaymentId ? data.tablePaymentId : null,
588
+ externalBasketItems: data.externalBasketItems ? data.externalBasketItems : null
589
+ }).then(res => {
590
+ // localStorage.setItem("orderId", res.data.data.order.id);
591
+ // commit('setproductOrder', res.data.data.product)
592
+ // commit('settotalPriceOrder', res.data.data.order.total_price)
593
+ // commit('setorder', res.data.data.order)
594
+ // commit('setuserOrder', JSON.parse(res.data.data.order.user_details));
595
+ // commit('setshippigPriceOrder', res.data.data.order.shipping_price);
593
596
 
594
- var index = data.basket
595
- ? basketData.indexOf(
596
- basketData.find((item) => item.basketId == data.basket)
597
- )
598
- : basketData.indexOf(
599
- basketData.find((item) => item.restaurantId == data.restaurantId)
600
- );
601
- if (index > -1) {
602
- basketData.splice(index, 1);
603
- }
604
- localStorage.setItem("basket", JSON.stringify(basketData));
605
- // console.log(basketData);
606
- commit("product/SetSubtotalPrice", "0.00", { root: true });
607
- commit("product/SetShoppingCart", [], { root: true });
608
- commit("product/setBasketInfo", {}, { root: true });
609
- commit("product/setDeliveryTime", "", { root: true });
610
- commit("product/SetTotalPrice", "0.00", { root: true });
611
- return {
612
- type: "success",
613
- msg: "ok",
614
- };
615
- })
616
- .catch(commonErrorCallback());
597
+ if (
598
+ (
599
+ res.data.data.paymentLink.includes("/paymentstatus/?") ||
600
+ res.data.data.paymentLink.includes("/paymentstatus?")
601
+ ) &&
602
+ data.$router &&
603
+ data.routeName
604
+ ) {
605
+ data.$router.push({
606
+ name: data.routeName,
607
+ query: res.data.data.paymentLink
608
+ .split("?")[1]
609
+ .split("&")
610
+ .map((query) => query.split("="))
611
+ .reduce((result, element) => {
612
+ if (!element) return result;
613
+ result[element[0]] = element[1];
614
+ return result;
615
+ }, {}),
616
+ });
617
+ } else {
618
+ window.location = res.data.data.paymentLink;
619
+ }
620
+
621
+ const basketData = JSON.parse(localStorage.getItem("basket"));
622
+ if (basketData) {
623
+ const index = basketId
624
+ ? basketData.findIndex(item => Number(item.basketId) === Number(basketId))
625
+ : basketData.findIndex(item => Number(item.restaurantId) === Number(data.restaurantId || restaurantIdEnv))
626
+ if (index > -1) {
627
+ basketData.splice(index, 1);
628
+ localStorage.setItem("basket", JSON.stringify(basketData));
629
+ }
630
+ }
631
+
632
+ commit("product/SetSubtotalPrice", "0.00", {root: true});
633
+ commit("product/SetShoppingCart", [], {root: true});
634
+ commit("product/setBasketInfo", {}, {root: true});
635
+ commit("product/setDeliveryTime", "", {root: true});
636
+ commit("product/SetTotalPrice", "0.00", { root: true });
637
+
638
+ return {
639
+ type: "success",
640
+ msg: "ok",
641
+ };
642
+ }).catch(commonErrorCallback());
617
643
  },
618
644
  getOrder({ commit }, orderId) {
619
645
  return $http
@@ -656,14 +682,17 @@ const actions = {
656
682
  .catch(commonErrorCallback());
657
683
  },
658
684
  preparePayment({ rootState }, data) {
659
- const basket = JSON.parse(localStorage.getItem("basket"));
660
- const basketId = !rootState.orderiomApiPackage.auth.privateToken
661
- ? {
662
- basketId: basket.find(
663
- (basket) => basket.restaurantId == data.restaurantId
664
- ).basketId,
665
- }
666
- : {};
685
+ let basketId = undefined;
686
+ try {
687
+ basketId = calculateBasketIdParameter(
688
+ !!rootState.orderiomApiPackage.auth.privateToken,
689
+ data.restaurantId
690
+ );
691
+ } catch (e) {
692
+ console.error(e);
693
+ return { type: "error", msg: "Basket not found" };
694
+ }
695
+
667
696
  const restaurantID = data.restaurantId
668
697
  ? { restaurantId: data.restaurantId }
669
698
  : {};
@@ -671,7 +700,7 @@ const actions = {
671
700
  $http
672
701
  .get("api/payment/prepare", {
673
702
  params: {
674
- ...basketId,
703
+ basketId,
675
704
  ...restaurantID,
676
705
  },
677
706
  })
@@ -706,6 +735,10 @@ const actions = {
706
735
  .catch(commonErrorCallback());
707
736
  },
708
737
  getShippingPrice({ commit, rootState }, data) {
738
+ if (!data.postalCode || !data.deliveryType) {
739
+ return;
740
+ }
741
+
709
742
  let basketId = undefined;
710
743
  try {
711
744
  basketId = calculateBasketIdParameter(
@@ -716,9 +749,7 @@ const actions = {
716
749
  console.error(e);
717
750
  return { type: "error", msg: "Basket not found" };
718
751
  }
719
- if (!data.postalCode || !data.deliveryType) {
720
- return;
721
- }
752
+
722
753
  return $http
723
754
  .get(`api/restaurant/shipping-price`, {
724
755
  params: {
@@ -753,21 +784,25 @@ const actions = {
753
784
  .catch(commonErrorCallback());
754
785
  },
755
786
  getTotalPrice({ commit, rootState }, data) {
756
- const basket = JSON.parse(localStorage.getItem("basket"));
757
- const basketId = !rootState.orderiomApiPackage.auth.privateToken
758
- ? {
759
- basketId: basket.find(
760
- (basket) => basket.restaurantId == data.restaurantId
761
- ).basketId,
762
- }
763
- : {};
764
- if (!data.postalCode || !data.deliveryType) {
765
- return;
766
- }
787
+ if (!data.postalCode || !data.deliveryType) {
788
+ return;
789
+ }
790
+
791
+ let basketId = undefined;
792
+ try {
793
+ basketId = calculateBasketIdParameter(
794
+ !!rootState.orderiomApiPackage.auth.privateToken,
795
+ data.restaurantId
796
+ );
797
+ } catch (e) {
798
+ console.error(e);
799
+ return { type: "error", msg: "Basket not found" };
800
+ }
801
+
767
802
  return $http
768
803
  .get(`api/basket/total-price`, {
769
804
  params: {
770
- ...basketId,
805
+ basketId,
771
806
  deliveryType: data.deliveryType,
772
807
  postalCode: data.postalCode,
773
808
  restaurantId: data.restaurantId,
@@ -783,20 +818,20 @@ const actions = {
783
818
  .catch(commonErrorCallback());
784
819
  },
785
820
  addDeliveryTimeToBasket({ dispatch, rootState }, data) {
786
- const basket = JSON.parse(localStorage.getItem("basket"));
787
- const restaurantId = parseInt(
788
- data.restaurantId ? data.restaurantId : restaurantIdEnv
789
- );
790
- const basketId = !rootState.orderiomApiPackage.auth.privateToken
791
- ? {
792
- basketId: basket.find(
793
- (basket) => parseInt(basket.restaurantId) === parseInt(restaurantId)
794
- ).basketId,
795
- }
796
- : {};
821
+ let basketId = undefined;
822
+ try {
823
+ basketId = calculateBasketIdParameter(
824
+ !!rootState.orderiomApiPackage.auth.privateToken,
825
+ data.restaurantId
826
+ );
827
+ } catch (e) {
828
+ console.error(e);
829
+ return { type: "error", msg: "Basket not found" };
830
+ }
831
+
797
832
  return $http
798
833
  .post("api/basket/add-delivery-time", {
799
- ...basketId,
834
+ basketId,
800
835
  delivery_time:
801
836
  data.date && data.time ? data.date + " " + data.time : null,
802
837
  deliveryType: data.deliveryType,
@@ -822,20 +857,23 @@ const actions = {
822
857
  .catch(commonErrorCallback());
823
858
  },
824
859
  addVoucher({ rootState, dispatch }, data) {
825
- const basket = JSON.parse(localStorage.getItem("basket"));
826
- const basketId = !rootState.orderiomApiPackage.auth.privateToken
827
- ? {
828
- basketId: basket.find(
829
- (basket) => basket.restaurantId == data.restaurantId
830
- ).basketId,
831
- }
832
- : {};
860
+ let basketId = undefined;
861
+ try {
862
+ basketId = calculateBasketIdParameter(
863
+ !!rootState.orderiomApiPackage.auth.privateToken,
864
+ data.restaurantId
865
+ );
866
+ } catch (e) {
867
+ console.error(e);
868
+ return { type: "error", msg: "Basket not found" };
869
+ }
870
+
833
871
  return $http
834
872
  .post("api/basket/add-voucher-to-basket", null, {
835
873
  params: {
836
874
  voucherCode: data.voucherCode,
837
875
  restaurantId: data.restaurantId,
838
- ...basketId,
876
+ basketId,
839
877
  },
840
878
  })
841
879
  .then((result) => {
@@ -848,17 +886,21 @@ const actions = {
848
886
  .catch(commonErrorCallback());
849
887
  },
850
888
  removeVoucher({ rootState, dispatch }, restaurantId) {
851
- const basket = JSON.parse(localStorage.getItem("basket"));
852
- const basketId = !rootState.orderiomApiPackage.auth.privateToken
853
- ? {
854
- basketId: basket.find((basket) => basket.restaurantId == restaurantId)
855
- .basketId,
856
- }
857
- : {};
889
+ let basketId = undefined;
890
+ try {
891
+ basketId = calculateBasketIdParameter(
892
+ !!rootState.orderiomApiPackage.auth.privateToken,
893
+ restaurantId
894
+ );
895
+ } catch (e) {
896
+ console.error(e);
897
+ return { type: "error", msg: "Basket not found" };
898
+ }
899
+
858
900
  return $http
859
901
  .get("api/basket/remove-voucher-from-basket", {
860
902
  params: {
861
- ...basketId,
903
+ basketId,
862
904
  restaurantId: restaurantId,
863
905
  },
864
906
  })
@@ -872,17 +914,20 @@ const actions = {
872
914
  .catch(commonErrorCallback());
873
915
  },
874
916
  addTip({ rootState, dispatch }, data) {
875
- const basket = JSON.parse(localStorage.getItem("basket"));
876
- const basketId = !rootState.orderiomApiPackage.auth.privateToken
877
- ? {
878
- basketId: basket.find(
879
- (basket) => basket.restaurantId == data.restaurantId
880
- ).basketId,
881
- }
882
- : {};
917
+ let basketId = undefined;
918
+ try {
919
+ basketId = calculateBasketIdParameter(
920
+ !!rootState.orderiomApiPackage.auth.privateToken,
921
+ data.restaurantId
922
+ );
923
+ } catch (e) {
924
+ console.error(e);
925
+ return { type: "error", msg: "Basket not found" };
926
+ }
927
+
883
928
  return $http
884
929
  .post("api/basket/add-tip", {
885
- ...basketId,
930
+ basketId,
886
931
  restaurantId: data.restaurantId,
887
932
  tip_price: data.tip_price,
888
933
  })
@@ -896,16 +941,20 @@ const actions = {
896
941
  .catch(commonErrorCallback());
897
942
  },
898
943
  removeTip({ rootState, dispatch }, restaurantId) {
899
- const basket = JSON.parse(localStorage.getItem("basket"));
900
- const basketId = !rootState.orderiomApiPackage.auth.privateToken
901
- ? {
902
- basketId: basket.find((basket) => basket.restaurantId == restaurantId)
903
- .basketId,
904
- }
905
- : {};
944
+ let basketId = undefined;
945
+ try {
946
+ basketId = calculateBasketIdParameter(
947
+ !!rootState.orderiomApiPackage.auth.privateToken,
948
+ restaurantId
949
+ );
950
+ } catch (e) {
951
+ console.error(e);
952
+ return { type: "error", msg: "Basket not found" };
953
+ }
954
+
906
955
  return $http
907
956
  .put("api/basket/remove-tip-from-basket", {
908
- ...basketId,
957
+ basketId,
909
958
  restaurantId: restaurantId,
910
959
  })
911
960
  .then((result) => {
@@ -918,19 +967,22 @@ const actions = {
918
967
  .catch(commonErrorCallback());
919
968
  },
920
969
  checkOrderValidity({ rootState }, data) {
921
- const basket = JSON.parse(localStorage.getItem("basket"));
922
- const basketId = !rootState.orderiomApiPackage.auth.privateToken
923
- ? {
924
- basketId: basket.find(
925
- (basket) => basket.restaurantId == data.restaurantId
926
- ).basketId,
927
- }
928
- : {};
970
+ let basketId = undefined;
971
+ try {
972
+ basketId = calculateBasketIdParameter(
973
+ !!rootState.orderiomApiPackage.auth.privateToken,
974
+ data.restaurantId
975
+ );
976
+ } catch (e) {
977
+ console.error(e);
978
+ return { type: "error", msg: "Basket not found" };
979
+ }
980
+
929
981
  return $http
930
982
  .post("api/order/is-valid", null, {
931
983
  params: {
932
984
  restaurantId: data.restaurantId,
933
- ...basketId,
985
+ basketId,
934
986
  },
935
987
  })
936
988
  .then((result) => {