From a24f4d03fbaee3c5df86e80251728e1b8d08bb79 Mon Sep 17 00:00:00 2001 From: Masroor Ehsan Date: Tue, 7 Jan 2025 17:53:15 +0600 Subject: [PATCH] locking --- app/Services/Workflow/Manager.php | 45 +++++++++++++++++-------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/app/Services/Workflow/Manager.php b/app/Services/Workflow/Manager.php index 55c5e4f..c548869 100644 --- a/app/Services/Workflow/Manager.php +++ b/app/Services/Workflow/Manager.php @@ -12,6 +12,8 @@ final class Manager { private ?User $user = null; + public function __construct(private readonly Study $study) {} + public static function of(Study $study): self { return new self($study); @@ -30,15 +32,6 @@ public function by(User|int|null $user): self return $this; } - private function checkUserPermission(Permission $permission): bool - { - if (is_null($this->user)) { - $this->user = auth()->user(); - } - - return $this->user->may($permission); - } - public function can(Permission $permission): bool { if (! $this->checkUserPermission($permission)) { @@ -53,15 +46,6 @@ public function can(Permission $permission): bool return $this->{$prop}(); } - private function activeStudy(Closure $fn): bool - { - if (! $this->study->isActive()) { - return false; - } - - return $fn(); - } - public function canAssignPhysician(): bool { return $this->activeStudy(fn () => $this->study->report_status <= ReportStatus::Preliminary); @@ -84,11 +68,32 @@ public function canReportDownload(): bool public function canReportEdit(): bool { - return $this->activeStudy(fn () => $this->study->is_locked === false && + return $this->activeStudy(fn () => $this->study->isLocked() === false && $this->study->report_status <= ReportStatus::Preliminary && $this->study->assigned_physician_id === $this->user->id ); } - public function __construct(private readonly Study $study) {} + public function canUnlockReport(): bool + { + return $this->activeStudy(fn () => $this->study->isLocked() && $this->study->locking_physician_id === $this->user->id); + } + + private function checkUserPermission(Permission $permission): bool + { + if (is_null($this->user)) { + $this->user = auth()->user(); + } + + return $this->user->may($permission); + } + + private function activeStudy(Closure $fn): bool + { + if (! $this->study->isActive()) { + return false; + } + + return $fn(); + } }