Name

sn_entitlement.UserHasLicenseInMemoryCache

Description

No description available

Script

var UserHasLicenseInMemoryCache = Class.create();
UserHasLicenseInMemoryCache.prototype = {
  /**
   * Create an in-memory cache of the data in license_subscribed_users
   */
  initialize: function() {
      this._membershipInMemoryCache = this._constructInMemoryCache();
  },
  /**
   * Retrieve the license ids assigned to the user
   *
   * @param userId The sys_user.sys_id to retrieve license ids for
   * @returns {string[]} An array of license_detail.sys_id values
   */
  getLicenseIdsByUserId: function(userId) {
      const vals = this._membershipInMemoryCache.getValsById(userId);
      return vals ? vals : [];
  },
  /**
   * Setup the in-memory cache to hold the appropriate data
   *
   * @returns {sn_entitlement.MembershipInMemoryCache} A configured in-memory cache object
   * @private
   */
  _constructInMemoryCache: function() {
      const licenseCount = this._getLicenseCount();
      const keyName = 'user';
      const valName = 'license';
      const gr = new GlideRecord('sys_user_has_license');
      this._addUnexpiredLicenceQueryCondition(gr, 'license');
      gr.addQuery('license.license_type', 0); // per-user
      gr.query();
      return new sn_entitlement.MembershipInMemoryCache(gr, keyName, valName, licenseCount);
  },

  _getLicenseCount: function() {
      const ga = new GlideAggregate('license_details');
      this._addUnexpiredLicenceQueryCondition(ga);
      ga.addQuery('license_type', 0); // per user
      ga.addAggregate('COUNT');
      ga.query();
      ga.next();
      return parseInt(ga.getAggregate('COUNT'));
  },
  /**
   * Adds query conditions for the license_details record to filter out
   * licenses that are not active
   *
   * @param {GlideRecord} gr The GlideRecord object to add the conditions to
   * @param {string} prefix Optional value to prefix the column names by (e.g. for dot walking)
   * @returns {array} An array of license_details.sys_id values the group is subscribed to
   */
  _addUnexpiredLicenceQueryCondition: function(gr, prefix = '') {
      prefix = prefix && prefix.length > 0 ?
          prefix + '.' :
          '';

      const now = new GlideDate().getValue();

      gr.addQuery(`${prefix}expired`, false);
      gr.addQuery(`${prefix}start_date`, '<=', now);
      gr.addQuery(`${prefix}end_date`, '>=', now);
  },

  type: 'UserHasLicenseInMemoryCache'
};

Sys ID

1f4de336ffc32110468365d7d3b8fe49

Offical Documentation

Official Docs: