Skip to content

Commit

Permalink
add check for single placing order by its hash
Browse files Browse the repository at this point in the history
  • Loading branch information
patzick committed Feb 26, 2019
1 parent d4459d3 commit 6bd9528
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 5 deletions.
11 changes: 10 additions & 1 deletion core/modules/order/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<OrderState, RootState> = {
/**
* 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
Expand Down Expand Up @@ -48,6 +56,7 @@ const actions: ActionTree<OrderState, RootState> = {
}
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'),
Expand Down
9 changes: 9 additions & 0 deletions core/modules/order/store/getters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { GetterTree } from 'vuex'
import OrderState from '../types/OrderState'
import RootState from '@vue-storefront/core/types/RootState'

const getters: GetterTree<OrderState, RootState> = {
getSessionOrderHashes: state => state.session_order_hashes
}

export default getters
7 changes: 5 additions & 2 deletions core/modules/order/store/index.ts
Original file line number Diff line number Diff line change
@@ -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<OrderState, RootState> = {
namespaced: true,
state: {
last_order_confirmation: null
last_order_confirmation: null,
session_order_hashes: []
},
actions,
mutations
mutations,
getters
}

4 changes: 3 additions & 1 deletion core/modules/order/store/mutation-types.ts
Original file line number Diff line number Diff line change
@@ -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'
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'
6 changes: 6 additions & 0 deletions core/modules/order/store/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ const mutations: MutationTree<OrderState> = {
},
[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)
}
}

Expand Down
3 changes: 2 additions & 1 deletion core/modules/order/types/OrderState.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export default interface OrderState {
last_order_confirmation: any
last_order_confirmation: any,
session_order_hashes: Array<string>,
}

0 comments on commit 6bd9528

Please sign in to comment.