diff --git a/app/Http/Controllers/Staff/StudyHistoryController.php b/app/Http/Controllers/Staff/StudyHistoryController.php index 3bddd58..dae377c 100644 --- a/app/Http/Controllers/Staff/StudyHistoryController.php +++ b/app/Http/Controllers/Staff/StudyHistoryController.php @@ -32,8 +32,11 @@ public function save(StudyHistoryRequest $request) abort_unless(auth()->user()->can(Permission::StudyHistoryEdit) || auth()->user()->isAdmin(), 403); $this->decodeKeys(); $details = StudyDetails::historyOnly($this->key); - $details->update($request->validated()); + $payload = array_trim_strings($request->validated()); + unset($payload['study_id']); + $details->update($payload); - return redirect()->route('staff.history.view', _h($this->key)); + // return redirect()->route('staff.history.view', _h($this->key)); + return redirect()->route('staff.history.view', $details->hash); } } diff --git a/app/Http/Requests/StudyHistoryRequest.php b/app/Http/Requests/StudyHistoryRequest.php index 4962818..fd433ea 100644 --- a/app/Http/Requests/StudyHistoryRequest.php +++ b/app/Http/Requests/StudyHistoryRequest.php @@ -2,6 +2,8 @@ namespace App\Http\Requests; +use App\Models\Study; +use App\Rules\ExistsByHash; use Illuminate\Foundation\Http\FormRequest; class StudyHistoryRequest extends FormRequest @@ -9,7 +11,7 @@ class StudyHistoryRequest extends FormRequest public function rules(): array { return [ - 'study_id' => ['required', 'exists:studies'], + 'study_id' => ['required', new ExistsByHash(Study::class)], 'clinical_history' => ['nullable'], 'surgical_history' => ['nullable'], 'lab_results' => ['nullable'], diff --git a/app/Models/StudyDetails.php b/app/Models/StudyDetails.php index 486ff08..ae965ee 100644 --- a/app/Models/StudyDetails.php +++ b/app/Models/StudyDetails.php @@ -30,11 +30,11 @@ protected function casts(): array public static function historyOnly(int $studyId): self { - return self::where('study_id', $studyId)->first(['id', 'study_id', 'clinical_history', 'surgical_history', 'lab_results', 'clinical_diagnosis']); + return self::where('study_id', $studyId)->firstOrFail(['id', 'study_id', 'clinical_history', 'surgical_history', 'lab_results', 'clinical_diagnosis']); } public static function seriesOnly(int $studyId): self { - return self::where('study_id', $studyId)->first(['id', 'study_id', 'series']); + return self::where('study_id', $studyId)->firstOrFail(['id', 'study_id', 'series']); } } diff --git a/app/Models/Traits/HashableId.php b/app/Models/Traits/HashableId.php index 029772b..07e6aa9 100644 --- a/app/Models/Traits/HashableId.php +++ b/app/Models/Traits/HashableId.php @@ -13,6 +13,11 @@ public static function idToHash(int $key): string return _h($key); } + public static function hashToId(string $hash): int + { + return unhash_it($hash); + } + /** * Get HashId column name. */ diff --git a/app/Rules/ExistsByHashRule.php b/app/Rules/ExistsByHash.php similarity index 85% rename from app/Rules/ExistsByHashRule.php rename to app/Rules/ExistsByHash.php index 9ce52f3..bd37aea 100644 --- a/app/Rules/ExistsByHashRule.php +++ b/app/Rules/ExistsByHash.php @@ -25,16 +25,16 @@ public function __construct(string $class) { $this->model = new $class; - if (! method_exists($this->model, 'bootHashableId')) { + if (! method_exists($this->model, 'getHashAttribute')) { throw new InvalidArgumentException('Class does not use HashableId'); } - parent::__construct($class, $this->model->shouldHashPersist() ? $this->model->getHashColumnName() : $this->model->getKeyName()); + parent::__construct($class, $this->model->getKeyName()); } public function validate(string $attribute, mixed $value, Closure $fail): void { - if (! $value || (! $this->model->shouldHashPersist() && ! $value = $this->model::hashToId($value))) { + if (! $value || (! $value = $this->model::hashToId($value))) { $this->fail($attribute, $fail); return; diff --git a/app/helpers.php b/app/helpers.php index a366c9b..52db5aa 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -31,6 +31,20 @@ function array_purge(array $ary): array } } +if (! function_exists('array_trim_strings')) { + function array_trim_strings(array $ary): array + { + return array_filter($ary, function ($v) { + if (! is_string($v)) { + return $v; + } + $v = trim($v); + + return blank($v) ? null : $v; + }); + } +} + if (! function_exists('sync_agent_id')) { function sync_agent_id(): int { diff --git a/resources/views/staff/history/edit.blade.php b/resources/views/staff/history/edit.blade.php index 793725a..9f8ad0a 100644 --- a/resources/views/staff/history/edit.blade.php +++ b/resources/views/staff/history/edit.blade.php @@ -11,40 +11,34 @@

Clinical Information

- @dd($details) - -
+ @csrf - +
Clinical History
- +
surgical history
- +
lab results
- +
clinical diagnosis
- +