Name
sn_entitlement.SubscriptionEntitlementDao
Description
No description available
Script
var SubscriptionEntitlementDao = Class.create();
SubscriptionEntitlementDao.prototype = {
METER_TYPE_MODULE: 'module',
initialize: function() {},
/**
* Retrieves all of the active subscriptionIds (subscription_entitlement.sys_id)
* That are of type Per-User
*
* @returns {array} An array of subscription_entitlement.sys_id values
*/
getSubscriptionIdsByIsPerUser: function() {
const now = new GlideDate().getValue();
// Note: start_date & end_date values in the sample data are null. It is unclear if this will be the case
// in production, but adding support for it due to the test data.
const gr = new GlideRecord('subscription_entitlement');
gr.addQuery('status', 0); // Active
gr.addQuery('start_date', '<=', now)
.addOrCondition('start_date', 'NULL');
gr.addQuery('end_date', '>=', now)
.addOrCondition('end_date', 'NULL');
gr.addQuery('subscription_type', 0); // Per-User
gr.query();
const ids = [];
while (gr.next())
ids.push(gr.getUniqueValue());
return ids;
},
/**
* Retrieves the subscriptionId associated with the subscription detail record
*
* @param {guid} subscriptionDetailId The subscription_detail.sys_id of the record to query
* @returns {guid} The subscription_entitlement.sys_id related to the record
*/
getSubscriptionIdBySubscriptionDetailId: function(subscriptionDetailId) {
const gr = new GlideRecord('subscription_detail');
return gr.get(subscriptionDetailId) ?
String(gr.getValue('subscription')) :
null;
},
/**
* Retrieves the subscriptionIds (subscription_entitlement.sys_id) belonging to a user that are
* per-user type
*
* @param {guid} userId The sys_user.sys_id for the user to retrieve subscription information for
* @returns {array} An array of subscription_entitlement.sys_id values the user is subscribed to
*/
getSubscriptionIdsByUserIdAndIsPerUser: function(userId) {
const now = new GlideDate().getValue();
// Note: start_date & end_date values in the sample data are null. It is unclear if this will be the case
// in production, but adding support for it due to the test data.
const gr = new GlideRecord('user_has_subscription');
gr.addQuery('subscription.status', 0); // Active
gr.addQuery('subscription.start_date', '<=', now)
.addOrCondition('subscription.start_date', 'NULL');
gr.addQuery('subscription.end_date', '>=', now)
.addOrCondition('subscription.end_date', 'NULL');
gr.addQuery('user.sys_id', userId); // sys_user.sys_id
gr.addQuery('subscription.subscription_type', 0); // Per-User
gr.query();
const ids = new Set();
while (gr.next())
ids.add(String(gr.subscription.sys_id));
return Array.from(ids);
},
/**
* Retrieves the subscription_entitlement.sys_id belonging to the group
* and are per-user
*
* @param {guid} groupId The sys_user_group.sys_id for the group to retrieve subscription information for
* @returns {array} An array of subscription_entitlement.sys_id values the group is subscribed to
*/
getSubscriptionIdsByGroupIdAndIsPerUser: function(groupId) {
const now = new GlideDate().getValue();
// Note: start_date & end_date values in the sample data are null. It is unclear if this will be the case
// in production, but adding support for it due to the test data.
const gr = new GlideRecord('group_has_subscription');
gr.addQuery('group.sys_id', groupId);
gr.addQuery('subscription.status', 0); // Active
gr.addQuery('subscription.start_date', '<=', now)
.addOrCondition('subscription.start_date', 'NULL');
gr.addQuery('subscription.end_date', '>=', now)
.addOrCondition('subscription.end_date', 'NULL');
gr.addQuery('subscription.subscription_type', 0); // Per-User
gr.query();
const ids = new Set();
while (gr.next())
ids.add(String(gr.subscription.sys_id));
return Array.from(ids);
},
/**
* Retrieves the subscriptionIds (subscription_entitlement.sys_id) belonging all active modules
* that do not have a meter
*
* @returns {array} An array of subscription_entitlement.sys_id values the user is subscribed to
*/
getSubscriptionIdsByIsModuleWithoutCapacityMeter: function() {
const now = new GlideDate().getValue();
// Note: start_date & end_date values in the sample data are null. It is unclear if this will be the case
// in production, but adding support for it due to the test data.
const gr = new GlideRecord('subscription_entitlement');
gr.addQuery('status', 0); // Active
gr.addQuery('start_date', '<=', now)
.addOrCondition('start_date', 'NULL');
gr.addQuery('end_date', '>=', now)
.addOrCondition('end_date', 'NULL');
gr.addQuery('meter_type', this.METER_TYPE_MODULE);
gr.addNullQuery('definition_id')
.addOrCondition('definition_id', '');
gr.query();
const ids = new Set();
while (gr.next())
ids.add(gr.getUniqueValue());
return Array.from(ids);
},
/**
* Retrieves all of the active subscription ids that have a subscription type
* of unrestricted user.
*
* @returns {array} An array of subscription IDs
*/
getSubscriptionIdsByActiveAndIsUnrestrictedUser: function() {
const now = new GlideDate().getValue();
const gr = new GlideRecord('subscription_entitlement');
gr.addQuery('status', 0); // Active
gr.addQuery('start_date', '<=', now)
.addOrCondition('start_date', 'NULL');
gr.addQuery('end_date', '>=', now)
.addOrCondition('end_date', 'NULL');
gr.addQuery('subscription_type', 2); // 2 = Unrestricted User
gr.query();
const ids = [];
while (gr.next())
ids.push(gr.getUniqueValue());
return ids;
},
/**
* Return information about active subscriptions which contain specified app bundles
* @param {array} appBundles names of app bundles
* @return {array} an array of SubscriptionEntitlementData
*/
getSubscriptionEtitlementByContainsAppBundles: function(appBundles) {
const gr = new GlideRecord('sku_metadata');
gr.addQuery('subscription.status', 0);
// look for app_bundle in column names
gr.addQuery('schema.column_names', 'LIKE', 'app_bundle');
// and look for app bundle values in column_values
// note that a simple contains is used instead of matching a value at specific position
// but this should be good enough given the columns are pretty different (VALIDATE)
const condition = gr.addQuery('column_values', 'LIKE', appBundles[0]);
for (let i = 1 ; i < appBundles.length; ++i) {
condition.addOrCondition('column_values', 'LIKE', appBundles[i]);
}
gr.query();
return this._distinct(this._collectRecords(gr, 'subscription'));
},
_collectRecords: function(gr, columnName) {
var result = [];
while (gr.next()) {
const record = columnName ? gr[columnName].getRefRecord() : gr;
result.push(this._mapToObject(gr, record));
}
return result;
},
_mapToObject: function(parent_record, record) {
return SubscriptionEntitlementData.fromRecord(
record.getUniqueValue(),
record.name.toString(),
record.start_date.toString(),
record.end_date.toString(),
record.purchased_count.toString() ,
parent_record.allocated_quota_count.toString()
);
},
_distinct: function(licenseDetails) {
const map = new Map();
licenseDetails.forEach(obj => map.set(obj.id, obj));
return Array.from(map.values());
},
type: 'SubscriptionEntitlementDao'
};
Sys ID
83186870ff0d6110468365d7d3b8fe0d