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 cc3e272 commit de59269
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 46 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 @@ -287,15 +287,6 @@ define([
return getLeaveType() !== 'toil' && !vm.request.change_balance;
}

/**
* Checks if the currently logged in user is a leave approver
*
* @return {Promise}
*/
function checkIfContactIsSelfLeaveApprover () {
return loggedInContact.checkIfSelfLeaveApprover();
}

/**
* Closes the error alerts if any
*/
Expand Down Expand Up @@ -616,37 +607,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 loggedInContact.checkIfSelfLeaveApprover()
.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 de59269

Please sign in to comment.