Skip to content

Commit

Permalink
Merge pull request #297 from Steinbeck-Lab/feat-report-suggestions
Browse files Browse the repository at this point in the history
Feat report suggestions - fixes to the workflow
  • Loading branch information
CS76 authored Nov 28, 2024
2 parents 033903a + 13a0997 commit a38339b
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 81 deletions.
17 changes: 1 addition & 16 deletions app/Filament/Dashboard/Resources/MoleculeResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,7 @@ public static function table(Table $table): Table
}),
])
->action(function (array $data, Molecule $record): void {
$record->active = ! $record->active;
$record->active ? $record->status = 'APPROVED' : $record->status = 'REVOKED';
$record->comment = prepareComment($data['reason']);
$record->save();
self::changeMoleculeStatus($record, $data['reason']);
})
->modalHidden(function (Molecule $record) {
return ! $record['active'];
}),
]),

Expand All @@ -146,10 +139,6 @@ public static function table(Table $table): Table
])
->action(function (array $data, Collection $records): void {
foreach ($records as $record) {
$record->active = ! $record->active;
$record->active ? $record->status = 'APPROVED' : $record->status = 'REVOKED';
$record->comment = prepareComment($data['reason']);
$record->save();
self::changeMoleculeStatus($record, $data['reason']);
}
})
Expand Down Expand Up @@ -219,11 +208,7 @@ public static function changeMoleculeStatus($record, $reason)
$record->active = ! $record->active;
$record->active ? $record->status = 'APPROVED' : $record->status = 'REVOKED';

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

