@funnelsgrove/runtime 0.1.37 → 0.1.38

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.
@@ -1,5 +1,9 @@
1
1
  const isRecord = (value) => (typeof value === 'object' && value !== null && !Array.isArray(value));
2
2
  const readString = (value) => (typeof value === 'string' && value.trim() ? value.trim() : null);
3
+ const normalizeCatalogLookupKey = (value) => (value
4
+ .trim()
5
+ .toLowerCase()
6
+ .replace(/[^a-z0-9]+/g, ''));
3
7
  const readFiniteNumber = (value) => {
4
8
  const numeric = typeof value === 'number'
5
9
  ? value
@@ -35,6 +39,24 @@ export const findGeneratedOfferSet = (offerSets, offerSetKey) => {
35
39
  const readProviderPlanId = (item, mode) => (mode === 'test'
36
40
  ? readString(item.testProviderPlanId)
37
41
  : readString(item.liveProviderPlanId));
42
+ const resolveCatalogKey = (baseCatalog, funnelPlanKey) => {
43
+ var _a;
44
+ const key = funnelPlanKey.trim();
45
+ if (!key) {
46
+ return null;
47
+ }
48
+ if (Object.prototype.hasOwnProperty.call(baseCatalog, key)) {
49
+ return key;
50
+ }
51
+ const normalizedKey = normalizeCatalogLookupKey(key);
52
+ return (_a = Object.keys(baseCatalog).find((catalogKey) => (normalizeCatalogLookupKey(catalogKey) === normalizedKey))) !== null && _a !== void 0 ? _a : null;
53
+ };
54
+ const resolveOfferItemRuntimeKey = (item, baseCatalogsByMode) => {
55
+ var _a, _b;
56
+ return (item
57
+ ? (_b = (_a = resolveCatalogKey(baseCatalogsByMode.test, item.funnelPlanKey)) !== null && _a !== void 0 ? _a : resolveCatalogKey(baseCatalogsByMode.live, item.funnelPlanKey)) !== null && _b !== void 0 ? _b : readString(item.funnelPlanKey)
58
+ : null);
59
+ };
38
60
  export const applyGeneratedOfferSetToPlanCatalog = (baseCatalog, mode, offerSet) => {
39
61
  var _a, _b, _c;
40
62
  if (!offerSet) {
@@ -46,14 +68,18 @@ export const applyGeneratedOfferSetToPlanCatalog = (baseCatalog, mode, offerSet)
46
68
  ...getSortedItemsByKind(offerSet, 'upsell'),
47
69
  ...getSortedItemsByKind(offerSet, 'downsell'),
48
70
  ];
49
- const defaultPlanKey = ((_a = planItems.find((item) => item.isDefault === true)) === null || _a === void 0 ? void 0 : _a.funnelPlanKey) ||
50
- ((_b = planItems[0]) === null || _b === void 0 ? void 0 : _b.funnelPlanKey) ||
51
- '';
52
71
  const nextCatalog = Object.assign({}, baseCatalog);
72
+ const defaultPlanItem = (_a = planItems.find((item) => item.isDefault === true)) !== null && _a !== void 0 ? _a : planItems[0];
73
+ const defaultPlanKey = defaultPlanItem
74
+ ? (_b = resolveCatalogKey(nextCatalog, defaultPlanItem.funnelPlanKey)) !== null && _b !== void 0 ? _b : defaultPlanItem.funnelPlanKey
75
+ : '';
53
76
  for (const item of catalogItems) {
54
- const key = item.funnelPlanKey.trim();
77
+ const key = resolveCatalogKey(nextCatalog, item.funnelPlanKey);
78
+ if (!key) {
79
+ continue;
80
+ }
55
81
  const basePlan = nextCatalog[key];
56
- if (!key || !basePlan) {
82
+ if (!basePlan) {
57
83
  continue;
58
84
  }
59
85
  const providerPlanId = readProviderPlanId(item, mode);
@@ -130,16 +156,21 @@ export const buildGeneratedOfferSetDiscountList = (offerSet, mode, fallbackDisco
130
156
  ];
131
157
  };
132
158
  export const resolveGeneratedOfferSetRuntime = (input) => {
133
- var _a, _b, _c, _d;
159
+ var _a, _b;
134
160
  const offerSet = findGeneratedOfferSet(input.offerSets, input.offerSetKey);
135
161
  const planItems = getSortedItemsByKind(offerSet, 'plan');
136
162
  const upsellItems = getSortedItemsByKind(offerSet, 'upsell');
137
163
  const downsellItems = getSortedItemsByKind(offerSet, 'downsell');
138
- const planKeys = planItems.map((item) => item.funnelPlanKey);
139
- const upsellPlanKeys = upsellItems.map((item) => item.funnelPlanKey);
164
+ const planKeys = planItems
165
+ .map((item) => resolveOfferItemRuntimeKey(item, input.baseCatalogsByMode))
166
+ .filter((key) => Boolean(key));
167
+ const upsellPlanKeys = upsellItems
168
+ .map((item) => resolveOfferItemRuntimeKey(item, input.baseCatalogsByMode))
169
+ .filter((key) => Boolean(key));
140
170
  const downsellPlanKeys = (downsellItems.length > 0 ? downsellItems : planItems)
141
- .map((item) => item.funnelPlanKey);
142
- const defaultPlanKey = (_d = (_b = (_a = planItems.find((item) => item.isDefault === true)) === null || _a === void 0 ? void 0 : _a.funnelPlanKey) !== null && _b !== void 0 ? _b : (_c = planItems[0]) === null || _c === void 0 ? void 0 : _c.funnelPlanKey) !== null && _d !== void 0 ? _d : '';
171
+ .map((item) => resolveOfferItemRuntimeKey(item, input.baseCatalogsByMode))
172
+ .filter((key) => Boolean(key));
173
+ const defaultPlanKey = (_b = resolveOfferItemRuntimeKey((_a = planItems.find((item) => item.isDefault === true)) !== null && _a !== void 0 ? _a : planItems[0], input.baseCatalogsByMode)) !== null && _b !== void 0 ? _b : '';
143
174
  const plansByMode = {
144
175
  test: applyGeneratedOfferSetToPlanCatalog(input.baseCatalogsByMode.test, 'test', offerSet),
145
176
  live: applyGeneratedOfferSetToPlanCatalog(input.baseCatalogsByMode.live, 'live', offerSet),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funnelsgrove/runtime",
3
- "version": "0.1.37",
3
+ "version": "0.1.38",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "main": "./dist/index.js",