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

#444 - overtime application process #447

Merged
merged 18 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 17 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
24 changes: 24 additions & 0 deletions app/Actions/OvertimeRequest/AcceptAsTechnicalAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Toby\Actions\OvertimeRequest;

use Toby\Domain\OvertimeRequestStateManager;
use Toby\Models\OvertimeRequest;
use Toby\Models\User;

class AcceptAsTechnicalAction
{
public function __construct(
protected OvertimeRequestStateManager $stateManager,
protected ApproveAction $approveAction,
) {}

public function execute(OvertimeRequest $overtimeRequest, User $user): void
{
$this->stateManager->acceptAsTechnical($overtimeRequest, $user);

$this->approveAction->execute($overtimeRequest);
}
}
21 changes: 21 additions & 0 deletions app/Actions/OvertimeRequest/ApproveAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Toby\Actions\OvertimeRequest;

use Toby\Domain\OvertimeRequestStateManager;
use Toby\Models\OvertimeRequest;
use Toby\Models\User;

class ApproveAction
{
public function __construct(
protected OvertimeRequestStateManager $stateManager,
) {}

public function execute(OvertimeRequest $overtimeRequest, ?User $user = null): void
{
$this->stateManager->approve($overtimeRequest, $user);
}
}
21 changes: 21 additions & 0 deletions app/Actions/OvertimeRequest/CancelAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Toby\Actions\OvertimeRequest;

use Toby\Domain\OvertimeRequestStateManager;
use Toby\Models\OvertimeRequest;
use Toby\Models\User;

class CancelAction
{
public function __construct(
protected OvertimeRequestStateManager $stateManager,
) {}

public function execute(OvertimeRequest $overtimeRequest, User $user): void
{
$this->stateManager->cancel($overtimeRequest, $user);
}
}
57 changes: 57 additions & 0 deletions app/Actions/OvertimeRequest/CreateAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Toby\Actions\OvertimeRequest;

use Illuminate\Validation\ValidationException;
use Toby\Domain\OvertimeCalculator;
use Toby\Domain\OvertimeRequestStateManager;
use Toby\Models\OvertimeRequest;
use Toby\Models\User;
use Toby\Validation\OvertimeRequestValidator;

class CreateAction
{
public function __construct(
protected OvertimeRequestStateManager $stateManager,
protected WaitForTechApprovalAction $waitForTechApprovalAction,
protected OvertimeCalculator $overtimeCalculator,
protected OvertimeRequestValidator $overtimeRequestValidator,
) {}

/**
* @throws ValidationException
*/
public function execute(array $data, User $creator): OvertimeRequest
{
$overtimeRequest = $this->createVacationRequest($data, $creator);

$this->handleCreatedOvertimeRequest($overtimeRequest);

return $overtimeRequest;
}

/**
* @throws ValidationException
*/
protected function createVacationRequest(array $data, User $creator): OvertimeRequest
{
/** @var OvertimeRequest $overtimeRequest */
$overtimeRequest = $creator->createdOvertimeRequests()->make($data);
$overtimeRequest->hours = $this->overtimeCalculator->calculateHours($overtimeRequest->from, $overtimeRequest->to);

$this->overtimeRequestValidator->validate($overtimeRequest);

$overtimeRequest->save();

$this->stateManager->markAsCreated($overtimeRequest);

return $overtimeRequest;
}

protected function handleCreatedOvertimeRequest(OvertimeRequest $overtimeRequest): void
{
$this->waitForTechApprovalAction->execute($overtimeRequest);
}
}
21 changes: 21 additions & 0 deletions app/Actions/OvertimeRequest/RejectAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Toby\Actions\OvertimeRequest;

use Toby\Domain\OvertimeRequestStateManager;
use Toby\Models\OvertimeRequest;
use Toby\Models\User;

class RejectAction
{
public function __construct(
protected OvertimeRequestStateManager $stateManager,
) {}

public function execute(OvertimeRequest $overtimeRequest, User $user): void
{
$this->stateManager->reject($overtimeRequest, $user);
}
}
21 changes: 21 additions & 0 deletions app/Actions/OvertimeRequest/SettleAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Toby\Actions\OvertimeRequest;

use Toby\Domain\OvertimeRequestStateManager;
use Toby\Models\OvertimeRequest;
use Toby\Models\User;

class SettleAction
{
public function __construct(
protected OvertimeRequestStateManager $stateManager,
) {}

public function execute(OvertimeRequest $overtimeRequest, User $user): void
{
$this->stateManager->settle($overtimeRequest, $user);
}
}
22 changes: 22 additions & 0 deletions app/Actions/OvertimeRequest/WaitForTechApprovalAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Toby\Actions\OvertimeRequest;

use Toby\Actions\VacationRequest\ApproveAction;
use Toby\Domain\OvertimeRequestStateManager;
use Toby\Models\OvertimeRequest;

class WaitForTechApprovalAction
{
public function __construct(
protected OvertimeRequestStateManager $stateManager,
protected ApproveAction $approveAction,
) {}

public function execute(OvertimeRequest $overtimeRequest): void
{
$this->stateManager->waitForTechnical($overtimeRequest);
}
}
18 changes: 18 additions & 0 deletions app/Domain/OvertimeCalculator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Toby\Domain;

