Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PCHR-4019: Allow self leave approver to select leave request status on My Leave in the Leave Request Modal and delete a leave request #2794

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we now don't use Session, can you please remove any reference to it from this file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... I wonder why linter did not notice this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The linter doesn't care if you don't use one of the parameters in your function. I guess it does that in case you need to use the third parameter, but not the second one.

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