@contrail/flexplm 3.1.0-alpha.6c6914d → 3.1.0-alpha.9a5a5c3

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.
@@ -107,7 +107,8 @@ export declare class BaseProcessPublishAssortment {
107
107
  * - The events are still uploaded to a VibeIQ file (auto-deleted) to make troubleshooting easier.
108
108
  * - Product-season events are all created before any SKU-season event is created.
109
109
  * - The external-event entity has no batch endpoint, so the events are created one by one,
110
- * a small group at a time (separateEventsConcurrency, default 10) because there can be thousands.
110
+ * a small group at a time (separateEventsConcurrency, default 1; see DEFAULT_SEPARATE_EVENTS_CONCURRENCY)
111
+ * because there can be thousands.
111
112
  */
112
113
  private sendAsSeparateVibeIQEvents;
113
114
  /** Builds the VibeIQ external-event body for one seasonal event. */
@@ -742,7 +742,8 @@ class BaseProcessPublishAssortment {
742
742
  * - The events are still uploaded to a VibeIQ file (auto-deleted) to make troubleshooting easier.
743
743
  * - Product-season events are all created before any SKU-season event is created.
744
744
  * - The external-event entity has no batch endpoint, so the events are created one by one,
745
- * a small group at a time (separateEventsConcurrency, default 10) because there can be thousands.
745
+ * a small group at a time (separateEventsConcurrency, default 1; see DEFAULT_SEPARATE_EVENTS_CONCURRENCY)
746
+ * because there can be thousands.
746
747
  */
747
748
  async sendAsSeparateVibeIQEvents(events, eventType) {
748
749
  const uploadFile = await this.uploadEventsFile(events);
@@ -769,6 +770,19 @@ class BaseProcessPublishAssortment {
769
770
  // Events created before the failure are already queued; report that so a re-publish is understood.
770
771
  error.eventsCreatedBeforeFailure = { ...eventsCreated };
771
772
  error.eventsFileId = uploadFile.id;
773
+ // ErrorResponseObject.getResponse only passes error.details through (as errorDetails), so the same
774
+ // information goes there too. Existing details keys stay where they are; details that are not an
775
+ // object are kept under batchErrorDetails.
776
+ const queuedInfo = { eventsCreatedBeforeFailure: error.eventsCreatedBeforeFailure, eventsFileId: error.eventsFileId };
777
+ if (error.details && typeof error.details === 'object' && !Array.isArray(error.details)) {
778
+ error.details = { ...error.details, ...queuedInfo };
779
+ }
780
+ else if (error.details !== undefined && error.details !== null) {
781
+ error.details = { batchErrorDetails: error.details, ...queuedInfo };
782
+ }
783
+ else {
784
+ error.details = queuedInfo;
785
+ }
772
786
  console.error('sendAsSeparateVibeIQEvents(): failed after creating: ' + JSON.stringify(eventsCreated) + ': ' + error.message);
773
787
  throw error;
774
788
  }
@@ -1220,4 +1234,6 @@ BaseProcessPublishAssortment.ASSORTMENT_NO_FED_INFO = 'Assortment doesn\'t have
1220
1234
  BaseProcessPublishAssortment.NOT_ABLE_TO_PROCESS_DELETE_CHANGES = 'Error: Not able to process delete changes';
1221
1235
  BaseProcessPublishAssortment.SEND_MODE_SEPARATE_VIBEIQ_EVENTS = 'separatevibeiqevents'; // compared lower-cased
1222
1236
  BaseProcessPublishAssortment.SEND_SEASONAL_EVENT_OBJECT_CLASS = 'SendSeasonalEventToFlexPLM';
1223
- BaseProcessPublishAssortment.DEFAULT_SEPARATE_EVENTS_CONCURRENCY = 10;
1237
+ // 1 until the external-events deduplication fix ships: the service uses a millisecond timestamp as the queue
1238
+ // dedup id, so events created at the same moment can be silently dropped (about 2 in 10 at a concurrency of 10).
1239
+ BaseProcessPublishAssortment.DEFAULT_SEPARATE_EVENTS_CONCURRENCY = 1;
@@ -10,6 +10,7 @@ const type_conversion_utils_1 = require("../util/type-conversion-utils");
10
10
  const map_utils_1 = require("../util/map-utils");
11
11
  const item_family_changes_1 = require("../interfaces/item-family-changes");
12
12
  const flexplm_connect_1 = require("../util/flexplm-connect");
13
+ const error_response_object_1 = require("../util/error-response-object");
13
14
  let federatedId = '';
14
15
  jest.mock('../util/data-converter', () => {
15
16
  return {
@@ -2399,7 +2400,7 @@ describe('sendEvents - separateVibeIQEvents', () => {
2399
2400
  expect(state.mixed).toBe(false);
2400
2401
  expect(result.eventsCreated).toEqual({ productSeasons: 5, colorwaySeasons: 3 });
2401
2402
  });
2402
- it('creates 10 events at a time when separateEventsConcurrency is not a valid number', async () => {
2403
+ it('creates 1 event at a time when separateEventsConcurrency is not a valid number', async () => {
2403
2404
  const bppa = newBppa(buildConfig({ separateEventsConcurrency: 'abc' }));
2404
2405
  const state = trackConcurrency();
2405
2406
  const events = [];
@@ -2408,7 +2409,7 @@ describe('sendEvents - separateVibeIQEvents', () => {
2408
2409
  }
2409
2410
  await bppa.sendEvents(events);
2410
2411
  expect(createCalls).toHaveLength(25);
2411
- expect(state.maxInFlight).toBe(10);
2412
+ expect(state.maxInFlight).toBe(1);
2412
2413
  });
2413
2414
  it('uploads the events file, and does not call FlexPLM or create AssortmentPublishedToFlexPLM', async () => {
2414
2415
  const bppa = newBppa(buildConfig());
@@ -2450,6 +2451,78 @@ describe('sendEvents - separateVibeIQEvents', () => {
2450
2451
  });
2451
2452
  expect(createCalls).toHaveLength(4);
2452
2453
  });
2454
+ describe('queued counts and file id in error.details', () => {
2455
+ const failOnCall = (failingCall, reason) => {
2456
+ let callCount = 0;
2457
+ createImpl = () => {
2458
+ callCount++;
2459
+ return callCount === failingCall ? Promise.reject(reason) : Promise.resolve({});
2460
+ };
2461
+ };
2462
+ const fiveProducts = () => [1, 2, 3, 4, 5].map(i => productEvent(i));
2463
+ beforeEach(() => {
2464
+ jest.spyOn(console, 'error').mockImplementation(() => undefined);
2465
+ });
2466
+ it('adds the counts and file id to details and keeps the existing details keys', async () => {
2467
+ const bppa = newBppa(buildConfig({ separateEventsConcurrency: 2 }));
2468
+ failOnCall(4, { statusCode: 404, message: 'Internal Server Error' });
2469
+ await expect(bppa.sendEvents(fiveProducts())).rejects.toMatchObject({
2470
+ details: {
2471
+ statusCode: 404,
2472
+ message: 'Internal Server Error',
2473
+ eventsCreatedBeforeFailure: { productSeasons: 3, colorwaySeasons: 0 },
2474
+ eventsFileId: 'file-123'
2475
+ }
2476
+ });
2477
+ });
2478
+ it('creates details when the error has none', async () => {
2479
+ const bppa = newBppa(buildConfig({ separateEventsConcurrency: 1 }));
2480
+ failOnCall(2, new Error('create failed'));
2481
+ await expect(bppa.sendEvents(fiveProducts())).rejects.toMatchObject({
2482
+ message: 'create failed',
2483
+ details: { eventsCreatedBeforeFailure: { productSeasons: 1, colorwaySeasons: 0 }, eventsFileId: 'file-123' }
2484
+ });
2485
+ });
2486
+ it('keeps details that are not an object under batchErrorDetails', async () => {
2487
+ const bppa = newBppa(buildConfig({ separateEventsConcurrency: 1 }));
2488
+ const error = new Error('create failed');
2489
+ error.details = 'plain text details';
2490
+ failOnCall(1, error);
2491
+ await expect(bppa.sendEvents(fiveProducts())).rejects.toMatchObject({
2492
+ details: {
2493
+ batchErrorDetails: 'plain text details',
2494
+ eventsCreatedBeforeFailure: { productSeasons: 0, colorwaySeasons: 0 },
2495
+ eventsFileId: 'file-123'
2496
+ }
2497
+ });
2498
+ });
2499
+ it('still sets the counts and file id as properties of the error', async () => {
2500
+ const bppa = newBppa(buildConfig({ separateEventsConcurrency: 1 }));
2501
+ failOnCall(3, new Error('create failed'));
2502
+ await expect(bppa.sendEvents(fiveProducts())).rejects.toMatchObject({
2503
+ eventsCreatedBeforeFailure: { productSeasons: 2, colorwaySeasons: 0 },
2504
+ eventsFileId: 'file-123'
2505
+ });
2506
+ });
2507
+ it('comes through ErrorResponseObject.getResponse as errorDetails', async () => {
2508
+ const bppa = newBppa(buildConfig({ separateEventsConcurrency: 1 }));
2509
+ failOnCall(3, { statusCode: 500, message: 'boom' });
2510
+ jest.spyOn(console, 'log').mockImplementation(() => undefined);
2511
+ let thrown;
2512
+ try {
2513
+ await bppa.sendEvents(fiveProducts());
2514
+ }
2515
+ catch (e) {
2516
+ thrown = e;
2517
+ }
2518
+ const response = error_response_object_1.ErrorResponseObject.getResponse(thrown);
2519
+ expect(response.output.errorDetails).toMatchObject({
2520
+ statusCode: 500,
2521
+ eventsCreatedBeforeFailure: { productSeasons: 2, colorwaySeasons: 0 },
2522
+ eventsFileId: 'file-123'
2523
+ });
2524
+ });
2525
+ });
2453
2526
  it('keeps a readable message when the SDK rejects with a plain object instead of an Error', async () => {
2454
2527
  const bppa = newBppa(buildConfig({ separateEventsConcurrency: 1 }));
2455
2528
  createImpl = () => Promise.reject({ statusCode: 404, message: 'Internal Server Error' });
@@ -6,6 +6,12 @@ export declare class ConfigDefaults {
6
6
  static PROTO_KEYS: string[];
7
7
  static stripProtoKeys(obj: any): any;
8
8
  static setConfigDefaults(config: any): Promise<FCConfig>;
9
+ /**
10
+ * Turns a sendMode given as a JSON string into an object, in place. Used for the action configuration
11
+ * and for the config file, so both behave the same.
12
+ * Throws INVALID_SEND_MODE when the string is not valid JSON or is not a JSON object.
13
+ */
14
+ static normalizeSendMode(config: any): void;
9
15
  static getDefaultConfig(): {
10
16
  urlContext: string;
11
17
  sendMode: {
@@ -29,19 +29,7 @@ class ConfigDefaults {
29
29
  }
30
30
  // sendMode can arrive as a JSON string (for example from an action configuration field).
31
31
  // Normalise it to an object so it merges with the defaults instead of replacing them.
32
- if (typeof config.sendMode === 'string') {
33
- let parsedSendMode;
34
- try {
35
- parsedSendMode = JSON.parse(config.sendMode);
36
- }
37
- catch (e) {
38
- throw new Error(ConfigDefaults.INVALID_SEND_MODE + config.sendMode);
39
- }
40
- if (parsedSendMode === null || typeof parsedSendMode !== 'object' || Array.isArray(parsedSendMode)) {
41
- throw new Error(ConfigDefaults.INVALID_SEND_MODE + config.sendMode);
42
- }
43
- config.sendMode = ConfigDefaults.stripProtoKeys(parsedSendMode);
44
- }
32
+ ConfigDefaults.normalizeSendMode(config);
45
33
  // List will be comma separated list in UI, so convert to array
46
34
  if (config?.itemPreDevelopmentLifecycleStages && !(config?.itemPreDevelopmentLifecycleStages instanceof Array)) {
47
35
  config.itemPreDevelopmentLifecycleStages = config.itemPreDevelopmentLifecycleStages.split(',');
@@ -52,6 +40,8 @@ class ConfigDefaults {
52
40
  const configArr = [ConfigDefaults.getDefaultConfig()];
53
41
  if (config.configFile) {
54
42
  const fileConfig = await ConfigDefaults.getConfigFile(config.configFile);
43
+ // The config file gets the same sendMode handling as the action configuration.
44
+ ConfigDefaults.normalizeSendMode(fileConfig);
55
45
  configArr.push(fileConfig);
56
46
  }
57
47
  configArr.push(config);
@@ -66,6 +56,27 @@ class ConfigDefaults {
66
56
  console.log('outputConfig: ' + JSON.stringify(outputConfig));
67
57
  return outputConfig;
68
58
  }
59
+ /**
60
+ * Turns a sendMode given as a JSON string into an object, in place. Used for the action configuration
61
+ * and for the config file, so both behave the same.
62
+ * Throws INVALID_SEND_MODE when the string is not valid JSON or is not a JSON object.
63
+ */
64
+ static normalizeSendMode(config) {
65
+ if (!config || typeof config.sendMode !== 'string') {
66
+ return;
67
+ }
68
+ let parsedSendMode;
69
+ try {
70
+ parsedSendMode = JSON.parse(config.sendMode);
71
+ }
72
+ catch (e) {
73
+ throw new Error(ConfigDefaults.INVALID_SEND_MODE + config.sendMode);
74
+ }
75
+ if (parsedSendMode === null || typeof parsedSendMode !== 'object' || Array.isArray(parsedSendMode)) {
76
+ throw new Error(ConfigDefaults.INVALID_SEND_MODE + config.sendMode);
77
+ }
78
+ config.sendMode = ConfigDefaults.stripProtoKeys(parsedSendMode);
79
+ }
69
80
  static getDefaultConfig() {
70
81
  return {
71
82
  urlContext: '/Windchill',
@@ -413,6 +413,53 @@ describe('all tests', () => {
413
413
  expect(Object.keys(config).length).toEqual(0);
414
414
  });
415
415
  });
416
+ describe('sendMode in the config file', () => {
417
+ const config = {
418
+ apiHost: 'http://test.com',
419
+ userName: 'vibeiq',
420
+ password: 'vibeiq',
421
+ configFile: 'id-sendmode'
422
+ };
423
+ beforeEach(() => {
424
+ config_defaults_1.ConfigDefaults.clearConfigCache();
425
+ entityObject = { downloadUrl: 'http://sss.com' };
426
+ });
427
+ afterEach(() => {
428
+ config_defaults_1.ConfigDefaults.clearConfigCache();
429
+ fetchJson = {};
430
+ });
431
+ it('a JSON string sendMode in the config file is parsed and merged with the defaults', async () => {
432
+ fetchJson = { sendMode: '{"ASYNC_PUBLISH_SEASON": "separateVibeIQEvents", "OTHER": "x"}' };
433
+ const fcConfig = await config_defaults_1.ConfigDefaults.setConfigDefaults(Object.assign({}, config));
434
+ expect(fcConfig.sendMode.ASYNC_PUBLISH_SEASON).toEqual('separateVibeIQEvents');
435
+ expect(fcConfig.sendMode.OTHER).toEqual('x');
436
+ });
437
+ it('an object sendMode in the config file still works', async () => {
438
+ fetchJson = { sendMode: { ASYNC_PUBLISH_SEASON: 'vibeiqfile-dontsendtoflexplm' } };
439
+ const fcConfig = await config_defaults_1.ConfigDefaults.setConfigDefaults(Object.assign({}, config));
440
+ expect(fcConfig.sendMode.ASYNC_PUBLISH_SEASON).toEqual('vibeiqfile-dontsendtoflexplm');
441
+ });
442
+ it('a sendMode in the action configuration overrides the one in the config file', async () => {
443
+ fetchJson = { sendMode: '{"ASYNC_PUBLISH_SEASON": "vibeiqfile-dontsendtoflexplm"}' };
444
+ const startConfig = Object.assign({}, config, { sendMode: { ASYNC_PUBLISH_SEASON: 'separateVibeIQEvents' } });
445
+ const fcConfig = await config_defaults_1.ConfigDefaults.setConfigDefaults(startConfig);
446
+ expect(fcConfig.sendMode.ASYNC_PUBLISH_SEASON).toEqual('separateVibeIQEvents');
447
+ });
448
+ it('invalid JSON in the config file sendMode throws', async () => {
449
+ fetchJson = { sendMode: '{not json' };
450
+ await expect(config_defaults_1.ConfigDefaults.setConfigDefaults(Object.assign({}, config))).rejects.toThrow(config_defaults_1.ConfigDefaults.INVALID_SEND_MODE);
451
+ });
452
+ it.each([['"separateVibeIQEvents"'], ['["separateVibeIQEvents"]'], ['null']])('a config file sendMode that is the JSON string %s (not an object) throws', async (value) => {
453
+ fetchJson = { sendMode: value };
454
+ await expect(config_defaults_1.ConfigDefaults.setConfigDefaults(Object.assign({}, config))).rejects.toThrow(config_defaults_1.ConfigDefaults.INVALID_SEND_MODE);
455
+ });
456
+ it('removes prototype keys from a parsed config file sendMode', async () => {
457
+ fetchJson = { sendMode: '{"__proto__": {"polluted": true}, "ASYNC_PUBLISH_SEASON": "separateVibeIQEvents"}' };
458
+ const fcConfig = await config_defaults_1.ConfigDefaults.setConfigDefaults(Object.assign({}, config));
459
+ expect(fcConfig.sendMode.ASYNC_PUBLISH_SEASON).toEqual('separateVibeIQEvents');
460
+ expect({}.polluted).toBeUndefined();
461
+ });
462
+ });
416
463
  describe('prototype pollution', () => {
417
464
  const config = {
418
465
  apiHost: 'http://test.com',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/flexplm",
3
- "version": "3.1.0-alpha.6c6914d",
3
+ "version": "3.1.0-alpha.9a5a5c3",
4
4
  "description": "Library used for integration with flexplm.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",