@veloceapps/sdk 11.0.0-126 → 11.0.0-128

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.
@@ -282,6 +282,21 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.9", ngImpor
282
282
  args: [FLOW_CUSTOMIZATION]
283
283
  }] }]; } });
284
284
 
285
+ class PCMUtils {
286
+ static mapByPrcId(pcm) {
287
+ const map = {};
288
+ if (pcm.productRelatedComponent) {
289
+ map[pcm.productRelatedComponent.id] = pcm;
290
+ }
291
+ for (const group of pcm.productComponentGroups) {
292
+ for (const gc of group.components) {
293
+ Object.assign(map, PCMUtils.mapByPrcId(gc));
294
+ }
295
+ }
296
+ return map;
297
+ }
298
+ }
299
+
285
300
  const findTransactionItem = (id, items) => {
286
301
  return findTransactionItemWithComparator(items, (ti) => ti.id === id);
287
302
  };
@@ -333,26 +348,13 @@ const replaceTransactionItem = (item, replaceTo) => {
333
348
  children: item.children.map(ti => replaceTransactionItem(ti, replaceTo)),
334
349
  };
335
350
  };
336
- const generateTransactionItem = (pcm, salesTransactionId) => {
337
- const defaultSTI = {
338
- id: UUID.UUID(),
339
- productId: pcm.id,
340
- };
341
- const defaultChildren = pcm.productComponentGroups.reduce((acc, group) => {
342
- group.components.forEach(component => {
343
- const { isComponentRequired, isDefaultComponent } = component.productRelatedComponent ?? {};
344
- if (isComponentRequired || isDefaultComponent) {
345
- acc.push(generateTransactionItemFromPCM(component, defaultSTI, salesTransactionId));
346
- }
347
- });
348
- return acc;
349
- }, []);
350
- if (!defaultChildren.length) {
351
- return defaultSTI;
352
- }
353
- return { ...defaultSTI, children: defaultChildren };
351
+ const generateTransactionItem = (option, salesTransactionId, qty, parentTi) => {
352
+ const newItem = generateTransactionItemFromPCM(option, salesTransactionId, parentTi);
353
+ // propagate Proportional quantities to children
354
+ const updatedNewItem = updateQuantity(newItem, qty ?? newItem.qty, option, parentTi?.qty);
355
+ return updatedNewItem;
354
356
  };
355
- const generateTransactionItemFromPCM = (option, parentTi, salesTransactionId) => {
357
+ const generateTransactionItemFromPCM = (option, salesTransactionId, parentTi) => {
356
358
  const childId = UUID.UUID();
357
359
  const newItem = {
358
360
  id: childId,
@@ -368,20 +370,65 @@ const generateTransactionItemFromPCM = (option, parentTi, salesTransactionId) =>
368
370
  SalesTransactionItemParent: salesTransactionId,
369
371
  ProductSellingModel: option.productSellingModelOptions?.[0]?.productSellingModel.id,
370
372
  },
371
- parentId: parentTi.id,
372
373
  qty: option.productRelatedComponent?.quantity ?? 1,
373
374
  };
375
+ if (parentTi) {
376
+ newItem.parentId = parentTi.id;
377
+ }
374
378
  newItem.children = option.productComponentGroups.reduce((acc, group) => {
375
379
  group.components.forEach(component => {
376
380
  if (component.productRelatedComponent?.isComponentRequired ||
377
381
  component.productRelatedComponent?.isDefaultComponent) {
378
- acc.push(generateTransactionItemFromPCM(component, newItem, salesTransactionId));
382
+ acc.push(generateTransactionItemFromPCM(component, salesTransactionId, newItem));
379
383
  }
380
384
  });
381
385
  return acc;
382
386
  }, []);
383
387
  return newItem;
384
388
  };
389
+ const flattenTransactionItem = (ti) => {
390
+ const result = [];
391
+ const traverse = (item) => {
392
+ if (!item)
393
+ return;
394
+ result.push(item);
395
+ if (Array.isArray(item.children) && item.children.length) {
396
+ for (const child of item.children) {
397
+ traverse(child);
398
+ }
399
+ }
400
+ };
401
+ traverse(ti);
402
+ return result;
403
+ };
404
+ const updateQuantity = (ti, qty, pcm, parentQty = 1) => {
405
+ const pcmMap = PCMUtils.mapByPrcId(pcm);
406
+ const calcNewQty = (item, parentPrevQty, parentNewQty, parentPcm, inputQty, isDirectChange) => {
407
+ const scaleMethod = parentPcm?.productRelatedComponent?.quantityScaleMethod;
408
+ if (isDirectChange) {
409
+ return scaleMethod === 'Proportional' ? parentPrevQty * inputQty : inputQty;
410
+ }
411
+ else {
412
+ return scaleMethod === 'Proportional' ? (item.qty / parentPrevQty) * parentNewQty : item.qty;
413
+ }
414
+ };
415
+ const updateItem = (item, parentPrevQty, parentNewQty, isDirectChange) => {
416
+ const pcm = pcmMap[item.productRelatedComponentId];
417
+ let nextQty = item.qty;
418
+ if (item.id === ti.id || isDirectChange) {
419
+ nextQty = calcNewQty(item, parentPrevQty, parentNewQty, pcm, qty, true);
420
+ }
421
+ else if (parentPrevQty !== parentNewQty) {
422
+ nextQty = calcNewQty(item, parentPrevQty, parentNewQty, pcm, qty, false);
423
+ }
424
+ return {
425
+ ...item,
426
+ qty: nextQty,
427
+ children: item.children.map(child => updateItem(child, item.qty, nextQty, false)),
428
+ };
429
+ };
430
+ return updateItem(ti, parentQty, parentQty, false);
431
+ };
385
432
 
386
433
  class TransactionItemWorker {
387
434
  constructor(src) {
@@ -510,10 +557,8 @@ class ConfigurationService {
510
557
  transactionItem = salesTransactionItems.find(item => item.productId === productId);
511
558
  }
512
559
  if (!transactionItem) {
513
- transactionItem = generateTransactionItem(this.getPCMModel(), this.state?.salesTransaction.id);
514
- if (typeof newProductQty === 'number' && newProductQty > 0) {
515
- transactionItem.qty = newProductQty;
516
- }
560
+ const quantity = typeof newProductQty === 'number' && newProductQty > 0 ? newProductQty : undefined;
561
+ transactionItem = generateTransactionItem(this.getPCMModel(), this.state?.salesTransaction.id, quantity);
517
562
  isRootGenerated = true;
518
563
  }
519
564
  const guidedSellingResult = this.guidedSellingService.guidedSellingResult;
@@ -593,7 +638,6 @@ class ConfigurationService {
593
638
  })));
