@contrail/flexplm 1.3.0-alpha.0 → 1.3.0-alpha.3

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.
Files changed (123) hide show
  1. package/.github/pull_request_template.md +31 -31
  2. package/.github/workflows/flexplm-lib.yml +27 -27
  3. package/CHANGELOG.md +1 -1
  4. package/lib/entity-processor/base-entity-processor.d.ts +42 -0
  5. package/lib/entity-processor/base-entity-processor.js +377 -0
  6. package/lib/entity-processor/base-entity-processor.spec.d.ts +1 -0
  7. package/lib/entity-processor/base-entity-processor.spec.js +426 -0
  8. package/lib/flexplm-request.d.ts +3 -0
  9. package/lib/flexplm-request.js +34 -0
  10. package/lib/flexplm-utils.d.ts +5 -0
  11. package/lib/flexplm-utils.js +33 -0
  12. package/lib/flexplm-utils.spec.d.ts +1 -0
  13. package/lib/flexplm-utils.spec.js +26 -0
  14. package/lib/index.d.ts +22 -0
  15. package/lib/index.js +38 -0
  16. package/lib/interfaces/interfaces.d.ts +105 -0
  17. package/lib/interfaces/interfaces.js +2 -0
  18. package/lib/interfaces/item-family-changes.d.ts +20 -0
  19. package/lib/interfaces/item-family-changes.js +56 -0
  20. package/lib/interfaces/publish-change-data.d.ts +19 -0
  21. package/lib/interfaces/publish-change-data.js +32 -0
  22. package/lib/publish/base-process-publish-assortment-callback.d.ts +9 -0
  23. package/lib/publish/base-process-publish-assortment-callback.js +38 -0
  24. package/lib/publish/base-process-publish-assortment.d.ts +93 -0
  25. package/lib/publish/base-process-publish-assortment.js +944 -0
  26. package/lib/publish/base-process-publish-assortment.spec.d.ts +1 -0
  27. package/lib/publish/base-process-publish-assortment.spec.js +1670 -0
  28. package/lib/publish/mockData.d.ts +1389 -0
  29. package/lib/publish/mockData.js +4519 -0
  30. package/lib/transform/identifier-conversion-spec-mockData.d.ts +0 -0
  31. package/lib/transform/identifier-conversion-spec-mockData.js +444 -0
  32. package/lib/transform/identifier-conversion.d.ts +15 -0
  33. package/lib/transform/identifier-conversion.js +212 -0
  34. package/lib/transform/identifier-conversion.spec.d.ts +1 -0
  35. package/lib/transform/identifier-conversion.spec.js +339 -0
  36. package/lib/util/config-defaults.d.ts +8 -0
  37. package/lib/util/config-defaults.js +85 -0
  38. package/lib/util/config-defaults.spec.d.ts +1 -0
  39. package/lib/util/config-defaults.spec.js +293 -0
  40. package/lib/util/data-converter-spec-mockData.d.ts +0 -0
  41. package/lib/util/data-converter-spec-mockData.js +205 -0
  42. package/lib/util/data-converter.d.ts +39 -0
  43. package/lib/util/data-converter.js +592 -0
  44. package/lib/util/data-converter.spec.d.ts +1 -0
  45. package/lib/util/data-converter.spec.js +904 -0
  46. package/lib/util/error-response-object.d.ts +4 -0
  47. package/lib/util/error-response-object.js +47 -0
  48. package/lib/util/error-response-object.spec.d.ts +1 -0
  49. package/lib/util/error-response-object.spec.js +99 -0
  50. package/lib/util/event-short-message-status.d.ts +18 -0
  51. package/lib/util/event-short-message-status.js +22 -0
  52. package/lib/util/federation.d.ts +15 -0
  53. package/lib/util/federation.js +149 -0
  54. package/lib/util/flexplm-connect.d.ts +22 -0
  55. package/lib/util/flexplm-connect.js +176 -0
  56. package/lib/util/flexplm-connect.spec.d.ts +1 -0
  57. package/lib/util/flexplm-connect.spec.js +88 -0
  58. package/lib/util/logger-config.d.ts +1 -0
  59. package/lib/util/logger-config.js +26 -0
  60. package/lib/util/map-util-spec-mockData.d.ts +0 -0
  61. package/lib/util/map-util-spec-mockData.js +205 -0
  62. package/lib/util/map-utils.d.ts +6 -0
  63. package/lib/util/map-utils.js +15 -0
  64. package/lib/util/map-utils.spec.d.ts +1 -0
  65. package/lib/util/map-utils.spec.js +89 -0
  66. package/lib/util/mockData.d.ts +79 -0
  67. package/lib/util/mockData.js +99 -0
  68. package/lib/util/thumbnail-util.d.ts +28 -0
  69. package/lib/util/thumbnail-util.js +202 -0
  70. package/lib/util/thumbnail-util.spec.d.ts +1 -0
  71. package/lib/util/thumbnail-util.spec.js +398 -0
  72. package/lib/util/type-conversion-utils-spec-mockData.d.ts +0 -0
  73. package/lib/util/type-conversion-utils-spec-mockData.js +259 -0
  74. package/lib/util/type-conversion-utils.d.ts +23 -0
  75. package/lib/util/type-conversion-utils.js +266 -0
  76. package/lib/util/type-conversion-utils.spec.d.ts +1 -0
  77. package/lib/util/type-conversion-utils.spec.js +868 -0
  78. package/lib/util/type-defaults.d.ts +16 -0
  79. package/lib/util/type-defaults.js +221 -0
  80. package/lib/util/type-defaults.spec.d.ts +1 -0
  81. package/lib/util/type-defaults.spec.js +516 -0
  82. package/lib/util/type-utils.d.ts +13 -0
  83. package/lib/util/type-utils.js +114 -0
  84. package/lib/util/type-utils.spec.d.ts +1 -0
  85. package/lib/util/type-utils.spec.js +190 -0
  86. package/package.json +1 -1
  87. package/publish.bat +4 -4
  88. package/publish.sh +4 -4
  89. package/src/entity-processor/base-entity-processor.spec.ts +157 -0
  90. package/src/entity-processor/base-entity-processor.ts +21 -2
  91. package/src/flexplm-request.ts +28 -28
  92. package/src/flexplm-utils.spec.ts +27 -27
  93. package/src/flexplm-utils.ts +29 -29
  94. package/src/index.ts +21 -21
  95. package/src/interfaces/item-family-changes.ts +66 -66
  96. package/src/interfaces/publish-change-data.ts +42 -42
  97. package/src/publish/base-process-publish-assortment-callback.ts +50 -50
  98. package/src/transform/identifier-conversion-spec-mockData.ts +495 -495
  99. package/src/transform/identifier-conversion.spec.ts +353 -353
  100. package/src/transform/identifier-conversion.ts +281 -281
  101. package/src/util/config-defaults.spec.ts +350 -350
  102. package/src/util/config-defaults.ts +92 -92
  103. package/src/util/data-converter-spec-mockData.ts +230 -230
  104. package/src/util/error-response-object.spec.ts +115 -115
  105. package/src/util/error-response-object.ts +49 -49
  106. package/src/util/federation.ts +172 -172
  107. package/src/util/logger-config.ts +19 -19
  108. package/src/util/map-util-spec-mockData.ts +230 -230
  109. package/src/util/map-utils.spec.ts +102 -102
  110. package/src/util/map-utils.ts +40 -40
  111. package/src/util/mockData.ts +97 -97
  112. package/src/util/thumbnail-util.spec.ts +190 -0
  113. package/src/util/thumbnail-util.ts +109 -1
  114. package/src/util/type-conversion-utils.spec.ts +25 -25
  115. package/src/util/type-conversion-utils.ts +10 -9
  116. package/src/util/type-defaults.spec.ts +668 -668
  117. package/src/util/type-defaults.ts +280 -280
  118. package/src/util/type-utils.spec.ts +227 -227
  119. package/src/util/type-utils.ts +144 -144
  120. package/tsconfig.json +28 -26
  121. package/tslint.json +57 -57
  122. package/scripts/output.png +0 -0
  123. package/scripts/test-get-request.ts +0 -35
