# All files / js/pages/audit-logs Index.vue

0% Statements 0/59
0% Branches 0/71
0% Functions 0/25
0% Lines 0/59

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341

     <script setup lang="ts">
    import AppLayout from '@/layouts/AppLayout.vue';
    import { Head, Link, router } from '@inertiajs/vue3';
    import { ref } from 'vue';
    import { Button } from '@/components/ui/button';
    import { Input } from '@/components/ui/input';
    import { Badge } from '@/components/ui/badge';
    import { Calendar, Download, Eye, Search } from 'lucide-vue-next';

    interface AuditLog {
        id: number;
        event: string;
        description: string;
        user: {
            id: number;
            name: string;
            email: string;
        } | null;
        auditable_type: string;
        auditable_id: number;
        created_at: string;
        ip_address: string | null;
        url: string | null;
    }

    const props = defineProps<{
        auditLogs: {
            data: AuditLog[];
            links: any[];
            current_page: number;
            last_page: number;
            per_page: number;
            total: number;
        };
        users: Array<{
            id: number;
            name: string;
            email: string;
        }>;
        modelTypes: Array<{
            value: string;
            label: string;
        }>;
        filters: {
            event: string;
            user_id: string;
            model_type: string;
            model_id: string;
            date_from: string;
            date_to: string;
            search: string;
        };
    }>();

    const eventFilter = ref(props.filters.event);
    const userFilter = ref(props.filters.user_id);
    const modelTypeFilter = ref(props.filters.model_type);
    const searchFilter = ref(props.filters.search);
    const dateFromFilter = ref(props.filters.date_from);
    const dateToFilter = ref(props.filters.date_to);

    const eventTypes = [
        { value: '', label: 'All Events' },
        { value: 'created', label: 'Created' },
        { value: 'updated', label: 'Updated' },
        { value: 'deleted', label: 'Deleted' },
        { value: 'restored', label: 'Restored' },
    ];

    function applyFilters() {
        router.get(
            '/audit-logs',
            {
                event: eventFilter.value,
                user_id: userFilter.value,
                model_type: modelTypeFilter.value,
                search: searchFilter.value,
                date_from: dateFromFilter.value,
                date_to: dateToFilter.value,
            },
            {
                preserveState: true,
                preserveScroll: true,
            }
        );
    }

    function exportLogs() {
        const params = new URLSearchParams({
            event: eventFilter.value || '',
            user_id: userFilter.value || '',
            model_type: modelTypeFilter.value || '',
            search: searchFilter.value || '',
            date_from: dateFromFilter.value || '',
            date_to: dateToFilter.value || '',
        });
        window.location.href = `/audit-logs/export?${params.toString()}`;
    }

    function getEventBadgeVariant(event: string): string {
        const variants: Record<string, string> = {
            created: 'default',
            updated: 'secondary',
            deleted: 'destructive',
            restored: 'outline',
        };
        return variants[event] || 'default';
    }

    function formatModelType(type: string): string {
        return type.split('\\').pop() || type;
    }
    </script>

    <template>
        <Head title="Audit Logs" />

        <AppLayout>
            <div class="space-y-6 p-6">
                <div class="flex items-center justify-between">
                    <div>
                        <h1 class="text-3xl font-bold text-gray-900">Audit Logs</h1>
                        <p class="mt-1 text-sm text-gray-600">
                            Track all changes and activities in the system
                        </p>
                    </div>
                    <Button @click="exportLogs" variant="outline">
                        <Download class="mr-2 h-4 w-4" />
                        Export CSV
                    </Button>
                </div>

                <!-- Filters -->
                <div class="rounded-lg border bg-white p-4">
                    <div class="grid grid-cols-1 gap-4 md:grid-cols-3 lg:grid-cols-6">
                        <div>
                            <label class="mb-2 block text-sm font-medium text-gray-700">Event Type</label>
                            <select
                                v-model="eventFilter"
                                @change="applyFilters"
                                class="w-full rounded-md border border-gray-300 bg-white px-3 py-2 text-sm shadow-sm focus:border-blue-500 focus:outline-none focus:ring-1 focus:ring-blue-500"
                            >
                                <option
                                    v-for="event in eventTypes"
                                    :key="event.value"
                                    :value="event.value"
                                >
                                    {{ event.label }}
                                </option>
                            </select>
                        </div>

                        <div>
                            <label class="mb-2 block text-sm font-medium text-gray-700">User</label>
                            <select
                                v-model="userFilter"
                                @change="applyFilters"
                                class="w-full rounded-md border border-gray-300 bg-white px-3 py-2 text-sm shadow-sm focus:border-blue-500 focus:outline-none focus:ring-1 focus:ring-blue-500"
                            >
                                <option value="">All Users</option>
                                <option
                                    v-for="user in users"
                                    :key="user.id"
                                    :value="user.id.toString()"
                                >
                                    {{ user.name }}
                                </option>
                            </select>
                        </div>

                        <div>
                            <label class="mb-2 block text-sm font-medium text-gray-700">Model Type</label>
                            <select
                                v-model="modelTypeFilter"
                                @change="applyFilters"
                                class="w-full rounded-md border border-gray-300 bg-white px-3 py-2 text-sm shadow-sm focus:border-blue-500 focus:outline-none focus:ring-1 focus:ring-blue-500"
                            >
                                <option value="">All Models</option>
                                <option
                                    v-for="modelType in modelTypes"
                                    :key="modelType.value"
                                    :value="modelType.value"
                                >
                                    {{ modelType.label }}
                                </option>
                            </select>
                        </div>

                        <div>
                            <label class="mb-2 block text-sm font-medium text-gray-700">Date From</label>
                            <Input
                                v-model="dateFromFilter"
                                type="date"
                                @change="applyFilters"
                            />
                        </div>

                        <div>
                            <label class="mb-2 block text-sm font-medium text-gray-700">Date To</label>
                            <Input
                                v-model="dateToFilter"
                                type="date"
                                @change="applyFilters"
                            />
                        </div>

                        <div>
                            <label class="mb-2 block text-sm font-medium text-gray-700">Search</label>
                            <div class="relative">
                                <Search class="absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-gray-400" />
                                <Input
                                    v-model="searchFilter"
                                    placeholder="Search..."
                                    class="pl-9"
                                    @keyup.enter="applyFilters"
                                />
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Audit Logs Table -->
                <div class="overflow-hidden rounded-lg border border-gray-200 bg-white shadow-sm">
                    <table class="w-full">
                        <thead class="bg-gray-50">
                            <tr>
                                <th class="px-6 py-3 text-left text-xs font-medium uppercase tracking-wider text-gray-500">
                                    Date & Time
                                </th>
                                <th class="px-6 py-3 text-left text-xs font-medium uppercase tracking-wider text-gray-500">
                                    User
                                </th>
                                <th class="px-6 py-3 text-left text-xs font-medium uppercase tracking-wider text-gray-500">
                                    Event
                                </th>
                                <th class="px-6 py-3 text-left text-xs font-medium uppercase tracking-wider text-gray-500">
                                    Model
                                </th>
                                <th class="px-6 py-3 text-left text-xs font-medium uppercase tracking-wider text-gray-500">
                                    Description
                                </th>
                                <th class="px-6 py-3 text-left text-xs font-medium uppercase tracking-wider text-gray-500">
                                    IP Address
                                </th>
                                <th class="px-6 py-3 text-left text-xs font-medium uppercase tracking-wider text-gray-500">
                                    Actions
                                </th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-200 bg-white">
                            <tr v-if="auditLogs.data.length === 0">
                                <td colspan="7" class="px-6 py-4 text-center text-sm text-gray-500">
                                    No audit logs found
                                </td>
                            </tr>
                            <tr
                                v-for="log in auditLogs.data"
                                :key="log.id"
                                class="cursor-pointer hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-inset focus:ring-blue-500"
                                tabindex="0"
                                role="link"
                                @click="router.visit(`/audit-logs/${log.id}`)"
                                @keydown.enter.prevent="router.visit(`/audit-logs/${log.id}`)"
                                @keydown.space.prevent="router.visit(`/audit-logs/${log.id}`)"
                            >
                                <td class="whitespace-nowrap px-6 py-4">
                                    <div class="flex items-center gap-2">
                                        <Calendar class="h-4 w-4 text-gray-400" />
                                        <span class="text-sm text-gray-900">
                                            {{ new Date(log.created_at).toLocaleString() }}
                                        </span>
                                    </div>
                                </td>
                                <td class="whitespace-nowrap px-6 py-4">
                                    <div v-if="log.user">
                                        <div class="text-sm font-medium text-gray-900">{{ log.user.name }}</div>
                                        <div class="text-sm text-gray-500">
                                            {{ log.user.email }}
                                        </div>
                                    </div>
                                    <span v-else class="text-sm text-gray-500">System</span>
                                </td>
                                <td class="whitespace-nowrap px-6 py-4">
                                    <Badge :variant="getEventBadgeVariant(log.event)">
                                        {{ log.event }}
                                    </Badge>
                                </td>
                                <td class="whitespace-nowrap px-6 py-4">
                                    <div class="text-sm">
                                        <div class="font-medium text-gray-900">
                                            {{ formatModelType(log.auditable_type) }}
                                        </div>
                                        <div class="text-gray-500">#{{ log.auditable_id }}</div>
                                    </div>
                                </td>
                                <td class="px-6 py-4">
                                    <div class="max-w-md truncate text-sm text-gray-900">{{ log.description }}</div>
                                </td>
                                <td class="whitespace-nowrap px-6 py-4">
                                    <span class="text-sm text-gray-500">
                                        {{ log.ip_address || 'N/A' }}
                                    </span>
                                </td>
                                <td class="whitespace-nowrap px-6 py-4 text-sm" @click.stop>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>

                <!-- Pagination -->
                <div v-if="auditLogs.last_page > 1" class="flex items-center justify-between">
                    <div class="text-sm text-gray-600">
                        Showing {{ (auditLogs.current_page - 1) * auditLogs.per_page + 1 }} to
                        {{ Math.min(auditLogs.current_page * auditLogs.per_page, auditLogs.total) }}
                        of {{ auditLogs.total }} results
                    </div>
                    <div class="flex gap-2">
                        <Button
                            v-if="auditLogs.current_page > 1"
                            variant="outline"
                            size="sm"
                            @click="router.get(auditLogs.links[0].url)"
                        >
                            Previous
                        </Button>
                        <Button
                            v-if="auditLogs.current_page < auditLogs.last_page"
                            variant="outline"
                            size="sm"
                            @click="router.get(auditLogs.links[auditLogs.links.length - 1].url)"
                        >
                            Next
                        </Button>
                    </div>
                </div>
            </div>
        </AppLayout>
    </template>