From f5628a9d54812f81e2e38571e38982920afa3126 Mon Sep 17 00:00:00 2001 From: Masroor Ehsan Date: Wed, 8 Jan 2025 17:16:52 +0600 Subject: [PATCH] wip --- app/Models/Study.php | 30 ++++++++++++++++++------------ app/Services/Workflow/Manager.php | 17 ++++++----------- app/helpers.php | 13 +++++++++++++ 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/app/Models/Study.php b/app/Models/Study.php index c8ceb76..b383865 100644 --- a/app/Models/Study.php +++ b/app/Models/Study.php @@ -149,7 +149,7 @@ public function getReportStatusLedAttribute(): string public function getArchiveLink(): ?string { - if (auth()->user()->may(Permission::StudyDownload)) { + if (me_or_other()->may(Permission::StudyDownload)) { return PacsUrlGen::archive($this->study_instance_uid); } @@ -158,7 +158,7 @@ public function getArchiveLink(): ?string public function getStoneLink(): ?string { - if (auth()->user()->may(Permission::StudyDownload)) { + if (me_or_other()->may(Permission::StudyDownload)) { return PacsUrlGen::stoneViewer($this->study_instance_uid); } @@ -167,7 +167,7 @@ public function getStoneLink(): ?string public function getOhifLink(): ?string { - if (auth()->user()->may(Permission::StudyDownload)) { + if (me_or_other()->may(Permission::StudyDownload)) { return PacsUrlGen::ohifViewer($this->study_instance_uid); } @@ -176,7 +176,7 @@ public function getOhifLink(): ?string public function getOhifSegmentationLink(): ?string { - if (auth()->user()->may(Permission::StudyDownload)) { + if (me_or_other()->may(Permission::StudyDownload)) { return PacsUrlGen::ohifSegmentation($this->study_instance_uid); } @@ -185,7 +185,7 @@ public function getOhifSegmentationLink(): ?string public function getOhifMprLink(): ?string { - if (auth()->user()->may(Permission::StudyDownload)) { + if (me_or_other()->may(Permission::StudyDownload)) { return PacsUrlGen::ohifViewerMpr($this->study_instance_uid); } @@ -293,11 +293,16 @@ public function isLocked(): bool return $this->locked_at !== null; } - public function lockStudy(?User $user = null): void + public function isUnlocked(): bool + { + return $this->locked_at === null; + } + + public function lockStudy(User|int|null $user = null): void { $this->update( [ - 'locking_physician_id' => $user?->id ?? auth()->id(), + 'locking_physician_id' => me_or_other($user)->id, 'locked_at' => now(), ] ); @@ -368,12 +373,13 @@ protected function casts(): array ]; } - public function isLockedBy(?User $user): bool + public function isLockedBy(User|int|null $user = null): bool { - if ($user === null) { - $user = auth()->user(); - } + return $this->locking_physician_id === me_or_other($user)->id; + } - return $this->locking_physician_id === $user->id; + public function isAssignedTo(User|int|null $user = null): bool + { + return $this->assigned_physician_id === me_or_other($user)->id; } } diff --git a/app/Services/Workflow/Manager.php b/app/Services/Workflow/Manager.php index 8fbd994..cb2c276 100644 --- a/app/Services/Workflow/Manager.php +++ b/app/Services/Workflow/Manager.php @@ -21,13 +21,7 @@ public static function of(Study $study): self public function by(User|int|null $user): self { - if (is_int($user)) { - $this->user = User::find($user); - } elseif ($user instanceof User) { - $this->user = $user; - } else { - $this->user = auth()->user(); - } + $this->user = me_or_other($user); return $this; } @@ -68,16 +62,17 @@ public function canReportDownload(): bool public function canReportEdit(): bool { - return $this->activeStudy(fn () => $this->study->isLocked() === false && + return $this->activeStudy(fn () => $this->study->isUnlocked() && $this->study->report_status <= ReportStatus::Preliminary && - $this->study->assigned_physician_id === $this->user->id + $this->study->isAssignedTo($this->user) ); } public function canUnlockReport(): bool { - return $this->activeStudy(fn () => $this->study->isLocked() - && $this->study->locking_physician_id === $this->user->id); + return $this->activeStudy(fn () => $this->study->isLocked() && + $this->study->isLockedBy($this->user) + ); } private function checkUserPermission(Permission $permission): bool diff --git a/app/helpers.php b/app/helpers.php index ad826bc..b22903e 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -111,3 +111,16 @@ function thumb_url(Media $media): string return $media->getUrl('tn') ?? asset('imgs/pdf.png'); } } + +if (! function_exists('me_or_other')) { + function me_or_other(User|int|null $user = null): User + { + if (is_int($user) && $user > 0) { + return User::find($user); + } elseif ($user instanceof User) { + return $user; + } else { + return auth()->user(); + } + } +}