@contrail/flexplm 1.1.63 → 1.1.65

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.
@@ -758,6 +758,51 @@ describe('checkKeysAndValues', () =>{
758
758
  });
759
759
  });
760
760
 
761
+ describe('filterOutArchivedAndTrashedEntities', () =>{
762
+ const config: FCConfig = {
763
+ apiHost: 'host',
764
+ userName: () => 'user',
765
+ password: () => 'pass',
766
+ urlContext: 'xxx',
767
+ vibeEventEndpoint: '/rfa/vibeiq/vibeEvents',
768
+ csrfEndpoint: '/servlet/rest/security/csrf',
769
+ itemPreDevelopmentLifecycleStages: ['concept']
770
+ };
771
+
772
+ const mapFileUtil = new MapFileUtil(new Entities());
773
+ const dc = new DataConverter(config, mapFileUtil);
774
+
775
+ it('filter out isArchived and isTrashed entities', () =>{
776
+ const entities = [
777
+ {id: '1', name: 'Entity 1', isArchived: false, isTrashed: false},
778
+ {id: '2', name: 'Entity 2', isArchived: true, isTrashed: false},
779
+ {id: '3', name: 'Entity 3', isArchived: false, isTrashed: true},
780
+ {id: '4', name: 'Entity 4', isArchived: false, isTrashed: false}
781
+ ];
782
+
783
+ const filteredEntities = dc.filterOutArchivedAndTrashedEntities(entities);
784
+ expect(filteredEntities.length).toBe(2);
785
+ expect(filteredEntities).toEqual([
786
+ {id: '1', name: 'Entity 1', isArchived: false, isTrashed: false},
787
+ {id: '4', name: 'Entity 4', isArchived: false, isTrashed: false}
788
+ ]);
789
+ });
790
+ it('no entities to filter', () =>{
791
+ const entities = [];
792
+ const filteredEntities = dc.filterOutArchivedAndTrashedEntities(entities);
793
+ expect(filteredEntities.length).toBe(0);
794
+ });
795
+ it('all entities are archived or trashed', () =>{
796
+ const entities = [
797
+ {id: '1', name: 'Entity 1', isArchived: true, isTrashed: false},
798
+ {id: '2', name: 'Entity 2', isArchived: false, isTrashed: true}
799
+ ];
800
+
801
+ const filteredEntities = dc.filterOutArchivedAndTrashedEntities(entities);
802
+ expect(filteredEntities.length).toBe(0);
803
+ });
804
+ });
805
+
761
806
  describe('setUserListValue', () =>{
762
807
  const config: FCConfig = {
763
808
  apiHost: 'host',
@@ -512,7 +512,7 @@ export class DataConverter {
512
512
  nextPageKey = loadPage?.nextPageKey;
513
513
  if (Object.keys(loadPage).includes('results')) {
514
514
  usedV2 = true;
515
- let postProcessedResults = loadPage.results;
515
+ let postProcessedResults = this.filterOutArchivedAndTrashedEntities(loadPage?.results);
516
516
  if(postProcessedResults?.length > 0 && postProcessCriteria && Object.keys(postProcessCriteria).length !== 0) {
517
517
  const subEntityTypePath = rootTypeCriteria.typePath || entityType;
518
518
  postProcessedResults = this.checkKeysAndValues(postProcessCriteria, postProcessedResults, subEntityTypePath);
@@ -534,7 +534,7 @@ export class DataConverter {
534
534
  take,
535
535
  skip,
536
536
  });
537
- let postProcessedResults = loadPage;
537
+ let postProcessedResults = this.filterOutArchivedAndTrashedEntities(loadPage);
538
538
  if(postProcessCriteria && Object.keys(postProcessCriteria).length !== 0) {
539
539
  const subEntityTypePath = rootTypeCriteria.typePath || entityType;
540
540
  postProcessedResults = this.checkKeysAndValues(postProcessCriteria, postProcessedResults, subEntityTypePath);
@@ -587,6 +587,23 @@ export class DataConverter {
587
587
  return arrOfMatchObjects;
588
588
  }
589
589
 
590
+ /** Filters out archived and trashed entities from the provided array of entities.
591
+ *
592
+ * @param entities
593
+ * @returns
594
+ */
595
+
596
+ filterOutArchivedAndTrashedEntities(entities: any[]) {
597
+ if(!entities || !Array.isArray(entities) || entities.length === 0){
598
+ return [];
599
+ }
600
+ return entities.filter(entity => {
601
+ const isArchived = entity?.isArchived;
602
+ const isTrashed = entity?.isTrashed;
603
+ return !isArchived && !isTrashed;
604
+ });
605
+ }
606
+
590
607
  /** Sets userListId value from FlexPLM data passed in
591
608
  *
592
609
  * @param prop the VibeIQ property
@@ -1,199 +1,199 @@
1
- import { Logger } from '@contrail/app-framework';
2
- import { FCConfig, FlexPLMResponseData, PayloadType } from '../interfaces/interfaces';
3
-
4
- export class FlexPLMConnect {
5
- private config: FCConfig;
6
- private vibeEventEndpoint = '';
7
- private staticHeaders = undefined;
8
- public payloadSendAsArray = true;
9
- constructor(_config: FCConfig, endPoint = undefined, payloadAsArray = undefined) {
10
- this.config = _config;
11
- this.vibeEventEndpoint = (endPoint)
12
- ? endPoint
13
- : this.config.vibeEventEndpoint;
14
- this.payloadSendAsArray = (payloadAsArray != undefined)
15
- ? payloadAsArray
16
- : this.config['payloadDefaultAsArray'];
17
- if(this.config?.flexplmConnect?.staticHeaders){
18
- this.staticHeaders = this.config?.flexplmConnect?.staticHeaders;
19
- }
20
- }
21
-
22
- /////////////////////////////////////////////////////////////////////////////
23
- ///////// Custom getRequestOptions: start
24
- /////////////////////////////////////////////////////////////////////////////
25
- private getRequestOptions(method: string) {
26
- const csrfOptions = {
27
- method,
28
- headers: {
29
- 'Content-Type': 'application/json',
30
- PLM_ENV: this.config.plmEnviornment,
31
- Authorization: 'Basic ' + Buffer.from(`${this.config.userName()}:${this.config.password()}`, 'binary').toString('base64')
32
- }
33
- };
34
- if(this.staticHeaders){
35
- Object.assign(csrfOptions.headers, this.staticHeaders);
36
- }
37
- if(Logger.isInfoOn()){
38
- const logOptions = JSON.parse(JSON.stringify(csrfOptions));
39
- logOptions['headers']['Authorization'] = logOptions?.headers?.Authorization.substring(0, 9);
40
- console.info('csrfOptions: ' + JSON.stringify(logOptions));
41
- }
42
- return csrfOptions;
43
- }
44
- /////////////////////////////////////////////////////////////////////////////
45
- ///////// Custom getRequestOptions: end
46
- /////////////////////////////////////////////////////////////////////////////
47
-
48
- async getCSRF() {
49
- const urlContext = this.config.urlContext;
50
- const csrfEndpoint = this.config.csrfEndpoint;
51
- const csrfURL: string = this.config.apiHost + urlContext + csrfEndpoint;
52
- console.info('csrfURL: ' + csrfURL);
53
-
54
- const csrfOptions = this.getRequestOptions('GET');
55
-
56
- const response = await fetch(csrfURL, csrfOptions);
57
- if(response.status >= 300){
58
- const message = 'Error connecting to FlexPLM:status: ' + response.status;
59
- console.error(message);
60
- console.error(await response.text());
61
- const e = new Error(message);
62
- e['httpResponseStatus'] = response.status;
63
- throw e;
64
- }
65
-
66
- try{
67
-
68
- const body = await response.json();
69
-
70
- const nonce_key = body.items[0].attributes.nonce_key;
71
- const nonce = body.items[0].attributes.nonce;
72
- console.info('nonce_key: ' + nonce_key);
73
- console.info('nonce: ' + nonce);
74
- return {
75
- nonce_key,
76
- nonce
77
- };
78
- }catch(e){
79
- const message = 'Error connecting to FlexPLM: ' + e.message;
80
- console.error(message);
81
- console.error(await response.text());
82
- throw new Error(message);
83
- }
84
- }
85
-
86
- async sendRequest(csrf, payload) {
87
-
88
- if(this.payloadSendAsArray && !Array.isArray(payload)){
89
- payload = [payload];
90
- }
91
-
92
- const xferOptions = this.getRequestOptions('POST');
93
-
94
- xferOptions['body'] = JSON.stringify(payload);
95
- xferOptions.headers[csrf.nonce_key] = csrf.nonce;
96
-
97
- const urlContext = this.config.urlContext;
98
- const vibeEventsURL: string = this.config.apiHost + urlContext + '/servlet/rest' + this.vibeEventEndpoint;
99
-
100
- if(Logger.isInfoOn()){
101
- console.info('Request:');
102
- console.info('vibeEventsURL: ' + vibeEventsURL);
103
- const logOptions = JSON.parse(JSON.stringify(xferOptions));
104
- logOptions['headers']['Authorization'] = logOptions?.headers?.Authorization.substring(0, 9);
105
- console.info('csrfOptions: ' + JSON.stringify(logOptions));
106
- console.info('Making call to xfer data to FlexPLM');
107
- }
108
- const eventResponse = await fetch(vibeEventsURL, xferOptions);
109
- return eventResponse;
110
- }
111
-
112
- protected async processRequest(payload:any){
113
- if(!payload){
114
- const message = 'No payload to send to FlexPLM';
115
- console.error(message);
116
- throw new Error(message);
117
- }
118
- if(Logger.isInfoOn()){
119
- console.info('payload: ' + JSON.stringify(payload));
120
- }
121
-
122
- const csrf = await this.getCSRF();
123
- if(!csrf){
124
- const message = 'Failed to get CSRF nonce';
125
- console.error(message);
126
- throw new Error(message);
127
- }
128
-
129
- const response = await this.sendRequest(csrf, payload);
130
- const status = response.status;
131
- if(status >= 300){
132
- const message = 'Error sending data to FlexPLM:status: ' + response.status;
133
- console.error(message);
134
- console.error(await response.text());
135
- const e = new Error(message);
136
- e['httpResponseStatus'] = status;
137
- throw e;
138
- }
139
-
140
- try{
141
- const res: FlexPLMResponseData = {
142
- status,
143
- };
144
- if(![204, 205].includes(status)){
145
- try{
146
- const data = await response.json();
147
- res.data = data;
148
- } catch(e){
149
- console.error('Error getting response body. Setting {} body: ' + e);
150
- res.data = {};
151
- }
152
- }
153
-
154
- console.log('eventResponse.status: ' + status);
155
- // console.log('eventBody: ', JSON.stringify(data));
156
- return res;
157
-
158
- }catch(e){
159
- const message = 'Error getting json data from FlexPLM: ' + e.message;
160
- console.error(message);
161
- console.error(await response.text());
162
- throw new Error(message);
163
- }
164
-
165
- }
166
- async sendToFlexPLM( payload: PayloadType){
167
- return await this.processRequest(payload);
168
- }
169
-
170
- async sendMultipleToFlexPLM(payload: PayloadType[]){
171
- return await this.processRequest(payload);
172
- }
173
-
174
- /** Runs a get request to FlexPLM using the endpoint provided in the constructor
175
- *
176
- */
177
- async getRequest(){
178
- const urlContext = this.config.urlContext;
179
- const vibeEventsURL: string = this.config.apiHost + urlContext + '/servlet/rest' + this.vibeEventEndpoint;
180
- const csrfOptions = this.getRequestOptions('GET');
181
- const response = await fetch(vibeEventsURL, csrfOptions);
182
-
183
- if(response.status >= 300){
184
- const message = 'Error connecting to FlexPLM:status: ' + response.status;
185
- console.error(message);
186
- console.error(await response.text());
187
- throw new Error(message);
188
- }
189
- try {
190
- const data = await response.json();
191
- return data;
192
- } catch(e){
193
- const message = 'Error getting json data from FlexPLM: ' + e.message;
194
- console.error(message);
195
- console.error(await response.text());
196
- throw new Error(message);
197
- }
198
- }
199
- }
1
+ import { Logger } from '@contrail/app-framework';
2
+ import { FCConfig, FlexPLMResponseData, PayloadType } from '../interfaces/interfaces';
3
+
4
+ export class FlexPLMConnect {
5
+ private config: FCConfig;
6
+ private vibeEventEndpoint = '';
7
+ private staticHeaders = undefined;
8
+ public payloadSendAsArray = true;
9
+ constructor(_config: FCConfig, endPoint = undefined, payloadAsArray = undefined) {
10
+ this.config = _config;
11
+ this.vibeEventEndpoint = (endPoint)
12
+ ? endPoint
13
+ : this.config.vibeEventEndpoint;
14
+ this.payloadSendAsArray = (payloadAsArray != undefined)
15
+ ? payloadAsArray
16
+ : this.config['payloadDefaultAsArray'];
17
+ if(this.config?.flexplmConnect?.staticHeaders){
18
+ this.staticHeaders = this.config?.flexplmConnect?.staticHeaders;
19
+ }
20
+ }
21
+
22
+ /////////////////////////////////////////////////////////////////////////////
23
+ ///////// Custom getRequestOptions: start
24
+ /////////////////////////////////////////////////////////////////////////////
25
+ private getRequestOptions(method: string) {
26
+ const csrfOptions = {
27
+ method,
28
+ headers: {
29
+ 'Content-Type': 'application/json',
30
+ PLM_ENV: this.config.plmEnviornment,
31
+ Authorization: 'Basic ' + Buffer.from(`${this.config.userName()}:${this.config.password()}`, 'binary').toString('base64')
32
+ }
33
+ };
34
+ if(this.staticHeaders){
35
+ Object.assign(csrfOptions.headers, this.staticHeaders);
36
+ }
37
+ if(Logger.isInfoOn()){
38
+ const logOptions = JSON.parse(JSON.stringify(csrfOptions));
39
+ logOptions['headers']['Authorization'] = logOptions?.headers?.Authorization.substring(0, 9);
40
+ console.info('csrfOptions: ' + JSON.stringify(logOptions));
41
+ }
42
+ return csrfOptions;
43
+ }
44
+ /////////////////////////////////////////////////////////////////////////////
45
+ ///////// Custom getRequestOptions: end
46
+ /////////////////////////////////////////////////////////////////////////////
47
+
48
+ async getCSRF() {
49
+ const urlContext = this.config.urlContext;
50
+ const csrfEndpoint = this.config.csrfEndpoint;
51
+ const csrfURL: string = this.config.apiHost + urlContext + csrfEndpoint;
52
+ console.info('csrfURL: ' + csrfURL);
53
+
54
+ const csrfOptions = this.getRequestOptions('GET');
55
+
56
+ const response = await fetch(csrfURL, csrfOptions);
57
+ if(response.status >= 300){
58
+ const message = 'Error connecting to FlexPLM:status: ' + response.status;
59
+ console.error(message);
60
+ console.error(await response.text());
61
+ const e = new Error(message);
62
+ e['httpResponseStatus'] = response.status;
63
+ throw e;
64
+ }
65
+
66
+ try{
67
+
68
+ const body = await response.json();
69
+
70
+ const nonce_key = body.items[0].attributes.nonce_key;
71
+ const nonce = body.items[0].attributes.nonce;
72
+ console.info('nonce_key: ' + nonce_key);
73
+ console.info('nonce: ' + nonce);
74
+ return {
75
+ nonce_key,
76
+ nonce
77
+ };
78
+ }catch(e){
79
+ const message = 'Error connecting to FlexPLM: ' + e.message;
80
+ console.error(message);
81
+ console.error(await response.text());
82
+ throw new Error(message);
83
+ }
84
+ }
85
+
86
+ async sendRequest(csrf, payload) {
87
+
88
+ if(this.payloadSendAsArray && !Array.isArray(payload)){
89
+ payload = [payload];
90
+ }
91
+
92
+ const xferOptions = this.getRequestOptions('POST');
93
+
94
+ xferOptions['body'] = JSON.stringify(payload);
95
+ xferOptions.headers[csrf.nonce_key] = csrf.nonce;
96
+
97
+ const urlContext = this.config.urlContext;
98
+ const vibeEventsURL: string = this.config.apiHost + urlContext + '/servlet/rest' + this.vibeEventEndpoint;
99
+
100
+ if(Logger.isInfoOn()){
101
+ console.info('Request:');
102
+ console.info('vibeEventsURL: ' + vibeEventsURL);
103
+ const logOptions = JSON.parse(JSON.stringify(xferOptions));
104
+ logOptions['headers']['Authorization'] = logOptions?.headers?.Authorization.substring(0, 9);
105
+ console.info('csrfOptions: ' + JSON.stringify(logOptions));
106
+ console.info('Making call to xfer data to FlexPLM');
107
+ }
108
+ const eventResponse = await fetch(vibeEventsURL, xferOptions);
109
+ return eventResponse;
110
+ }
111
+
112
+ protected async processRequest(payload:any){
113
+ if(!payload){
114
+ const message = 'No payload to send to FlexPLM';
115
+ console.error(message);
116
+ throw new Error(message);
117
+ }
118
+ if(Logger.isInfoOn()){
119
+ console.info('payload: ' + JSON.stringify(payload));
120
+ }
121
+
122
+ const csrf = await this.getCSRF();
123
+ if(!csrf){
124
+ const message = 'Failed to get CSRF nonce';
125
+ console.error(message);
126
+ throw new Error(message);
127
+ }
128
+
129
+ const response = await this.sendRequest(csrf, payload);
130
+ const status = response.status;
131
+ if(status >= 300){
132
+ const message = 'Error sending data to FlexPLM:status: ' + response.status;
133
+ console.error(message);
134
+ console.error(await response.text());
135
+ const e = new Error(message);
136
+ e['httpResponseStatus'] = status;
137
+ throw e;
138
+ }
139
+
140
+ try{
141
+ const res: FlexPLMResponseData = {
142
+ status,
143
+ };
144
+ if(![204, 205].includes(status)){
145
+ try{
146
+ const data = await response.json();
147
+ res.data = data;
148
+ } catch(e){
149
+ console.error('Error getting response body. Setting {} body: ' + e);
150
+ res.data = {};
151
+ }
152
+ }
153
+
154
+ console.log('eventResponse.status: ' + status);
155
+ // console.log('eventBody: ', JSON.stringify(data));
156
+ return res;
157
+
158
+ }catch(e){
159
+ const message = 'Error getting json data from FlexPLM: ' + e.message;
160
+ console.error(message);
161
+ console.error(await response.text());
162
+ throw new Error(message);
163
+ }
164
+
165
+ }
166
+ async sendToFlexPLM( payload: PayloadType){
167
+ return await this.processRequest(payload);
168
+ }
169
+
170
+ async sendMultipleToFlexPLM(payload: PayloadType[]){
171
+ return await this.processRequest(payload);
172
+ }
173
+
174
+ /** Runs a get request to FlexPLM using the endpoint provided in the constructor
175
+ *
176
+ */
177
+ async getRequest(){
178
+ const urlContext = this.config.urlContext;
179
+ const vibeEventsURL: string = this.config.apiHost + urlContext + '/servlet/rest' + this.vibeEventEndpoint;
180
+ const csrfOptions = this.getRequestOptions('GET');
181
+ const response = await fetch(vibeEventsURL, csrfOptions);
182
+
183
+ if(response.status >= 300){
184
+ const message = 'Error connecting to FlexPLM:status: ' + response.status;
185
+ console.error(message);
186
+ console.error(await response.text());
187
+ throw new Error(message);
188
+ }
189
+ try {
190
+ const data = await response.json();
191
+ return data;
192
+ } catch(e){
193
+ const message = 'Error getting json data from FlexPLM: ' + e.message;
194
+ console.error(message);
195
+ console.error(await response.text());
196
+ throw new Error(message);
197
+ }
198
+ }
199
+ }