fix(admin): audit-logs count query params mismatch
This commit is contained in:
parent
954628a3b9
commit
e6a5828d14
@ -23,71 +23,65 @@ export async function GET(req: NextRequest) {
|
|||||||
const action = searchParams.get("action") || "";
|
const action = searchParams.get("action") || "";
|
||||||
const offset = (page - 1) * pageSize;
|
const offset = (page - 1) * pageSize;
|
||||||
|
|
||||||
// Build queries with proper parameter indexing
|
const actionPattern = action ? `%${action}%` : null;
|
||||||
|
const limitOffsetParams: unknown[] = [pageSize, offset];
|
||||||
|
|
||||||
let userQuery = "";
|
let userQuery = "";
|
||||||
let projectQuery = "";
|
let projectQuery = "";
|
||||||
let queryParams: unknown[] = [];
|
let userParams: unknown[] = [];
|
||||||
let userCountQuery = "";
|
let projectParams: unknown[] = [];
|
||||||
let projectCountQuery = "";
|
|
||||||
let paramIdx = 1;
|
|
||||||
|
|
||||||
|
// Build user_activity_log query
|
||||||
if (source !== "project") {
|
if (source !== "project") {
|
||||||
if (action) {
|
if (action) {
|
||||||
|
userParams = [actionPattern, ...limitOffsetParams];
|
||||||
userQuery = `SELECT 'user_activity' as source, id,
|
userQuery = `SELECT 'user_activity' as source, id,
|
||||||
COALESCE(user_uid::text, '') as actor_uid,
|
COALESCE(user_uid::text, '') as actor_uid,
|
||||||
action, NULL::text as resource,
|
action, NULL::text as resource,
|
||||||
ip_address, user_agent, created_at::text as created_at
|
ip_address, user_agent, created_at::text as created_at
|
||||||
FROM user_activity_log
|
FROM user_activity_log
|
||||||
WHERE action ILIKE $${paramIdx}
|
WHERE action ILIKE $1
|
||||||
ORDER BY created_at DESC
|
ORDER BY created_at DESC
|
||||||
LIMIT $${paramIdx + 1} OFFSET $${paramIdx + 2}`;
|
LIMIT $2 OFFSET $3`;
|
||||||
userCountQuery = `SELECT COUNT(*) FROM user_activity_log WHERE action ILIKE $${paramIdx}`;
|
|
||||||
queryParams.push(`%${action}%`, pageSize, offset);
|
|
||||||
paramIdx += 3;
|
|
||||||
} else {
|
} else {
|
||||||
|
userParams = limitOffsetParams;
|
||||||
userQuery = `SELECT 'user_activity' as source, id,
|
userQuery = `SELECT 'user_activity' as source, id,
|
||||||
COALESCE(user_uid::text, '') as actor_uid,
|
COALESCE(user_uid::text, '') as actor_uid,
|
||||||
action, NULL::text as resource,
|
action, NULL::text as resource,
|
||||||
ip_address, user_agent, created_at::text as created_at
|
ip_address, user_agent, created_at::text as created_at
|
||||||
FROM user_activity_log
|
FROM user_activity_log
|
||||||
ORDER BY created_at DESC
|
ORDER BY created_at DESC
|
||||||
LIMIT $${paramIdx} OFFSET $${paramIdx + 1}`;
|
LIMIT $1 OFFSET $2`;
|
||||||
userCountQuery = `SELECT COUNT(*) FROM user_activity_log`;
|
|
||||||
queryParams.push(pageSize, offset);
|
|
||||||
paramIdx += 2;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Build project_audit_log query
|
||||||
if (source !== "user") {
|
if (source !== "user") {
|
||||||
if (action) {
|
if (action) {
|
||||||
|
projectParams = [actionPattern, ...limitOffsetParams];
|
||||||
projectQuery = `SELECT 'project_audit' as source, id,
|
projectQuery = `SELECT 'project_audit' as source, id,
|
||||||
COALESCE(actor::text, '') as actor_uid,
|
COALESCE(actor::text, '') as actor_uid,
|
||||||
action, details as resource, ip_address,
|
action, details as resource, ip_address,
|
||||||
NULL as user_agent, created_at::text as created_at
|
NULL as user_agent, created_at::text as created_at
|
||||||
FROM project_audit_log
|
FROM project_audit_log
|
||||||
WHERE action ILIKE $${paramIdx}
|
WHERE action ILIKE $1
|
||||||
ORDER BY created_at DESC
|
ORDER BY created_at DESC
|
||||||
LIMIT $${paramIdx + 1} OFFSET $${paramIdx + 2}`;
|
LIMIT $2 OFFSET $3`;
|
||||||
projectCountQuery = `SELECT COUNT(*) FROM project_audit_log WHERE action ILIKE $${paramIdx}`;
|
|
||||||
queryParams.push(`%${action}%`, pageSize, offset);
|
|
||||||
paramIdx += 3;
|
|
||||||
} else {
|
} else {
|
||||||
|
projectParams = limitOffsetParams;
|
||||||
projectQuery = `SELECT 'project_audit' as source, id,
|
projectQuery = `SELECT 'project_audit' as source, id,
|
||||||
COALESCE(actor::text, '') as actor_uid,
|
COALESCE(actor::text, '') as actor_uid,
|
||||||
action, details as resource, ip_address,
|
action, details as resource, ip_address,
|
||||||
NULL as user_agent, created_at::text as created_at
|
NULL as user_agent, created_at::text as created_at
|
||||||
FROM project_audit_log
|
FROM project_audit_log
|
||||||
ORDER BY created_at DESC
|
ORDER BY created_at DESC
|
||||||
LIMIT $${paramIdx} OFFSET $${paramIdx + 1}`;
|
LIMIT $1 OFFSET $2`;
|
||||||
projectCountQuery = `SELECT COUNT(*) FROM project_audit_log`;
|
|
||||||
queryParams.push(pageSize, offset);
|
|
||||||
paramIdx += 2;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const [userLogs, projectLogs] = await Promise.all([
|
const [userLogs, projectLogs] = await Promise.all([
|
||||||
userQuery ? query<AuditLog>(userQuery, queryParams) : Promise.resolve({ rows: [] as AuditLog[] }),
|
userQuery ? query<AuditLog>(userQuery, userParams) : Promise.resolve({ rows: [] as AuditLog[] }),
|
||||||
projectQuery ? query<AuditLog>(projectQuery, queryParams) : Promise.resolve({ rows: [] as AuditLog[] }),
|
projectQuery ? query<AuditLog>(projectQuery, projectParams) : Promise.resolve({ rows: [] as AuditLog[] }),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// 合并并排序
|
// 合并并排序
|
||||||
@ -97,8 +91,8 @@ export async function GET(req: NextRequest) {
|
|||||||
|
|
||||||
// 总数
|
// 总数
|
||||||
const [userCountRes, projectCountRes] = await Promise.all([
|
const [userCountRes, projectCountRes] = await Promise.all([
|
||||||
userCountQuery ? query<{ count: string }>(userCountQuery, action ? [`%${action}%`] : []) : Promise.resolve({ rows: [{ count: "0" }] }),
|
userCountQuery(userParams, action),
|
||||||
projectCountQuery ? query<{ count: string }>(projectCountQuery, action ? [`%${action}%`] : []) : Promise.resolve({ rows: [{ count: "0" }] }),
|
projectCountQuery(projectParams, action),
|
||||||
]);
|
]);
|
||||||
const total = parseInt(String(userCountRes.rows[0]?.count || "0"), 10) +
|
const total = parseInt(String(userCountRes.rows[0]?.count || "0"), 10) +
|
||||||
parseInt(String(projectCountRes.rows[0]?.count || "0"), 10);
|
parseInt(String(projectCountRes.rows[0]?.count || "0"), 10);
|
||||||
@ -109,3 +103,25 @@ export async function GET(req: NextRequest) {
|
|||||||
return NextResponse.json({ error: "服务器错误" }, { status: 500 });
|
return NextResponse.json({ error: "服务器错误" }, { status: 500 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function userCountQuery(params: unknown[], action: string | null) {
|
||||||
|
if (!params.length) return { rows: [] as { count: string }[] };
|
||||||
|
if (action) {
|
||||||
|
return query<{ count: string }>(
|
||||||
|
`SELECT COUNT(*) FROM user_activity_log WHERE action ILIKE $1`,
|
||||||
|
[params[0]]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return query<{ count: string }>(`SELECT COUNT(*) FROM user_activity_log`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function projectCountQuery(params: unknown[], action: string | null) {
|
||||||
|
if (!params.length) return { rows: [] as { count: string }[] };
|
||||||
|
if (action) {
|
||||||
|
return query<{ count: string }>(
|
||||||
|
`SELECT COUNT(*) FROM project_audit_log WHERE action ILIKE $1`,
|
||||||
|
[params[0]]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return query<{ count: string }>(`SELECT COUNT(*) FROM project_audit_log`);
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user