-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
413 lines (397 loc) · 19.9 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import * as types from "@vue-storefront/core/modules/cart/store/mutation-types"
import { Logger } from "@vue-storefront/core/lib/logger"
import isString from "lodash-es/isString"
import rootStore from "@vue-storefront/core/store"
import { TaskQueue } from "@vue-storefront/core/lib/sync"
import config from "config"
import SearchQuery from '@vue-storefront/core/lib/search/searchQuery'
import i18n from '@vue-storefront/i18n'
import Vue from 'vue'
import Task from '@vue-storefront/core/lib/sync/types/Task'
import * as extendTypes from './store/mutation-types'
const MAX_BYPASS_COUNT = 10
let _connectBypassCount = 0
function _getDifflogPrototype() {
return { items: [], serverResponses: [], clientNotifications: [] }
}
function _serverDeleteItem ({ cartServerToken, cartItem }): Promise<Task> {
cartItem = Object.assign(cartItem, { quoteId: cartServerToken })
return TaskQueue.execute({ url: config.cart.deleteitem_endpoint, // sync the cart
payload: {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
mode: 'cors',
body: JSON.stringify({
cartItem: cartItem
})
},
silent: true
})
}
function _serverUpdateItem ({ cartServerToken, cartItem }): Promise<Task> {
if (!cartItem.quoteId) {
cartItem = Object.assign(cartItem, { quoteId: cartServerToken })
}
return TaskQueue.execute({ url: config.cart.updateitem_endpoint, // sync the cart
payload: {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
mode: 'cors',
body: JSON.stringify({
cartItem: cartItem
})
}
})
}
export const cartExtend = {
key: "cart",
store: {
modules: [
{
key: "cart",
module: {
actions: {
servercartAfterTotals(context, event) {
if (event.resultCode === 200) {
const totalsObj = event.result.totals
? event.result.totals
: event.result
Logger.info("Overriding server totals. ", "cart", totalsObj)()
let itemsAfterTotal = {}
let platformTotalSegments = totalsObj.total_segments
for (let item of totalsObj.items) {
if (item.options && isString(item.options))
item.options = JSON.parse(item.options)
itemsAfterTotal[item.item_id] = item
rootStore.dispatch(
"cart/updateItem",
{
product: {
server_item_id: item.item_id,
totals: item,
qty: item.qty
}
},
{ root: true }
) // update the server_id reference
}
for (let segment of totalsObj.total_segments) {
if (segment.code == "shipping") {
segment.value = totalsObj.shipping_incl_tax
}
}
rootStore.commit(types.SN_CART + "/" + types.CART_UPD_TOTALS, {
itemsAfterTotal: itemsAfterTotal,
totals: totalsObj,
platformTotalSegments: platformTotalSegments
})
} else {
Logger.error(event.result, "cart")()
}
},
async sync(
{ getters, rootGetters, commit, dispatch },
{ forceClientState = false, dryRun = false, pullItemsFromServer = false }
) {
// pull current cart FROM the server
const isUserInCheckout = rootGetters["checkout/isUserInCheckout"]
let diffLog = _getDifflogPrototype()
if (!localStorage.getItem('vsf_processing_add_to_cart')) {
localStorage.setItem('vsf_processing_add_to_cart', 'true')
} else {
return diffLog
}
if (isUserInCheckout) forceClientState = true // never surprise the user in checkout - #
if (getters.isCartSyncEnabled && getters.isCartConnected) {
if (getters.isSyncRequired) {
// cart hash empty or not changed
/** @todo: move this call to data resolver; shouldn't be a part of public API no more */
commit(types.CART_SET_SYNC)
const task = await TaskQueue.execute({
url: config.cart.pull_endpoint, // sync the cart
payload: {
method: "GET",
headers: { "Content-Type": "application/json" },
mode: "cors"
},
silent: true
}).then(async task => {
if (task.resultCode === 200) {
diffLog = await dispatch("merge", {
serverItems: task.result,
clientItems: getters.getCartItems,
dryRun: dryRun,
forceClientState: forceClientState,
pullItemsFromServer: pullItemsFromServer
})
} else {
Logger.error(task.result, "cart") // override with guest cart()
if (_connectBypassCount < MAX_BYPASS_COUNT) {
Logger.log(
"Bypassing with guest cart" + _connectBypassCount,
"cart"
)()
_connectBypassCount = _connectBypassCount + 1
await dispatch("connect", { guestCart: true })
if (
!task.result.includes("No such entity with cartId")
) {
Logger.error(task.result, "cart")()
}
}
}
})
localStorage.removeItem('vsf_processing_add_to_cart')
return diffLog
} else {
localStorage.removeItem('vsf_processing_add_to_cart')
return diffLog
}
} else {
localStorage.removeItem('vsf_processing_add_to_cart')
return diffLog
}
},
async merge ({ getters, dispatch, commit, rootGetters }, { serverItems, clientItems, dryRun = false, forceClientState = false, pullItemsFromServer = false }) {
const diffLog = _getDifflogPrototype()
let totalsShouldBeRefreshed = getters.isTotalsSyncRequired // when empty it means no sync has yet been executed
let serverCartUpdateRequired = false
let clientCartUpdateRequired = false
let cartHasItems = false
const clientCartAddItems = []
/** helper to find the item to be added to the cart by sku */
let productActionOptions = (serverItem) => {
return new Promise(resolve => {
if (serverItem.product_type === 'configurable') {
let searchQuery = new SearchQuery()
searchQuery = searchQuery.applyFilter({key: 'configurable_children.sku', value: {'eq': serverItem.sku}})
dispatch('product/list', {query: searchQuery, start: 0, size: 1, updateState: false}, { root: true }).then((resp) => {
if (resp.items.length >= 1) {
resolve({ sku: resp.items[0].sku, childSku: serverItem.sku })
}
})
} else {
resolve({ sku: serverItem.sku })
}
})
}
/** helper - sub method to update the item in the cart */
const _updateClientItem = async function ({ dispatch }, event, clientItem) {
if (typeof event.result.item_id !== 'undefined') {
await dispatch('updateItem', { product: { server_item_id: event.result.item_id, sku: clientItem.sku, server_cart_id: event.result.quote_id, prev_qty: clientItem.qty } }) // update the server_id reference
Vue.prototype.$bus.$emit('cart-after-itemchanged', { item: clientItem })
}
}
/** helper - sub method to react for the server response after the sync */
const _afterServerItemUpdated = async function ({ dispatch, commit }, event, clientItem = null) {
Logger.debug('Cart item server sync' + event, 'cart')()
diffLog.serverResponses.push({ 'status': event.resultCode, 'sku': clientItem.sku, 'result': event })
if (event.resultCode !== 200) {
// TODO: add the strategy to configure behaviour if the product is (confirmed) out of the stock
if (clientItem.server_item_id) {
dispatch('getItem', clientItem.sku).then((cartItem) => {
if (cartItem) {
Logger.log('Restoring qty after error' + clientItem.sku + cartItem.prev_qty, 'cart')()
if (cartItem.prev_qty > 0) {
dispatch('updateItem', { product: { qty: cartItem.prev_qty } }) // update the server_id reference
Vue.prototype.$bus.$emit('cart-after-itemchanged', { item: cartItem })
} else {
dispatch('removeItem', { product: cartItem, removeByParentSku: false }) // update the server_id reference
}
}
})
} else {
Logger.warn('Removing product from cart', 'cart', clientItem)()
commit(types.CART_DEL_NON_CONFIRMED_ITEM, { product: clientItem })
}
} else {
const isUserInCheckout = rootGetters['checkout/isUserInCheckout']
if (!isUserInCheckout) { // if user is in the checkout - this callback is just a result of server sync
const isThisNewItemAddedToTheCart = (!clientItem || !clientItem.server_item_id)
const notificationData = {
type: 'success',
message: isThisNewItemAddedToTheCart ? i18n.t('Product has been added to the cart!') : i18n.t('Product quantity has been updated!'),
action1: { label: i18n.t('OK') },
action2: null
}
if (!config.externalCheckout) { // if there is externalCheckout enabled we don't offer action to go to checkout as it can generate cart desync
notificationData.action2 = { label: i18n.t('Proceed to checkout'),
action: () => {
dispatch('goToCheckout')
}}
}
diffLog.clientNotifications.push(notificationData) // display the notification only for newly added products
}
}
if (clientItem === null) {
const cartItem = await dispatch('getItem', event.result.sku)
if (cartItem) {
await _updateClientItem({ dispatch }, event, cartItem)
}
} else {
await _updateClientItem({ dispatch }, event, clientItem)
}
}
for (const clientItem of clientItems) {
cartHasItems = true
const serverItem = serverItems.find((itm) => {
return itm.sku === clientItem.sku || itm.sku.indexOf(clientItem.sku + '-') === 0 /* bundle products */
})
if (!serverItem) {
Logger.warn('No server item with sku ' + clientItem.sku + ' on stock.', 'cart')()
diffLog.items.push({ 'party': 'server', 'sku': clientItem.sku, 'status': 'no-item' })
if (!dryRun) {
if (forceClientState || !config.cart.serverSyncCanRemoveLocalItems) {
const event = await _serverUpdateItem({
cartServerToken: getters.getCartToken,
cartItem: {
sku: clientItem.parentSku && config.cart.setConfigurableProductOptions ? clientItem.parentSku : clientItem.sku,
qty: clientItem.qty,
product_option: clientItem.product_option
}
})
_afterServerItemUpdated({ dispatch, commit }, event, clientItem)
serverCartUpdateRequired = true
totalsShouldBeRefreshed = true
} else {
dispatch('removeItem', {
product: clientItem
})
}
}
} else if (serverItem.qty !== clientItem.qty) {
Logger.log('Wrong qty for ' + clientItem.sku, clientItem.qty, serverItem.qty)()
diffLog.items.push({ 'party': 'server', 'sku': clientItem.sku, 'status': 'wrong-qty', 'client-qty': clientItem.qty, 'server-qty': serverItem.qty })
if (!dryRun) {
if (forceClientState || !config.cart.serverSyncCanModifyLocalItems) {
const event = await _serverUpdateItem({
cartServerToken: getters.getCartToken,
cartItem: {
sku: clientItem.parentSku && config.cart.setConfigurableProductOptions ? clientItem.parentSku : clientItem.sku,
qty: clientItem.qty,
item_id: serverItem.item_id,
quoteId: serverItem.quote_id,
product_option: clientItem.product_option
}
})
_afterServerItemUpdated({ dispatch, commit }, event, clientItem)
totalsShouldBeRefreshed = true
serverCartUpdateRequired = true
} else {
await dispatch('updateItem', {
product: serverItem
})
}
}
} else {
Logger.info('Server and client item with SKU ' + clientItem.sku + ' synced. Updating cart.', 'cart', 'cart')()
if (!dryRun) {
await dispatch('updateItem', { product: { sku: clientItem.sku, server_cart_id: serverItem.quote_id, server_item_id: serverItem.item_id, product_option: serverItem.product_option } })
}
}
}
for (const serverItem of serverItems) {
if (serverItem) {
const clientItem = clientItems.find((itm) => {
return itm.sku === serverItem.sku || serverItem.sku.indexOf(itm.sku + '-') === 0 /* bundle products */
})
if (!clientItem) {
Logger.info('No client item for' + serverItem.sku, 'cart')()
diffLog.items.push({ 'party': 'client', 'sku': serverItem.sku, 'status': 'no-item' })
if (!dryRun) {
if (forceClientState && !pullItemsFromServer) {
Logger.info('Removing product from cart', 'cart', serverItem)()
Logger.log('Removing item' + serverItem.sku + serverItem.item_id, 'cart')()
serverCartUpdateRequired = true
totalsShouldBeRefreshed = true
const res = await _serverDeleteItem({
cartServerToken: getters.getCartToken,
cartItem: {
sku: serverItem.sku,
item_id: serverItem.item_id,
quoteId: serverItem.quote_id
}
})
diffLog.serverResponses.push({ 'status': res.resultCode, 'sku': serverItem.sku, 'result': res })
} else {
clientCartAddItems.push(
new Promise(resolve => {
productActionOptions(serverItem).then((actionOtions) => {
dispatch('product/single', { options: actionOtions, assignDefaultVariant: true, setCurrentProduct: false, selectDefaultVariant: false }, { root: true }).then((product) => {
resolve({ product: product, serverItem: serverItem })
})
})
})
)
}
}
}
}
}
if (clientCartAddItems.length) {
totalsShouldBeRefreshed = true
clientCartUpdateRequired = true
cartHasItems = true
}
diffLog.items.push({ 'party': 'client', 'status': clientCartUpdateRequired ? 'update-required' : 'no-changes' })
diffLog.items.push({ 'party': 'server', 'status': serverCartUpdateRequired ? 'update-required' : 'no-changes' })
Promise.all(clientCartAddItems).then((items) => {
items.map(({ product, serverItem }) => {
product.server_item_id = serverItem.item_id
product.qty = serverItem.qty
product.server_cart_id = serverItem.quote_id
if (serverItem.product_option) {
product.product_option = serverItem.product_option
}
dispatch('addItem', { productToAdd: product, forceServerSilence: true })
})
})
if (!dryRun) {
if (totalsShouldBeRefreshed && cartHasItems) {
await dispatch('syncTotals')
}
commit(types.CART_SET_ITEMS_HASH, getters.getCurrentCartHash) // update the cart hash
}
// Re-mapping server items to client items one more time.
// Delete client items if server items were deleted - freegift or backend automatically deleted item to quote.
const task = await TaskQueue.execute({
url: config.cart.pull_endpoint, // sync the cart
payload: {
method: "GET",
headers: { "Content-Type": "application/json" },
mode: "cors"
},
silent: true
})
if (task.result) {
for (const clientItem of clientItems) {
const serverItemAfterPulled = task.result.find((item) => {
return item.sku === clientItem.sku || item.sku.includes(clientItem.sku + '-') /* bundle products */
})
if (!serverItemAfterPulled) {
dispatch('removeItem', {
product: clientItem
})
}
}
}
Vue.prototype.$bus.$emit('servercart-after-diff', { diffLog: diffLog, serverItems: serverItems, clientItems: clientItems, dryRun: dryRun, event: event }) // send the difflog
Logger.info('Client/Server cart synchronised ', 'cart', diffLog)()
return diffLog
},
/** Force sync cart items from server to client with pullItemsFromServer */
async forceSync ({ getters, rootGetters, commit, dispatch }, { forceClientState = true, dryRun = false, pullItemsFromServer = true }) { // force pull current cart FROM the server
commit(extendTypes.CART_SET_FORCESYNC)
return dispatch('sync', { forceClientState, dryRun, pullItemsFromServer})
},
},
mutations: {
[extendTypes.CART_SET_FORCESYNC] (state) {
state.cartServerLastSyncDate = 0
},
}
}
}
]
}
}