Skip to content

Commit

Permalink
reset later-week approvals when resetting one weeks approval
Browse files Browse the repository at this point in the history
  • Loading branch information
KatjaGlassConsulting committed Aug 22, 2022
1 parent 81d4d3f commit 133fd88
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

## 0.9 to 0.9.1

- enhance tooltips
- enhance tooltips
- when a week is set to no approval, all later weeks which had been submitted or approved are reset to open
16 changes: 15 additions & 1 deletion Controller/ApprovalController.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ public function notApprovedAction(Request $request, string $approveId): Redirect
$approval
);
if ($approval) {
// set all approvals + following approvals to NOT_SUBMITTED
$this->resetAllLaterApprovals($this->approvalRepository->findAllLaterApprovals($approveId));
// set current approvals to NOT_SUBMITTED
$approval = $this->createNewApproveHistory($approveId, ApprovalStatus::NOT_SUBMITTED);
// update lockdown period
$this->lockdownRepository->updateLockWeek($approval, $this->approvalRepository);
}

Expand Down Expand Up @@ -176,6 +180,10 @@ public function deniedAction(Request $request, string $approveId): RedirectRespo
$this->approvalRepository->getUrl($approval->getUser()->getId(), $approval->getStartDate()->format('Y-m-d'))
);
$this->createNewApproveHistory($approveId, ApprovalStatus::NOT_SUBMITTED, '', (new DateTime())->modify('+2 second')->format('d.m.Y H:i:s'));

// set all approvals + following approvals to NOT_SUBMITTED
$this->resetAllLaterApprovals($this->approvalRepository->findAllLaterApprovals($approveId));

$this->lockdownRepository->updateLockWeek($approval, $this->approvalRepository);
}

Expand All @@ -185,6 +193,12 @@ public function deniedAction(Request $request, string $approveId): RedirectRespo
]));
}

private function resetAllLaterApprovals($approvalIdArray){
foreach ($approvalIdArray as $approvalId){
$this->createNewApproveHistory($approvalId, ApprovalStatus::NOT_SUBMITTED, 'Reset due to earlier approval cancellation');
}
}

/**
* @throws Exception
*/
Expand All @@ -208,7 +222,7 @@ private function createAddToApproveForm($userId, $date): ?Approval
* @throws Exception
*/
private function createNewApproveHistory(string $approveId, string $status, string $message = null, string $dateTime = 'now'): ?Approval
{
{
if ($approveId > 0) {
$dateTime = new DateTime($dateTime);
$approve = $this->approvalRepository->find($approveId);
Expand Down
4 changes: 2 additions & 2 deletions Entity/ApprovalStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*/
class ApprovalStatus
{
public const SUBMITTED = 'submitted'; //WRONG - only for migration
public const GRANTED = 'granted';
public const SUBMITTED = 'submitted';
public const GRANTED = 'granted'; //WRONG - only for migration
public const DENIED = 'denied';
public const APPROVED = 'approved';
public const NOT_SUBMITTED = 'not_submitted';
Expand Down
52 changes: 52 additions & 0 deletions Repository/ApprovalRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,29 @@ public function getNewestPerUser(?array $array): ?array
return $arrayToReturn;
}

/* returns an array of approval IDs */
public function findAllLaterApprovals(string $approveId)
{
$originApproval = $this->find($approveId);
$user = $originApproval->getUser();
$userId = $originApproval->getUser()->getId();
$date = $originApproval->getEndDate();

$allRows = $this->findAllWeek([$user]);
$allRows = $this->filterWeeksApprovedOrSubmitted($allRows);
$allRows = $this->filterWeeksLaterThan($allRows, $date->format('Y-m-d'));

$result = [];
foreach ($allRows as $week){
$approvalId = end($this->findHistoryForUserAndWeek($userId, $week['startDate']))->getId();
if ($approvalId){
array_push($result, $approvalId);
}
}

return $result;
}

public function findCurrentWeekToApprove(array $users, UserInterface $currentUser): int
{
$usersId = array_map(function ($user) {
Expand Down Expand Up @@ -487,6 +510,35 @@ function ($response, $approve) {
);
}

public function filterWeeksApprovedOrSubmitted($parseToViewArray): array
{
return array_reduce(
$parseToViewArray,
function ($response, $approve) {
if (\in_array($approve['status'], [ApprovalStatus::APPROVED, ApprovalStatus::SUBMITTED])) {
$response[] = $approve;
}

return $response;
},
[]
);
}

public function filterWeeksLaterThan($rowArray, $dateString): array
{
return array_reduce(
$rowArray,
function ($response, $item) use ($dateString) {
if ($item['startDate'] > $dateString) {
$response[] = $item;
}
return $response;
},
[]
);
}

private function getWeekUserList($month): ?array
{
$week = $this->findBy(['startDate' => $month], ['startDate' => 'ASC', 'user' => 'ASC']);
Expand Down

0 comments on commit 133fd88

Please sign in to comment.