@contrail/flexplm 1.1.14 → 1.1.16

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.
@@ -7,6 +7,7 @@ export declare class DataConverter {
7
7
  private transformMapFile;
8
8
  private useDisplayForEnumerationMatching;
9
9
  private objRefCache;
10
+ private userRefCache;
10
11
  constructor(config: FCConfig, mapFileUtil: MapFileUtil);
11
12
  getFlexPLMObjectDataFromEvent(event: any, dataToSkip: string[]): Promise<{}>;
12
13
  getFlexPLMObjectData(newData: any, dataToSkip: string[], inflateObjRef: boolean): Promise<{}>;
@@ -19,4 +20,9 @@ export declare class DataConverter {
19
20
  getEntityValue(prop: any, data: any): Promise<any>;
20
21
  setEnumerationKeys(prop: any, nd: any, matchByDisplay: any): any;
21
22
  getPersistableChanges(entity: object, changes: object): object;
23
+ setUserListValue(prop: any, nd: any): Promise<any>;
24
+ getUserByEmail(nd: any): Promise<any>;
25
+ processGroupMemberCheck(prop: any, userEmail: any): Promise<void>;
26
+ getUserListValue(prop: any, newData: any): Promise<any>;
27
+ getUserById(userId: any): Promise<any>;
22
28
  }
@@ -13,6 +13,7 @@ class DataConverter {
13
13
  this.mapFileUtil = mapFileUtil;
14
14
  this.useDisplayForEnumerationMatching = false;
15
15
  this.objRefCache = {};
16
+ this.userRefCache = {};
16
17
  this.typeUtils = new type_utils_1.TypeUtils();
17
18
  this.transformMapFile = this.config['transformMapFile'];
18
19
  this.useDisplayForEnumerationMatching = this.config['dataConverter']
@@ -88,6 +89,9 @@ class DataConverter {
88
89
  else if ('json' === propertyType) {
89
90
  value = nd;
90
91
  }
92
+ else if ('userList' === propertyType) {
93
+ value = await this.getUserListValue(prop, newData);
94
+ }
91
95
  return value;
92
96
  }
93
97
  getEnumerationValue(prop, nd) {
@@ -194,8 +198,13 @@ class DataConverter {
194
198
  const dataKeys = Object.getOwnPropertyNames(data);
195
199
  typeProps = typeProps.filter(prop => dataKeys.includes(prop['slug']));
196
200
  for (const prop of typeProps) {
201
+ const propertyType = prop['propertyType'];
197
202
  const slug = prop['slug'];
198
- entity[slug] = await this.getEntityValue(prop, data);
203
+ let keyName = slug;
204
+ if (propertyType === 'userList') {
205
+ keyName = slug + 'Id';
206
+ }
207
+ entity[keyName] = await this.getEntityValue(prop, data);
199
208
  }
200
209
  return entity;
201
210
  }
@@ -260,6 +269,9 @@ class DataConverter {
260
269
  }
261
270
  else if ('json' === propertyType) {
262
271
  }
272
+ else if ('userList' === propertyType) {
273
+ value = await this.setUserListValue(prop, nd);
274
+ }
263
275
  return value;
264
276
  }
265
277
  setEnumerationKeys(prop, nd, matchByDisplay) {
@@ -309,5 +321,103 @@ class DataConverter {
309
321
  }
310
322
  return diffValues;
311
323
  }
324
+ async setUserListValue(prop, nd) {
325
+ if (!nd?.email) {
326
+ return "";
327
+ }
328
+ if (this.userRefCache[nd.email]) {
329
+ if (app_framework_1.Logger.isDebugOn()) {
330
+ console.debug('user cache hit: ' + nd.email);
331
+ }
332
+ await this.processGroupMemberCheck(prop, nd.email);
333
+ return this.userRefCache[nd.email];
334
+ }
335
+ const user = await this.getUserByEmail(nd);
336
+ const userId = (user) ? user.id : undefined;
337
+ if (userId) {
338
+ this.userRefCache[nd.email] = userId;
339
+ await this.processGroupMemberCheck(prop, nd.email);
340
+ }
341
+ return userId;
342
+ }
343
+ async getUserByEmail(nd) {
344
+ let userOrg = undefined;
345
+ let count = 0;
346
+ let size = 0;
347
+ const entities = new sdk_1.Entities();
348
+ const getOptionsCriteria = {
349
+ entityName: 'user-org',
350
+ take: 1000
351
+ };
352
+ do {
353
+ const userBatch = await entities.get(getOptionsCriteria);
354
+ userOrg = userBatch.find(uo => uo?.userEmail === nd?.email);
355
+ } while (!userOrg && size == getOptionsCriteria.take && count < 15);
356
+ return userOrg?.user;
357
+ }
358
+ async processGroupMemberCheck(prop, userEmail) {
359
+ let arrUserList = [];
360
+ if (this.userRefCache[prop.typePropertyUserListId]) {
361
+ arrUserList = this.userRefCache[prop.typePropertyUserListId];
362
+ }
363
+ else {
364
+ const userListResult = await new sdk_1.Entities().get({
365
+ entityName: 'user-list',
366
+ id: prop.typePropertyUserListId
367
+ });
368
+ if (userListResult && Object.keys(userListResult).length) {
369
+ arrUserList = userListResult.userList;
370
+ this.userRefCache[prop.typePropertyUserListId] = arrUserList;
371
+ }
372
+ }
373
+ if (arrUserList.length) {
374
+ const result = arrUserList.find(element => element['display'] === userEmail);
375
+ if (!result) {
376
+ console.warn(`The passed in user ${userEmail} in property slug ${prop.slug} should be a member of the group associated with the property 'typePropertyUserListId'`);
377
+ }
378
+ }
379
+ }
380
+ async getUserListValue(prop, newData) {
381
+ const slug = prop['slug'];
382
+ if (app_framework_1.Logger.isDebugOn()) {
383
+ console.debug('getUserListValue-prop: ' + slug);
384
+ }
385
+ const entityId = newData[slug + 'Id'];
386
+ if (!entityId) {
387
+ return {};
388
+ }
389
+ if (this.userRefCache[entityId]) {
390
+ if (app_framework_1.Logger.isDebugOn()) {
391
+ console.debug('user cache hit: ' + entityId);
392
+ }
393
+ return this.userRefCache[entityId];
394
+ }
395
+ const user = await this.getUserById(entityId);
396
+ const value = (user) ? {
397
+ email: user.email,
398
+ firstName: user.first,
399
+ lastName: user.last,
400
+ isSsoUser: user.isSsoUser,
401
+ } : undefined;
402
+ if (value) {
403
+ this.userRefCache[entityId] = value;
404
+ }
405
+ return value;
406
+ }
407
+ async getUserById(userId) {
408
+ let userOrg = undefined;
409
+ let count = 0;
410
+ let size = 0;
411
+ const entities = new sdk_1.Entities();
412
+ const getOptionsCriteria = {
413
+ entityName: 'user-org',
414
+ take: 1000
415
+ };
416
+ do {
417
+ const userBatch = await entities.get(getOptionsCriteria);
418
+ userOrg = userBatch.find(uo => uo?.user?.id === userId);
419
+ } while (!userOrg && size == getOptionsCriteria.take && count < 15);
420
+ return userOrg?.user;
421
+ }
312
422
  }
313
423
  exports.DataConverter = DataConverter;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/flexplm",
3
- "version": "1.1.14",
3
+ "version": "1.1.16",
4
4
  "description": "Library used for integration with flexplm.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -1,7 +1,7 @@
1
1
  import { Entities, TypeClientOptions } from '@contrail/sdk';
2
2
  import { TypeUtils } from './type-utils';
3
3
  import { FCConfig } from '../interfaces/interfaces';
4
- import { MapFileUtil, TransformProcessor, TransformTask } from '@contrail/transform-data';
4
+ import { MapFileUtil } from '@contrail/transform-data';
5
5
  import { Logger } from '@contrail/app-framework';
6
6
  import { ObjectDiff, ObjectUtil } from '@contrail/util';
7
7
  import { TypeConversionUtils } from './type-conversion-utils';
@@ -12,6 +12,7 @@ export class DataConverter {
12
12
  private transformMapFile: string;
13
13
  private useDisplayForEnumerationMatching = false;
14
14
  private objRefCache = {};
15
+ private userRefCache = {};
15
16
  constructor(private config: FCConfig, private mapFileUtil: MapFileUtil){
16
17
  this.typeUtils = new TypeUtils();
17
18
  this.transformMapFile = this.config['transformMapFile'];
@@ -95,6 +96,8 @@ export class DataConverter {
95
96
  }else if('json' === propertyType){
96
97
  // console.log('json-TODO');
97
98
  value = nd;
99
+ } else if ('userList' === propertyType) {
100
+ value = await this.getUserListValue(prop, newData);
98
101
  }
99
102
 
100
103
  return value;
@@ -234,9 +237,13 @@ export class DataConverter {
234
237
  typeProps = typeProps.filter( prop => dataKeys.includes(prop['slug']));
235
238
 
236
239
  for(const prop of typeProps){
237
- // const propertyType = prop['propertyType'];
240
+ const propertyType = prop['propertyType'];
238
241
  const slug = prop['slug'];
239
- entity[slug] = await this.getEntityValue(prop, data);
242
+ let keyName = slug;
243
+ if(propertyType === 'userList') {
244
+ keyName = slug + 'Id';
245
+ }
246
+ entity[keyName] = await this.getEntityValue(prop, data);
240
247
  // console.log('entity[slug]: ' + entity[slug]);
241
248
  }
242
249
 
@@ -324,6 +331,8 @@ export class DataConverter {
324
331
  // console.log('TODO-formula');
325
332
  } else if ('json' === propertyType) {
326
333
  // console.log('TODO-json');
334
+ } else if ('userList' === propertyType) {
335
+ value = await this.setUserListValue(prop, nd);
327
336
  }
328
337
 
329
338
  // console.log(value);
@@ -386,4 +395,156 @@ export class DataConverter {
386
395
 
387
396
  }
388
397
 
398
+ /** Sets userListId value from FlexPLM data passed in
399
+ *
400
+ * @param prop the VibeIQ property
401
+ * @param nd the VibeIQ data
402
+ * @returns the modified entity with FlexPLM values
403
+ */
404
+ async setUserListValue(prop, nd) {
405
+ if(!nd?.email) {
406
+ return "";
407
+ }
408
+
409
+ if (this.userRefCache[nd.email]) {
410
+ if (Logger.isDebugOn()) {
411
+ console.debug('user cache hit: ' + nd.email);
412
+ }
413
+
414
+ await this.processGroupMemberCheck(prop, nd.email);
415
+ return this.userRefCache[nd.email];
416
+ }
417
+
418
+ const user = await this.getUserByEmail(nd);
419
+ const userId = (user)?user.id:undefined;
420
+
421
+ if(userId) {
422
+ this.userRefCache[nd.email] = userId;
423
+ await this.processGroupMemberCheck(prop, nd.email);
424
+ }
425
+
426
+ return userId;
427
+ }
428
+
429
+ /** Makes batch calls of 1000 of user-org entities until
430
+ * it find one with userEmail of passed in nd.email.
431
+ * Maxes out after querying for 15,000 user-org entities
432
+ *
433
+ * @param nd
434
+ * @returns
435
+ */
436
+ async getUserByEmail(nd: any) {
437
+ let userOrg = undefined;
438
+ let count =0;
439
+ let size = 0;
440
+ const entities = new Entities();
441
+ const getOptionsCriteria = {
442
+ entityName: 'user-org',
443
+ take:1000
444
+ }
445
+
446
+ do {
447
+ const userBatch: [any] = await entities.get(getOptionsCriteria);
448
+ userOrg = userBatch.find(uo => uo?.userEmail === nd?.email);
449
+
450
+ }while( !userOrg && size == getOptionsCriteria.take && count < 15);
451
+ return userOrg?.user
452
+
453
+ }
454
+
455
+ /** Shows warning if user email address not present in group associated to property
456
+ *
457
+ * @param prop the VibeIQ property
458
+ * @param userEmail user email address
459
+ */
460
+ async processGroupMemberCheck(prop, userEmail) {
461
+ let arrUserList = [];
462
+ if(this.userRefCache[prop.typePropertyUserListId]) {
463
+ arrUserList = this.userRefCache[prop.typePropertyUserListId];
464
+ } else {
465
+ const userListResult = await new Entities().get({
466
+ entityName: 'user-list',
467
+ id: prop.typePropertyUserListId
468
+ });
469
+
470
+ if(userListResult && Object.keys(userListResult).length) {
471
+ arrUserList = userListResult.userList;
472
+ this.userRefCache[prop.typePropertyUserListId] = arrUserList;
473
+ }
474
+ }
475
+
476
+ if(arrUserList.length) {
477
+ const result = arrUserList.find(element => element['display'] === userEmail);
478
+ if(!result) {
479
+ console.warn(`The passed in user ${userEmail} in property slug ${prop.slug} should be a member of the group associated with the property 'typePropertyUserListId'`)
480
+ }
481
+ }
482
+ }
483
+
484
+ /** Gets the VibeIQ value for the userList property from data
485
+ *
486
+ * @param prop the VibeIQ property
487
+ * @param newData the FlexPLM data
488
+ * @returns value to be set in VibeIQ
489
+ */
490
+ async getUserListValue(prop, newData) {
491
+ const slug = prop['slug'];
492
+ if (Logger.isDebugOn()) {
493
+ console.debug('getUserListValue-prop: ' + slug);
494
+ }
495
+
496
+ const entityId = newData[slug + 'Id'];
497
+
498
+ if(!entityId) {
499
+ return {};
500
+ }
501
+
502
+ if (this.userRefCache[entityId]) {
503
+ if (Logger.isDebugOn()) {
504
+ console.debug('user cache hit: ' + entityId);
505
+ }
506
+ return this.userRefCache[entityId];
507
+ }
508
+
509
+ const user = await this.getUserById(entityId);
510
+
511
+ const value = (user) ? {
512
+ email: user.email,
513
+ firstName: user.first,
514
+ lastName: user.last,
515
+ isSsoUser: user.isSsoUser,
516
+ } : undefined;
517
+
518
+ if(value) {
519
+ this.userRefCache[entityId] = value;
520
+ }
521
+
522
+ return value;
523
+ }
524
+
525
+ /** Makes batch calls of 1000 of user-org entities until
526
+ * it find one with user.id of passed in userId.
527
+ * Maxes out after querying for 15,000 user-org entities
528
+ *
529
+ * @param userId
530
+ * @returns
531
+ */
532
+ async getUserById(userId: any) {
533
+ let userOrg = undefined;
534
+ let count =0;
535
+ let size = 0;
536
+ const entities = new Entities();
537
+ const getOptionsCriteria = {
538
+ entityName: 'user-org',
539
+ take:1000
540
+ }
541
+
542
+ do {
543
+ const userBatch: [any] = await entities.get(getOptionsCriteria);
544
+ userOrg = userBatch.find(uo => uo?.user?.id === userId);
545
+
546
+ }while( !userOrg && size == getOptionsCriteria.take && count < 15);
547
+ return userOrg?.user
548
+ }
549
+
389
550
  }