@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.
@@ -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 { payload: product } = action;
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 product = action.payload;
152
+ const {
153
+ payload: { product }
154
+ } = action;
136
155
  const sellingPlanGroup = getOGSellingPlanGroup(product);
137
- const frequencies = sellingPlanGroup?.selling_plans?.map(({ id }) => `${id}`);
156
+ const frequencies = sellingPlansToFrequencies(sellingPlanGroup);
138
157
  if (frequencies?.length) {
139
- const frequenciesEveryPeriod = sellingPlanGroup?.selling_plans
140
- ?.map(({ options }) => options || [])
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 product = action.payload;
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 { autoship, autoship_by_default, in_stock, offer: offerEl } = action.payload;
199
-
200
- return Object.keys(autoship).reduce(
201
- (acc, id) =>
202
- acc.concat(
203
- !acc.some(it => it.id === id) && autoship[id] && autoship_by_default[id] && in_stock[id]
204
- ? { id, frequency: offerEl.defaultFrequency }
205
- : []
206
- ),
207
- state
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 productIds = action.payload.variants.map(variant => variant.id).map(id => `${id}`);
213
- const sellingPlanGroup = getOGSellingPlanGroup(action.payload);
214
- const frequencies = sellingPlanGroup?.selling_plans?.map(({ id }) => `${id}`);
215
- // if the product is in the list of variants and its frequency isn't a valid selling plan, replace with the first valid selling plan
216
- return state.map(cur => ({
217
- ...cur,
218
- frequency:
219
- productIds.some(id => id === cur.id) && !frequencies.some(freq => freq === cur.frequency)
220
- ? frequencies[0]
221
- : cur.frequency
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 product = action.payload;
302
+ const {
303
+ payload: { product }
304
+ } = action;
232
305
  return (
233
306
  [product, ...product?.variants]?.reduce(
234
307
  (acc, cur) => ({