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-4051: Allow Own Approver And Admin Cancel Own Leave Request Even When Absence Type Does Not Allow It #2805

Merged

Conversation

tunbola
Copy link
Contributor

@tunbola tunbola commented Jul 31, 2018

Overview

There are Absence type settings that determine whether a Leave request for that absence type can be cancelled or not. Currently the Admin and Leave manager can cancel leave requests irrespective of whether the Absence type allows it or not, however they cannot do so for their own requests. Other staff not Admin or Leave manager are bound by the Can staff cancel requests for this leave type after they have been made? setting of the Absence Type.
This PR makes the changes needed to allow Admins and Leave managers who are their own leave approvers cancel own requests irrespective of whether the absence type allows it or not.

Before

  • Leave Managers who are own leave approvers and Admins cannot cancel their own requests when absence type does not allow it

After

  • Leave Managers who are own leave approvers and Admins can now cancel their own requests irrespective of whether the absence type allows it or not.

Technical Details

  • The validateAbsenceTypeAllowRequestCancellationForLeaveRequestCancellation method of the LeaveRequest BAO is what determines who can cancel a leave request based on the Absence type allow_request_cancelation value. Since the checks will now involve roles and relationships, This method was removed and a new method that does similarly, canCancelForAbsenceType was added in the LeaveRequestRights service because this service allows checks for user roles and relationships.
 public function canCancelForAbsenceType($absenceTypeId, $contactId, DateTime $leaveFromDate) {
    if ($this->currentUserIsAdmin() || $this->currentUserIsLeaveManagerOf($contactId)) {
      return TRUE;
    }

    $absenceType = AbsenceType::findById($absenceTypeId);
    if ($absenceType->allow_request_cancelation == AbsenceType::REQUEST_CANCELATION_ALWAYS) {
      return TRUE;
    }

    $today = new DateTime('today');

    if ($absenceType->allow_request_cancelation != AbsenceType::REQUEST_CANCELATION_NO &&
      $leaveFromDate > $today
    ) {
      return TRUE;
    }

    return FALSE;
  }

Copy link
Contributor

@mickadoo mickadoo left a comment

Choose a reason for hiding this comment

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

Mostly formatting stuff

$isCancelledStatus = $this->isCancelledStatus($params);

if ($isCancelledStatus) {
if(!$this->canCancelForAbsenceType($params)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Space after if


$today = new DateTime('today');

if ($absenceType->allow_request_cancelation != AbsenceType::REQUEST_CANCELATION_NO &&
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a bit long, and also a bit confusing to read, do you think you can make

$absenceType->allow_request_cancelation != AbsenceType::REQUEST_CANCELATION_NO into a descriptive variable?

}

public function testCanCancelForAbsenceTypeReturnsTrueWhenAbsenceTypeAllowsCancellationForStaff() {
$absenceType = AbsenceTypeFabricator::fabricate(['allow_request_cancelation' => AbsenceType::REQUEST_CANCELATION_ALWAYS]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Indentation is a bit strange, maybe you could put the array on the next line?

@@ -499,6 +500,18 @@ private function getLeaveRequestService($isAdmin = false, $isManager = false, $a
);
}

private function getLeaveRequestServiceForWhenAbsenceTypeCannotBeCancelled($typeId, $contactID, $leaveFromDate) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe put the arguments on separate lines since this is very long?

private function getLeaveRequestServiceForWhenAbsenceTypeCannotBeCancelled($typeId, $contactID, $leaveFromDate) {
$leaveRightsService = $this->prophesize(LeaveRequestRightsService::class);
$leaveRightsService->canCreateAndUpdateFor($contactID)->willReturn(TRUE);
$leaveRightsService->canCancelForAbsenceType($typeId, $contactID, new DateTime($leaveFromDate))->willReturn(FALSE);
Copy link
Contributor

Choose a reason for hiding this comment

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

Long line, maybe split?

@@ -1116,7 +1129,8 @@ public function testGetBreakdownIncludeOnlyTheLeaveBalanceChangesOfTheLeaveReque
public function testToilRequestWithPastDatesCanNotBeCancelledWhenUserIsLeaveContactAndAbsenceTypeDoesNotAllowPastAccrual() {
$absenceType = AbsenceTypeFabricator::fabricate([
'allow_accruals_request' => true,
'allow_accrue_in_the_past' => false
'allow_accrue_in_the_past' => false,
Copy link
Contributor

Choose a reason for hiding this comment

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

boolean case (and more in this file) - I think maybe we skipped this before but it should be fixed for all bools in this file at some point

$leaveRequestService = $this->getLeaveRequestServiceForWhenAbsenceTypeCannotBeCancelled(
$params['type_id'], $params['contact_id'], $params['from_date']);

$this->setExpectedException(RuntimeException::class, 'You cannot cancel leave requests for this Absence type');
Copy link
Contributor

Choose a reason for hiding this comment

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

Long line, and above on 1264

if ($absenceType->allow_request_cancelation != AbsenceType::REQUEST_CANCELATION_NO &&
$leaveFromDate > $today
) {
$absenceTypeRequestCancellationNotNo =
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry to be picky about this but NotNo seems like it's possible to be improved, would it be possible to name this something like absenceTypeAllowsCancellation?

@tunbola tunbola force-pushed the PCHR-4051-allow-own-approver-cancel branch from 85d88bf to 38f1808 Compare July 31, 2018 16:03
Copy link
Contributor

@mickadoo mickadoo left a comment

Choose a reason for hiding this comment

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

Looks good to me 👍

@tunbola tunbola merged commit da99782 into PCHR-3997-allow-own-leave-approver Jul 31, 2018
@tunbola tunbola deleted the PCHR-4051-allow-own-approver-cancel branch July 31, 2018 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants