@vita-mojo/basket-quantifier 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/lib/quantify.js +49 -14
- package/src/lib/quantify.js.map +1 -1
package/package.json
CHANGED
package/src/lib/quantify.js
CHANGED
|
@@ -1,28 +1,48 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.quantify = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
4
5
|
const ulid_1 = require("ulid");
|
|
6
|
+
const isEqual_1 = tslib_1.__importDefault(require("lodash/isEqual"));
|
|
7
|
+
const isNil_1 = tslib_1.__importDefault(require("lodash/isNil"));
|
|
8
|
+
const uniq_1 = tslib_1.__importDefault(require("lodash/uniq"));
|
|
5
9
|
const quantityHash_1 = require("./quantityHash");
|
|
6
10
|
const constants_1 = require("./constants");
|
|
7
11
|
const ulid = (0, ulid_1.monotonicFactory)();
|
|
8
|
-
const ignoredPropsChanges = ['basketUUID', 'quantity'];
|
|
12
|
+
const ignoredPropsChanges = ['_mainBasketEntry', '_mainBundleKeys', 'basketUUID', 'quantity', 'basketEntries', 'rootCategory', 'appStraightToPickup', 'category', 'promotion', 'kitchenStation', 'meta'];
|
|
9
13
|
const handleDiffs = (mainBundle, bundle, handle) => {
|
|
10
|
-
Object.keys(bundle)
|
|
11
|
-
|
|
14
|
+
const props = Object.keys(bundle);
|
|
15
|
+
props.push(...(mainBundle['_mainBundleKeys'] || []));
|
|
16
|
+
(0, uniq_1.default)(props).forEach((prop) => {
|
|
17
|
+
var _a, _b;
|
|
12
18
|
if (prop === 'deal') {
|
|
13
|
-
bundle.deal && handle(prop, {
|
|
19
|
+
bundle.deal && handle(prop, {
|
|
20
|
+
basketUUID: bundle.deal.basketUUID,
|
|
21
|
+
dealCategoryUUID: bundle.deal.dealCategoryUUID,
|
|
22
|
+
}, {
|
|
23
|
+
basketUUID: (_a = mainBundle.deal) === null || _a === void 0 ? void 0 : _a.basketUUID,
|
|
24
|
+
dealCategoryUUID: (_b = mainBundle.deal) === null || _b === void 0 ? void 0 : _b.dealCategoryUUID
|
|
25
|
+
});
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (ignoredPropsChanges.includes(prop))
|
|
29
|
+
return;
|
|
30
|
+
if (Array.isArray(bundle[prop]))
|
|
31
|
+
return;
|
|
32
|
+
if ((0, isNil_1.default)(bundle[prop]) && (0, isNil_1.default)(mainBundle[prop]))
|
|
14
33
|
return;
|
|
34
|
+
if (typeof bundle[prop] === 'object') {
|
|
35
|
+
const propChanged = !(0, isEqual_1.default)(mainBundle[prop] || {}, bundle[prop]);
|
|
36
|
+
if (propChanged)
|
|
37
|
+
handle(prop, bundle[prop]);
|
|
15
38
|
}
|
|
16
|
-
|
|
17
|
-
typeof bundle[prop] !== 'object' &&
|
|
18
|
-
!ignoredPropsChanges.includes(prop)) {
|
|
39
|
+
else {
|
|
19
40
|
if (bundle[prop] !== mainBundle[prop]) {
|
|
20
|
-
handle(prop, bundle[prop], mainBundle[prop]);
|
|
41
|
+
handle(prop, bundle[prop] || null, mainBundle[prop]);
|
|
21
42
|
}
|
|
22
43
|
}
|
|
23
44
|
});
|
|
24
45
|
};
|
|
25
|
-
const getMainBundleULID = (entries) => Object.keys(entries).sort()[0];
|
|
26
46
|
const hasSamePromotion = (mainBundle, bundle) => {
|
|
27
47
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
28
48
|
if (bundle.promotion) {
|
|
@@ -41,20 +61,26 @@ const quantify = (bundles, useHash = true, params = { ignoreDeals: false }) => {
|
|
|
41
61
|
const hashIndex = {};
|
|
42
62
|
return (bundles || []).reduce((acc, bundle) => {
|
|
43
63
|
const [basketUUID, entryULID] = (bundle.basketUUID || '').split(constants_1.Delimiter);
|
|
64
|
+
/**
|
|
65
|
+
* if there's an entryULID, bundle was already grouped, so we're trying to regroup it
|
|
66
|
+
* after an update or in the api after a dequantify action for example.
|
|
67
|
+
* We need to make sure all main properties are the same */
|
|
44
68
|
if (entryULID) {
|
|
45
69
|
bundle = Object.assign({}, bundle);
|
|
70
|
+
/**
|
|
71
|
+
* any items updates should ungroup the bundle */
|
|
46
72
|
if (bundle['hasItemsUpdates'] || ((params === null || params === void 0 ? void 0 : params.ignoreDeals) && bundle.deal)) {
|
|
47
73
|
acc.push(Object.assign(Object.assign({}, bundle), { quantity: 1 }));
|
|
48
74
|
return acc;
|
|
49
75
|
}
|
|
50
76
|
const mainBundle = acc.find((b) => b.basketUUID === basketUUID && hasSamePromotion(b, bundle));
|
|
51
77
|
if (!mainBundle) {
|
|
52
|
-
|
|
78
|
+
const mainBasketEntry = {};
|
|
79
|
+
acc.push(Object.assign(Object.assign({}, bundle), { basketUUID, quantity: 1, _mainBasketEntry: mainBasketEntry, _mainBundleKeys: Object.keys(bundle), basketEntries: { [entryULID]: mainBasketEntry } }));
|
|
53
80
|
return acc;
|
|
54
81
|
}
|
|
55
82
|
mainBundle.quantity = mainBundle.quantity + 1;
|
|
56
|
-
const
|
|
57
|
-
const mainBasketEntry = mainBundle.basketEntries[mainBundleULID];
|
|
83
|
+
const mainBasketEntry = mainBundle['_mainBasketEntry'];
|
|
58
84
|
const basketEntry = {};
|
|
59
85
|
handleDiffs(mainBundle, bundle, (prop, value, mainValue) => {
|
|
60
86
|
basketEntry[prop] = value;
|
|
@@ -65,6 +91,9 @@ const quantify = (bundles, useHash = true, params = { ignoreDeals: false }) => {
|
|
|
65
91
|
mainBundle.basketEntries[entryULID] = basketEntry;
|
|
66
92
|
return acc;
|
|
67
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* bundle was not grouped before, we need to create a hash in order to group it
|
|
96
|
+
* */
|
|
68
97
|
if (useHash) {
|
|
69
98
|
bundle = Object.assign({}, bundle);
|
|
70
99
|
// bundle updates without entryULID should not group
|
|
@@ -78,12 +107,18 @@ const quantify = (bundles, useHash = true, params = { ignoreDeals: false }) => {
|
|
|
78
107
|
const mainBundle = acc[hashIndex[hash]];
|
|
79
108
|
if (!mainBundle.basketEntries) {
|
|
80
109
|
mainBundle.quantity = 1;
|
|
81
|
-
|
|
110
|
+
const mainBasketEntry = {};
|
|
111
|
+
mainBundle['_mainBasketEntry'] = mainBasketEntry;
|
|
112
|
+
mainBundle['_mainBundleKeys'] = Object.keys(mainBundle);
|
|
113
|
+
mainBundle.basketEntries = { [ulid()]: mainBasketEntry };
|
|
82
114
|
}
|
|
83
115
|
mainBundle.quantity += 1;
|
|
84
116
|
const basketEntry = {};
|
|
85
|
-
|
|
117
|
+
const mainBasketEntry = mainBundle['_mainBasketEntry'];
|
|
118
|
+
handleDiffs(mainBundle, bundle, (prop, value, mainValue) => {
|
|
86
119
|
basketEntry[prop] = value;
|
|
120
|
+
if (typeof mainValue !== 'undefined')
|
|
121
|
+
mainBasketEntry[prop] = mainValue;
|
|
87
122
|
});
|
|
88
123
|
mainBundle.basketEntries[ulid()] = basketEntry;
|
|
89
124
|
return acc;
|
package/src/lib/quantify.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"quantify.js","sourceRoot":"","sources":["../../../../../packages/basket-quantifier/src/lib/quantify.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"quantify.js","sourceRoot":"","sources":["../../../../../packages/basket-quantifier/src/lib/quantify.ts"],"names":[],"mappings":";;;;AACA,+BAAwC;AACxC,qEAAqC;AACrC,iEAAiC;AACjC,+DAA+B;AAE/B,iDAA8C;AAC9C,2CAAwC;AAExC,MAAM,IAAI,GAAG,IAAA,uBAAgB,GAAE,CAAC;AAOhC,MAAM,mBAAmB,GAAG,CAAC,kBAAkB,EAAE,iBAAiB,EAAE,YAAY,EAAE,UAAU,EAAE,eAAe,EAAE,cAAc,EAAE,qBAAqB,EAAE,UAAU,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,CAAC,CAAC;AAEzM,MAAM,WAAW,GAAG,CA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