Skip to content

Commit

Permalink
PCHR-4019: Improve the code
Browse files Browse the repository at this point in the history
  • Loading branch information
igorpavlov committed Jul 26, 2018
1 parent 90a8d9e commit 0dab694
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 37 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ define([
.then(initIsSelfRecord)
.then(function () {
return $q.all([
initRoles(),
initRole(),
loadAbsencePeriods(),
loadStatuses()
]);
Expand Down Expand Up @@ -616,37 +616,51 @@ define([
*
* @return {Promise}
*/
function initRoles () {
function initRole () {
role = 'staff';

// If the user is creating or editing their own leave, they will be
// treated as a staff regardless of their actual role.
if (vm.isSelfRecord) {
return checkIfContactIsSelfLeaveApprover()
.then(function (isSelfLeaveApprover) {
if (!isSelfLeaveApprover) {
return;
}

role = 'admin';
vm.isSelfLeaveApprover = true;
vm.canManage = true;
});
}
return (vm.isSelfRecord
? initRoleBasedOnSelfLeaveApproverState()
: initRoleBasedOnPermissions())
.finally(function () {
vm.canManage = vm.isRole('manager') || vm.isRole('admin');
});
}

/**
* Initiates the user role based on their self leave approver state.
* If the user is creating or editing their own leave request, they will be
* treated as an "admin".
*
* @return {Promise}
*/
function initRoleBasedOnSelfLeaveApproverState () {
return checkIfContactIsSelfLeaveApprover()
.then(function (isSelfLeaveApprover) {
if (!isSelfLeaveApprover) {
return;
}

role = 'admin';
vm.isSelfLeaveApprover = true;
});
}

/**
* Initiates user role based on their permissions
*
* @return {Promise}
*/
function initRoleBasedOnPermissions () {
return checkPermissions(sharedSettings.permissions.admin.administer)
.then(function (isAdmin) {
isAdmin && (role = 'admin');
})
.then(function () {
// (role === 'staff') means it is not admin so need to check if manager
return (role === 'staff') && checkPermissions(sharedSettings.permissions.ssp.manage)
.then(function (isManager) {
isManager && (role = 'manager');
});
return (role !== 'admin') && checkPermissions(sharedSettings.permissions.ssp.manage);
})
.finally(function () {
vm.canManage = vm.isRole('manager') || vm.isRole('admin');
.then(function (isManager) {
isManager && (role = 'manager');
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1488,7 +1488,7 @@
expect(controller.request.status_id).toEqual(approvalStatus);
});

it('calls corresponding API end points', function () {
it('calls the corresponding API endpoint', function () {
expect(LeaveRequestAPI.create).toHaveBeenCalled();
});
});
Expand Down

0 comments on commit 0dab694

Please sign in to comment.