104 lines
No EOL
3 KiB
TypeScript
104 lines
No EOL
3 KiB
TypeScript
import { query as queryAuthDb } from '../../utils/db'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const queryParams = getQuery(event)
|
|
|
|
const page = Math.max(1, parseInt(String(queryParams.page || '1'), 10))
|
|
const limit = Math.max(1, Math.min(100, parseInt(String(queryParams.limit || '20'), 10)))
|
|
const offset = (page - 1) * limit
|
|
|
|
const searchTerm = String(queryParams.q || queryParams.search || '').trim()
|
|
const logType = String(queryParams.type || 'all').trim()
|
|
const dateFrom = String(queryParams.from || '').trim()
|
|
const dateTo = String(queryParams.to || '').trim()
|
|
|
|
try {
|
|
const whereConditions: string[] = []
|
|
const sqlParams: any[] = []
|
|
let paramIndex = 1
|
|
|
|
if (searchTerm) {
|
|
whereConditions.push(`(
|
|
CAST(log_id AS TEXT) ILIKE $${paramIndex}
|
|
OR editor_sfid ILIKE $${paramIndex}
|
|
OR target_name ILIKE $${paramIndex}
|
|
OR CAST(target_pid AS TEXT) ILIKE $${paramIndex}
|
|
OR CAST(editor_pid AS TEXT) ILIKE $${paramIndex}
|
|
OR changes::text ILIKE $${paramIndex}
|
|
)`)
|
|
sqlParams.push(`%${searchTerm}%`)
|
|
paramIndex++
|
|
}
|
|
|
|
if (logType === 'ban') {
|
|
whereConditions.push(`(
|
|
EXISTS (
|
|
SELECT 1
|
|
FROM jsonb_array_elements(
|
|
CASE
|
|
WHEN jsonb_typeof(changes::jsonb->'changes') = 'array'
|
|
THEN changes::jsonb->'changes'
|
|
ELSE '[]'::jsonb
|
|
END
|
|
) elem
|
|
WHERE elem->>'field' = 'account_level'
|
|
AND (elem->>'newVal')::numeric < 0
|
|
)
|
|
OR changes::text ILIKE '%"account_level"%' AND changes::text ILIKE '%"newVal":-%'
|
|
)`)
|
|
}
|
|
|
|
if (dateFrom) {
|
|
whereConditions.push(`created_at >= $${paramIndex}`)
|
|
sqlParams.push(dateFrom)
|
|
paramIndex++
|
|
}
|
|
|
|
if (dateTo) {
|
|
whereConditions.push(`created_at <= $${paramIndex}`)
|
|
sqlParams.push(`${dateTo} 23:59:59`)
|
|
paramIndex++
|
|
}
|
|
|
|
const whereClause = whereConditions.length > 0
|
|
? `WHERE ${whereConditions.join(' AND ')}`
|
|
: ''
|
|
|
|
const countResult = await queryAuthDb(
|
|
`SELECT COUNT(*) AS total FROM audit_logs ${whereClause}`,
|
|
sqlParams
|
|
)
|
|
|
|
const total = parseInt(countResult.rows[0]?.total || '0', 10)
|
|
const totalPages = Math.ceil(total / limit)
|
|
|
|
const dataSqlParams = [...sqlParams, limit, offset]
|
|
const limitIndex = paramIndex
|
|
const offsetIndex = paramIndex + 1
|
|
|
|
const dataResult = await queryAuthDb(
|
|
`SELECT * FROM audit_logs
|
|
${whereClause}
|
|
ORDER BY created_at DESC
|
|
LIMIT $${limitIndex} OFFSET $${offsetIndex}`,
|
|
dataSqlParams
|
|
)
|
|
|
|
return {
|
|
logs: dataResult.rows,
|
|
total,
|
|
pagination: {
|
|
total,
|
|
page,
|
|
limit,
|
|
totalPages
|
|
}
|
|
}
|
|
} catch (error: any) {
|
|
console.error('[AUDIT LOGS DB QUERY ERROR]:', error)
|
|
throw createError({
|
|
statusCode: 500,
|
|
statusMessage: `Audit logs database query failed: ${error.message}`
|
|
})
|
|
}
|
|
}) |