From f23d77231ac11137bc8714a2759e3a293502d7c7 Mon Sep 17 00:00:00 2001 From: matheusgnreis Date: Tue, 22 Aug 2023 17:52:14 -0300 Subject: [PATCH 1/6] fix(sync-cart-to-api): avoid to create two different completed carts in conformation --- .../src/lib/sync-cart-to-api.js | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/@ecomplus/storefront-app/src/lib/sync-cart-to-api.js b/@ecomplus/storefront-app/src/lib/sync-cart-to-api.js index b8066b84c..2814eda7e 100644 --- a/@ecomplus/storefront-app/src/lib/sync-cart-to-api.js +++ b/@ecomplus/storefront-app/src/lib/sync-cart-to-api.js @@ -82,6 +82,9 @@ const prepareCart = cart => { } else { url = '/carts.json' method = 'POST' + if (window.storefrontApp && window.storefrontApp.router && window.storefrontApp.router.currentRoute && window.storefrontApp.router.currentRoute.name === 'confirmation' && completed === undefined && orders === undefined) { + return {} + } } return { url, @@ -111,20 +114,26 @@ const queueUpdateCart = tryRequestApi => new Promise(resolve => { const upsertCart = () => { if (ecomPassport.checkAuthorization() && ecomCart.data.items.length) { const { url, method, cleanedData } = prepareCart(ecomCart.data) - const tryRequestApi = () => { - return ecomPassport.requestApi(url, method, cleanedData) - .catch(console.error) + if (url && method && cleanedData) { + const tryRequestApi = () => { + return ecomPassport.requestApi(url, method, cleanedData) + .catch(console.error) + } + console.log('before create') + console.log(url, method, cleanedData) + return method === 'POST' + ? tryRequestApi().then(({ data }) => { + fetchCart(data._id) + setTimeout(() => { + ecomPassport.requestApi(`/carts/${data._id}.json`, 'PATCH', { + permalink: `https://${window.location.host}${window.location.pathname}#/cart/${data._id}` + }).catch(console.error) + }, 300) + }) + : queueUpdateCart(tryRequestApi) + } else { + return Promise.resolve() } - return method === 'POST' - ? tryRequestApi().then(({ data }) => { - fetchCart(data._id) - setTimeout(() => { - ecomPassport.requestApi(`/carts/${data._id}.json`, 'PATCH', { - permalink: `https://${window.location.host}${window.location.pathname}#/cart/${data._id}` - }).catch(console.error) - }, 300) - }) - : queueUpdateCart(tryRequestApi) } else { return Promise.resolve() } From 475b2957e5c67a17bb2392f9aaad52209a206578 Mon Sep 17 00:00:00 2001 From: matheusgnreis Date: Tue, 22 Aug 2023 17:53:08 -0300 Subject: [PATCH 2/6] chore: remove logs --- @ecomplus/storefront-app/src/lib/sync-cart-to-api.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/@ecomplus/storefront-app/src/lib/sync-cart-to-api.js b/@ecomplus/storefront-app/src/lib/sync-cart-to-api.js index 2814eda7e..bbdcca94a 100644 --- a/@ecomplus/storefront-app/src/lib/sync-cart-to-api.js +++ b/@ecomplus/storefront-app/src/lib/sync-cart-to-api.js @@ -119,8 +119,6 @@ const upsertCart = () => { return ecomPassport.requestApi(url, method, cleanedData) .catch(console.error) } - console.log('before create') - console.log(url, method, cleanedData) return method === 'POST' ? tryRequestApi().then(({ data }) => { fetchCart(data._id) From fc1a7642a8e815e2b2a80ddf43c69406a813bbbf Mon Sep 17 00:00:00 2001 From: Matheus Reis <35343551+matheusgnreis@users.noreply.github.com> Date: Wed, 23 Aug 2023 10:05:09 -0300 Subject: [PATCH 3/6] Update @ecomplus/storefront-app/src/lib/sync-cart-to-api.js Co-authored-by: Leonardo Matos --- .../src/lib/sync-cart-to-api.js | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/@ecomplus/storefront-app/src/lib/sync-cart-to-api.js b/@ecomplus/storefront-app/src/lib/sync-cart-to-api.js index bbdcca94a..3cfe26aff 100644 --- a/@ecomplus/storefront-app/src/lib/sync-cart-to-api.js +++ b/@ecomplus/storefront-app/src/lib/sync-cart-to-api.js @@ -114,23 +114,23 @@ const queueUpdateCart = tryRequestApi => new Promise(resolve => { const upsertCart = () => { if (ecomPassport.checkAuthorization() && ecomCart.data.items.length) { const { url, method, cleanedData } = prepareCart(ecomCart.data) - if (url && method && cleanedData) { - const tryRequestApi = () => { - return ecomPassport.requestApi(url, method, cleanedData) - .catch(console.error) - } - return method === 'POST' - ? tryRequestApi().then(({ data }) => { - fetchCart(data._id) - setTimeout(() => { - ecomPassport.requestApi(`/carts/${data._id}.json`, 'PATCH', { - permalink: `https://${window.location.host}${window.location.pathname}#/cart/${data._id}` - }).catch(console.error) - }, 300) - }) - : queueUpdateCart(tryRequestApi) - } else { - return Promise.resolve() + if (!cleanedData) { + return Promise.resolve(null) + } + const tryRequestApi = () => { + return ecomPassport.requestApi(url, method, cleanedData) + .catch(console.error) + } + return method === 'POST' + ? tryRequestApi().then(({ data }) => { + fetchCart(data._id) + setTimeout(() => { + ecomPassport.requestApi(`/carts/${data._id}.json`, 'PATCH', { + permalink: `https://${window.location.host}${window.location.pathname}#/cart/${data._id}` + }).catch(console.error) + }, 300) + }) + : queueUpdateCart(tryRequestApi) } } else { return Promise.resolve() From 6da5d737abd5a4263a50625c0081f9127d264498 Mon Sep 17 00:00:00 2001 From: matheusgnreis Date: Wed, 23 Aug 2023 13:01:35 -0300 Subject: [PATCH 4/6] chore(sync-cart): import router instead of using global variable --- @ecomplus/storefront-app/src/lib/sync-cart-to-api.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/@ecomplus/storefront-app/src/lib/sync-cart-to-api.js b/@ecomplus/storefront-app/src/lib/sync-cart-to-api.js index 3cfe26aff..61a729a69 100644 --- a/@ecomplus/storefront-app/src/lib/sync-cart-to-api.js +++ b/@ecomplus/storefront-app/src/lib/sync-cart-to-api.js @@ -1,6 +1,7 @@ import ecomPassport from '@ecomplus/passport-client' import ecomClient from '@ecomplus/client' import ecomCart from '@ecomplus/shopping-cart' +import router from '../router/' let resolveCartId const fetchingCartId = new Promise(resolve => (resolveCartId = resolve)) @@ -82,7 +83,7 @@ const prepareCart = cart => { } else { url = '/carts.json' method = 'POST' - if (window.storefrontApp && window.storefrontApp.router && window.storefrontApp.router.currentRoute && window.storefrontApp.router.currentRoute.name === 'confirmation' && completed === undefined && orders === undefined) { + if (router && router.currentRoute && router.currentRoute.name === 'confirmation' && completed === undefined) { return {} } } @@ -131,10 +132,9 @@ const upsertCart = () => { }, 300) }) : queueUpdateCart(tryRequestApi) + } else { + return Promise.resolve() } - } else { - return Promise.resolve() - } } export { fetchCart, fetchingCartId, upsertCart } From e2cb19d0baa7788d800895031e7614e0737c3b5c Mon Sep 17 00:00:00 2001 From: Leonardo Matos Date: Thu, 24 Aug 2023 18:45:34 -0300 Subject: [PATCH 5/6] Update @ecomplus/storefront-app/src/lib/sync-cart-to-api.js --- @ecomplus/storefront-app/src/lib/sync-cart-to-api.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/@ecomplus/storefront-app/src/lib/sync-cart-to-api.js b/@ecomplus/storefront-app/src/lib/sync-cart-to-api.js index 61a729a69..9001f12b6 100644 --- a/@ecomplus/storefront-app/src/lib/sync-cart-to-api.js +++ b/@ecomplus/storefront-app/src/lib/sync-cart-to-api.js @@ -83,7 +83,7 @@ const prepareCart = cart => { } else { url = '/carts.json' method = 'POST' - if (router && router.currentRoute && router.currentRoute.name === 'confirmation' && completed === undefined) { + if (router.currentRoute.name === 'confirmation' && !completed) { return {} } } From 060025ac431c780786029884b4eb5d5b334dccc9 Mon Sep 17 00:00:00 2001 From: Leonardo Matos Date: Thu, 24 Aug 2023 18:45:43 -0300 Subject: [PATCH 6/6] Update @ecomplus/storefront-app/src/lib/sync-cart-to-api.js --- @ecomplus/storefront-app/src/lib/sync-cart-to-api.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/@ecomplus/storefront-app/src/lib/sync-cart-to-api.js b/@ecomplus/storefront-app/src/lib/sync-cart-to-api.js index 9001f12b6..a7116ac3a 100644 --- a/@ecomplus/storefront-app/src/lib/sync-cart-to-api.js +++ b/@ecomplus/storefront-app/src/lib/sync-cart-to-api.js @@ -132,9 +132,9 @@ const upsertCart = () => { }, 300) }) : queueUpdateCart(tryRequestApi) - } else { - return Promise.resolve() - } + } else { + return Promise.resolve() + } } export { fetchCart, fetchingCartId, upsertCart }