orderiom-api-package 0.2.49 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orderiom-api-package",
3
- "version": "0.2.49",
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,279 +243,189 @@ 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
-
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
256
 
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
288
 
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
-
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
-
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
321
 
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
- let item = rootState.orderiomApiPackage.product.ShoppingCart.find(
354
- (f) => f.RestaurantProductId == data.productId
338
+ const basketProduct = rootState.orderiomApiPackage.product.ShoppingCart.find(f =>
339
+ f.RestaurantProductId === Number(data.productId)
355
340
  );
341
+
342
+ if(!basketProduct){
343
+ return {
344
+ type: 'error',
345
+ msg: 'Product not found'
346
+ }
347
+ }
348
+
356
349
  const basket = JSON.parse(localStorage.getItem("basket"));
357
350
  const basketId = !rootState.orderiomApiPackage.auth.privateToken ? {
358
351
  basketId: basket.find((basket) => basket.restaurantId == data.restaurantId).basketId
359
352
  } : {}
360
- axios
361
- .get("api/basket/delete-basket-product", {
362
- params: {
363
- ...basketId,
364
- basketProductId: item.basketProductId,
365
- restaurantId: data.restaurantId,
366
- },
367
- })
368
- .then(() => {
369
- dispatch("product/getBasket", data.restaurantId, { root: true }).then(() => {
370
- dispatch(
371
- "product/getProducts",
372
- { category: rootState.orderiomApiPackage.product.selectedCategory, restaurantId: data.restaurantId }, { root: true }
373
- );
374
- })
375
- })
376
- .catch((error) => {
377
- if (error.response) {
378
- if (error.response.status == 422) {
379
- return {
380
- type: 'error',
381
- msg: Object.values(error.response.data.error.validation).map(m => { return m[0] }).toString()
382
-
383
- }
384
- }
385
- if (error.response.status == 401 || error.response.status == 403 || error.response.status == 404 || error.response.status == 400) {
386
-
387
- return {
388
- type: 'error',
389
- msg: error.response.data.message.body
390
-
391
- }
392
- }
393
-
394
- }
395
353
 
396
- });
354
+ basketProduct.deleting = true;
355
+ return axios.get("api/basket/delete-basket-product", {
356
+ params: {
357
+ ...basketId,
358
+ basketProductId: basketProduct.basketProductId,
359
+ restaurantId: data.restaurantId,
360
+ },
361
+ }).then(async () => {
362
+ return await dispatch('shoppingCartUpdateCallback', { restaurantId: data.restaurantId });
363
+ }).catch(
364
+ commonErrorCallback
365
+ ).finally(() => {
366
+ basketProduct.deleting = false;
367
+ });
397
368
  },
369
+ //TODO: this function is deprecated. use deleteFromBasket instead.
398
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
+
399
382
  const basket = JSON.parse(localStorage.getItem("basket"));
400
383
  const basketId = !rootState.orderiomApiPackage.auth.privateToken ? {
401
384
  basketId: basket.find((basket) => basket.restaurantId == data.restaurantId).basketId
402
385
  } : {}
403
- axios
404
- .get("api/basket/delete-basket-product", {
405
- params: {
406
- ...basketId,
407
- basketProductId: data.basketProductId,
408
- restaurantId: data.restaurantId,
409
- },
410
- })
411
- .then(() => {
412
- dispatch("product/getBasket", data.restaurantId, { root: true }).then(() => {
413
- dispatch(
414
- "product/getProducts",
415
- { category: rootState.orderiomApiPackage.product.selectedCategory, restaurantId: data.restaurantId }, { root: true }
416
- );
417
- })
418
- })
419
- .catch((error) => {
420
- if (error.response) {
421
- if (error.response.status == 422) {
422
- return {
423
- type: 'error',
424
- msg: Object.values(error.response.data.error.validation).map(m => { return m[0] }).toString()
425
-
426
- }
427
- }
428
- if (error.response.status == 401 || error.response.status == 403 || error.response.status == 404 || error.response.status == 400) {
429
-
430
- return {
431
- type: 'error',
432
- msg: error.response.data.message.body
433
386
 
434
- }
435
- }
436
-
437
- }
438
-
439
- });
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
+ });
440
401
  },
441
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
+
442
414
  const basket = JSON.parse(localStorage.getItem("basket"));
443
415
  const basketId = !rootState.orderiomApiPackage.auth.privateToken ? {
444
416
  basketId: basket.find((basket) => basket.restaurantId == data.restaurantId).basketId
445
417
  } : {}
418
+
419
+ basketProduct.changingQuantity = true;
446
420
  return axios.post('api/basket/add-product-package-items', {
447
421
  ...basketId,
448
422
  ...data
449
- }).then((result) => {
450
- dispatch("product/getBasket", data.restaurantId, { root: true }).then(() => {
451
- dispatch(
452
- "product/getProducts",
453
- { category: rootState.orderiomApiPackage.product.selectedCategory, restaurantId: data.restaurantId }, { root: true }
454
- );
455
- });
456
- return {
457
- type: 'success',
458
- msg: 'added'
459
- }
460
- }).catch((error) => {
461
- if (error.response) {
462
- if (error.response.status == 422) {
463
- return {
464
- type: 'error',
465
- msg: Object.values(error.response.data.error.validation).map(m => { return m[0] }).toString()
466
-
467
- }
468
- }
469
- if (error.response.status == 401 || error.response.status == 403 || error.response.status == 404 || error.response.status == 400) {
470
-
471
- return {
472
- type: 'error',
473
- msg: error.response.data.message.body
474
-
475
- }
476
- }
477
-
478
- }
423
+ }).then(async () => {
424
+ return await dispatch('shoppingCartUpdateCallback', { restaurantId: data.restaurantId });
425
+ }).catch(
426
+ commonErrorCallback
427
+ ).finally(() => {
428
+ basketProduct.changingQuantity = false;
479
429
  });
480
430
  },
481
431
  addAddress({ rootState }, data) {
@@ -69,7 +69,11 @@ const mutations = {
69
69
  state.selectedProduct = product
70
70
  },
71
71
  SetShoppingCart(state, products) {
72
- state.ShoppingCart = products;
72
+ state.ShoppingCart = products.map(item => ({
73
+ ...item,
74
+ deleting: false,
75
+ changingQuantity: false,
76
+ }));
73
77
  },
74
78
  SetSubtotalPrice(state, SubtotalPrice) {
75
79
  state.SubtotalPrice = SubtotalPrice;
@@ -299,21 +303,22 @@ const actions = {
299
303
  commit("setProduct", products);
300
304
  return { type: 'success', data: products }
301
305
  }).catch(error => {
302
- if(!error.response) return;
303
-
304
- const status = error.response.status;
306
+ const status = error.response ? error.response.status : null;
305
307
  if (status === 422) {
306
308
  return {
307
309
  type: 'error',
308
310
  msg: Object.values(error.response.data.error.validation).map(m => { return m[0] }).toString()
309
311
  }
310
- }
311
- if ([401, 403, 404, 400].includes(status)) {
312
+ } else if ([401, 403, 404, 400].includes(status)) {
312
313
  return {
313
314
  type: 'error',
314
315
  msg: error.response.data.message.body
315
316
  }
316
317
  }
318
+ return {
319
+ type: 'error',
320
+ msg: 'Something went wrong'
321
+ }
317
322
  });
318
323
  },
319
324
  getBasket({ commit, rootState }, restaurantId) {