SHARE
TWEET

Untitled

a guest Sep 22nd, 2019 104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var LoanerUtils = Class.create();
  2. LoanerUtils.prototype = {
  3.   /**********
  4.    * Initialization stuff here...
  5.    *
  6.    **********/
  7.   initialize : function() {
  8.     this.overdueRemindTime = gs.getProperty('x_cdltd_loaner_req.overdue.remindtime', 24) * 60 * 60 * 1000;
  9.     this.pickupLeadTime    = gs.getProperty('x_cdltd_loaner_req.pickup.leadtime', 24) * 60 * 60 * 1000;
  10.     this.returnRemindTime  = gs.getProperty('x_cdltd_loaner_req.return.remindtime', 24) * 60 * 60 * 1000;
  11.      
  12.     this.appScopeName = gs.getCurrentScopeName();
  13.     this.loanerTableName = this.appScopeName + '_loaner_request';
  14.     this.pickupEvent = this.appScopeName + '.pickup';
  15.     this.returnEvent = this.appScopeName + '.return';
  16.     this.remindEvent = this.appScopeName + '.reminder';
  17.     this.overdueEvent = this.appScopeName + '.overdue';
  18.   },
  19.  
  20.   /**********
  21.    *
  22.    * getOverdueRequests - retrieve a list of overdue records
  23.    *
  24.    * @param - None
  25.    * @return: array of sys_ids
  26.    *
  27.    **********/
  28.   getOverdueRequests : function() {
  29.  
  30.     var a = [];
  31.     var gr = new GlideRecord(this.loanerTableName);
  32.    
  33.     var rightNow = new GlideDateTime().getDisplayValue();
  34.     gr.addQuery('end_date', '<=', rightNow);
  35.     gr.addQuery('state', 17);
  36.     gr.query();
  37.  
  38.     while (gr.next()) {
  39.       a.push(gr.sys_id.toString());
  40.     }
  41.     return a;
  42.   },
  43.  
  44.   /**********
  45.    *
  46.    * timeToRemindOverdue - Use the passed in ID to check if the record is time to remind again
  47.    *
  48.    * @param - id: sys_id of the loaner record to check
  49.    * @return - boolean: true = time to remind
  50.    *
  51.    **********/
  52.   timeToRemindOverdue : function(id) {
  53.  
  54.     var gr = new GlideRecord(this.loanerTableName);
  55.      
  56.       var noticeTime = new GlideDateTime();
  57.       var tempDate = noticeTime.add(this.overdueRemindTime) + '';
  58.  
  59.     if (!gr.get(id)) {
  60.       gs.log('>>>DEBUG: LoanerUtils: timeToRemindOverdue(): FAILED TO RETRIEVE RECORD');
  61.       return false;
  62.     }
  63.     var baseDate = (gr.last_overdue_reminder == '') ? gr.end_date : gr.last_overdue_reminder;
  64.  
  65.     return (baseDate <= tempDate) ? true : false;
  66.   },
  67.  
  68.   /**********
  69.    *
  70.    * sendOverdueReminder - Queue an event to send a reminder and update the necessary
  71.    *                       record fields to indicate they've been reminded
  72.    *
  73.    * @param - id: sys_id of the loaner record to check
  74.    * @return - None
  75.    *
  76.    **********/
  77.   sendOverdueReminder : function(id) {
  78.  
  79.     var gr = new GlideRecord(this.loanerTableName);
  80.  
  81.     if (!gr.get(id)) {
  82.       gs.log('>>>DEBUG: LoanerUtils: setLastOverdueReminder(): FAILED TO RETRIEVE RECORD');
  83.       return;
  84.     }
  85.     ++gr.overdue_count;
  86.  
  87.     // Record the last time a reminder was sent
  88.     gr.last_overdue_reminder = new GlideDateTime().getDisplayValue();
  89.     gr.update();
  90.  
  91.     // Send the overdue reminder
  92.     gs.eventQueue(this.overdueEvent, gr, gr.start_date, gr.end_date);
  93.   },
  94.  
  95.   /**********
  96.    *
  97.    * getNullPickupReminders - Get a list of records in state 'Ready for Pickup'
  98.    *                          that haven't been reminded before
  99.    *
  100.    * @param - None
  101.    * @return - array of sys_ids
  102.    *
  103.    **********/
  104.   getNullPickupReminders : function() {
  105.  
  106.     var a = [];
  107.     var gr = new GlideRecord(this.loanerTableName);
  108.     var noticeTime = new GlideDateTime();
  109.     noticeTime.add(this.pickupLeadTime);
  110.    
  111.     gr.addQuery('state', 16);
  112.     gr.addQuery('pick_up_reminder', '');
  113.     gr.addQuery('start_date', '<=', noticeTime);
  114.     gr.query();
  115.  
  116.     while (gr.next()) {
  117.       a.push(gr.sys_id.toString());
  118.     }
  119.     return a;
  120.   },
  121.  
  122.   /**********
  123.    *
  124.    * sendPickupReminder - Queue an event to send a reminder and update the pickup reminder field
  125.    *
  126.    * @param - id: sys_id of the loaner record to check
  127.    * @return - None
  128.    *
  129.    **********/
  130.   sendPickupReminder : function(id) {
  131.  
  132.     var gr = new GlideRecord(this.loanerTableName);
  133.  
  134.     if (!gr.get(id)) {
  135.       gs.debug('>>>DEBUG: LoanerUtils: sendPickupReminder(): FAILED TO RETRIEVE RECORD');
  136.       return;
  137.     }
  138.  
  139.     // Log the time the pickup reminder was sent and avoid sending again
  140.     gr.pick_up_reminder = new GlideDateTime().getDisplayValue();
  141.     gr.update();
  142.  
  143.     // Send the pickup reminder
  144.     gs.eventQueue(this.pickupEvent, gr, gr.start_date, gr.end_date);
  145.     return;
  146.   },
  147.  
  148.   /**********
  149.    *
  150.    * getNullReturnReminders - Get a list of records that need reminding to bring their item back
  151.    *
  152.    * @param - None
  153.    * @return - array of sys_ids
  154.    *
  155.    **********/
  156.   getNullReturnReminders : function() {
  157.  
  158.     var a = [];
  159.     var gr = new GlideRecord(this.loanerTableName);
  160.      
  161.     var noticeTime = new GlideDateTime();
  162.     var tempDate = noticeTime.add(this.returnRemindTime) + '';
  163.  
  164.     gr.addQuery('state', 17);
  165.     gr.addQuery('return_reminder', '');
  166.     gr.addQuery('end_date', '<=', tempDate);
  167.     gr.query();
  168.  
  169.     while (gr.next()) {
  170.       a.push(gr.sys_id.toString());
  171.     }
  172.     return a;
  173.   },
  174.  
  175.   /**********
  176.    *
  177.    * setReturnReminder - Queue a return reminder and update field when reminder was sent
  178.    *
  179.    * @param - id: sys_id of the loaner record to check
  180.    * @return - None
  181.    *
  182.    **********/
  183.   sendReturnReminder : function(id) {
  184.  
  185.     var gr = new GlideRecord(this.loanerTableName);
  186.  
  187.     if (!gr.get(id)) {
  188.       gs.debug('>>>DEBUG: LoanerUtils: sendReturnReminder(): FAILED TO RETRIEVE RECORD');
  189.       return;
  190.     }
  191.  
  192.     // Set this record to never remind again
  193.     gr.return_reminder = new GlideDateTime().getDisplayValue();
  194.     gr.update();
  195.  
  196.     // Send the return reminder
  197.     gs.eventQueue(this.returnEvent, gr, gr.start_date, gr.end_date);
  198.   },
  199.  
  200.   type: 'LoanerUtils'
  201. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top