orderiom-api-package 0.2.50 → 0.2.51

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/modules/order.js +158 -227
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orderiom-api-package",
3
- "version": "0.2.50",
3
+ "version": "0.2.51",
4
4
  "description": "this package will install all neccessary api calls for every orderiom restaurant",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -19,6 +19,25 @@ function calculateBasketIdParameter(isLogin) {
19
19
  return foundBasket.basketId;
20
20
  }
21
21
 
22
+ function commonErrorCallback(error){
23
+ const status = error.response ? error.response.status : null;
24
+ if (status === 422) {
25
+ return {
26
+ type: 'error',
27
+ msg: Object.values(error.response.data.error.validation).map(m => m[0]).toString()
28
+ }
29
+ } else if ([401, 403, 404, 400].includes(status)) {
30
+ return {
31
+ type: 'error',
32
+ msg: error.response.data.message.body
33
+ }
34
+ }
35
+ return {
36
+ type: 'error',
37
+ msg: 'Something went wrong'
38
+ }
39
+ }
40
+
22
41
  const state = () => ({
23
42
  productOrder: null,
24
43
  userOrder: {},
@@ -66,6 +85,27 @@ const mutations = {
66
85
 
67
86
  }
68
87
  const actions = {
88
+ async shoppingCartUpdateCallback({ dispatch, rootState }, data){
89
+ const getBasketSuccess = await dispatch("product/getBasket", data.restaurantId, { root: true });
90
+ const getProductsResult = await dispatch(
91
+ "product/getProducts",
92
+ {
93
+ category: rootState.orderiomApiPackage.product.selectedCategory,
94
+ restaurantId: data.restaurantId
95
+ },
96
+ { root: true }
97
+ );
98
+ if(!getBasketSuccess || getProductsResult.type !== 'success'){
99
+ return {
100
+ type: 'error',
101
+ msg: 'There was an error in fetching basket and products'
102
+ }
103
+ }
104
+ return {
105
+ type: 'success',
106
+ msg: ''
107
+ }
108
+ },
69
109
  createBasket({ }, data) {
70
110
  var basketData = JSON.parse(localStorage.getItem("basket") || "[]");
71
111
  let delivery_time = data.delivery_time ? { delivery_time: data.delivery_time } : null
@@ -203,151 +243,96 @@ const actions = {
203
243
  });
204
244
  },
205
245
  addToBasket({ dispatch, rootState }, data) {
246
+ // might be null
247
+ const basketProduct = rootState.orderiomApiPackage.product.ShoppingCart.find(f =>
248
+ f.RestaurantProductId === Number(data.productId)
249
+ );
250
+
206
251
  const basket = JSON.parse(localStorage.getItem("basket"));
207
252
  const basketId = !rootState.orderiomApiPackage.auth.privateToken ? {
208
253
  basketId: basket.find((basket) => basket.restaurantId == data.restaurantId).basketId
209
254
  } : {}
210
255
  const attributeItems = data.attributeItems.length ? { attributeItems: data.attributeItems } : {}
211
- axios
212
- .post("api/basket/add", {
213
- ...basketId,
214
- productId: data.productId,
215
- quantity: 1,
216
- ...attributeItems,
217
- restaurantId: data.restaurantId,
218
- })
219
- .then(() => {
220
- dispatch("product/getBasket", data.restaurantId, { root: true }).then(() => {
221
256
 
222
- dispatch(
223
- "product/getProducts",
224
- { category: rootState.orderiomApiPackage.product.selectedCategory, restaurantId: data.restaurantId }, { root: true }
225
- );
226
- });
227
-
228
- // dispatch(
229
- // "product/getAttributeItems",
230
- // rootState.orderiomApiPackage.product.selectedCategory, { root: true }
231
- // );
232
- })
233
- .catch((error) => {
234
- // dispatch('createBasket')
235
- if (error.response) {
236
- if (error.response.status == 422) {
237
- return {
238
- type: 'error',
239
- msg: Object.values(error.response.data.error.validation).map(m => { return m[0] }).toString()
240
-
241
- }
242
- }
243
- if (error.response.status == 401 || error.response.status == 403 || error.response.status == 404 || error.response.status == 400) {
244
-
245
- return {
246
- type: 'error',
247
- msg: error.response.data.message.body
248
-
249
- }
250
- }
251
-
252
- }
253
- });
257
+ if(basketProduct) basketProduct.changingQuantity = true;
258
+ axios.post("api/basket/add", {
259
+ ...basketId,
260
+ productId: data.productId,
261
+ quantity: 1,
262
+ ...attributeItems,
263
+ restaurantId: data.restaurantId,
264
+ }).then(async () => {
265
+ return await dispatch('shoppingCartUpdateCallback', { restaurantId: data.restaurantId });
266
+ }).catch(
267
+ commonErrorCallback
268
+ ).finally(() => {
269
+ if(basketProduct) basketProduct.changingQuantity = false;
270
+ });
254
271
  },
255
272
  removeFromBasket({ dispatch, rootState }, data) {
256
- let item = rootState.orderiomApiPackage.product.ShoppingCart.find(
257
- (f) => f.RestaurantProductId == data.productId
273
+ const basketProduct = rootState.orderiomApiPackage.product.ShoppingCart.find(f =>
274
+ f.RestaurantProductId === Number(data.productId)
258
275
  );
276
+
277
+ if(!basketProduct){
278
+ return {
279
+ type: 'error',
280
+ msg: 'Product not found'
281
+ }
282
+ }
283
+
259
284
  const basket = JSON.parse(localStorage.getItem("basket"));
260
285
  const basketId = !rootState.orderiomApiPackage.auth.privateToken ? {
261
286
  basketId: basket.find((basket) => basket.restaurantId == data.restaurantId).basketId
262
287
  } : {}
263
- axios
264
- .get("api/basket/remove", {
265
- params: {
266
- ...basketId,
267
- basketProductId: item.basketProductId,
268
- restaurantId: data.restaurantId,
269
- },
270
- })
271
- .then(() => {
272
- dispatch("product/getBasket", data.restaurantId, { root: true }).then(() => {
273
- dispatch(
274
- "product/getProducts",
275
- { category: rootState.orderiomApiPackage.product.selectedCategory, restaurantId: data.restaurantId }, { root: true }
276
- );
277
- });
278
-
279
- // dispatch(
280
- // "product/getAttributeItems",
281
- // rootState.orderiomApiPackage.product.selectedCategory, { root: true }
282
- // );
283
- })
284
- .catch((error) => {
285
- if (error.response) {
286
- if (error.response.status == 422) {
287
- return {
288
- type: 'error',
289
- msg: Object.values(error.response.data.error.validation).map(m => { return m[0] }).toString()
290
288
 
291
- }
292
- }
293
- if (error.response.status == 401 || error.response.status == 403 || error.response.status == 404 || error.response.status == 400) {
294
-
295
- return {
296
- type: 'error',
297
- msg: error.response.data.message.body
298
-
299
- }
300
- }
301
-
302
- }
303
- });
289
+ basketProduct.changingQuantity = true;
290
+ axios.get("api/basket/remove", {
291
+ params: {
292
+ ...basketId,
293
+ basketProductId: basketProduct.basketProductId,
294
+ restaurantId: data.restaurantId,
295
+ },
296
+ }).then(async () => {
297
+ return await dispatch('shoppingCartUpdateCallback', { restaurantId: data.restaurantId });
298
+ }).catch(
299
+ commonErrorCallback
300
+ ).finally(() => {
301
+ basketProduct.changingQuantity = false;
302
+ });
304
303
  },
304
+ //TODO: this function is deprecated. use removeFromBasket instead.
305
305
  removeFromBasketWithAttr({ dispatch, rootState }, data) {
306
+ const basketProduct = rootState.orderiomApiPackage.product.ShoppingCart.find(f =>
307
+ f.basketProductId === Number(data.basketProductId)
308
+ );
309
+
310
+ if(!basketProduct){
311
+ return {
312
+ type: 'error',
313
+ msg: 'Product not found'
314
+ }
315
+ }
316
+
306
317
  const basket = JSON.parse(localStorage.getItem("basket"));
307
318
  const basketId = !rootState.orderiomApiPackage.auth.privateToken ? {
308
319
  basketId: basket.find((basket) => basket.restaurantId == data.restaurantId).basketId
309
320
  } : {}
310
- axios
311
- .get("api/basket/remove", {
312
- params: {
313
- ...basketId,
314
- basketProductId: data.basketProductId,
315
- restaurantId: data.restaurantId,
316
- },
317
- })
318
- .then(() => {
319
- dispatch("product/getBasket", data.restaurantId, { root: true }).then(() => {
320
- dispatch(
321
- "product/getProducts",
322
- { category: rootState.orderiomApiPackage.product.selectedCategory, restaurantId: data.restaurantId }, { root: true }
323
- );
324
- });
325
321
 
326
- // dispatch(
327
- // "product/getAttributeItems",
328
- // rootState.orderiomApiPackage.product.selectedCategory, { root: true }
329
- // );
330
- })
331
- .catch((error) => {
332
- if (error.response) {
333
- if (error.response.status == 422) {
334
- return {
335
- type: 'error',
336
- msg: Object.values(error.response.data.error.validation).map(m => { return m[0] }).toString()
337
-
338
- }
339
- }
340
- if (error.response.status == 401 || error.response.status == 403 || error.response.status == 404 || error.response.status == 400) {
341
-
342
- return {
343
- type: 'error',
344
- msg: error.response.data.message.body
345
-
346
- }
347
- }
348
-
349
- }
350
- });
322
+ basketProduct.changingQuantity = true;
323
+ axios.get("api/basket/remove", {
324
+ params: {
325
+ ...basketId,
326
+ basketProductId: data.basketProductId,
327
+ restaurantId: data.restaurantId,
328
+ },
329
+ }).then(async () => {
330
+ return await dispatch('shoppingCartUpdateCallback', { restaurantId: data.restaurantId });
331
+ }).catch(
332
+ commonErrorCallback
333
+ ).finally(() => {
334
+ basketProduct.changingQuantity = false;
335
+ });
351
336
  },
352
337
  deleteFromBasket({ dispatch, rootState }, data) {
353
338
  const basketProduct = rootState.orderiomApiPackage.product.ShoppingCart.find(f =>
@@ -374,127 +359,73 @@ const actions = {
374
359
  restaurantId: data.restaurantId,
375
360
  },
376
361
  }).then(async () => {
377
- const getBasketSuccess = await dispatch("product/getBasket", data.restaurantId, { root: true });
378
- const getProductsResult = await dispatch(
379
- "product/getProducts",
380
- {
381
- category: rootState.orderiomApiPackage.product.selectedCategory,
382
- restaurantId: data.restaurantId
383
- },
384
- { root: true }
385
- );
386
- if(!getBasketSuccess || getProductsResult.type !== 'success'){
387
- return {
388
- type: 'error',
389
- msg: 'There was an error in fetching basket and products'
390
- }
391
- }
392
- return {
393
- type: 'success',
394
- msg: ''
395
- }
396
- }).catch(error => {
397
- const status = error.response ? error.response.status : null;
398
- if (status === 422) {
399
- return {
400
- type: 'error',
401
- msg: Object.values(error.response.data.error.validation).map(m => { return m[0] }).toString()
402
- }
403
- } else if ([401, 403, 404, 400].includes(status)) {
404
- return {
405
- type: 'error',
406
- msg: error.response.data.message.body
407
- }
408
- }
409
- return {
410
- type: 'error',
411
- msg: 'Something went wrong'
412
- }
413
- }).finally(() => {
362
+ return await dispatch('shoppingCartUpdateCallback', { restaurantId: data.restaurantId });
363
+ }).catch(
364
+ commonErrorCallback
365
+ ).finally(() => {
414
366
  basketProduct.deleting = false;
415
367
  });
416
368
  },
369
+ //TODO: this function is deprecated. use deleteFromBasket instead.
417
370
  deleteFromBasketWithAttr({ dispatch, rootState }, data) {
371
+ const basketProduct = rootState.orderiomApiPackage.product.ShoppingCart.find(f =>
372
+ f.basketProductId === Number(data.basketProductId)
373
+ );
374
+
375
+ if(!basketProduct){
376
+ return {
377
+ type: 'error',
378
+ msg: 'Product not found'
379
+ }
380
+ }
381
+
418
382
  const basket = JSON.parse(localStorage.getItem("basket"));
419
383
  const basketId = !rootState.orderiomApiPackage.auth.privateToken ? {
420
384
  basketId: basket.find((basket) => basket.restaurantId == data.restaurantId).basketId
421
385
  } : {}
422
- axios
423
- .get("api/basket/delete-basket-product", {
424
- params: {
425
- ...basketId,
426
- basketProductId: data.basketProductId,
427
- restaurantId: data.restaurantId,
428
- },
429
- })
430
- .then(() => {
431
- dispatch("product/getBasket", data.restaurantId, { root: true }).then(() => {
432
- dispatch(
433
- "product/getProducts",
434
- { category: rootState.orderiomApiPackage.product.selectedCategory, restaurantId: data.restaurantId }, { root: true }
435
- );
436
- })
437
- })
438
- .catch((error) => {
439
- if (error.response) {
440
- if (error.response.status == 422) {
441
- return {
442
- type: 'error',
443
- msg: Object.values(error.response.data.error.validation).map(m => { return m[0] }).toString()
444
386
 
445
- }
446
- }
447
- if (error.response.status == 401 || error.response.status == 403 || error.response.status == 404 || error.response.status == 400) {
448
-
449
- return {
450
- type: 'error',
451
- msg: error.response.data.message.body
452
-
453
- }
454
- }
455
-
456
- }
457
-
458
- });
387
+ basketProduct.deleting = true;
388
+ axios.get("api/basket/delete-basket-product", {
389
+ params: {
390
+ ...basketId,
391
+ basketProductId: data.basketProductId,
392
+ restaurantId: data.restaurantId,
393
+ },
394
+ }).then(async () => {
395
+ return await dispatch('shoppingCartUpdateCallback', { restaurantId: data.restaurantId });
396
+ }).catch(
397
+ commonErrorCallback
398
+ ).finally(() => {
399
+ basketProduct.deleting = false;
400
+ });
459
401
  },
460
402
  addProductPackageItem({ dispatch, rootState }, data) {
403
+ const basketProduct = rootState.orderiomApiPackage.product.ShoppingCart.find(f =>
404
+ f.RestaurantProductId === Number(data.productId)
405
+ );
406
+
407
+ if(!basketProduct){
408
+ return {
409
+ type: 'error',
410
+ msg: 'Product not found'
411
+ }
412
+ }
413
+
461
414
  const basket = JSON.parse(localStorage.getItem("basket"));
462
415
  const basketId = !rootState.orderiomApiPackage.auth.privateToken ? {
463
416
  basketId: basket.find((basket) => basket.restaurantId == data.restaurantId).basketId
464
417
  } : {}
418
+
419
+ basketProduct.changingQuantity = true;
465
420
  return axios.post('api/basket/add-product-package-items', {
466
421
  ...basketId,
467
422
  ...data
468
- }).then((result) => {
469
- dispatch("product/getBasket", data.restaurantId, { root: true }).then(() => {
470
- dispatch(
471
- "product/getProducts",
472
- { category: rootState.orderiomApiPackage.product.selectedCategory, restaurantId: data.restaurantId }, { root: true }
473
- );
474
- });
475
- return {
476
- type: 'success',
477
- msg: 'added'
478
- }
479
- }).catch((error) => {
480
- if (error.response) {
481
- if (error.response.status == 422) {
482
- return {
483
- type: 'error',
484
- msg: Object.values(error.response.data.error.validation).map(m => { return m[0] }).toString()
485
-
486
- }
487
- }
488
- if (error.response.status == 401 || error.response.status == 403 || error.response.status == 404 || error.response.status == 400) {
489
-
490
- return {
491
- type: 'error',
492
- msg: error.response.data.message.body
493
-
494
- }
495
- }
496
-
497
- }
423
+ }).then(async () => {
424
+ return await dispatch('shoppingCartUpdateCallback', { restaurantId: data.restaurantId });
425
+ }).catch(
426
+ commonErrorCallback
427
+ ).finally(() => {
428
+ basketProduct.changingQuantity = false;
498
429
  });
499
430
  },
500
431
  addAddress({ rootState }, data) {