@@ -1,116 +1,116 @@
1
- import { AppActionCallbackStatus } from "@contrail/app-framework";
2
- import { ErrorResponseObject } from "./error-response-object";
3
-
4
- describe('getResponse() Tests', () =>{
5
- it('no error provided', () => {
6
- const e = undefined;
7
-
8
- const response = ErrorResponseObject.getResponse(e);
9
- expect(response.status).toMatch(AppActionCallbackStatus.FAILURE);
10
- expect(response?.output?.message).toMatch('No error provided');
11
- });
12
- it('error with message', () => {
13
- const e = {
14
- message: 'Test'
15
- };
16
-
17
- const response = ErrorResponseObject.getResponse(e);
18
- expect(response.status).toMatch(AppActionCallbackStatus.FAILURE);
19
- expect(response?.output?.message).toMatch('Test');
20
- });
21
-
22
- it('error with details', () => {
23
- const details = {
24
- message: 'Detail Message',
25
- itemType: [
26
- {
27
- message: "Type 'item:product:newBalance:apparel' cannot be assigned to a property with type 'item'."
28
- }]
29
- };
30
- const e = {
31
- message: 'Test',
32
- details
33
-
34
- };
35
-
36
- const response = ErrorResponseObject.getResponse(e);
37
- expect(response).toBeTruthy();
38
- expect(response.status).toMatch(AppActionCallbackStatus.FAILURE);
39
- expect(response.output.message).toMatch('Test');
40
-
41
- const errorDetails = response.output.errorDetails;
42
- expect(errorDetails).toBeTruthy();
43
- expect(errorDetails.message).toMatch('Detail Message');
44
- expect(errorDetails).toHaveProperty('itemType');
45
- });
46
-
47
- it('error with cause', () => {
48
- const cause = {
49
- code: 401,
50
- message: 'cause message'
51
- };
52
- const e = {
53
- message: 'Test',
54
- cause
55
- };
56
-
57
- const response = ErrorResponseObject.getResponse(e);
58
- expect(response.status).toMatch(AppActionCallbackStatus.FAILURE);
59
- expect(response?.output?.message).toMatch('Test');
60
- const resCause = response.output.cause;
61
-
62
- expect(resCause).toBeTruthy();
63
- expect(resCause.code).toBe(401);
64
- expect(resCause.message).toMatch('cause message');
65
-
66
- });
67
-
68
- it('error with code', () => {
69
- const e = {
70
- message: 'Test',
71
- code: 404
72
- };
73
-
74
- const response = ErrorResponseObject.getResponse(e);
75
- expect(response.status).toMatch(AppActionCallbackStatus.FAILURE);
76
- expect(response?.output?.message).toMatch('Test');
77
- expect(response.output.code).toBe(404);
78
- });
79
-
80
- it('error with code', () => {
81
- const e = {
82
- message: 'Test',
83
- name: 'Error'
84
- };
85
-
86
- const response = ErrorResponseObject.getResponse(e);
87
- expect(response.status).toMatch(AppActionCallbackStatus.FAILURE);
88
- expect(response?.output?.message).toMatch('Test');
89
- expect(response.output.errorName).toMatch('Error');
90
- });
91
-
92
- it('error with errno', () => {
93
- const e = {
94
- message: 'Test',
95
- errno: 500
96
- };
97
-
98
- const response = ErrorResponseObject.getResponse(e);
99
- expect(response.status).toMatch(AppActionCallbackStatus.FAILURE);
100
- expect(response?.output?.message).toMatch('Test');
101
- expect(response.output.errno).toBe(500);
102
- });
103
-
104
- it('error with type', () => {
105
- const e = {
106
- message: 'Test',
107
- type: 'system'
108
- };
109
-
110
- const response = ErrorResponseObject.getResponse(e);
111
- expect(response.status).toMatch(AppActionCallbackStatus.FAILURE);
112
- expect(response?.output?.message).toMatch('Test');
113
- expect(response.output.type).toMatch('system');
114
- });
115
-
1
+ import { AppActionCallbackStatus } from "@contrail/app-framework";
2
+ import { ErrorResponseObject } from "./error-response-object";
3
+
4
+ describe('getResponse() Tests', () =>{
5
+ it('no error provided', () => {
6
+ const e = undefined;
7
+
8
+ const response = ErrorResponseObject.getResponse(e);
9
+ expect(response.status).toMatch(AppActionCallbackStatus.FAILURE);
10
+ expect(response?.output?.message).toMatch('No error provided');
11
+ });
12
+ it('error with message', () => {
13
+ const e = {
14
+ message: 'Test'
15
+ };
16
+
17
+ const response = ErrorResponseObject.getResponse(e);
18
+ expect(response.status).toMatch(AppActionCallbackStatus.FAILURE);
19
+ expect(response?.output?.message).toMatch('Test');
20
+ });
21
+
22
+ it('error with details', () => {
23
+ const details = {
24
+ message: 'Detail Message',
25
+ itemType: [
26
+ {
27
+ message: "Type 'item:product:newBalance:apparel' cannot be assigned to a property with type 'item'."
28
+ }]
29
+ };
30
+ const e = {
31
+ message: 'Test',
32
+ details
33
+
34
+ };
35
+
36
+ const response = ErrorResponseObject.getResponse(e);
37
+ expect(response).toBeTruthy();
38
+ expect(response.status).toMatch(AppActionCallbackStatus.FAILURE);
39
+ expect(response.output.message).toMatch('Test');
40
+
41
+ const errorDetails = response.output.errorDetails;
42
+ expect(errorDetails).toBeTruthy();
43
+ expect(errorDetails.message).toMatch('Detail Message');
44
+ expect(errorDetails).toHaveProperty('itemType');
45
+ });
46
+
47
+ it('error with cause', () => {
48
+ const cause = {
49
+ code: 401,
50
+ message: 'cause message'
51
+ };
52
+ const e = {
53
+ message: 'Test',
54
+ cause
55
+ };
56
+
57
+ const response = ErrorResponseObject.getResponse(e);
58
+ expect(response.status).toMatch(AppActionCallbackStatus.FAILURE);
59
+ expect(response?.output?.message).toMatch('Test');
60
+ const resCause = response.output.cause;
61
+
62
+ expect(resCause).toBeTruthy();
63
+ expect(resCause.code).toBe(401);
64
+ expect(resCause.message).toMatch('cause message');
65
+
66
+ });
67
+
68
+ it('error with code', () => {
69
+ const e = {
70
+ message: 'Test',
71
+ code: 404
72
+ };
73
+
74
+ const response = ErrorResponseObject.getResponse(e);
75
+ expect(response.status).toMatch(AppActionCallbackStatus.FAILURE);
76
+ expect(response?.output?.message).toMatch('Test');
77
+ expect(response.output.code).toBe(404);
78
+ });
79
+
80
+ it('error with code', () => {
81
+ const e = {
82
+ message: 'Test',
83
+ name: 'Error'
84
+ };
85
+
86
+ const response = ErrorResponseObject.getResponse(e);
87
+ expect(response.status).toMatch(AppActionCallbackStatus.FAILURE);
88
+ expect(response?.output?.message).toMatch('Test');
89
+ expect(response.output.errorName).toMatch('Error');
90
+ });
91
+
92
+ it('error with errno', () => {
93
+ const e = {
94
+ message: 'Test',
95
+ errno: 500
96
+ };
97
+
98
+ const response = ErrorResponseObject.getResponse(e);
99
+ expect(response.status).toMatch(AppActionCallbackStatus.FAILURE);
100
+ expect(response?.output?.message).toMatch('Test');
101
+ expect(response.output.errno).toBe(500);
102
+ });
103
+
104
+ it('error with type', () => {
105
+ const e = {
106
+ message: 'Test',
107
+ type: 'system'
108
+ };
109
+
110
+ const response = ErrorResponseObject.getResponse(e);
111
+ expect(response.status).toMatch(AppActionCallbackStatus.FAILURE);
112
+ expect(response?.output?.message).toMatch('Test');
113
+ expect(response.output.type).toMatch('system');
114
+ });
115
+
116
116
  });
@@ -1,50 +1,50 @@
1
- import { AppActionCallBack, AppActionCallbackStatus } from "@contrail/app-framework";
2
-
3
- export class ErrorResponseObject {
4
-
5
- /** Returns a response, checking for multiple possible error details
6
- *
7
- * @param e
8
- * @returns
9
- */
10
- public static getResponse(e): AppActionCallBack {
11
- if(!e){
12
- const response: AppActionCallBack = {
13
- status: AppActionCallbackStatus.FAILURE,
14
- output: {
15
- message: 'No error provided.'
16
- }
17
- };
18
- return response;
19
- }
20
-
21
- const response: AppActionCallBack = {
22
- status: AppActionCallbackStatus.FAILURE,
23
- output: {
24
- message: e.message
25
- }
26
- };
27
- const output = response.output;
28
-
29
- //Errors when persisting in VibeIQ
30
- if(e.details) output.errorDetails = e.details;
31
-
32
- if(e.code) output.code = e.code;
33
- if(e.errno) output.errno = e.errno;
34
- if(e.type) output.type = e.type;
35
- if(e.name) output.errorName = e.name;
36
-
37
- //Has original cause
38
- if(e.cause){
39
- output.cause = {};
40
- const cause = output.cause;
41
- if(e.cause.code) cause.code = e.cause.code;
42
- if(e.cause.message) cause.message = e.cause.message;
43
- }
44
-
45
- if(e.stack){
46
- console.log('ErrorResponseObject.getResponse stack: ' + e.stack);
47
- }
48
- return response;
49
- }
1
+ import { AppActionCallBack, AppActionCallbackStatus } from "@contrail/app-framework";
2
+
3
+ export class ErrorResponseObject {
4
+
5
+ /** Returns a response, checking for multiple possible error details
6
+ *
7
+ * @param e
8
+ * @returns
9
+ */
10
+ public static getResponse(e): AppActionCallBack {
11
+ if(!e){
12
+ const response: AppActionCallBack = {
13
+ status: AppActionCallbackStatus.FAILURE,
14
+ output: {
15
+ message: 'No error provided.'
16
+ }
17
+ };
18
+ return response;
19
+ }
20
+
21
+ const response: AppActionCallBack = {
22
+ status: AppActionCallbackStatus.FAILURE,
23
+ output: {
24
+ message: e.message
25
+ }
26
+ };
27
+ const output = response.output;
28
+
29
+ //Errors when persisting in VibeIQ
30
+ if(e.details) output.errorDetails = e.details;
31
+
32
+ if(e.code) output.code = e.code;
33
+ if(e.errno) output.errno = e.errno;
34
+ if(e.type) output.type = e.type;
35
+ if(e.name) output.errorName = e.name;
36
+
37
+ //Has original cause
38
+ if(e.cause){
39
+ output.cause = {};
40
+ const cause = output.cause;
41
+ if(e.cause.code) cause.code = e.cause.code;
42
+ if(e.cause.message) cause.message = e.cause.message;
43
+ }
44
+
45
+ if(e.stack){
46
+ console.log('ErrorResponseObject.getResponse stack: ' + e.stack);
47
+ }
48
+ return response;
49
+ }
50
50
  }
@@ -1,172 +1,172 @@
1
- import { Entities } from '@contrail/sdk';
2
-
3
- import { FederationRecord } from '../interfaces/interfaces';
4
- import pLimit from 'p-limit';
5
- const limit = pLimit(30);
6
-
7
- interface fedConfigDef {
8
- appIdentifier: string,
9
- federationSchema: string
10
- }
11
- const FED_CONFIG: fedConfigDef = {
12
- appIdentifier: '@vibeiq/flexplm-connector',
13
- federationSchema: 'DEFAULT'
14
- };
15
- export class Federation {
16
-
17
- private CHUNK_SIZE = 50;
18
-
19
- async getFederatedMappedRefId(entityType: string, entityId: string) {
20
- const criteria = {
21
- reference: entityType + ':' + entityId,
22
- appIdentifier: FED_CONFIG.appIdentifier,
23
- federationSchema: FED_CONFIG.federationSchema
24
- };
25
- console.log('getFederatedMappedRefId: ' + JSON.stringify(criteria));
26
- const fedRecords: FederationRecord[] = await new Entities().get({
27
- entityName: 'federation',
28
- criteria
29
- });
30
-
31
- const federatedId = (fedRecords[0])
32
- ? fedRecords[0]['mappedReference']
33
- : '';
34
-
35
- return federatedId;
36
- }
37
-
38
- async createFederatedRecord(eventBody) {
39
- const itemResults = eventBody.payload;
40
- for (let i = 0; i < itemResults.length; i++) {
41
- if (!itemResults[i].federatedId) {
42
- continue;
43
- }
44
- const payload: FederationRecord = {
45
- appIdentifier: FED_CONFIG.appIdentifier,
46
- reference: itemResults[i].entityReference,
47
- mappedReference: itemResults[i].federatedId,
48
- federationSchema: FED_CONFIG.federationSchema
49
- };
50
- // console.log('createFederatedRecord: ' + JSON.stringify(payload));
51
-
52
- try {
53
- const results = await new Entities().create({ entityName: 'federation', object: payload });
54
- return results;
55
- // console.log(JSON.stringify(results));
56
- } catch (error) {
57
- console.log('createFederatedRecord-error: ', error);
58
- throw new Error('Error creating federation record: ' + error.message);
59
- }
60
- }
61
- }
62
-
63
- async getFederationRecordFromMappedRefId(mappedRefId: string) {
64
- const criteria = {
65
- appIdentifier: FED_CONFIG.appIdentifier,
66
- mappedReference: mappedRefId,
67
- federationSchema: FED_CONFIG.federationSchema
68
- };
69
- try {
70
- const fedRecords: FederationRecord[] = await new Entities().get({
71
- entityName: 'federation',
72
- criteria
73
- });
74
- return (fedRecords && fedRecords[0]) ? fedRecords[0] : undefined;
75
- } catch (e) {
76
- console.log('getFederationData-error: ' + e.message);
77
- }
78
- return undefined;
79
- }
80
-
81
- static getEntityId(fedRecord: FederationRecord) {
82
- const entityString = fedRecord['reference'];
83
- const entitySplit = entityString.split(':');
84
- const entityType = entitySplit[0];
85
- const entityId = entitySplit[1];
86
-
87
- return { entityType, entityId };
88
-
89
- }
90
-
91
- async getEntityFromMappedRefId(mappedRefId: string) {
92
- // console.log('!---getEntityFromMappedRefId: ' + mappedRefId);
93
- const fedRecord = await this.getFederationRecordFromMappedRefId(mappedRefId);
94
- console.log('fedRecord: ' + JSON.stringify(fedRecord));
95
- if (!fedRecord) {
96
- //Not creating from FlexPLM at this time.
97
- console.log('Federation Record doesnt exist. Cant get entity!');
98
- return;
99
- }
100
-
101
- const { entityType, entityId } = Federation.getEntityId(fedRecord);
102
- // console.log(entityType + ':' + entityId);
103
- const criteria = {
104
- id: entityId
105
- };
106
- const entities = await new Entities().get({
107
- entityName: entityType,
108
- criteria
109
- });
110
- const entity = (entities && entities[0]) ? entities[0] : undefined;
111
- // console.log(' entities: ' +JSON.stringify(entities));
112
- return entity;
113
- }
114
-
115
- async getFederationRecordsFromIds(ids: string[]): Promise<FederationRecord[]> {
116
- const fedRecords = [];
117
- for (let i = 0; i < ids.length; i++) {
118
- const criteria = {
119
- reference: ids[i],
120
- appIdentifier: FED_CONFIG.appIdentifier,
121
- federationSchema: FED_CONFIG.federationSchema
122
- };
123
- // this.logger.log('getFederatedMappedRefId: ' + JSON.stringify(criteria));
124
- const recs: FederationRecord[] = await new Entities().get({
125
- entityName: 'federation',
126
- criteria
127
- });
128
- if (recs[0]) {
129
- fedRecords.push(recs[0]);
130
- }
131
- }
132
-
133
- return fedRecords;
134
- }
135
-
136
- async getFederationRecordsFromIdsBulk(ids: string[]): Promise<FederationRecord[]> {
137
-
138
- const chunks: string[][] = this.splitIntoChunksByLen(ids, this.CHUNK_SIZE);
139
- const fedRecords = [];
140
- const federatedPromises = [];
141
- const entities: Entities = new Entities();
142
- for (const chunk of chunks) {
143
- const fedPromise = limit(async () => {
144
- const criteria = {
145
- references: chunk,
146
- appIdentifier: FED_CONFIG.appIdentifier,
147
- federationSchema: FED_CONFIG.federationSchema
148
- };
149
- // this.logger.log('getFederatedMappedRefId: ' + JSON.stringify(criteria));
150
- const records: FederationRecord[] = await entities.get({
151
- entityName: 'federation',
152
- criteria
153
- });
154
- fedRecords.push(...records);
155
-
156
- });
157
- federatedPromises.push(fedPromise);
158
- }
159
- await Promise.all(federatedPromises);
160
-
161
- return fedRecords;
162
- }
163
-
164
- splitIntoChunksByLen(arr, len) {
165
- const chunks = [], n = arr?.length;
166
- let i = 0;
167
- while (i < n) {
168
- chunks.push(arr.slice(i, i += len));
169
- }
170
- return chunks;
171
- }
172
- }
1
+ import { Entities } from '@contrail/sdk';
2
+
3
+ import { FederationRecord } from '../interfaces/interfaces';
4
+ import pLimit from 'p-limit';
5
+ const limit = pLimit(30);
6
+
7
+ interface fedConfigDef {
8
+ appIdentifier: string,
9
+ federationSchema: string
10
+ }
11
+ const FED_CONFIG: fedConfigDef = {
12
+ appIdentifier: '@vibeiq/flexplm-connector',
13
+ federationSchema: 'DEFAULT'
14
+ };
15
+ export class Federation {
16
+
17
+ private CHUNK_SIZE = 50;
18
+
19
+ async getFederatedMappedRefId(entityType: string, entityId: string) {
20
+ const criteria = {
21
+ reference: entityType + ':' + entityId,
22
+ appIdentifier: FED_CONFIG.appIdentifier,
23
+ federationSchema: FED_CONFIG.federationSchema
24
+ };
25
+ console.log('getFederatedMappedRefId: ' + JSON.stringify(criteria));
26
+ const fedRecords: FederationRecord[] = await new Entities().get({
27
+ entityName: 'federation',
28
+ criteria
29
+ });
30
+
31
+ const federatedId = (fedRecords[0])
32
+ ? fedRecords[0]['mappedReference']
33
+ : '';
34
+
35
+ return federatedId;
36
+ }
37
+
38
+ async createFederatedRecord(eventBody) {
39
+ const itemResults = eventBody.payload;
40
+ for (let i = 0; i < itemResults.length; i++) {
41
+ if (!itemResults[i].federatedId) {
42
+ continue;
43
+ }
44
+ const payload: FederationRecord = {
45
+ appIdentifier: FED_CONFIG.appIdentifier,
46
+ reference: itemResults[i].entityReference,
47
+ mappedReference: itemResults[i].federatedId,
48
+ federationSchema: FED_CONFIG.federationSchema
49
+ };
50
+ // console.log('createFederatedRecord: ' + JSON.stringify(payload));
51
+
52
+ try {
53
+ const results = await new Entities().create({ entityName: 'federation', object: payload });
54
+ return results;
55
+ // console.log(JSON.stringify(results));
56
+ } catch (error) {
57
+ console.log('createFederatedRecord-error: ', error);
58
+ throw new Error('Error creating federation record: ' + error.message);
59
+ }
60
+ }
61
+ }
62
+
63
+ async getFederationRecordFromMappedRefId(mappedRefId: string) {
64
+ const criteria = {
65
+ appIdentifier: FED_CONFIG.appIdentifier,
66
+ mappedReference: mappedRefId,
67
+ federationSchema: FED_CONFIG.federationSchema
68
+ };
69
+ try {
70
+ const fedRecords: FederationRecord[] = await new Entities().get({
71
+ entityName: 'federation',
72
+ criteria
73
+ });
74
+ return (fedRecords && fedRecords[0]) ? fedRecords[0] : undefined;
75
+ } catch (e) {
76
+ console.log('getFederationData-error: ' + e.message);
77
+ }
78
+ return undefined;
79
+ }
80
+
81
+ static getEntityId(fedRecord: FederationRecord) {
82
+ const entityString = fedRecord['reference'];
83
+ const entitySplit = entityString.split(':');
84
+ const entityType = entitySplit[0];
85
+ const entityId = entitySplit[1];
86
+
87
+ return { entityType, entityId };
88
+
89
+ }
90
+
91
+ async getEntityFromMappedRefId(mappedRefId: string) {
92
+ // console.log('!---getEntityFromMappedRefId: ' + mappedRefId);
93
+ const fedRecord = await this.getFederationRecordFromMappedRefId(mappedRefId);
94
+ console.log('fedRecord: ' + JSON.stringify(fedRecord));
95
+ if (!fedRecord) {
96
+ //Not creating from FlexPLM at this time.
97
+ console.log('Federation Record doesnt exist. Cant get entity!');
98
+ return;
99
+ }
100
+
101
+ const { entityType, entityId } = Federation.getEntityId(fedRecord);
102
+ // console.log(entityType + ':' + entityId);
103
+ const criteria = {
104
+ id: entityId
105
+ };
106
+ const entities = await new Entities().get({
107
+ entityName: entityType,
108
+ criteria
109
+ });
110
+ const entity = (entities && entities[0]) ? entities[0] : undefined;
111
+ // console.log(' entities: ' +JSON.stringify(entities));
112
+ return entity;
113
+ }
114
+
115
+ async getFederationRecordsFromIds(ids: string[]): Promise<FederationRecord[]> {
116
+ const fedRecords = [];
117
+ for (let i = 0; i < ids.length; i++) {
118
+ const criteria = {
119
+ reference: ids[i],
120
+ appIdentifier: FED_CONFIG.appIdentifier,
121
+ federationSchema: FED_CONFIG.federationSchema
122
+ };
123
+ // this.logger.log('getFederatedMappedRefId: ' + JSON.stringify(criteria));
124
+ const recs: FederationRecord[] = await new Entities().get({
125
+ entityName: 'federation',
126
+ criteria
127
+ });
128
+ if (recs[0]) {
129
+ fedRecords.push(recs[0]);
130
+ }
131
+ }
132
+
133
+ return fedRecords;
134
+ }
135
+
136
+ async getFederationRecordsFromIdsBulk(ids: string[]): Promise<FederationRecord[]> {
137
+
138
+ const chunks: string[][] = this.splitIntoChunksByLen(ids, this.CHUNK_SIZE);
139
+ const fedRecords = [];
140
+ const federatedPromises = [];
141
+ const entities: Entities = new Entities();
142
+ for (const chunk of chunks) {
143
+ const fedPromise = limit(async () => {
144
+ const criteria = {
145
+ references: chunk,
146
+ appIdentifier: FED_CONFIG.appIdentifier,
147
+ federationSchema: FED_CONFIG.federationSchema
148
+ };
149
+ // this.logger.log('getFederatedMappedRefId: ' + JSON.stringify(criteria));
150
+ const records: FederationRecord[] = await entities.get({
151
+ entityName: 'federation',
152
+ criteria
153
+ });
154
+ fedRecords.push(...records);
155
+
156
+ });
157
+ federatedPromises.push(fedPromise);
158
+ }
159
+ await Promise.all(federatedPromises);
160
+
161
+ return fedRecords;
162
+ }
163
+
164
+ splitIntoChunksByLen(arr, len) {
165
+ const chunks = [], n = arr?.length;
166
+ let i = 0;
167
+ while (i < n) {
168
+ chunks.push(arr.slice(i, i += len));
169
+ }
170
+ return chunks;
171
+ }
172
+ }