51 lines
1.6 KiB
TypeScript
51 lines
1.6 KiB
TypeScript
import vine from '@vinejs/vine'
|
|
import db from '@adonisjs/lucid/services/db'
|
|
import type { HttpContext } from '@adonisjs/core/http'
|
|
import AuditLog from '#models/audit_log'
|
|
import UserError from '#exceptions/user_error'
|
|
|
|
const indexValidator = vine.create({
|
|
page: vine.number().withoutDecimals().min(1).optional(),
|
|
status: vine.enum(['running', 'success', 'error'] as const).optional(),
|
|
})
|
|
|
|
export default class AuditController {
|
|
async index({ request }: HttpContext) {
|
|
const { page = 1, status } = await indexValidator.validate(request.qs())
|
|
|
|
const query = AuditLog.query()
|
|
.select('id', 'username', 'ip', 'action', 'target', 'status', 'created_at', 'finished_at')
|
|
.select(db.raw('length(output) > 0 as hasOutput'))
|
|
.orderBy('id', 'desc')
|
|
if (status) query.where('status', status)
|
|
|
|
const result = await query.paginate(page, 50)
|
|
return {
|
|
meta: { total: result.total, page: result.currentPage, lastPage: result.lastPage },
|
|
data: result.all().map((entry) => ({
|
|
id: entry.id,
|
|
username: entry.username,
|
|
ip: entry.ip,
|
|
action: entry.action,
|
|
target: entry.target,
|
|
status: entry.status,
|
|
createdAt: entry.createdAt.toISO(),
|
|
finishedAt: entry.finishedAt?.toISO() ?? null,
|
|
hasOutput: Boolean(entry.$extras.hasOutput),
|
|
})),
|
|
}
|
|
}
|
|
|
|
async show({ params }: HttpContext) {
|
|
const entry = await AuditLog.find(params.id)
|
|
if (!entry) throw new UserError('Entrée introuvable.', 404)
|
|
return {
|
|
id: entry.id,
|
|
action: entry.action,
|
|
target: entry.target,
|
|
status: entry.status,
|
|
output: entry.output,
|
|
}
|
|
}
|
|
}
|