$record->save();
}
Expand Down
154 changes: 93 additions & 61 deletions app/Filament/Dashboard/Resources/ReportResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\Fieldset;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\KeyValue;
use Filament\Forms\Components\Radio;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\SpatieTagsInput;
use Filament\Forms\Components\Tabs;
Expand Down Expand Up @@ -72,22 +71,26 @@ public static function form(Form $form): Form
->live()
->default(false)
->options([
false => 'Report Synthetic Compound(s)',
true => 'Request Changes to Data',
false => 'Report',
true => 'Request Changes',
])
->inline()
->hidden(function (string $operation, $get) {
return $operation == 'create' ? $get('type') != null : true;
->hidden(function (string $operation) {
return $operation == 'create';
})
->disabled(function (string $operation) {
return $operation == 'edit';
})
->columnSpan(2),

Actions::make([
Action::make('approve')
->form(function ($record, $livewire) {
->form(function ($record, $livewire, $get) {
if ($record['is_change']) {
self::$approved_changes = self::prepareApprovedChanges($record, $livewire);
$key_value_fields = getChangesToDisplayModal(self::$approved_changes);
array_unshift($key_value_fields,
array_unshift(
$key_value_fields,
Textarea::make('reason')
->helperText(function ($get) {
if (count(self::$approved_changes) <= 1) {
Expand All @@ -101,20 +104,29 @@ public static function form(Form $form): Form

return $key_value_fields;
} else {
return [
Textarea::make('reason'),
];
if ($get('report_type') == 'molecule') {
return [
Textarea::make('reason')
->required(),
];
} else {
return [
Textarea::make('reason')
->required()
->helperText(new HtmlString('<span style="color:red">Make sure you manually made all the changes requested before approving. This will only change the status of the report.</span>')),
];
}
}
})
->hidden(function (Get $get, string $operation) {
return ! auth()->user()->roles()->exists() || $get('status') == 'rejected' || $get('status') == 'approved' || $operation != 'edit';
})
->action(function (array $data, Report $record, Molecule $molecule, $set, $livewire): void {
->action(function (array $data, Report $record, Molecule $molecule, $set, $livewire, $get): void {
self::approveReport($data, $record, $molecule, $livewire);
$set('status', 'approved');
})
->modalSubmitAction(function () {
if (count(self::$approved_changes) <= 1) {
if (! empty(self::$approved_changes) && count(self::$approved_changes) <= 1) {
return false;
}
}),
Expand All @@ -139,35 +151,40 @@ public static function form(Form $form): Form
}),
Action::make('assign')
->hidden(function (Get $get, string $operation, ?Report $record) {
return ! (auth()->user()->roles()->exists() && ($operation == 'view' || $operation == 'edit') && ($get('status') != 'approved' || $get('status') != 'rejected'));
return ! (auth()->user()->roles()->exists() && ($operation == 'view' || $operation == 'edit') && ($record->status != 'approved') && ($record->status != 'rejected'));
})
->form([
Radio::make('curator')
->label('Choose a curator')
->default(function ($record) {
return $record->assigned_to;
})
->options(function () {
return $users = User::whereHas('roles')->pluck('name', 'id');
}),
])
->action(function (array $data, Report $record): void {
->action(function (array $data, Report $record, $livewire): void {
$record['assigned_to'] = $data['curator'];
$record->save();
$record->refresh();
if (auth()->id() == $data['curator']) {
$livewire->redirect(ReportResource::getUrl('edit', ['record' => $record->id]));
} else {
$livewire->redirect(ReportResource::getUrl('view', ['record' => $record->id]));
}
})
->modalHeading('')
->modalSubmitActionLabel('Assign')
->iconButton()
->icon('heroicon-m-user-group')
// ->badge(function (?Report $record) {
// return $record->curator->name ?? null;
// })
->extraAttributes([
'class' => 'ml-1 mr-0',
])
->size('xl'),
])
->hidden(function (Get $get) {
return $get('report_type') != 'molecule';
})
// ->hidden(function (Get $get) {
// return $get('report_type') != 'molecule';
// })
->verticalAlignment(VerticalAlignment::End)
->columnStart(4),
])
Expand Down Expand Up @@ -256,7 +273,7 @@ public static function form(Form $form): Form
$synonyms = self::$molecule->synonyms;
}

return $synonyms;
return empty($synonyms) ? [] : $synonyms;
})
->disabled(function (Get $get, string $operation) {
return ! $get('show_synonym_existing') && $operation == 'edit';
Expand All @@ -265,8 +282,9 @@ public static function form(Form $form): Form
->columnSpan(4),
TagsInput::make('new_synonyms')
->label('New')
->separator(',')
->splitKeys([','])
->hint("Use '|' (pipe) to separate synonyms")
->separator('|')
->splitKeys(['|'])
->disabled(function (Get $get, string $operation) {
return ! $get('show_synonym_new') && $operation == 'edit';
})
Expand Down Expand Up @@ -564,6 +582,13 @@ public static function table(Table $table): Table
TextColumn::make('title')
->wrap()
->description(fn (Report $record): string => Str::of($record->evidence)->words(10)),
TextColumn::make('is_change')
->label('Type')
->badge()
->color(fn (Report $record): string => $record->is_change ? 'warning' : 'gray')
->formatStateUsing(function (Report $record): string {
return $record->is_change ? 'change' : 'report';
}),
Tables\Columns\TextColumn::make('curator.name')
->searchable()
->placeholder('Choose a curator')
Expand All @@ -586,7 +611,7 @@ public static function table(Table $table): Table
$record->refresh();
})
->modalSubmitActionLabel('Assign')
->modalHidden(fn (): bool => ! auth()->user()->roles()->exists()),
->modalHidden(fn (Report $record): bool => ! auth()->user()->roles()->exists() || $record['status'] == 'approved' || $record['status'] == 'rejected'),
),
])
->defaultSort('created_at', 'desc')
Expand All @@ -597,32 +622,32 @@ public static function table(Table $table): Table
->actions([
Tables\Actions\EditAction::make()
->visible(function ($record) {
// return auth()->user()->roles()->exists() && $record['status'] == 'submitted';
// }),
// Tables\Actions\Action::make('approve')
// // ->button()
// ->hidden(function (Report $record) {
// return ! auth()->user()->roles()->exists() || $record['status'] == 'draft' || $record['status'] == 'rejected' || $record['status'] == 'approved';
// })
// ->form([
// Textarea::make('reason'),
// ])
// ->action(function (array $data, Report $record, Molecule $molecule, $livewire): void {
// self::approveReport($data, $record, $molecule, $livewire);
// }),
// Tables\Actions\Action::make('reject')
// // ->button()
// ->color('danger')
// ->hidden(function (Report $record) {
// return ! auth()->user()->roles()->exists() || $record['status'] == 'draft' || $record['status'] == 'rejected' || $record['status'] == 'approved';
// })
// ->form([
// Textarea::make('reason'),

// ])
// ->action(function (array $data, Report $record): void {
// self::rejectReport($data, $record, $livewire);
// }),
// return auth()->user()->roles()->exists() && $record['status'] == 'submitted';
// }),
// Tables\Actions\Action::make('approve')
// // ->button()
// ->hidden(function (Report $record) {
// return ! auth()->user()->roles()->exists() || $record['status'] == 'draft' || $record['status'] == 'rejected' || $record['status'] == 'approved';
// })
// ->form([
// Textarea::make('reason'),
// ])
// ->action(function (array $data, Report $record, Molecule $molecule, $livewire): void {
// self::approveReport($data, $record, $molecule, $livewire);
// }),
// Tables\Actions\Action::make('reject')
// // ->button()
// ->color('danger')
// ->hidden(function (Report $record) {
// return ! auth()->user()->roles()->exists() || $record['status'] == 'draft' || $record['status'] == 'rejected' || $record['status'] == 'approved';
// })
// ->form([
// Textarea::make('reason'),

// ])
// ->action(function (array $data, Report $record): void {
// self::rejectReport($data, $record, $livewire);
// }),
return auth()->user()->roles()->exists() && $record['status'] == 'submitted' && ($record['assigned_to'] == null || $record['assigned_to'] == auth()->id());
}),
Tables\Actions\ViewAction::make(),
Expand Down Expand Up @@ -720,15 +745,21 @@ public static function prepareApprovedChanges(Report $record, $livewire)
public static function approveReport(array $data, Report $record, Molecule $molecule, $livewire): void
{
// 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();
if (! $record['is_change']) {
if ($record['report_type'] == 'molecule') {
$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();
}
}
$record['status'] = 'approved';
$record['comment'] = prepareComment($data['reason']);
$record['assigned_to'] = auth()->id();
$record->save();
} else {
// In case of Changes
// Run SQL queries for the approved changes
Expand All @@ -753,6 +784,7 @@ public static function rejectReport(array $data, Report $record, $livewire): voi
{
$record['status'] = 'rejected';
$record['comment'] = $data['reason'];
$record['assigned_to'] = auth()->id();
$record->save();

$livewire->redirect(ReportResource::getUrl('view', ['record' => $record->id]));
Expand Down Expand Up @@ -783,7 +815,7 @@ public static function runSQLQueries(Report $record): void

// Apply Synonym Changes
if (array_key_exists('synonym_changes', self::$overall_changes)) {
$db_synonyms = $molecule->synonyms;
$db_synonyms = $molecule->synonyms ?? [];
if (! empty(self::$overall_changes['synonym_changes']['delete'])) {
$db_synonyms = array_diff($db_synonyms, self::$overall_changes['synonym_changes']['delete']);
$molecule->synonyms = $db_synonyms;
Expand All @@ -804,7 +836,7 @@ public static function runSQLQueries(Report $record): void

// Apply CAS Changes
if (array_key_exists('cas_changes', self::$overall_changes)) {
$db_cas = $molecule->cas;
$db_cas = $molecule->cas ?? [];
if (! empty(self::$overall_changes['cas_changes']['delete'])) {
$db_cas = array_diff($db_cas, self::$overall_changes['cas_changes']['delete']);
$molecule->cas = $db_cas;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function getPresetViews(): array
];
if (auth()->user()->roles()->exists()) {
$presetViews['assigned'] = PresetView::make()
->modifyQueryUsing(fn ($query) => $query->where('assigned_to', auth()->id()))
->modifyQueryUsing(fn ($query) => $query->where('assigned_to', auth()->id())->where('status', 'submitted'))
->favorite()
->badge(Report::query()->where('assigned_to', auth()->id())->where('status', 'submitted')->count())
->preserveAll();
Expand Down
Loading

0 comments on commit a38339b

Please sign in to comment.