libra/docs/stories/story-6.10-audit-log-viewer.md

2.6 KiB

Story 6.10: Audit Log Viewer

Epic Reference

Epic 6: Admin Dashboard

User Story

As an admin, I want to view admin action history, So that I can maintain accountability and track changes.

Acceptance Criteria

Display

  • Action type (create, update, delete)
  • Target (user, consultation, timeline, etc.)
  • Old and new values (for updates)
  • Timestamp
  • IP address

Filtering

  • Filter by action type
  • Filter by target type
  • Filter by date range
  • Search by target name/ID

Features

  • Pagination
  • Export audit log (CSV)

Technical Notes

new class extends Component {
    use WithPagination;

    public string $actionFilter = '';
    public string $targetFilter = '';
    public string $dateFrom = '';
    public string $dateTo = '';
    public string $search = '';

    public function with(): array
    {
        return [
            'logs' => AdminLog::query()
                ->with('admin')
                ->when($this->actionFilter, fn($q) => $q->where('action_type', $this->actionFilter))
                ->when($this->targetFilter, fn($q) => $q->where('target_type', $this->targetFilter))
                ->when($this->dateFrom, fn($q) => $q->whereDate('created_at', '>=', $this->dateFrom))
                ->when($this->dateTo, fn($q) => $q->whereDate('created_at', '<=', $this->dateTo))
                ->when($this->search, fn($q) => $q->where('target_id', $this->search))
                ->latest()
                ->paginate(25),
            'actionTypes' => AdminLog::distinct()->pluck('action_type'),
            'targetTypes' => AdminLog::distinct()->pluck('target_type'),
        ];
    }

    public function exportCsv()
    {
        // Export filtered logs to CSV
    }
};

Template

@foreach($logs as $log)
    <tr>
        <td>{{ $log->created_at->format('d/m/Y H:i') }}</td>
        <td>{{ $log->admin?->name ?? __('admin.system') }}</td>
        <td>
            <flux:badge>{{ $log->action_type }}</flux:badge>
        </td>
        <td>{{ $log->target_type }} #{{ $log->target_id }}</td>
        <td>{{ $log->ip_address }}</td>
        <td>
            <flux:button size="sm" wire:click="showDetails({{ $log->id }})">
                {{ __('admin.details') }}
            </flux:button>
        </td>
    </tr>
@endforeach

Definition of Done

  • Logs display correctly
  • All filters work
  • Search works
  • Pagination works
  • CSV export works
  • Old/new values viewable
  • Tests pass

Estimation

Complexity: Medium | Effort: 3-4 hours