Implement superadmin audit log for mutations
This commit is contained in:
@@ -4,6 +4,7 @@ namespace App\Filament\Resources\TaskResource\Pages;
|
||||
|
||||
use App\Filament\Resources\TaskResource;
|
||||
use App\Models\Task;
|
||||
use App\Services\Audit\SuperAdminAuditLogger;
|
||||
use Filament\Forms\Components\FileUpload;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Notifications\Notification;
|
||||
@@ -14,7 +15,9 @@ use Illuminate\Support\Facades\Storage;
|
||||
class ImportTasks extends Page
|
||||
{
|
||||
protected static string $resource = TaskResource::class;
|
||||
|
||||
protected string $view = 'filament.resources.task-resource.pages.import-tasks';
|
||||
|
||||
protected ?string $heading = null;
|
||||
|
||||
public ?string $file = null;
|
||||
@@ -35,8 +38,9 @@ class ImportTasks extends Page
|
||||
$this->validate();
|
||||
|
||||
$path = $this->form->getState()['file'] ?? null;
|
||||
if (!$path || !Storage::disk('public')->exists($path)) {
|
||||
if (! $path || ! Storage::disk('public')->exists($path)) {
|
||||
Notification::make()->danger()->title(__('admin.notifications.file_not_found'))->send();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -58,14 +62,14 @@ class ImportTasks extends Page
|
||||
private function importTasksCsv(string $file): array
|
||||
{
|
||||
$handle = fopen($file, 'r');
|
||||
if (!$handle) {
|
||||
if (! $handle) {
|
||||
return [0, 0];
|
||||
}
|
||||
|
||||
$ok = 0;
|
||||
$fail = 0;
|
||||
$headers = fgetcsv($handle, 0, ',');
|
||||
if (!$headers) {
|
||||
if (! $headers) {
|
||||
return [0, 0];
|
||||
}
|
||||
|
||||
@@ -87,7 +91,7 @@ class ImportTasks extends Page
|
||||
$emotionId = DB::table('emotions')->where('name->en', $emotionNameEn)->value('id');
|
||||
}
|
||||
|
||||
if (!$emotionId) {
|
||||
if (! $emotionId) {
|
||||
throw new \Exception('Emotion not found.');
|
||||
}
|
||||
|
||||
@@ -97,7 +101,7 @@ class ImportTasks extends Page
|
||||
$eventTypeId = DB::table('event_types')->where('slug', $eventTypeSlug)->value('id');
|
||||
}
|
||||
|
||||
Task::create([
|
||||
$task = Task::create([
|
||||
'emotion_id' => $emotionId,
|
||||
'event_type_id' => $eventTypeId,
|
||||
'title' => [
|
||||
@@ -113,10 +117,17 @@ class ImportTasks extends Page
|
||||
'de' => $row[$map['example_text_de']] ?? null,
|
||||
'en' => $row[$map['example_text_en']] ?? null,
|
||||
],
|
||||
'sort_order' => (int)($row[$map['sort_order']] ?? 0),
|
||||
'is_active' => (int)($row[$map['is_active']] ?? 1) ? 1 : 0,
|
||||
'sort_order' => (int) ($row[$map['sort_order']] ?? 0),
|
||||
'is_active' => (int) ($row[$map['is_active']] ?? 1) ? 1 : 0,
|
||||
]);
|
||||
|
||||
app(SuperAdminAuditLogger::class)->recordModelMutation(
|
||||
'created',
|
||||
$task,
|
||||
SuperAdminAuditLogger::fieldsMetadata($task->getChanges()),
|
||||
source: static::class
|
||||
);
|
||||
|
||||
$ok++;
|
||||
});
|
||||
} catch (\Throwable $e) {
|
||||
@@ -125,6 +136,7 @@ class ImportTasks extends Page
|
||||
}
|
||||
|
||||
fclose($handle);
|
||||
|
||||
return [$ok, $fail];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user