Name

sn_irm_shared_cmn.LicenseUtils

Description

No description available

Script

var LicenseUtils = Class.create();
LicenseUtils.prototype = {

  CONST_OPERATOR: 'operator',
  CONST_LITE_OPERATOR: 'lite_operator',

  initialize: function() {},

  //This global variable is type of map. This variable will keep the snapshot of IRM: unique user usage table. Key for this map is application family and value is array of records sysId . At max 50K records will be fetched per application family.
  uniqueUserUsageData: {},

  // This global variable will be true if already data is present in unique user usage table. If this variable is true then only recently added record will be fetch from sys_user_has_role table.
  isEntriesPresent: false,

  // This global variable will store recently active users from system. 
  newActiveUserList: [],

  // This global variable will store list of Non Shared role present in IRM roly type table.
  allLicesnsingNonSharedRoles: [],

  // This global variable is of type map. This variable will keep the snapshot of IRM role type table. Key for this map is application family and value is non shared role belongs to this family. Only Non shared roles will present here.
  rolesBasedOnApplication: {},

  //This global variable is of type map. This variable will keep the snapshot of IRM role type table. Key for this map is application family and value is shared role belongs to this family. Only shared roles will present here.
  sharedRoleListObject: {},

  // These users are OOB user from GRC applications therefore these users should not get counted under any licensing application family.
  userExceptionList: ["40705f9473d1001083d1d8b0caf6a7d7"],

  allLicesnsingRoles: [],

  // GRC Employee user role
  GRC_EMPLOYEE_USER: 'aeeb25e5c30b2110b77661ba3c40ddc5',

  //API to insert records in unique user usage table. Business Rules can call this method to do direct insert or update records in unique user usage table.
  updateUniqueUserTable: function(user, appFamily, userType, reason) {
      if (this._checkValidAppFamily(appFamily)) {
          var timeStamp = this.getCurrentMonthAccrualPeriod();
          if (userType == this.CONST_LITE_OPERATOR) {
              var liteAppFamily = this._getLiteAppFamily(appFamily);
              if (liteAppFamily) {
                  appFamily = liteAppFamily;
              }
          }
          this.addToGRCUsageTable(user, appFamily, timeStamp, reason);
      }
  },

  // This function will return sys_id of licensable application family based on name. 
  getAppFamilyRecordFromName: function(name) {
      var appFamily = new GlideRecord("sn_irm_shared_cmn_licensable_application_family");
      appFamily.addQuery("application_family_name", name);
      appFamily.setLimit(1);
      appFamily.query();
      if (appFamily.next()) {
          return appFamily;
      }
      return "";
  },

  //This function is trigger point for role based approach. Insert/Update user into unique user usage table based on role assigned to it.
  updateUniqueUserTableBasedOnUserRole: function() {
      var roleType = new GlideRecord("sn_irm_shared_cmn_licensing_role_type");
      roleType.orderBy("is_shared_role");
      roleType.orderBy("role");
      roleType.query();
      while (roleType.next()) {

          var appFamily = this._getApplicationFamilyFromRoleType(roleType.getValue("role"), roleType.getValue("licensing_user_type"));

          if (roleType.getValue("is_shared_role") == false) {
              this.allLicesnsingNonSharedRoles.push(roleType.getValue("role"));

              if (!this.rolesBasedOnApplication[appFamily]) {
                  this.rolesBasedOnApplication[appFamily] = [];
              }
              this.rolesBasedOnApplication[appFamily].push(roleType.getValue("role"));
          } else {
              if (!this.sharedRoleListObject[appFamily]) {
                  this.sharedRoleListObject[appFamily] = [];
              }
              this.sharedRoleListObject[appFamily].push(roleType.getValue("role"));
          }
          this.allLicesnsingRoles.push(roleType.getValue("role"));
      }

      this._isEntriesPresentForCurrentMonth();
      this._getAllDataFromUniqueUserUsageTable();

      this._runLisensingAlogForEachApplicationFamily(this.rolesBasedOnApplication, true);
      this._insertUsersDirectlyAssignedWithSharedRoles(this.sharedRoleListObject);
      this._runLisensingAlogForEachApplicationFamily(this.sharedRoleListObject, false);
  },

  _insertUsersDirectlyAssignedWithSharedRoles: function(roleMap) {
      var appFamilyList = new GlideRecord("sn_irm_shared_cmn_licensable_application_family");
      appFamilyList.orderBy("execution_order");
      appFamilyList.query();
      while (appFamilyList.next()) {
          if (roleMap[appFamilyList.getUniqueValue()]) {
              this._insertUsersDirectlyAssignedWithThisListedRoles(roleMap[appFamilyList.getUniqueValue()], appFamilyList.getUniqueValue());
          }
      }
  },

  _insertUsersDirectlyAssignedWithThisListedRoles: function(roleList, appFamily) {
      var userWithRole = new GlideRecord("sys_user_has_role");
      userWithRole.addQuery("role", "IN", roleList);
      userWithRole.addQuery("user.active", true);
      userWithRole.addQuery("user", "NOT IN", this.userExceptionList);
      userWithRole.addQuery("inherited", false);

      if (this.isEntriesPresent) {
          var recentUpdate = userWithRole.addQuery("sys_updated_on", ">=", this._calulateLastDay());
          recentUpdate.addOrCondition("user", "IN", this.newActiveUserList);
      }

      userWithRole.orderBy("user");
      userWithRole.query();
      var userRole = "";
      var userRoleType = "";
      while (userWithRole.next()) {
          userRole = this._getRoleForTheUserFromList(userWithRole.getValue("user"), roleList);
          userRoleType = this._getLicensingRoleType(userRole);
          if (userRole && userRoleType) {
              this.updateUniqueUserTable(userWithRole.getValue("user"), appFamily, userRoleType, userRole);
          }
      }
  },

  getAppFamilyByRAMDomain: function(domain, userType) {
      var appFamily = 'IRM';
      if (domain == '2') {
          appFamily = 'BCM';
      } else if (domain == '3') {
          appFamily = 'PRM';
      }
      var appFamilyID = this.getAppFamilyRecordFromName(appFamily).getUniqueValue();
      if (userType == 'operator') {
          return appFamilyID;
      }
      var liteAppFamilyID = this._getLiteAppFamily(appFamilyID);
      if (liteAppFamilyID == '')
          return appFamilyID;
      else
          return liteAppFamilyID;
  },

  _runLisensingAlogForEachApplicationFamily: function(roleMap, nonSharedRole) {
      var appFamilyList = new GlideRecord("sn_irm_shared_cmn_licensable_application_family");
      appFamilyList.orderBy("execution_order");
      appFamilyList.query();
      while (appFamilyList.next()) {
          if (roleMap[appFamilyList.getUniqueValue()]) {
              this._getUserWithTheseRole(roleMap[appFamilyList.getUniqueValue()], appFamilyList.getUniqueValue(), nonSharedRole);
          }
      }
  },

  _getRoleForTheUserFromList: function(user, roleList) {
      var gr = new GlideRecord("sys_user_has_role");
      gr.addQuery("user", user);
      gr.addQuery("role", "IN", roleList);
      gr.setLimit(1);
      gr.query();
      if (gr.next()) {
          return gr.getValue("role");
      }
      return "";
  },

  _getLicensingRoleType: function(role) {
      var gr = new GlideRecord("sn_irm_shared_cmn_licensing_role_type");
      gr.addQuery("role", role);
      gr.setLimit(1);
      gr.query();
      if (gr.next()) {
          return gr.getValue("licensing_user_type");
      }
      return "";
  },

  _isEntriesPresentForCurrentMonth: function() {
      var usageTable = new GlideRecord("sn_irm_shared_cmn_unique_user_usage");
      usageTable.addQuery("accrual_period", this.getCurrentMonthAccrualPeriod());
      usageTable.query();
      if (usageTable.hasNext()) {
          this.isEntriesPresent = true;
          this._getNewActiveUser();
      } else {
          this.isEntriesPresent = false;
      }
  },

  _getNewActiveUser: function() {

      var newActiveUser = new GlideRecord("sys_user");
      newActiveUser.addActiveQuery();
      newActiveUser.addQuery("sys_updated_on", ">=", this._calulateLastDay());
      newActiveUser.addQuery("sys_created_on", "<", this._calulateLastDay());
      newActiveUser.query();
      while (newActiveUser.next()) {
          if (this._getRoleForTheUserFromList(newActiveUser.getUniqueValue(), this.allLicesnsingRoles))
              this.newActiveUserList.push(newActiveUser.getUniqueValue());
      }
  },

  getLastMonthAccrualPeriod: function() {
      var month = new GlideDateTime(gs.beginningOfLastMonth()).getMonthLocalTime();
      var year = new GlideDateTime(gs.beginningOfLastMonth()).getYearLocalTime();
      var timeStamp = year + "-" + month;
      if (month < 10)
          timeStamp = year + "-0" + month;
      return timeStamp;
  },

  addToGRCUsageTable: function(user, appFamily, accrualPeriod, reason) {
      //In case if entry is getting inserted with role-based approach, the reason is having role sysId and from there, reason is getting converted into proper msg.
      //In case if entry is getting inserted with Usage-based approach or through Business rule, no need to modify msg and it will get inserted directly. 
      if (reason) {
          var roleRecord = new GlideRecord("sys_user_role");
          roleRecord.addQuery("sys_id", reason + '');
          roleRecord.query();
          if (roleRecord.next()) {
              var operatorAppFamilyName = this._getAppFamilyFromLite(appFamily);
              if (operatorAppFamilyName) {
                  reason = "User has the Lite Operator role : " + roleRecord.getValue("name");
              } else {
                  reason = "User has the Operator role : " + roleRecord.getValue("name");
              }
          }
      }
      //Check if the user entry exists in Unique User Usage and insert if not exists
      if (this._hasUniqueUserUsageEntry(user, appFamily, accrualPeriod)) {
          var liteAppFamilyName = this._getLiteAppFamily(appFamily);
          if (liteAppFamilyName) {
              var usageEntry = this._getUniqueUserUsageEntry(user, liteAppFamilyName, accrualPeriod);
              if (usageEntry) {
                  usageEntry.setValue("licensable_application_family", appFamily);
                  usageEntry.setValue("reason", usageEntry.getValue("reason") + " , " + reason);
                  usageEntry.update();
              }
          }
      } else {

          if (!this.getExceptionForThisUser(user, appFamily)) {
              this._createUniquerUserUsageRecord(user, appFamily, accrualPeriod, reason);
          }
      }
  },

  getExceptionForThisUser: function(user, appFamily) {
      var gr = new GlideRecord("sn_irm_shared_cmn_licensable_application_family");
      gr.addQuery("sys_id", appFamily);
      gr.setLimit(1);
      gr.query();
      if (gr.next()) {
          if (this.checkUserContainOnlyThisExceptionRoles(user)) {
              return true;
          }

      }
      return false;
  },

  checkUserContainOnlyThisExceptionRoles: function(user) {
      var exceptionRoles = ["sn_vdr_risk_asmt.vendor_assessment_reviewer", "sn_vdr_risk_asmt.vendor_assessor", "sn_vdr_risk_asmt.vendor_risk_manager", "sn_vdr_risk_asmt.vendor_risk_admin", "sn_vdr_risk_asmt.approver", "sn_vdr_risk_asmt.contract_negotiator", "sn_esg.program_manager", "sn_esg.reader", "sn_esg.data_owner", "sn_esg.metrics_manager", "sn_esg.admin"];
      var exceptionRolesId = this.getRoleIdFromRoleName(exceptionRoles);
      if (exceptionRolesId.length > 0) {
          if (this.userHasThisRoles(user, exceptionRolesId)) {
              var exceptionListOfIRMRole = ["sn_risk_advanced.qualitative_risk_appetite_reader", "sn_data_registry.reader", "sn_data_registry.admin", "sn_grc.business_user", "sn_risk_advanced.ara_reader", "sn_compliance.reader", "sn_risk.reader", "sn_grc.user_hierarchy_reader", "sn_grc_workspace.task_reader", "sn_grc.reader", "sn_grc_taxonomy.taxonomy_user", "sn_grc.user", "sn_risk_advanced.ara_admin", "sn_risk_advanced.ara_creator", "sn_risk_advanced.ara_approver", "sn_risk_advanced.ara_assessor", "sn_grc_pa.sn_grc_pa_viewer, sn_grc.manager", "sn_grc_metric.user", "sn_grc_metric.reader", "sn_grc_metric.manager", "sn_grc_metric.admin", "sn_grc_case_mgmt.grc_case_business_user", "sn_comp_case.compliance_case_business_user"];

              var exceptionListOfIRMRoleIds = this.getRoleIdFromRoleName(exceptionListOfIRMRole);
              if (this.userHasThisRolesDirectlyAssigned(user, exceptionListOfIRMRoleIds)) {
                  return false;
              }
              var irmRoleFromRoleType = this.getAllIRMRolesFromRoleTypeTable(exceptionListOfIRMRoleIds);
              return !this.userHasThisRoles(user, irmRoleFromRoleType);
          }
      }
      return false;
  },

  userHasThisRolesDirectlyAssigned: function(user, roles) {
      var gr = new GlideRecord("sys_user_has_role");
      gr.addQuery("user", user);
      gr.addQuery("role", "IN", roles);
      gr.addQuery("inherited", false);
      gr.query();
      if (gr.next()) {
          return true;
      }
      return false;
  },

  getRoleIdFromRoleName: function(roleNames) {
      var rolesId = [];
      var role = new GlideRecord("sys_user_role");
      role.addQuery("name", "IN", roleNames);
      role.query();
      while (role.next()) {
          rolesId.push(role.getUniqueValue());
      }
      return rolesId;
  },

  userHasThisRoles: function(user, roles) {
      var gr = new GlideRecord("sys_user_has_role");
      gr.addQuery("user", user);
      gr.addQuery("role", "IN", roles);
      gr.query();
      if (gr.next()) {
          return true;
      }
      return false;
  },

  getAllIRMRolesFromRoleTypeTable: function(exceptionListOfIRMRoleIds) {
      var irmAppList = [];
      var gr = new GlideRecord("sn_irm_shared_cmn_m2m_application_family_application");
      gr.addQuery("licensable_application_family.application_family_name", "CONTAINS", "IRM");
      gr.query();
      while (gr.next()) {
          var appId = new GlideRecord("sn_irm_shared_cmn_licensing_application");
          appId.get(gr.getValue("application_for_licensing"));
          irmAppList.push(appId.getValue("application_name"));
      }

      var roleTypeList = [];
      var apps = new GlideRecord("sn_irm_shared_cmn_licensing_role_type");
      apps.addQuery("role.sys_scope", "IN", irmAppList);
      apps.addQuery("role.sys_id", "NOT IN", exceptionListOfIRMRoleIds);
      apps.query();
      while (apps.next()) {
          roleTypeList.push(apps.getValue("role"));
      }
      return roleTypeList;
  },

  getCurrentMonthAccrualPeriod: function() {
      var month = new GlideDateTime(gs.beginningOfThisMonth()).getMonthLocalTime();
      var year = new GlideDateTime(gs.beginningOfThisMonth()).getYearLocalTime();
      var timeStamp = year + "-" + month;
      if (month < 10)
          timeStamp = year + "-0" + month;
      return timeStamp;
  },

  //Access or Usage based approach: Update user usage table based on access.
  updateUniqueUserTableBasedOnAccess: function() {
      var appFamily = new GlideRecord("sn_irm_shared_cmn_licensable_application_family");
      appFamily.query();

      while (appFamily.next()) {
          this._copyPrevDayTableData(appFamily.getUniqueValue());
      }

      // Calculate the GRC Employee user roles
      if (GlidePluginManager.isActive('sn_grc_emp_user') || GlidePluginManager.isActive('com.sn_grc_emp_user')) {
          new sn_grc_emp_user.GRCEmployeeUserLicenseUtils().updateGRCEmployeeUsersForLastMonth();
      }

  },

  getRolesByApplicationFamily: function(appFamilyNames, userTypes, isAllUserType) {
      var roles = [];
      var applicationFamily = new GlideRecord('sn_irm_shared_cmn_m2m_application_family_application');
      applicationFamily.addQuery('licensable_application_family.application_family_name', 'IN', appFamilyNames);
      applicationFamily.query();
      while (applicationFamily.next()) {
          var roleType = new GlideRecord("sn_irm_shared_cmn_licensing_role_type");
          roleType.addQuery('role.sys_scope', applicationFamily.application_for_licensing.application_name);
          if (!isAllUserType) {
              roleType.addQuery('licensing_user_type', 'IN', userTypes);
          }
          roleType.query();
          while (roleType.next()) {
              roles.push(roleType.role.toString());
          }
      }
      return roles;
  },

  _getApplicationFamilyFromRoleType: function(role, roleType) {
      var roleRecord = new GlideRecord("sys_user_role");
      roleRecord.get(role);
      return this._getApplicationFamilyFromRole(roleRecord, roleType);
  },

  _getApplicationFamilyFromRole: function(roleRecord, roleType) {
      var applicationFamilyList = "";
      var application = roleRecord.getValue("sys_scope");
      var licApplication = new GlideRecord("sn_irm_shared_cmn_licensing_application");
      licApplication.addQuery("application_name", application);
      licApplication.query();
      if (licApplication.next()) {
          var applicationFamily = new GlideRecord("sn_irm_shared_cmn_m2m_application_family_application");
          applicationFamily.addQuery("application_for_licensing", licApplication.getUniqueValue());
          applicationFamily.setLimit(1);
          applicationFamily.query();
          if (applicationFamily.next()) {
              applicationFamilyList = applicationFamily.getValue("licensable_application_family");
          }
      }

      if (roleType == this.CONST_LITE_OPERATOR) {
          var liteAppFamily = this._getLiteAppFamily(applicationFamilyList);
          if (liteAppFamily) {
              applicationFamilyList = liteAppFamily;
          }
      }
      return applicationFamilyList;
  },

  _calulateLastDay: function() {
      var g1 = new GlideDateTime();
      g1.addDaysUTC(-1);
      var g2 = new GlideDateTime(g1.getDate() + " 00:00:00");
      return g2;
  },

  _getUserWithTheseRole: function(role, appFamily, nonSharedRole) {
      var userWithRole = new GlideRecord("sys_user_has_role");
      userWithRole.addQuery("role", "IN", role);
      userWithRole.addQuery("user.active", true);
      userWithRole.addQuery("user", "NOT IN", this.userExceptionList);

      if (this.isEntriesPresent) {
          var recentUpdate = userWithRole.addQuery("sys_updated_on", ">=", this._calulateLastDay());
          recentUpdate.addOrCondition("user", "IN", this.newActiveUserList);
      }
      if (nonSharedRole) {
          var operatorAppFamily = this._getAppFamilyFromLite(appFamily);
          if (operatorAppFamily) {
              if (!this.uniqueUserUsageData[operatorAppFamily].length) {
                  this._getAllDataFromUniqueUserUsageTableForAppFamliy(operatorAppFamily);
              }
              userWithRole.addQuery("user", "NOT IN", this.uniqueUserUsageData[operatorAppFamily]);
          }
          userWithRole.addQuery("user", "NOT IN", this.uniqueUserUsageData[appFamily]);
      }

      userWithRole.orderBy("user");
      userWithRole.query();

      var batchUsers = [];
      var count = 0;
      while (userWithRole.hasNext()) {
          var batchComplete = true;
          if (count < 10000) {
              batchComplete = false;
              count++;
              userWithRole.next();

              if (batchUsers[batchUsers.length - 1] != userWithRole.getValue("user") && batchUsers.indexOf(userWithRole.getValue("user")) == -1) {
                  batchUsers.push(userWithRole.getValue("user"));
              }
              if (!userWithRole.hasNext() || count >= 10000) {
                  batchComplete = true;
              } else {
                  continue;
              }
          }

          if (batchComplete) {
              count = 0;
              var userNotInUsage = this._getListUsersWhichAreNotPresentInUsageTable(batchUsers, appFamily, nonSharedRole);

              for (var j = 0; j < userNotInUsage.length; j++) {
                  var userRole = "";
                  var userRoleType = "";
                  userRole = this._getRoleForTheUserFromList(userNotInUsage[j], role);
                  userRoleType = this._getLicensingRoleType(userRole);
                  if (userRole && userRoleType) {
                      this.updateUniqueUserTable(userNotInUsage[j], appFamily, userRoleType, userRole);
                  }

              }

              batchUsers = [];

          }
      }
  },

  _getListUsersWhichAreNotPresentInUsageTable: function(usersList, appFamily, nonSharedRole) {

      var userAlreadyInUsage = [];
      var usageData = new GlideRecord("sn_irm_shared_cmn_unique_user_usage");
      usageData.addQuery("user", "IN", usersList);
      usageData.addQuery("accrual_period", this.getCurrentMonthAccrualPeriod());
      if (nonSharedRole) {
          var q = usageData.addQuery("licensable_application_family", appFamily);
          var operatorFamily = this._getAppFamilyFromLite(appFamily);
          if (operatorFamily) {
              q.addOrCondition("licensable_application_family", operatorFamily);
          }
      }
      usageData.query();
      while (usageData.next()) {
          userAlreadyInUsage.push(usageData.getValue("user"));
      }

      if (userAlreadyInUsage.length) {
          var userNotInUsage = [];
          for (var i = 0; i < usersList.length; i++) {
              if (userAlreadyInUsage.indexOf(usersList[i]) == -1) {
                  userNotInUsage.push(usersList[i]);
              }
          }
          return userNotInUsage;
      }

      return usersList;
  },

  //Access or Usage based approach: Update user usage table based on access.
  _getAllDataFromUniqueUserUsageTable: function() {
      this.uniqueUserUsageData["ALL"] = [];
      var appFamily = new GlideRecord("sn_irm_shared_cmn_licensable_application_family");
      appFamily.orderBy("execution_order");
      appFamily.query();

      while (appFamily.next()) {
          this._getAllDataFromUniqueUserUsageTableForAppFamliy(appFamily.getUniqueValue());
      }
  },

  _getAllDataFromUniqueUserUsageTableForAppFamliy: function(appFamily) {
      this.uniqueUserUsageData[appFamily] = this._getAllUserFromUsageTable(appFamily);
      this.uniqueUserUsageData["ALL"].push(this.uniqueUserUsageData[appFamily]);
  },

  // For Shared role appFamily will be empty because we don't wish to count a user with shared role double. A user with Shared role can do operation related to multiple app family but only be license based on application context.  
  _getAllUserFromUsageTable: function(appFamily) {
      var userList = [];
      var user = new GlideRecord("sn_irm_shared_cmn_unique_user_usage");
      user.addQuery("accrual_period", this.getCurrentMonthAccrualPeriod());
      if (appFamily) {
          user.addQuery("licensable_application_family", appFamily);
      }
      user.setLimit(50000);
      user.query();
      while (user.next()) {
          userList.push(user.getValue("user"));
      }
      return userList;
  },

  _hasUniqueUserUsageEntry: function(user, appFamily, accrualPeriod) {
      if (user != "" && appFamily != "" && accrualPeriod != "") {
          var liteAppFamilyName = this._getLiteAppFamily(appFamily);
          var operatorAppFamilyName = this._getAppFamilyFromLite(appFamily);
          var usageEntry = new GlideRecord("sn_irm_shared_cmn_unique_user_usage");
          usageEntry.addQuery("user", user);
          usageEntry.addQuery("accrual_period", accrualPeriod);
          var qc = usageEntry.addQuery("licensable_application_family", appFamily);
          if (liteAppFamilyName) {
              qc.addOrCondition("licensable_application_family", liteAppFamilyName);
          } else if (operatorAppFamilyName) {
              qc.addOrCondition("licensable_application_family", operatorAppFamilyName);
          }
          usageEntry.setLimit(1);
          usageEntry.query();
          return usageEntry.hasNext();
      }
      return false;
  },

  _getUniqueUserUsageEntry: function(user, appFamily, accrualPeriod) {
      if (user != "" && appFamily != "" && accrualPeriod != "") {
          var usageEntry = new GlideRecord("sn_irm_shared_cmn_unique_user_usage");
          usageEntry.addQuery("user", user);
          usageEntry.addQuery("accrual_period", accrualPeriod);
          usageEntry.addQuery("licensable_application_family", appFamily);
          usageEntry.setLimit(1);
          usageEntry.query();
          if (usageEntry.next())
              return usageEntry;
      }
      return "";
  },

  _createUniquerUserUsageRecord: function(user, appFamily, accrualPeriod, reason) {
      try {
          if (!(user == "" || appFamily == "" || accrualPeriod == "")) {
              var usageEntry = new GlideRecord("sn_irm_shared_cmn_unique_user_usage");
              usageEntry.setValue("user", user);
              usageEntry.setValue("licensable_application_family", appFamily);
              usageEntry.setValue("accrual_period", accrualPeriod);
              usageEntry.setValue("reason", reason);
              usageEntry.insert();
          }
      } catch (e) {}
  },

  _getApps: function(appFamily) {
      var apps = [];
      var m2mOfApplicationFamilyApplications = new GlideRecord("sn_irm_shared_cmn_m2m_application_family_application");
      m2mOfApplicationFamilyApplications.addQuery("licensable_application_family", appFamily);
      m2mOfApplicationFamilyApplications.query();

      while (m2mOfApplicationFamilyApplications.next()) {
          var pluginId = m2mOfApplicationFamilyApplications.application_for_licensing.application_name.scope;
          if (!gs.nil(pluginId)) {
              pluginId = pluginId.replace("com.", "");
              apps.push(pluginId);
          }
      }
      return apps;
  },

  _copyPrevDayTableData: function(appFamily) {
      var uaUsageEntry = new GlideRecord("ua_app_usage");
      uaUsageEntry.addQuery("app_id", 'IN', this._getApps(appFamily));
      uaUsageEntry.addQuery("sys_updated_on", ">=", this._calulateLastDay());
      uaUsageEntry.addQuery("user", "NOT IN", this.userExceptionList);
      uaUsageEntry.query();
      while (uaUsageEntry.next()) {
          if (!this._hasUniqueUserUsageEntry(uaUsageEntry.user, appFamily, uaUsageEntry.time_stamp)) {
              // Skip the users having GRC Employee user role
              if (this.userHasThisRoles(uaUsageEntry.user, this.GRC_EMPLOYEE_USER)) {
                  continue;
              }

              var reason = "User performed CRUD operation on GRC tables. For more information, check Usage Data for Applications table.";
              var sharedRoleList = this._getUserSharedRoles(uaUsageEntry.user);
              if (sharedRoleList.length) {
                  var listOfAppFamily = [];
                  var foundSharedAppFamily = false;
                  for (var i = 0; i < sharedRoleList.length; i++) {
                      var roleRecord = new GlideRecord("sys_user_role");
                      roleRecord.get(sharedRoleList[i]);
                      listOfAppFamily = this._getApplicationFamilyFromRole(roleRecord, "");
                      if (JSON.stringify(listOfAppFamily).includes(JSON.stringify(appFamily))) {
                          foundSharedAppFamily = true;
                          break;
                      }
                  }
                  if (!foundSharedAppFamily) {
                      this.addToGRCUsageTable(uaUsageEntry.user, appFamily, uaUsageEntry.time_stamp, reason);
                  }
              } else {
                  this.updateUniqueUserTable(uaUsageEntry.user, appFamily, this.CONST_OPERATOR, reason);
              }
          }
      }
  },

  _getUserSharedRoles: function(user) {
      var allSharedRoles = [];
      var sharedRolesList = [];
      var sharedRoles = new GlideRecord("sn_irm_shared_cmn_licensing_role_type");
      sharedRoles.addQuery("is_shared_role", true);
      sharedRoles.query();
      while (sharedRoles.next()) {
          allSharedRoles.push(sharedRoles.getValue("role"));
      }

      var userHavingRoles = new GlideRecord("sys_user_has_role");
      userHavingRoles.addQuery("user", user);
      userHavingRoles.addQuery("role", "IN", allSharedRoles);
      userHavingRoles.query();
      while (userHavingRoles.next()) {
          sharedRolesList.push(userHavingRoles.getValue("role"));
      }
      return sharedRolesList;
  },

  _getLiteAppFamily: function(appFamily) {
      var gr = new GlideRecord("sn_irm_shared_cmn_app_family_map_lite_app_family");
      gr.addQuery("application_family", appFamily);
      gr.setLimit(1);
      gr.query();
      if (gr.next()) {
          var applicationPlugin = new GlideRecord("sn_irm_shared_cmn_m2m_application_family_application");
          applicationPlugin.addQuery("licensable_application_family", gr.getValue("lite_application_family"));
          applicationPlugin.query();
          if (applicationPlugin.hasNext()) {
              return gr.getValue("lite_application_family");
          }
      }
      return "";
  },

  _getAppFamilyFromLite: function(liteAppFamily) {
      var gr = new GlideRecord("sn_irm_shared_cmn_app_family_map_lite_app_family");
      gr.addQuery("lite_application_family", liteAppFamily);
      gr.setLimit(1);
      gr.query();
      if (gr.next()) {
          return gr.getValue("application_family");
      }
      return "";
  },

  _checkValidAppFamily: function(appFamily) {
      var gr = new GlideRecord("sn_irm_shared_cmn_licensable_application_family");
      gr.addQuery("sys_id", appFamily);
      gr.setLimit(1);
      gr.query();
      if (gr.next()) {
          return true;
      }
      return false;
  },

  type: 'LicenseUtils'
};

Sys ID

06d5a867531401101d7dddeeff7b124b

Offical Documentation

Official Docs: