diff --git a/src/modules/icmaa-external-checkout/index.ts b/src/modules/icmaa-external-checkout/index.ts index 5d80de186b..aaa5a9e2bf 100755 --- a/src/modules/icmaa-external-checkout/index.ts +++ b/src/modules/icmaa-external-checkout/index.ts @@ -26,12 +26,14 @@ export const IcmaaExternalCheckoutModule: StorefrontModule = function ({ router, EventBus.$on('session-after-nonauthorized', async () => { const customerToken = Vue.$cookies.get('vsf_token_customer') const quoteToken = Vue.$cookies.get('vsf_token_quote') + const lastOrderToken = Vue.$cookies.get('vsf_token_lastorder') - if (!store.getters['user/isLoggedIn'] && (customerToken || quoteToken)) { + if (!store.getters['user/isLoggedIn'] && (customerToken || quoteToken || lastOrderToken)) { if (customerToken) { Logger.info('Customer token found in cookie – try to login:', 'external-checkout', customerToken)() store.dispatch('user/startSessionWithToken', customerToken).then(() => { Vue.$cookies.remove('vsf_token_customer', undefined, getCookieHostname()) + Vue.$cookies.remove('vsf_token_lastorder', undefined, getCookieHostname()) }) } @@ -41,6 +43,13 @@ export const IcmaaExternalCheckoutModule: StorefrontModule = function ({ router, Vue.$cookies.remove('vsf_token_quote', undefined, getCookieHostname()) }) } + + if (!customerToken && lastOrderToken) { + Logger.info('Last-order token found in cookie – try to load last order:', 'external-checkout', lastOrderToken)() + store.dispatch('user/loadLastOrderToHistory', { token: lastOrderToken }).then(() => { + Vue.$cookies.remove('vsf_token_lastorder', undefined, getCookieHostname()) + }) + } } }) diff --git a/src/modules/icmaa-external-checkout/pages/ExternalSuccess.vue b/src/modules/icmaa-external-checkout/pages/ExternalSuccess.vue index d3c33d957e..456a5efe7c 100755 --- a/src/modules/icmaa-external-checkout/pages/ExternalSuccess.vue +++ b/src/modules/icmaa-external-checkout/pages/ExternalSuccess.vue @@ -54,8 +54,11 @@ export default { * logged in when the beforeMount event hook is called. Otherwise the `checkout-success-last-order-loaded` * event won't ever be fired on first request because `isLoggedIn` is false. */ - isLoggedIn (isLoggedIn) { - this.onLogin(isLoggedIn) + isLoggedIn (value) { + this.onLogin(value) + }, + lastOrder () { + this.guestOrder() } }, methods: { @@ -69,6 +72,13 @@ export default { if (value || this.isLoggedIn) { await this.$store.dispatch('user/refreshOrdersHistory', { resolvedFromCache: false }) this.$bus.$emit('checkout-success-last-order-loaded', this.lastOrder) + } else if (!value && !this.isLoggedIn) { + await this.$store.dispatch('user/loadLastOrderFromCache') + } + }, + async guestOrder () { + if (!this.isLoggedIn && this.lastOrder) { + this.$bus.$emit('checkout-success-last-order-loaded', this.lastOrder) } } } diff --git a/src/modules/icmaa-user/data-resolver/UserService.ts b/src/modules/icmaa-user/data-resolver/UserService.ts new file mode 100644 index 0000000000..38c706619d --- /dev/null +++ b/src/modules/icmaa-user/data-resolver/UserService.ts @@ -0,0 +1,26 @@ +import { processLocalizedURLAddress } from '@vue-storefront/core/helpers' +import { TaskQueue } from '@vue-storefront/core/lib/sync' +import Task from '@vue-storefront/core/lib/sync/types/Task' +import config from 'config' +import getApiEndpointUrl from '@vue-storefront/core/helpers/getApiEndpointUrl' + +const headers = { + 'Accept': 'application/json, text/plain, */*', + 'Content-Type': 'application/json' +} + +const getLastOrder = async (token: string): Promise => + TaskQueue.execute({ + url: processLocalizedURLAddress( + getApiEndpointUrl(config.users, 'last_order').replace('{{token}}', token) + ), + payload: { + method: 'GET', + mode: 'cors', + headers + } + }) + +export const UserService: any = { + getLastOrder +} diff --git a/src/modules/icmaa-user/store/actions.ts b/src/modules/icmaa-user/store/actions.ts index 971891dc0e..3420ea8596 100644 --- a/src/modules/icmaa-user/store/actions.ts +++ b/src/modules/icmaa-user/store/actions.ts @@ -4,6 +4,7 @@ import RootState from '@vue-storefront/core/types/RootState' import UserState from '../types/UserState' import { UserProfile } from '@vue-storefront/core/modules/user/types/UserProfile' import { UserService } from '@vue-storefront/core/data-resolver' +import { UserService as IcmaaUserService } from '../data-resolver/UserService' import * as types from './mutation-types' import * as userTypes from '@vue-storefront/core/modules/user/store/mutation-types' import { SearchQuery } from 'storefront-query-builder' @@ -95,6 +96,30 @@ const actions: ActionTree = { return resp }, + async loadLastOrderToHistory ({ commit, dispatch }, { token }) { + const resp = await IcmaaUserService.getLastOrder(token) + + if (resp.code === 200) { + const order = await dispatch('loadOrderProducts', { order: resp.result, history: [ resp.result ] }) + + commit(userTypes.USER_ORDERS_HISTORY_LOADED, { items: [ order ] }) + EventBus.$emit('user-after-loaded-orders', resp.result) + } + + return resp + }, + async loadLastOrderFromCache ({ dispatch }) { + let resolvedFromCache = false + const ordersHistory = await dispatch('loadOrdersFromCache') + if (ordersHistory) { + Logger.log('Current user order history served from cache', 'user')() + resolvedFromCache = true + } + + if (!resolvedFromCache) { + Promise.resolve(null) + } + }, async loadOrderProducts ({ dispatch }, { order, history }) { const index = history.findIndex(o => o.id === order.id) if (history[index] && history[index].products) {