Last active
September 21, 2015 19:06
-
-
Save jmbauguess/29c934455d900d239d15 to your computer and use it in GitHub Desktop.
Checks to see if a user has already had a chance to approve a record
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @description Looks at other approvals to determine if one needs to be generated | |
* @type {Class} | |
*/ | |
var ApproveAhead = Class.create(); | |
ApproveAhead.prototype = { | |
/** | |
* @description The name of the approval table | |
* @type {String} | |
*/ | |
APPROVER_TABLE : 'sysapproval_approver', | |
/** | |
* @description Given a record and an approver, determines if that approver already had a chance to approve | |
* @param {String} approver An approver's sys_id | |
* @param {String} record A record's sys_id | |
* @return {Boolean} True if the user had a chance to approve; false if they need a chance to approve | |
*/ | |
didApproverAlreadyGo: function(approver, record) { | |
var approvers = new GlideRecord(this.APPROVER_TABLE); | |
approvers.addEncodedQuery('approver=' + approver + "^sysapproval=" + | |
record + "^state=approved^ORstate=not_required"); | |
approvers.query(); | |
if (approvers.next()) { | |
return true; | |
} | |
return false; | |
}, | |
'type' : 'ApproveAhead' | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Approval Table | |
function onBefore(current, previous) { | |
if (!current.sysapproval.parent.nil()) { | |
current.setAbortAction(new ApproveAhead().didApproverAlreadyGo(current.approver, current.sysapproval.parent)); | |
new Workflow().runFlows(getRecord(current.sysapproval), 'update'); | |
return; | |
} | |
current.setAbortAction(new ApproveAhead().didApproverAlreadyGo(current.approver, current.sysapproval)); | |
new Workflow().runFlows(getRecord(current.sysapproval), 'update'); | |
function getRecord(sysId) { | |
var gr = new GlideRecord('task'); | |
if (gr.get(sysId)) { | |
return gr; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
var unit = new SNUnit(); | |
var ah = new ApproveAhead(); | |
unit.framework.setTableName('sysapproval_approver'); | |
unit.test("Records with no parent function properly", function() { | |
unit.framework.setFunctionName('didApproverAlreadyGo'); | |
var change = new GlideRecord('change_request'); | |
change.initialize(); | |
change.short_description = 'Testing ApproveAhead'; | |
change.cmdb_ci = 'c097e3ddb419d4001f6b6136d35d2d8d'; | |
change.type = 'Std-Application'; | |
change.u_life_cycle_status = 'Planning in Progress'; | |
var id = change.insert(); | |
change.u_life_cycle_status = 'Scheduled for Approval'; | |
change.approval = 'requested'; | |
change.update(); | |
unit.framework.assertFalse(ah.didApproverAlreadyGo('a93e07e3b4b014001f6b6136d35d2d2f', id), "User hasn't already taken action"); | |
var approval = new GlideRecord('sysapproval_approver'); | |
approval.addQuery('approver', 'a93e07e3b4b014001f6b6136d35d2d2f'); | |
approval.addQuery('sysapproval', id); | |
approval.query(); | |
if (approval.next()) { | |
approval.state = 'approved'; | |
approval.update(); | |
} | |
unit.framework.assertTrue(ah.didApproverAlreadyGo('a93e07e3b4b014001f6b6136d35d2d2f', id), "User has already taken action"); | |
}); | |
unit.test("Records with a parent function propertly", function() { | |
var rt = new RequestTester(); | |
var myVariables = [{'name' : 'Asset', 'value' : 'myCI'}]; | |
var req = rt.createRequestByName('Infrastructure Access Request', myVariables); | |
var ritm = rt.getRequestedItemByRequest(req); | |
rt.makeApprovals(ritm); | |
var change = new GlideRecord('change_request'); | |
change.initialize(); | |
change.parent = ritm.sys_id; | |
change.short_description = 'Testing ApproveAhead with a parent'; | |
change.cmdb_ci = 'c097e3ddb419d4001f6b6136d35d2d8d'; | |
change.type = 'Std-Application'; | |
change.u_life_cycle_status = 'Planning in Progress'; | |
var id = change.insert(); | |
change.u_life_cycle_status = 'Scheduled for Approval'; | |
change.approval = 'requested'; | |
change.update(); | |
unit.framework.assertTrue(ah.didApproverAlreadyGo('a93e07e3b4b014001f6b6136d35d2d2f', change.parent.sys_id), "User has already taken action"); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment