comprodls-sdk 2.36.0 → 2.37.0

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.
@@ -4077,6 +4077,7 @@ function auth() {
4077
4077
  pisImport: pisImport.bind(this),
4078
4078
 
4079
4079
  getAllUsers: getAllUsers.bind(this),
4080
+ getAllUsersV2: getAllUsersV2.bind(this),
4080
4081
  getUserProfile: getUserProfile.bind(this),
4081
4082
  deleteUserProfile: deleteUserProfile.bind(this),
4082
4083
  updateUserRole: updateUserRole.bind(this),
@@ -4390,6 +4391,218 @@ function getAllUsers(options) {
4390
4391
  return dfd.promise;
4391
4392
  }
4392
4393
 
4394
+ /**
4395
+ * This function is used to fetch all the users of a particular org.
4396
+ * Params:
4397
+ * {
4398
+ * role: 'string' // Optional
4399
+ * lookup: 'string', // Optional. Search query (',' separated key-value pairs)
4400
+ * lookup-operator: 'string', // Optional. Default - 'OR'. Permitted values - [OR]
4401
+ * sortField: 'string', // Default - 'last_name'.
4402
+ * sortOrder: 'string', // Default - 'desc'. Permitted values - [desc, asc]
4403
+ * limit: number, // Optional. Default - 10
4404
+ * cursor: '<cursor>' // Optional
4405
+ * }
4406
+ * @param {*} options
4407
+ */
4408
+ function getAllUsersV2(options) {
4409
+ var self = this;
4410
+
4411
+ // Initializing promise
4412
+ var deferred = q.defer();
4413
+
4414
+ // Validations
4415
+ var error = helpers.validations.isAuthenticated(self.orgId, self.token);
4416
+ if (error) {
4417
+ deferred.reject(error);
4418
+ } else {
4419
+ // Initialize query object.
4420
+ var queryObject = {
4421
+ query: { bool: { must: [] } },
4422
+ size: options.limit || 10 // (DEFAULT) ES returns only 10 elements at max.
4423
+ };
4424
+
4425
+ // Prepare query_string for fetching all the users of a particular school (org).
4426
+ var queryStr = 'orgid:"' + self.orgId + '"';
4427
+
4428
+ // Update the query_string to filter the users based on their user-role.
4429
+ if (options.role) {
4430
+ var roleNameString, roleNameArray;
4431
+
4432
+ roleNameString = options.role;
4433
+ roleNameArray = roleNameString.split(',').map(function (item) {
4434
+ return item.trim();
4435
+ });
4436
+
4437
+ for (var idx in roleNameArray) {
4438
+ roleNameArray[idx] = 'roles:' + roleNameArray[idx];
4439
+ }
4440
+ queryStr = queryStr + ' AND (' + roleNameArray.join(' OR ') + ')';
4441
+ }
4442
+
4443
+ // Update the queryObject with the query_string.
4444
+ queryObject.query.bool.must.push({
4445
+ query_string: { query: queryStr }
4446
+ });
4447
+
4448
+ // Update the queryObject to filter the users based on lookup's key-value pairs.
4449
+ if (options.lookup && options['lookup-operator']) {
4450
+ var lookupString, lookupArray;
4451
+
4452
+ // Update the 'lookupString' to remove the prefix 'ext_' from keys of the key-value pairs.
4453
+ lookupString = options.lookup.replace(/ext_/ig, '');
4454
+ lookupArray = lookupString.split(',');
4455
+
4456
+ // Iterate over all the key-value pairs and prepare lookupOptions.
4457
+ var lookupOptions = { bool: {} };
4458
+ for (var index in lookupArray) {
4459
+ var lookupItem = lookupArray[index].trim(),
4460
+ tempArr = lookupItem.split(':'),
4461
+ lookupObject = { wildcard: {} },
4462
+ lookupKey, lookupValue;
4463
+
4464
+ /**
4465
+ * The suffix '.keyword' is ONLY required for fields of type 'string'.
4466
+ * Note: Currently lookup is ONLY supported for fields of type 'string'.
4467
+ */
4468
+ lookupKey = tempArr[0] + '.keyword';
4469
+ lookupValue = tempArr[1];
4470
+
4471
+ // Case-insensitive lookup is currently ONLY SUPPORTED on the field 'ext_email'.
4472
+ if (lookupKey === 'email.keyword') {
4473
+ lookupObject.wildcard[lookupKey] = { value: lookupValue, case_insensitive: true };
4474
+ } else {
4475
+ lookupObject.wildcard[lookupKey] = { value: lookupValue, case_insensitive: false };
4476
+ }
4477
+
4478
+ if (options['lookup-operator'] === 'OR') {
4479
+ if (!lookupOptions.bool.should) { lookupOptions.bool.should = []; }
4480
+ lookupOptions.bool.should.push(lookupObject);
4481
+ } else {
4482
+ /**
4483
+ * If the 'lookup-operator' is 'AND', use the 'must' clause.
4484
+ * Note: This value is currently NOT SUPPORTED.
4485
+ * Prepare error and reject the request.
4486
+ */
4487
+ var errMsg = 'The value \'AND\' for the field \'lookup-operator\' is currently NOT supported';
4488
+ error = {};
4489
+ error.message = error.description = errMsg;
4490
+ error = new DLSError(helpers.errors.ERROR_TYPES.SDK_ERROR, error);
4491
+
4492
+ deferred.reject(error);
4493
+ return deferred.promise;
4494
+ }
4495
+ }
4496
+ queryObject.query.bool.must.push(lookupOptions);
4497
+ }
4498
+
4499
+ // Update the queryObject to sort the results based on sortField / sortOrder.
4500
+ var sortField, sortOrder, sortObj = {}, sortArr = [];
4501
+ sortField = options.sortField || 'last_name';
4502
+ sortOrder = options.sortOrder || 'asc';
4503
+
4504
+ // Update the sortField to remove prefix: 'ext_' and trim any unnecessary whitespaces.
4505
+ sortField = sortField.replace(/ext_/ig, '').split(',').shift().trim();
4506
+ if (sortField !== 'created') {
4507
+ sortField = sortField + '.keyword';
4508
+ }
4509
+
4510
+ sortObj[sortField] = sortOrder;
4511
+ sortArr.push(sortObj);
4512
+ queryObject.sort = sortArr;
4513
+
4514
+ // Update the queryObject to fetch the next set of records from ES.
4515
+ if (options.cursor) {
4516
+ queryObject.search_after = JSON.parse(options.cursor);
4517
+ }
4518
+
4519
+ // Passed all validations, Construct API URL
4520
+ var url = self.config.DEFAULT_HOSTS.INTEGRATION + self.config.INTEGRATIONS_API_URLS.queryDataFromSearch;
4521
+ url = helpers.api.constructAPIUrl(url, { index: 'users' });
4522
+
4523
+ // Setup request with URL and Params
4524
+ var params = { query: JSON.stringify(queryObject)};
4525
+ var requestAPI = request.get(url).query(params);
4526
+ if (self.traceid) {
4527
+ requestAPI.set('X-Amzn-Trace-Id', self.traceid);
4528
+ }
4529
+
4530
+ // Setup token in Authorization header
4531
+ requestAPI = helpers.api.setupAPIToken(requestAPI, self.token);
4532
+
4533
+ // Call the ES API to fetch users from ES.
4534
+ requestAPI.end(function (err, response) {
4535
+ if (err) {
4536
+ // Prepare and send error response.
4537
+ var esErrOptions = { type: helpers.errors.ERROR_TYPES.API_ERROR, err: err },
4538
+ error = converter.convertElasticsearchErrResponse(esErrOptions);
4539
+
4540
+ deferred.reject(error);
4541
+ } else {
4542
+ // Prepare and send success response.
4543
+ var responseObject = {
4544
+ entities: [], count: response.body.hits.hits.length
4545
+ };
4546
+
4547
+ if (responseObject.count && options.limit === responseObject.count) {
4548
+ var cursor = response.body.hits.hits[response.body.hits.hits.length - 1].sort;
4549
+ responseObject.cursor = JSON.stringify(cursor);
4550
+ }
4551
+
4552
+ for (var index in response.body.hits.hits) {
4553
+ var userEntity = response.body.hits.hits[index]._source,
4554
+ userObj = {
4555
+ uuid: userEntity.dls_user_id,
4556
+ name: userEntity.first_name + ' ' + userEntity.last_name,
4557
+ email: userEntity.email || userEntity.ext_email,
4558
+ username: userEntity.userid + '--ACCOUNT',
4559
+ created: userEntity.created,
4560
+ org: { id: self.orgId }
4561
+ };
4562
+
4563
+ if (userEntity.roles) {
4564
+ userObj.roles = userEntity.roles;
4565
+ }
4566
+
4567
+ if (userEntity.provision_method) {
4568
+ userObj.provision_method = userEntity.provision_method;
4569
+ }
4570
+
4571
+ if (userEntity.first_name) {
4572
+ userObj.first_name = userEntity.first_name;
4573
+ }
4574
+
4575
+ if (userEntity.last_name) {
4576
+ userObj.last_name = userEntity.last_name;
4577
+ }
4578
+
4579
+ if (userEntity.ext_parent_id) {
4580
+ userObj.ext_parent_id = userEntity.ext_parent_id;
4581
+ }
4582
+
4583
+ if (userEntity.username) {
4584
+ userObj.ext_username = userEntity.username;
4585
+ }
4586
+
4587
+ if (userEntity.userid) {
4588
+ userObj.ext_user_id = userEntity.userid;
4589
+ }
4590
+
4591
+ if (userEntity.address) {
4592
+ userObj.address = userEntity.address;
4593
+ }
4594
+
4595
+ responseObject.entities.push(userObj);
4596
+ }
4597
+
4598
+ deferred.resolve(responseObject);
4599
+ }
4600
+ });
4601
+ }
4602
+
4603
+ return deferred.promise;
4604
+ }
4605
+
4393
4606
  //options = {
4394
4607
  // userid: 'dlsUserId', //mandatory
4395
4608
  // target_role: 'string', //mandatory