From ca3dfdf4f629b0d124643e3a6155ce6ff3a58cbe Mon Sep 17 00:00:00 2001 From: Sagar Date: Thu, 10 Oct 2024 16:49:59 +0200 Subject: [PATCH] fix: various fixes to the reporting workflow --- .../Dashboard/Resources/MoleculeResource.php | 19 ++--- .../Dashboard/Resources/ReportResource.php | 76 +++++++++++-------- .../ReportResource/Pages/CreateReport.php | 8 +- app/Helper.php | 9 +++ 4 files changed, 63 insertions(+), 49 deletions(-) diff --git a/app/Filament/Dashboard/Resources/MoleculeResource.php b/app/Filament/Dashboard/Resources/MoleculeResource.php index 79ed12c5..52aed617 100644 --- a/app/Filament/Dashboard/Resources/MoleculeResource.php +++ b/app/Filament/Dashboard/Resources/MoleculeResource.php @@ -125,7 +125,9 @@ public static function table(Table $table): Table ]) ->action(function (array $data, Molecule $record): void { $record->active = ! $record->active; - self::saveComment($record, $data['reason']); + $record->active ? $record->status = 'APPROVED' : $record->status = 'REVOKED'; + $record->comment = prepareComment($data['reason']); + $record->save(); }) ->modalHidden(function (Molecule $record) { return ! $record['active']; @@ -144,7 +146,9 @@ public static function table(Table $table): Table ->action(function (array $data, Collection $records): void { foreach ($records as $record) { $record->active = ! $record->active; - self::saveComment($record, $data['reason']); + $record->active ? $record->status = 'APPROVED' : $record->status = 'REVOKED'; + $record->comment = prepareComment($data['reason']); + $record->save(); } }) // ->modalHidden(function (Molecule $record) { @@ -207,15 +211,4 @@ public static function getNavigationBadge(): ?string { return Cache::get('stats.molecules'); } - - public static function saveComment($record, $reason) - { - $record->comment = [[ - 'timestamp' => now(), - 'changed_by' => auth()->user()->id, - 'comment' => $reason, - ]]; - - $record->save(); - } } diff --git a/app/Filament/Dashboard/Resources/ReportResource.php b/app/Filament/Dashboard/Resources/ReportResource.php index 5fd48424..7a065ade 100644 --- a/app/Filament/Dashboard/Resources/ReportResource.php +++ b/app/Filament/Dashboard/Resources/ReportResource.php @@ -79,11 +79,15 @@ public static function form(Form $form): Form Actions::make([ Action::make('approve') ->form(function ($record, $livewire) { - self::$approved_changes = self::prepareApprovedChanges($record, $livewire); - $key_value_fields = getChangesToDisplayModal(self::$approved_changes); - array_unshift($key_value_fields, Textarea::make('reason')); - - return $key_value_fields; + if ($record['is_change']) { + self::$approved_changes = self::prepareApprovedChanges($record, $livewire); + $key_value_fields = getChangesToDisplayModal(self::$approved_changes); + array_unshift($key_value_fields, Textarea::make('reason')); + + return $key_value_fields; + } else { + return [Textarea::make('reason')]; + } }) ->hidden(function (Get $get, string $operation) { return ! auth()->user()->roles()->exists() || $get('status') == 'rejected' || $get('status') == 'approved' || $operation != 'edit'; @@ -107,8 +111,14 @@ public static function form(Form $form): Form Action::make('viewCompoundPage') ->color('info') ->url(fn (string $operation, $record): string => $operation === 'create' ? env('APP_URL').'/compounds/'.request()->compound_id : env('APP_URL').'/compounds/'.$record->mol_id_csv) - ->openUrlInNewTab(), + ->openUrlInNewTab() + ->hidden(function (Get $get, string $operation) { + return ! request()->has('type'); + }), ]) + ->hidden(function (Get $get) { + return $get('report_type') != 'molecule'; + }) ->verticalAlignment(VerticalAlignment::End) ->columnStart(4), ]) @@ -589,17 +599,6 @@ public static function prepareApprovedChanges(Report $record, $livewire) { $approved_changes = []; - // In case of reporting a synthetic molecule, Deactivate Molecules - if ($record['mol_id_csv'] && ! $record['is_change']) { - $molecule_ids = explode(',', $record['mol_id_csv']); - $molecule = Molecule::whereIn('id', $molecule_ids)->get(); - foreach ($molecule as $mol) { - $mol->active = false; - $mol->save(); - } - } - - // In case of Changes, run SQL queries for the approved changes $approved_changes['mol_id_csv'] = $record['mol_id_csv']; if ($record['is_change']) { @@ -650,22 +649,33 @@ public static function prepareApprovedChanges(Report $record, $livewire) public static function approveReport(array $data, Report $record, Molecule $molecule, $livewire): void { - // Run SQL queries for the approved changes - self::runSQLQueries($record); - - $suggested_changes = $record['suggested_changes']; - - $suggested_changes['curator']['approved_changes'] = self::$overall_changes; - $record['suggested_changes'] = $suggested_changes; - $record['comment'] = $data['reason']; - $record['status'] = 'approved'; - $formData = copyChangesToCuratorJSON($record, $livewire->data); - $suggested_changes['curator'] = $formData['suggested_changes']['curator']; - $record['suggested_changes'] = $suggested_changes; - - // Save the report record in any case - $record->save(); - + // In case of reporting a synthetic molecule, Deactivate Molecules + if ($record['mol_id_csv'] && ! $record['is_change']) { + $molecule_ids = explode(',', $record['mol_id_csv']); + $molecule = Molecule::whereIn('identifier', $molecule_ids)->get(); + foreach ($molecule as $mol) { + $mol->active = false; + $mol->status = 'REVOKED'; + $mol->comment = prepareComment($data['reason']); + $mol->save(); + } + } else { + // In case of Changes + // Run SQL queries for the approved changes + self::runSQLQueries($record); + + $suggested_changes = $record['suggested_changes']; + + $suggested_changes['curator']['approved_changes'] = self::$overall_changes; + $record['suggested_changes'] = $suggested_changes; + $record->comment = prepareComment($data['reason']); + $record['status'] = 'approved'; + $formData = copyChangesToCuratorJSON($record, $livewire->data); + $suggested_changes['curator'] = $formData['suggested_changes']['curator']; + $record['suggested_changes'] = $suggested_changes; + + $record->save(); + } $livewire->redirect(ReportResource::getUrl('view', ['record' => $record->id])); } diff --git a/app/Filament/Dashboard/Resources/ReportResource/Pages/CreateReport.php b/app/Filament/Dashboard/Resources/ReportResource/Pages/CreateReport.php index c23f02c0..d42df368 100644 --- a/app/Filament/Dashboard/Resources/ReportResource/Pages/CreateReport.php +++ b/app/Filament/Dashboard/Resources/ReportResource/Pages/CreateReport.php @@ -43,9 +43,11 @@ protected function afterFill(): void $this->data['is_change'] = true; $this->data['existing_geo_locations'] = $this->molecule->geo_locations->pluck('name')->toArray(); $this->data['existing_synonyms'] = $this->molecule->synonyms; - $this->data['existing_cas'] = array_values($this->molecule->cas); + $this->data['existing_cas'] = array_values($this->molecule->cas ?? []); $this->data['existing_organisms'] = $this->molecule->organisms->pluck('name')->toArray(); $this->data['existing_citations'] = $this->molecule->citations->where('title', '!=', null)->pluck('title')->toArray(); + } else { + $this->data['is_change'] = false; } if ($request->has('collection_uuid')) { @@ -94,8 +96,7 @@ protected function mutateFormDataBeforeCreate(array $data): array { $data['user_id'] = auth()->id(); $data['status'] = 'submitted'; - - if ($data['is_change'] == true) { + if ($data['is_change']) { $suggested_changes = []; $suggested_changes['existing_geo_locations'] = $data['existing_geo_locations']; $suggested_changes['new_geo_locations'] = $data['new_geo_locations']; @@ -153,6 +154,7 @@ protected function getCreateFormAction(): Action if (! $this->data['is_change']) { return parent::getCreateFormAction(); } + return parent::getCreateFormAction() ->submit(null) ->form(function () { diff --git a/app/Helper.php b/app/Helper.php index 36ab3aae..47688cc8 100644 --- a/app/Helper.php +++ b/app/Helper.php @@ -410,3 +410,12 @@ function copyChangesToCuratorJSON($record, $data) return $data; } + +function prepareComment($reason) +{ + return [[ + 'timestamp' => now(), + 'changed_by' => auth()->user()->id, + 'comment' => $reason, + ]]; +}