@ordergroove/offers 2.27.22 → 2.27.23-alpha-PR-672-4.2
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/dist/bundle-report.html +7 -7
- package/dist/offers.js +24 -24
- package/dist/offers.js.map +2 -2
- package/package.json +2 -2
- package/src/shopify/__tests__/shopifyReducer.spec.js +271 -137
- package/src/shopify/shopifyMiddleware.ts +7 -15
- package/src/shopify/shopifyReducer.js +107 -34
|
@@ -88,7 +88,9 @@ export const autoshipEligible = (state = {}, action) => {
|
|
|
88
88
|
return cart.items.reduce(reduceProductCartLine, state);
|
|
89
89
|
}
|
|
90
90
|
if (constants.SETUP_PRODUCT === action.type) {
|
|
91
|
-
const {
|
|
91
|
+
const {
|
|
92
|
+
payload: { product }
|
|
93
|
+
} = action;
|
|
92
94
|
return [product, ...(product?.variants || [])]?.reduce(
|
|
93
95
|
(acc, cur) => ({
|
|
94
96
|
...overrideLineKey(acc, cur.id, cur.selling_plan_allocations?.length > 0),
|
|
@@ -109,6 +111,21 @@ export function textToFreq(text) {
|
|
|
109
111
|
return null;
|
|
110
112
|
}
|
|
111
113
|
|
|
114
|
+
export function sellingPlansToEveryPeriod(sellingPlanGroup) {
|
|
115
|
+
return sellingPlanGroup?.selling_plans
|
|
116
|
+
?.map(({ options }) => options || [])
|
|
117
|
+
.flat()
|
|
118
|
+
.map(({ value }) => textToFreq(value));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function sellingPlansToText(sellingPlanGroup, frequencies) {
|
|
122
|
+
return sellingPlanGroup.options?.[0]?.values || frequencies;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function sellingPlansToFrequencies(sellingPlanGroup) {
|
|
126
|
+
return sellingPlanGroup?.selling_plans?.map(({ id }) => `${id}`);
|
|
127
|
+
}
|
|
128
|
+
|
|
112
129
|
export const config = (
|
|
113
130
|
state = {
|
|
114
131
|
frequencies: [],
|
|
@@ -132,18 +149,16 @@ export const config = (
|
|
|
132
149
|
}
|
|
133
150
|
|
|
134
151
|
if (constants.SETUP_PRODUCT === action.type) {
|
|
135
|
-
const
|
|
152
|
+
const {
|
|
153
|
+
payload: { product }
|
|
154
|
+
} = action;
|
|
136
155
|
const sellingPlanGroup = getOGSellingPlanGroup(product);
|
|
137
|
-
const frequencies = sellingPlanGroup
|
|
156
|
+
const frequencies = sellingPlansToFrequencies(sellingPlanGroup);
|
|
138
157
|
if (frequencies?.length) {
|
|
139
|
-
const frequenciesEveryPeriod = sellingPlanGroup
|
|
140
|
-
|
|
141
|
-
.flat()
|
|
142
|
-
.map(({ value }) => textToFreq(value));
|
|
143
|
-
const frequenciesText = sellingPlanGroup.options?.[0]?.values || frequencies;
|
|
158
|
+
const frequenciesEveryPeriod = sellingPlansToEveryPeriod(sellingPlanGroup);
|
|
159
|
+
const frequenciesText = sellingPlansToText(sellingPlanGroup, frequencies);
|
|
144
160
|
return {
|
|
145
161
|
...state,
|
|
146
|
-
defaultFrequency: frequencies[0],
|
|
147
162
|
frequenciesEveryPeriod,
|
|
148
163
|
frequencies,
|
|
149
164
|
frequenciesText
|
|
@@ -166,8 +181,9 @@ export const inStock = (state = {}, action) => {
|
|
|
166
181
|
}
|
|
167
182
|
|
|
168
183
|
if (constants.SETUP_PRODUCT === action.type) {
|
|
169
|
-
const
|
|
170
|
-
|
|
184
|
+
const {
|
|
185
|
+
payload: { product }
|
|
186
|
+
} = action;
|
|
171
187
|
return [product, ...product?.variants]?.reduce(productOrVariantInStockReducer, state) || state;
|
|
172
188
|
}
|
|
173
189
|
// force offer to refresh when requesting a new one
|
|
@@ -179,6 +195,31 @@ export const inStock = (state = {}, action) => {
|
|
|
179
195
|
|
|
180
196
|
export const offer = (state = {}, action) => state;
|
|
181
197
|
|
|
198
|
+
export const lookupFrequencySellingPlan = (frequencies, sellingPlans, frequency) => {
|
|
199
|
+
const index = frequencies.findIndex(it => it === frequency);
|
|
200
|
+
if (index >= 0) {
|
|
201
|
+
return sellingPlans[index];
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return null;
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
export const resolveSellingPlanFrequency = (sellingPlans, frequencies, frequency, defaultFrequency) => {
|
|
208
|
+
if (!frequency.includes('_')) return frequency;
|
|
209
|
+
|
|
210
|
+
if (sellingPlans.length !== frequencies.length) {
|
|
211
|
+
return frequency;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
let sellingPlan = lookupFrequencySellingPlan(frequencies, sellingPlans, frequency);
|
|
215
|
+
|
|
216
|
+
if (!sellingPlan) {
|
|
217
|
+
sellingPlan = lookupFrequencySellingPlan(frequencies, sellingPlans, defaultFrequency);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return sellingPlan || frequency;
|
|
221
|
+
};
|
|
222
|
+
|
|
182
223
|
export const optedin = (state = [], action) => {
|
|
183
224
|
if (constants.SETUP_CART === action.type) {
|
|
184
225
|
const cart = action.payload;
|
|
@@ -195,32 +236,62 @@ export const optedin = (state = [], action) => {
|
|
|
195
236
|
);
|
|
196
237
|
}
|
|
197
238
|
if (constants.RECEIVE_OFFER === action.type) {
|
|
198
|
-
const {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
239
|
+
const {
|
|
240
|
+
autoship = {},
|
|
241
|
+
autoship_by_default = {},
|
|
242
|
+
default_frequencies = {},
|
|
243
|
+
in_stock = {},
|
|
244
|
+
offer: offerEl = {}
|
|
245
|
+
} = action.payload;
|
|
246
|
+
const { frequencies, frequenciesEveryPeriod } = offerEl.config || {};
|
|
247
|
+
|
|
248
|
+
return Object.keys(autoship).reduce((acc, id) => {
|
|
249
|
+
const existingOptin = state.find(it => it.id === id);
|
|
250
|
+
if (existingOptin) return acc;
|
|
251
|
+
|
|
252
|
+
const shouldBeOptedIn = autoship[id] && autoship_by_default[id] && in_stock[id];
|
|
253
|
+
|
|
254
|
+
if (shouldBeOptedIn) {
|
|
255
|
+
const defaultFrequency = default_frequencies[id]
|
|
256
|
+
? `${default_frequencies[id].every}_${default_frequencies[id].every_period}`
|
|
257
|
+
: offerEl.productDefaultFrequency || '';
|
|
258
|
+
|
|
259
|
+
const frequency = resolveSellingPlanFrequency(
|
|
260
|
+
frequencies,
|
|
261
|
+
frequenciesEveryPeriod,
|
|
262
|
+
defaultFrequency,
|
|
263
|
+
offerEl.defaultFrequency
|
|
264
|
+
);
|
|
265
|
+
|
|
266
|
+
return acc.concat({
|
|
267
|
+
id,
|
|
268
|
+
frequency
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return acc;
|
|
273
|
+
}, state);
|
|
209
274
|
}
|
|
210
275
|
|
|
211
276
|
if (constants.SETUP_PRODUCT === action.type) {
|
|
212
|
-
const
|
|
213
|
-
const sellingPlanGroup = getOGSellingPlanGroup(
|
|
214
|
-
const frequencies = sellingPlanGroup
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
277
|
+
const { product, offer: offerEl } = action.payload;
|
|
278
|
+
const sellingPlanGroup = getOGSellingPlanGroup(product);
|
|
279
|
+
const frequencies = sellingPlansToFrequencies(sellingPlanGroup);
|
|
280
|
+
const frequenciesEveryPeriod = sellingPlansToEveryPeriod(sellingPlanGroup);
|
|
281
|
+
const defaultFrequency = offerEl.productDefaultFrequency;
|
|
282
|
+
const productIds = product.variants.map(variant => variant.id).map(id => `${id}`);
|
|
283
|
+
return state.map(curr => {
|
|
284
|
+
if (productIds.some(id => id === curr.id)) {
|
|
285
|
+
return {
|
|
286
|
+
...curr,
|
|
287
|
+
frequency: resolveSellingPlanFrequency(frequencies, frequenciesEveryPeriod, curr.frequency, defaultFrequency)
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
return curr;
|
|
292
|
+
});
|
|
223
293
|
}
|
|
294
|
+
|
|
224
295
|
return coreOptedin(state, action);
|
|
225
296
|
};
|
|
226
297
|
|
|
@@ -228,7 +299,9 @@ export const productOffer = (state = {}, action) => state;
|
|
|
228
299
|
|
|
229
300
|
export const productPlans = (state = {}, action) => {
|
|
230
301
|
if (constants.SETUP_PRODUCT === action.type) {
|
|
231
|
-
const
|
|
302
|
+
const {
|
|
303
|
+
payload: { product }
|
|
304
|
+
} = action;
|
|
232
305
|
return (
|
|
233
306
|
[product, ...product?.variants]?.reduce(
|
|
234
307
|
(acc, cur) => ({
|