@veloceapps/sdk 11.0.0-127 → 11.0.0-129

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.
@@ -38,6 +38,21 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.9", ngImpor
38
38
  type: Injectable
39
39
  }], ctorParameters: function () { return [{ type: i1.PCMApiService }]; } });
40
40
 
41
+ class PCMUtils {
42
+ static mapByPrcId(pcm) {
43
+ const map = {};
44
+ if (pcm.productRelatedComponent) {
45
+ map[pcm.productRelatedComponent.id] = pcm;
46
+ }
47
+ for (const group of pcm.productComponentGroups) {
48
+ for (const gc of group.components) {
49
+ Object.assign(map, PCMUtils.mapByPrcId(gc));
50
+ }
51
+ }
52
+ return map;
53
+ }
54
+ }
55
+
41
56
  const findTransactionItem = (id, items) => {
42
57
  return findTransactionItemWithComparator(items, (ti) => ti.id === id);
43
58
  };
@@ -80,27 +95,13 @@ const replaceTransactionItem = (item, replaceTo) => {
80
95
  }
81
96
  return Object.assign(Object.assign({}, item), { children: item.children.map(ti => replaceTransactionItem(ti, replaceTo)) });
82
97
  };
83
- const generateTransactionItem = (pcm, salesTransactionId) => {
84
- const defaultSTI = {
85
- id: UUID.UUID(),
86
- productId: pcm.id,
87
- };
88
- const defaultChildren = pcm.productComponentGroups.reduce((acc, group) => {
89
- group.components.forEach(component => {
90
- var _a;
91
- const { isComponentRequired, isDefaultComponent } = (_a = component.productRelatedComponent) !== null && _a !== void 0 ? _a : {};
92
- if (isComponentRequired || isDefaultComponent) {
93
- acc.push(generateTransactionItemFromPCM(component, defaultSTI, salesTransactionId));
94
- }
95
- });
96
- return acc;
97
- }, []);
98
- if (!defaultChildren.length) {
99
- return defaultSTI;
100
- }
101
- return Object.assign(Object.assign({}, defaultSTI), { children: defaultChildren });
98
+ const generateTransactionItem = (option, salesTransactionId, qty, parentTi) => {
99
+ const newItem = generateTransactionItemFromPCM(option, salesTransactionId, parentTi);
100
+ // propagate Proportional quantities to children
101
+ const updatedNewItem = updateQuantity(newItem, qty !== null && qty !== void 0 ? qty : newItem.qty, option, parentTi === null || parentTi === void 0 ? void 0 : parentTi.qty);
102
+ return updatedNewItem;
102
103
  };
103
- const generateTransactionItemFromPCM = (option, parentTi, salesTransactionId) => {
104
+ const generateTransactionItemFromPCM = (option, salesTransactionId, parentTi) => {
104
105
  var _a, _b, _c, _d, _e;
105
106
  const childId = UUID.UUID();
106
107
  const newItem = {
@@ -117,21 +118,63 @@ const generateTransactionItemFromPCM = (option, parentTi, salesTransactionId) =>
117
118
  SalesTransactionItemParent: salesTransactionId,
118
119
  ProductSellingModel: (_c = (_b = option.productSellingModelOptions) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.productSellingModel.id,
119
120
  },
120
- parentId: parentTi.id,
121
121
  qty: (_e = (_d = option.productRelatedComponent) === null || _d === void 0 ? void 0 : _d.quantity) !== null && _e !== void 0 ? _e : 1,
122
122
  };
123
+ if (parentTi) {
124
+ newItem.parentId = parentTi.id;
125
+ }
123
126
  newItem.children = option.productComponentGroups.reduce((acc, group) => {
124
127
  group.components.forEach(component => {
125
128
  var _a, _b;
126
129
  if (((_a = component.productRelatedComponent) === null || _a === void 0 ? void 0 : _a.isComponentRequired) ||
127
130
  ((_b = component.productRelatedComponent) === null || _b === void 0 ? void 0 : _b.isDefaultComponent)) {
128
- acc.push(generateTransactionItemFromPCM(component, newItem, salesTransactionId));
131
+ acc.push(generateTransactionItemFromPCM(component, salesTransactionId, newItem));
129
132
  }
130
133
  });
131
134
  return acc;
132
135
  }, []);
133
136
  return newItem;
134
137
  };
138
+ const flattenTransactionItem = (ti) => {
139
+ const result = [];
140
+ const traverse = (item) => {
141
+ if (!item)
142
+ return;
143
+ result.push(item);
144
+ if (Array.isArray(item.children) && item.children.length) {
145
+ for (const child of item.children) {
146
+ traverse(child);
147
+ }
148
+ }
149
+ };
150
+ traverse(ti);
151
+ return result;
152
+ };
153
+ const updateQuantity = (ti, qty, pcm, parentQty = 1) => {
154
+ const pcmMap = PCMUtils.mapByPrcId(pcm);
155
+ const calcNewQty = (item, parentPrevQty, parentNewQty, parentPcm, inputQty, isDirectChange) => {
156
+ var _a;
157
+ const scaleMethod = (_a = parentPcm === null || parentPcm === void 0 ? void 0 : parentPcm.productRelatedComponent) === null || _a === void 0 ? void 0 : _a.quantityScaleMethod;
158
+ if (isDirectChange) {
159
+ return scaleMethod === 'Proportional' ? parentPrevQty * inputQty : inputQty;
160
+ }
161
+ else {
162
+ return scaleMethod === 'Proportional' ? (item.qty / parentPrevQty) * parentNewQty : item.qty;
163
+ }
164
+ };
165
+ const updateItem = (item, parentPrevQty, parentNewQty, isDirectChange) => {
166
+ const pcm = pcmMap[item.productRelatedComponentId];
167
+ let nextQty = item.qty;
168
+ if (item.id === ti.id || isDirectChange) {
169
+ nextQty = calcNewQty(item, parentPrevQty, parentNewQty, pcm, qty, true);
170
+ }
171
+ else if (parentPrevQty !== parentNewQty) {
172
+ nextQty = calcNewQty(item, parentPrevQty, parentNewQty, pcm, qty, false);
173
+ }
174
+ return Object.assign(Object.assign({}, item), { qty: nextQty, children: item.children.map(child => updateItem(child, item.qty, nextQty, false)) });
175
+ };
176
+ return updateItem(ti, parentQty, parentQty, false);
177
+ };
135
178
 
136
179
  class TransactionItemWorker {
137
180
  constructor(src) {
@@ -1098,10 +1141,28 @@ class FlowStateConfigurationService {
1098
1141
  this.salesTransactionService = salesTransactionService;
1099
1142
  this.flowConfigurationService = flowConfigurationService;
1100
1143
  this.pcmApiService = pcmApiService;
1144
+ this.pcmCache = {};
1145
+ }
1146
+ updateQuantity$(props) {
1147
+ var _a, _b, _c;
1148
+ const allItems = (_c = (_b = (_a = this.salesTransactionService.state) === null || _a === void 0 ? void 0 : _a.salesTransaction) === null || _b === void 0 ? void 0 : _b.salesTransactionItems) !== null && _c !== void 0 ? _c : [];
1149
+ const ti = allItems.find(item => item.id === props.id);
1150
+ if (!ti) {
1151
+ return of(undefined);
1152
+ }
1153
+ return this.getPCMProduct$(ti.productId).pipe(map(pcm => updateQuantity(ti, props.qty, pcm)), switchMap(updatedTi => {
1154
+ const state = this.salesTransactionService.state;
1155
+ if (!state) {
1156
+ return of(undefined);
1157
+ }
1158
+ const updatedState = Object.assign(Object.assign({}, state), { salesTransaction: Object.assign(Object.assign({}, state.salesTransaction), { salesTransactionItems: state.salesTransaction.salesTransactionItems.map(item => {
1159
+ return updatedTi.id === item.id ? updatedTi : item;
1160
+ }) }) });
1161
+ return this.flowConfigurationService.calculate$(updatedState);
1162
+ }), switchMap(() => this.flowStateService.executeRequest$({}, true)), map(noop));
1101
1163
  }
1102
1164
  addToCart$(props) {
1103
1165
  var _a;
1104
- console.log(props);
1105
1166
  let request$;
1106
1167
  const stateful = (_a = this.flowInfoService.flow) === null || _a === void 0 ? void 0 : _a.properties.stateful;
1107
1168
  if (stateful) {
@@ -1120,14 +1181,14 @@ class FlowStateConfigurationService {
1120
1181
  return request$.pipe(switchMap(() => this.flowStateService.executeRequest$({}, true)), map(noop));
1121
1182
  }
1122
1183
  configureExternal$(props) {
1123
- return this.pcmApiService.fetchPCMByProductId(props.productId).pipe(switchMap(pcm => {
1184
+ return this.getPCMProduct$(props.productId).pipe(switchMap(pcm => {
1124
1185
  var _a, _b, _c;
1125
1186
  const { state } = this.salesTransactionService;
1126
1187
  if (!state) {
1127
1188
  return of();
1128
1189
  }
1129
1190
  const stateToConfigure = Object.assign(Object.assign({}, state), { salesTransaction: Object.assign(Object.assign({}, state.salesTransaction), { salesTransactionItems: [
1130
- Object.assign(Object.assign({}, generateTransactionItem(pcm, (_a = this.configurationService.state) === null || _a === void 0 ? void 0 : _a.salesTransaction.id)), { qty: (_b = props.qty) !== null && _b !== void 0 ? _b : 1, stiAttributes: Object.entries((_c = props.attributesMap) !== null && _c !== void 0 ? _c : {}).map(([attributeName, value]) => ({
1191
+ Object.assign(Object.assign({}, generateTransactionItem(pcm, (_a = this.configurationService.state) === null || _a === void 0 ? void 0 : _a.salesTransaction.id, (_b = props.qty) !== null && _b !== void 0 ? _b : 1)), { stiAttributes: Object.entries((_c = props.attributesMap) !== null && _c !== void 0 ? _c : {}).map(([attributeName, value]) => ({
1131
1192
  attributeName,
1132
1193
  value,
1133
1194
  })) }),
@@ -1142,6 +1203,15 @@ class FlowStateConfigurationService {
1142
1203
  }));
1143
1204
  }));
1144
1205
  }
1206
+ getPCMProduct$(productId) {
1207
+ const cached = this.pcmCache[productId];
1208
+ if (cached) {
1209
+ return of(cached);
1210
+ }
1211
+ return this.pcmApiService
1212
+ .fetchPCMByProductId(productId)
1213
+ .pipe(tap(pcmProduct => (this.pcmCache[productId] = pcmProduct)));
1214
+ }
1145
1215
  }
1146
1216
  FlowStateConfigurationService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.9", ngImport: i0, type: FlowStateConfigurationService, deps: [{ token: FlowInfoService }, { token: FlowStateService }, { token: ConfigurationService }, { token: SalesTransactionService }, { token: FlowConfigurationService }, { token: i1.PCMApiService }], target: i0.ɵɵFactoryTarget.Injectable });
1147
1217
  FlowStateConfigurationService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.2.9", ngImport: i0, type: FlowStateConfigurationService });
@@ -1278,10 +1348,8 @@ class ConfigurationService {
1278
1348
  transactionItem = salesTransactionItems.find(item => item.productId === productId);
1279
1349
  }
1280
1350
  if (!transactionItem) {
1281
- transactionItem = generateTransactionItem(this.getPCMModel(), (_b = this.state) === null || _b === void 0 ? void 0 : _b.salesTransaction.id);
1282
- if (typeof newProductQty === 'number' && newProductQty > 0) {
1283
- transactionItem.qty = newProductQty;
1284
- }
1351
+ const quantity = typeof newProductQty === 'number' && newProductQty > 0 ? newProductQty : undefined;
1352
+ transactionItem = generateTransactionItem(this.getPCMModel(), (_b = this.state) === null || _b === void 0 ? void 0 : _b.salesTransaction.id, quantity);
1285
1353
  isRootGenerated = true;
1286
1354
  }
1287
1355
  const guidedSellingResult = this.guidedSellingService.guidedSellingResult;
@@ -1349,7 +1417,6 @@ class ConfigurationService {
1349
1417
  })));
1350
1418
  }
1351
1419
  getPCMModel() {
1352
- console.log('GET PCM');
1353
1420
  const pcmModel = this.configurationRuntimeService.pcmModel;
1354
1421
  if (!pcmModel) {
1355
1422
  throw new Error('PCM model not initialized');
@@ -2005,5 +2072,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.9", ngImpor
2005
2072
  * Generated bundle index. Do not edit.
2006
2073
  */
2007
2074
 
2008
- export { ActionCodePipe, CalendarDirective, CatalogProductsService, ConfigurationRuntimeService, ConfigurationService, ConfigurationStateService, DEFAULT_FORMATTING_SETTINGS, DatePipe, FLOW_CUSTOMIZATION, FORMATTING_SETTINGS_TOKEN, FlowConfigurationService, FlowInfoService, FlowStateConfigurationService, FlowStateService, GuidedSellingService, IntegrationState, NumberPipe, PricePipe, ProductImagesService, RuntimeSettingsService, SalesTransactionService, SdkCoreModule, SdkDirectivesModule, SdkPipesModule, TestModeConfigurationService, TransactionItemWorker, UI_DEFINITION_VERSION, extractMetadata, filterSuccessfulExecute, findTransactionItem, findTransactionItemWithComparator, generateTransactionItem, generateTransactionItemFromPCM, insertTransactionItem, removeTransactionItem, replaceTransactionItem };
2075
+ export { ActionCodePipe, CalendarDirective, CatalogProductsService, ConfigurationRuntimeService, ConfigurationService, ConfigurationStateService, DEFAULT_FORMATTING_SETTINGS, DatePipe, FLOW_CUSTOMIZATION, FORMATTING_SETTINGS_TOKEN, FlowConfigurationService, FlowInfoService, FlowStateConfigurationService, FlowStateService, GuidedSellingService, IntegrationState, NumberPipe, PCMUtils, PricePipe, ProductImagesService, RuntimeSettingsService, SalesTransactionService, SdkCoreModule, SdkDirectivesModule, SdkPipesModule, TestModeConfigurationService, TransactionItemWorker, UI_DEFINITION_VERSION, extractMetadata, filterSuccessfulExecute, findTransactionItem, findTransactionItemWithComparator, flattenTransactionItem, generateTransactionItem, generateTransactionItemFromPCM, insertTransactionItem, removeTransactionItem, replaceTransactionItem, updateQuantity };
2009
2076
  //# sourceMappingURL=veloceapps-sdk-core.mjs.map