Name

sn_entitlement.dao_SubscriptionDetail

Description

No description available

Script

var dao_SubscriptionDetail = Class.create();
dao_SubscriptionDetail.prototype = {
  initialize: function() {
      this._detailTable = "subscription_detail";
      this._entitlementTable = "subscription_entitlement";
      this._isV2Schema = new sn_entitlement.UnallocatedEntityCalculation_ContextFactory().v2SchemaIsInUse();
      this._v1Dao = new sn_entitlement.dao_SubscriptionDetailDaoV1();
  },

  /**
   * Creates a set of subscription_detail records in the database and returns a object map of subscription_entitlement ids to subscription_detail ids
   *
   *
   * @return {object} an object map of subscription_entitlement sysids to subscription_detail sysids
   */
  createSubscriptionDetailsMap: function() {
      if (!this._isV2Schema)
          return this._v1Dao.createSubscriptionDetailsMap();

      let objectMap = {};
      const entitlementGr = new GlideRecord(this._entitlementTable);
      entitlementGr.addQuery('status', 0); // active
      entitlementGr.query();
      while (entitlementGr.next()) {
          objectMap[entitlementGr.getUniqueValue()] = this._insertSubscriptionDetail(entitlementGr);
      }

      return objectMap;
  },

  /**
   * Creates a subscription_details record in the database from a subscription_entitlement record and returns the id of the created record
   *
   * @param {GlideRecord} the GlideRecord of the subscription_entitlement record to use in creating the subscription_detail record
   * @return {string} sysid of the inserted subscription_detail record
   */
  _insertSubscriptionDetail: function(entitlementGr) {
      const detailGr = new GlideRecord(this._detailTable);
      detailGr.setValue('subscription_name', entitlementGr.getValue('name'));
      detailGr.setValue('subscription', entitlementGr.getUniqueValue());
      detailGr.setValue('calculated_on', new GlideDateTime());
      return detailGr.insert();
  },

  /**
   * Marks the set of subscription_detail records referenced in the object map of subscription_entitlement ids to subscription_detail ids
   *  as is_latest and processing_complete both to 'true'
   *
   * @param {object} an object map of subscription_entitlement sysids to subscription_detail sysids
   */
  completeSubscriptionDetails: function(map) {
      const subIds = Object.keys(map);
      this._markOldDetailRecords(subIds);

      const detailGr = new GlideRecord(this._detailTable);
      const detailIds = Object.values(map);
      detailGr.addQuery("sys_id", "IN", detailIds);
      detailGr.query();
      detailGr.setValue('processing_complete', 'true');
      detailGr.setValue('is_latest', 'true');
      detailGr.updateMultiple();
  },

  /**
   * Marks old subscription_details records in the database as is_latest = false
   *
   * @param {array} the list of the subscription_entitlement ids to use in selecting the subscription_detail records to update
   */
  _markOldDetailRecords: function(subIds) {
      if (!this._isV2Schema) {
          this._v1Dao.markOldDetailRecords(subIds);
          return;
      }

      const detailGr = new GlideRecord(this._detailTable);
      detailGr.addQuery("subscription", "IN", subIds)
          .addOrCondition("subscription", 'NULL');

      detailGr.query();
      detailGr.setValue('is_latest', 'false');
      detailGr.updateMultiple();
  },

  /**
   * Updates an existing subscription_details record in the database
   *
   * @param {object} the subscriptionDetail entry to update into the database
   */
  updateRecord: function(detailEntry) {
      const gr = new GlideRecord(this._detailTable);
      if (!gr.get(detailEntry.sysId)) {
          gs.warn(`Unable to find ${this._detailTable} entry with sys_id ${detailEntry.sysId} , aborting update.`);
          return;
      }

      gr.setValue("unconfirmed_user_count", detailEntry.unconfirmedUserCount);
      gr.setValue("allocated_user_count", detailEntry.allocatedUserCount);
      gr.setValue("unconfirmed_group_count", detailEntry.unconfirmedGroupCount);
      gr.setValue("allocated_user_without_licensable_role_count", detailEntry.allocatedUserWithoutLicensableRoleCount);
      gr.setValue("allocated_user_oob_count", detailEntry.allocatedUserOobCount);
      gr.setValue("unconfirmed_user_oob_count", detailEntry.unconfirmedUserOobCount);
      gr.setValue("licensable_group_count", detailEntry.licensableGroupCount);
      gr.setValue("licensable_group_subscribed_count", detailEntry.licensableGroupSubscribedCount);
      gr.setValue("allocated_quota_count", detailEntry.allocatedQuotaCount);
      gr.setValue("allocated_custom_table_count", detailEntry.allocatedCustomTableCount);

      gr.update();
  },

  type: 'dao_SubscriptionDetail'
};

Sys ID

a64f8d1e5395e910d185ddeeff7b1226

Offical Documentation

Official Docs: