Name

sn_entitlement.AllocationDataService

Description

No description available

Script

var AllocationDataService = Class.create();
AllocationDataService.prototype = {
  initialize: function() {
      this._writeUserAllocationDataEnabled = gs.getProperty('glide.entitlement.write_user_allocation_data.enabled', false).toLowerCase() === 'true';
      this._writeGroupAllocationDataEnabled = gs.getProperty('glide.entitlement.write_group_allocation_data.enabled', true).toLowerCase() === 'true';

      this._subscriptionService = new sn_entitlement.SubscriptionService(false);
      this._subscriptionDetailDao = new sn_entitlement.dao_SubscriptionDetail();
      this._userM2mDao = new sn_entitlement.dao_userAllocationDataM2mSubscriptionDetailDao();
      this._userUtil = new sn_entitlement.dao_AllocationDataDaoUtils('user_allocation_data', 'sys_user.sys_id', 'user');
      this._groupM2mDao = new sn_entitlement.dao_groupAllocationDataM2mSubscriptionDetailDao();
      this._groupUtil = new sn_entitlement.dao_AllocationDataDaoUtils('group_allocation_data', 'sys_user_group.sys_id', 'group');

      this._subscriptionStatsAccumulator = new sn_entitlement.SubscriptionStatsAccumulator();
  },

  /**
   * Creates a set of subscription_detail entries, populates their relationships down to the user and and group allocation table
   *
   * @returns {object} A object map of subscrition_entitlements to subscription_detail records created in this operation
   */
  createEntries: function() {
      const users = this._subscriptionService.getUnallocatedUserCalculationsByAllSubscriptions();
      const groups = this._subscriptionService.getUnallocatedGroupCalculationsByAllSubscriptions();
      const map = this._subscriptionDetailDao.createSubscriptionDetailsMap();

      this._subscriptionStatsAccumulator.addSubscriptionIds(Object.keys(map));

      this._persistUserAllocationData(map, users);

      this._persistGroupAllocationData(map, groups);

      return map;
  },

  /**
   * Returns a reference to the SubscriptionStatsAccumulator used when processing the user/group data
   * 
   * @returns {object} Returns a reference to the SubscriptionStatsAccumulator used when processing the user/group data
   */
  getSubscriptionStatsAccumulator: function() {
      return this._subscriptionStatsAccumulator;
  },

  /**
   * Persists an array of user allocation data to the database
   *
   * @param {object} an object map of subscription_entitlement sysids to subscription_detail sysids 
   * @param {stream} allocatedUsers A stream of allocatedUser values to persist to the database
   */
  _persistUserAllocationData: function(map, allocatedUsers) {
      allocatedUsers.forEach((user) => {
          if (this._writeUserAllocationDataEnabled) {
              const userId = this._userUtil.insertAllocatedEntry(user);
              if (user.unallocated_subscription_ids) {
                  user.unallocated_subscription_ids.forEach((subId) => {
                      this._userM2mDao.insertEntry(userId, map[subId], 'false');
                  });
              }
              if (user.allocated_subscription_ids) {
                  user.allocated_subscription_ids.forEach((subId) => {
                      this._userM2mDao.insertEntry(userId, map[subId], 'true');

                  });
              }
          }

          this.getSubscriptionStatsAccumulator().addUserData(user);
      });
  },

  /**
   * Persists an array of group allocation data to the database
   *
   * @param {object} an object map of subscription_entitlement sysids to subscription_detail sysids 
   * @param {stream} allocatedGroups A stream of allocatedGroup values to persist to the database
   */
  _persistGroupAllocationData: function(map, allocatedGroups) {
      allocatedGroups.forEach((group) => {
          if (this._writeGroupAllocationDataEnabled) {
              const groupId = this._groupUtil.insertAllocatedEntry(group);
              if (group.unallocated_subscription_ids) {
                  group.unallocated_subscription_ids.forEach((subId) => {
                      this._groupM2mDao.insertEntry(groupId, map[subId], 'false');
                  });
              }
              if (group.allocated_subscription_ids) {
                  group.allocated_subscription_ids.forEach((subId) => {
                      this._groupM2mDao.insertEntry(groupId, map[subId], 'true');
                  });
              }
          }

          this.getSubscriptionStatsAccumulator().addGroupData(group);
      });
  },

  type: 'AllocationDataService'
};

Sys ID

d12cdda7ffdd2110d185612a453bf1ee

Offical Documentation

Official Docs: