Skip to content

Commit

Permalink
fix: various fixes to the reporting workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
sriramkanakam87 committed Oct 10, 2024
1 parent 658ff2f commit ca3dfdf
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 49 deletions.
19 changes: 6 additions & 13 deletions app/Filament/Dashboard/Resources/MoleculeResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
}
}
76 changes: 43 additions & 33 deletions app/Filament/Dashboard/Resources/ReportResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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),
])
Expand Down Expand Up @@ -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']) {

Expand Down Expand Up @@ -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]));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -153,6 +154,7 @@ protected function getCreateFormAction(): Action
if (! $this->data['is_change']) {
return parent::getCreateFormAction();
}

return parent::getCreateFormAction()
->submit(null)
->form(function () {
Expand Down
9 changes: 9 additions & 0 deletions app/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,12 @@ function copyChangesToCuratorJSON($record, $data)

return $data;
}

function prepareComment($reason)
{
return [[
'timestamp' => now(),
'changed_by' => auth()->user()->id,
'comment' => $reason,
]];
}

0 comments on commit ca3dfdf

Please sign in to comment.