594
639
  }
595
640
  getPCMModel() {
596
- console.log('GET PCM');
597
641
  const pcmModel = this.configurationRuntimeService.pcmModel;
598
642
  if (!pcmModel) {
599
643
  throw new Error('PCM model not initialized');
@@ -1230,9 +1274,32 @@ class FlowStateConfigurationService {
1230
1274
  this.salesTransactionService = salesTransactionService;
1231
1275
  this.flowConfigurationService = flowConfigurationService;
1232
1276
  this.pcmApiService = pcmApiService;
1277
+ this.pcmCache = {};
1278
+ }
1279
+ updateQuantity$(props) {
1280
+ const allItems = this.salesTransactionService.state?.salesTransaction?.salesTransactionItems ?? [];
1281
+ const ti = allItems.find(item => item.id === props.id);
1282
+ if (!ti) {
1283
+ return of(undefined);
1284
+ }
1285
+ return this.getPCMProduct$(ti.productId).pipe(map(pcm => updateQuantity(ti, props.qty, pcm)), switchMap(updatedTi => {
1286
+ const state = this.salesTransactionService.state;
1287
+ if (!state) {
1288
+ return of(undefined);
1289
+ }
1290
+ const updatedState = {
1291
+ ...state,
1292
+ salesTransaction: {
1293
+ ...state.salesTransaction,
1294
+ salesTransactionItems: state.salesTransaction.salesTransactionItems.map(item => {
1295
+ return updatedTi.id === item.id ? updatedTi : item;
1296
+ }),
1297
+ },
1298
+ };
1299
+ return this.flowConfigurationService.calculate$(updatedState);
1300
+ }), switchMap(() => this.flowStateService.executeRequest$({}, true)), map(noop));
1233
1301
  }
1234
1302
  addToCart$(props) {
1235
- console.log(props);
1236
1303
  let request$;
1237
1304
  const stateful = this.flowInfoService.flow?.properties.stateful;
1238
1305
  if (stateful) {
@@ -1251,7 +1318,7 @@ class FlowStateConfigurationService {
1251
1318
  return request$.pipe(switchMap(() => this.flowStateService.executeRequest$({}, true)), map(noop));
1252
1319
  }
1253
1320
  configureExternal$(props) {
1254
- return this.pcmApiService.fetchPCMByProductId(props.productId).pipe(switchMap(pcm => {
1321
+ return this.getPCMProduct$(props.productId).pipe(switchMap(pcm => {
1255
1322
  const { state } = this.salesTransactionService;
1256
1323
  if (!state) {
1257
1324
  return of();
@@ -1262,8 +1329,7 @@ class FlowStateConfigurationService {
1262
1329
  ...state.salesTransaction,
1263
1330
  salesTransactionItems: [
1264
1331
  {
1265
- ...generateTransactionItem(pcm, this.configurationService.state?.salesTransaction.id),
1266
- qty: props.qty ?? 1,
1332
+ ...generateTransactionItem(pcm, this.configurationService.state?.salesTransaction.id, props.qty ?? 1),
1267
1333
  stiAttributes: Object.entries(props.attributesMap ?? {}).map(([attributeName, value]) => ({
1268
1334
  attributeName,
1269
1335
  value,
@@ -1288,6 +1354,15 @@ class FlowStateConfigurationService {
1288
1354
  }));
1289
1355
  }));
1290
1356
  }
1357
+ getPCMProduct$(productId) {
1358
+ const cached = this.pcmCache[productId];
1359
+ if (cached) {
1360
+ return of(cached);
1361
+ }
1362
+ return this.pcmApiService
1363
+ .fetchPCMByProductId(productId)
1364
+ .pipe(tap(pcmProduct => (this.pcmCache[productId] = pcmProduct)));
1365
+ }
1291
1366
  }
1292
1367
  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 });
1293
1368
  FlowStateConfigurationService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.2.9", ngImport: i0, type: FlowStateConfigurationService });
@@ -1994,5 +2069,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.9", ngImpor
1994
2069
  * Generated bundle index. Do not edit.
1995
2070
  */
1996
2071
 
1997
- 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 };
2072
+ 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 };
1998
2073
  //# sourceMappingURL=veloceapps-sdk-core.mjs.map