use Carbon\Carbon;
use Carbon\CarbonInterface;

class OvertimeCalculator
{
public function calculateHours(CarbonInterface $from, CarbonInterface $to): int
{
$hours = Carbon::create($from)->diffInMinutes($to);

return (int)ceil($hours / 60);
}
}
80 changes: 80 additions & 0 deletions app/Domain/OvertimeRequestStateManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace Toby\Domain;

use Illuminate\Contracts\Events\Dispatcher;
use Toby\Models\OvertimeRequest;
use Toby\Models\User;
use Toby\States\OvertimeRequest\AcceptedByTechnical;
use Toby\States\OvertimeRequest\Approved;
use Toby\States\OvertimeRequest\Cancelled;
use Toby\States\OvertimeRequest\OvertimeRequestState;
use Toby\States\OvertimeRequest\Rejected;
use Toby\States\OvertimeRequest\Settled;
use Toby\States\OvertimeRequest\WaitingForTechnical;

class OvertimeRequestStateManager
{
public function __construct(
protected Dispatcher $dispatcher,
) {}

public function markAsCreated(OvertimeRequest $overtimeRequest): void
{
$this->createActivity($overtimeRequest, null, $overtimeRequest->state, $overtimeRequest->creator);
}

public function approve(OvertimeRequest $overtimeRequest, ?User $user = null): void
{
$this->changeState($overtimeRequest, Approved::class, $user);
}

public function reject(OvertimeRequest $overtimeRequest, User $user): void
{
$this->changeState($overtimeRequest, Rejected::class, $user);
}

public function cancel(OvertimeRequest $overtimeRequest, User $user): void
{
$this->changeState($overtimeRequest, Cancelled::class, $user);
}

public function acceptAsTechnical(OvertimeRequest $overtimeRequest, User $user): void
{
$this->changeState($overtimeRequest, AcceptedByTechnical::class, $user);
}

public function waitForTechnical(OvertimeRequest $overtimeRequest): void
{
$this->changeState($overtimeRequest, WaitingForTechnical::class);
}

public function settle(OvertimeRequest $overtimeRequest, User $user): void
{
$this->changeState($overtimeRequest, Settled::class, $user);
}

protected function changeState(OvertimeRequest $overtimeRequest, string $state, ?User $user = null): void
{
$previousState = $overtimeRequest->state;
$overtimeRequest->state->transitionTo($state);
$overtimeRequest->save();

$this->createActivity($overtimeRequest, $previousState, $overtimeRequest->state, $user);
}

protected function createActivity(
OvertimeRequest $overtimeRequest,
?OvertimeRequestState $from,
OvertimeRequestState $to,
?User $user = null,
): void {
$overtimeRequest->activities()->create([
"from" => $from,
"to" => $to,
"user_id" => $user?->id,
]);
}
}
75 changes: 75 additions & 0 deletions app/Domain/OvertimeRequestStatesRetriever.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace Toby\Domain;

use Toby\Enums\Role;
use Toby\Models\User;
use Toby\States\OvertimeRequest\AcceptedByTechnical;
use Toby\States\OvertimeRequest\Approved;
use Toby\States\OvertimeRequest\Cancelled;
use Toby\States\OvertimeRequest\Created;
use Toby\States\OvertimeRequest\Rejected;
use Toby\States\OvertimeRequest\Settled;
use Toby\States\OvertimeRequest\WaitingForTechnical;

class OvertimeRequestStatesRetriever
{
public static function pendingStates(): array
{
return [
Created::class,
WaitingForTechnical::class,
AcceptedByTechnical::class,
];
}

public static function successStates(): array
{
return [Approved::class];
}

public static function settledStates(): array
{
return [Settled::class];
}

public static function failedStates(): array
{
return [
Rejected::class,
Cancelled::class,
];
}

public static function waitingForUserActionStates(User $user): array
{
return match ($user->role) {
Role::TechnicalApprover, Role::Administrator => [WaitingForTechnical::class],
default => [],
};
}

public static function all(): array
{
return [
...self::pendingStates(),
...self::successStates(),
...self::failedStates(),
...self::settledStates(),
];
}

public static function filterByStatusGroup(string $filter, ?User $user = null): array
{
return match ($filter) {
"pending" => self::pendingStates(),
"success" => self::successStates(),
"failed" => self::failedStates(),
"waiting_for_action" => self::waitingForUserActionStates($user),
"settled" => self::settledStates(),
default => self::all(),
};
}
}
27 changes: 27 additions & 0 deletions app/Enums/SettlementType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Toby\Enums;

enum SettlementType: string
{
case Hours = "hours";
case Money = "money";

public function label(): string
{
return __($this->value);
}

public static function casesToSelect(): array
{
return array_map(
fn(SettlementType $enum): array => [
"label" => $enum->label(),
"value" => $enum->value,
],
SettlementType::cases(),
);
}
}
Loading
Loading