Skip to content

Commit

Permalink
Merge pull request #2794 from compucorp/PCHR-4019-allow-self-approver…
Browse files Browse the repository at this point in the history
…-to-select-lr-status

PCHR-4019: Allow self leave approver to select leave request status on My Leave in the Leave Request Modal and delete a leave request
  • Loading branch information
igorpavlov authored Jul 27, 2018
2 parents b8fe30f + 1438816 commit 7768069
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 68 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 @@ -6,7 +6,6 @@ define([
'common/lodash',
'common/moment',
'common/models/contact',
'common/models/session.model',
'common/services/api/option-group',
'common/services/hr-settings',
'common/services/pub-sub',
Expand All @@ -25,11 +24,11 @@ define([
controllers.controller('RequestCtrl', RequestCtrl);

RequestCtrl.$inject = ['$log', '$q', '$rootScope', '$scope', '$uibModalInstance', 'checkPermissions', 'api.optionGroup',
'dialog', 'pubSub', 'directiveOptions', 'Contact', 'Session', 'AbsencePeriod', 'AbsenceType', 'Entitlement',
'dialog', 'pubSub', 'directiveOptions', 'Contact', 'AbsencePeriod', 'AbsenceType', 'Entitlement',
'LeaveRequest', 'LeaveRequestInstance', 'shared-settings', 'SicknessRequestInstance', 'TOILRequestInstance', 'LeaveRequestService'];

function RequestCtrl ($log, $q, $rootScope, $scope, $modalInstance, checkPermissions, OptionGroup, dialog, pubSub,
directiveOptions, Contact, Session, AbsencePeriod, AbsenceType, Entitlement, LeaveRequest,
directiveOptions, Contact, AbsencePeriod, AbsenceType, Entitlement, LeaveRequest,
LeaveRequestInstance, sharedSettings, SicknessRequestInstance, TOILRequestInstance, LeaveRequestService) {
$log.debug('RequestCtrl');

Expand All @@ -38,7 +37,7 @@ define([
var childComponentsCount = 0;
var initialLeaveRequestAttributes = {}; // used to compare the change in leaverequest in edit mode
var listeners = [];
var loggedInContactId = '';
var loggedInContact;
var NO_ENTITLEMENT_ERROR = 'No entitlement';
var role = '';
var tabs = [];
Expand All @@ -49,6 +48,7 @@ define([
vm.canManage = false; // vm flag is set on initialisation of the controller
vm.contactName = null; // contact name of the owner of leave request
vm.errors = [];
vm.isSelfLeaveApprover = false;
vm.loading = { absenceTypes: true, entitlements: true };
vm.managedContacts = [];
vm.mode = ''; // can be edit, create, view
Expand Down Expand Up @@ -95,15 +95,16 @@ define([
initAvailableStatusesMatrix();
initListeners();

return loadLoggedInContactId()
return loadLoggedInContact()
.then(initIsSelfRecord)
.then(function () {
return $q.all([
initRoles(),
initRole(),
loadAbsencePeriods(),
loadStatuses()
]);
})
.then(initCanManage)
.then(initRequest)
.then(setModalMode)
.then(setInitialAbsencePeriod)
Expand Down Expand Up @@ -248,7 +249,7 @@ define([
* If manager or admin have changed the status through dropdown, assign the same before calling API
*/
function changeStatusBeforeSave () {
if (vm.isSelfRecord) {
if (vm.isSelfRecord && !vm.isSelfLeaveApprover) {
vm.request.status_id = vm.requestStatuses[sharedSettings.statusNames.awaitingApproval].value;
} else if (vm.canManage) {
vm.request.status_id = vm.newStatusOnSave || vm.request.status_id;
Expand Down Expand Up @@ -507,6 +508,13 @@ define([
].concat(defaultStatuses);
}

/**
* Defines if the contact can manage the leave request
*/
function initCanManage () {
vm.canManage = vm.isRole('manager') || vm.isRole('admin');
}

/**
* Initialize contact
*
Expand All @@ -530,7 +538,7 @@ define([
*/
function initIsSelfRecord () {
var isSectionMyLeave = $rootScope.section === 'my-leave';
var isMyOwnRequest = +loggedInContactId === +_.get(vm, 'leaveRequest.contact_id');
var isMyOwnRequest = +loggedInContact.id === +_.get(vm, 'leaveRequest.contact_id');
var isNewRequest = !_.get(vm, 'leaveRequest.id');

vm.isSelfRecord = isSectionMyLeave && (isMyOwnRequest || isNewRequest);
Expand Down Expand Up @@ -594,7 +602,7 @@ define([
if (vm.request) {
attributes = vm.request.attributes();
} else if (!vm.canManage) {
attributes = { contact_id: loggedInContactId };
attributes = { contact_id: loggedInContact.id };
}

return attributes;
Expand All @@ -607,28 +615,48 @@ 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;
}
return (vm.isSelfRecord
? initRoleBasedOnSelfLeaveApproverState()
: initRoleBasedOnPermissions());
}

/**
* 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 Expand Up @@ -767,10 +795,11 @@ define([
*
* @return {Promise}
*/
function loadLoggedInContactId () {
return Session.get().then(function (value) {
loggedInContactId = value.contactId;
});
function loadLoggedInContact () {
return Contact.getLoggedIn()
.then(function (_loggedInContact_) {
loggedInContact = _loggedInContact_;
});
}

/**
Expand All @@ -791,15 +820,12 @@ define([
.then(function (contacts) {
vm.managedContacts = _.remove(contacts.list, function (contact) {
// Removes the admin from the list of contacts
return contact.id !== loggedInContactId;
return contact.id !== loggedInContact.id;
});
});
} else {
// In any other case (including managing)
return Contact.find(loggedInContactId)
.then(function (contact) {
return contact.leaveManagees();
})
return loggedInContact.leaveManagees()
.then(function (contacts) {
vm.managedContacts = contacts;
});
Expand Down
Loading

0 comments on commit 7768069

Please sign in to comment.