diff --git a/core/modules/order/store/actions.ts b/core/modules/order/store/actions.ts index ea9f093e23..57b3e02cde 100644 --- a/core/modules/order/store/actions.ts +++ b/core/modules/order/store/actions.ts @@ -9,13 +9,21 @@ import rootStore from '@vue-storefront/core/store' import { isOnline } from '@vue-storefront/core/lib/search' import i18n from '@vue-storefront/i18n' import { TaskQueue } from '@vue-storefront/core/lib/sync' +import { sha3_224 } from 'js-sha3' + const actions: ActionTree = { /** * Place order - send it to service worker queue * @param {Object} commit method * @param {Order} order order data to be send */ - async placeOrder ({ commit }, order:Order) { + async placeOrder ({ commit, getters }, order:Order) { + // Check if order is already processed/processing + const currentOrderHash = sha3_224(JSON.stringify(order)) + const isAlreadyProcessed = getters.getSessionOrderHashes.includes(currentOrderHash) + if (isAlreadyProcessed) return + commit(types.ORDER_ADD_SESSION_ORDER_HASH, currentOrderHash) + const storeView = currentStoreView() if (storeView.storeCode) { order.store_code = storeView.storeCode @@ -48,6 +56,7 @@ const actions: ActionTree = { } return task } catch (e) { + commit(types.ORDER_REMOVE_SESSION_ORDER_HASH, currentOrderHash) rootStore.dispatch('notification/spawnNotification', { type: 'error', message: i18n.t('The order can not be transfered because of server error. Order has been queued'), diff --git a/core/modules/order/store/getters.ts b/core/modules/order/store/getters.ts new file mode 100644 index 0000000000..8b73ef39c3 --- /dev/null +++ b/core/modules/order/store/getters.ts @@ -0,0 +1,9 @@ +import { GetterTree } from 'vuex' +import OrderState from '../types/OrderState' +import RootState from '@vue-storefront/core/types/RootState' + +const getters: GetterTree = { + getSessionOrderHashes: state => state.session_order_hashes +} + +export default getters diff --git a/core/modules/order/store/index.ts b/core/modules/order/store/index.ts index e075c81fdc..39cfb9779c 100644 --- a/core/modules/order/store/index.ts +++ b/core/modules/order/store/index.ts @@ -1,15 +1,18 @@ import { Module } from 'vuex' import actions from './actions' import mutations from './mutations' +import getters from './getters' import RootState from '@vue-storefront/core/types/RootState' import OrderState from '../types/OrderState' export const module: Module = { namespaced: true, state: { - last_order_confirmation: null + last_order_confirmation: null, + session_order_hashes: [] }, actions, - mutations + mutations, + getters } diff --git a/core/modules/order/store/mutation-types.ts b/core/modules/order/store/mutation-types.ts index 2f00db7984..cb9dba69a1 100644 --- a/core/modules/order/store/mutation-types.ts +++ b/core/modules/order/store/mutation-types.ts @@ -1,4 +1,6 @@ export const SN_ORDER = 'order' export const ORDER_PLACE_ORDER = SN_ORDER + '/PLACE_ORDER' export const ORDER_PROCESS_QUEUE = SN_ORDER + '/PROCESS_QUEUE' -export const ORDER_LAST_ORDER_WITH_CONFIRMATION = SN_ORDER + '/LAST_ORDER_CONFIRMATION' \ No newline at end of file +export const ORDER_LAST_ORDER_WITH_CONFIRMATION = SN_ORDER + '/LAST_ORDER_CONFIRMATION' +export const ORDER_ADD_SESSION_ORDER_HASH = SN_ORDER + '/ADD_SESSION_ORDER_HASH' +export const ORDER_REMOVE_SESSION_ORDER_HASH = SN_ORDER + '/REMOVE_SESSION_ORDER_HASH' \ No newline at end of file diff --git a/core/modules/order/store/mutations.ts b/core/modules/order/store/mutations.ts index ef766181d6..bb6780e52c 100644 --- a/core/modules/order/store/mutations.ts +++ b/core/modules/order/store/mutations.ts @@ -30,6 +30,12 @@ const mutations: MutationTree = { }, [types.ORDER_LAST_ORDER_WITH_CONFIRMATION] (state, payload) { state.last_order_confirmation = payload + }, + [types.ORDER_ADD_SESSION_ORDER_HASH] (state, hash: string) { + state.session_order_hashes.push(hash) + }, + [types.ORDER_REMOVE_SESSION_ORDER_HASH] (state, hash: string) { + state.session_order_hashes = state.session_order_hashes.filter(sessionHash => sessionHash !== hash) } } diff --git a/core/modules/order/types/OrderState.ts b/core/modules/order/types/OrderState.ts index 93cedffff4..b46794fc79 100644 --- a/core/modules/order/types/OrderState.ts +++ b/core/modules/order/types/OrderState.ts @@ -1,3 +1,4 @@ export default interface OrderState { - last_order_confirmation: any + last_order_confirmation: any, + session_order_hashes: Array, }