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 offset = (page - 1) * pageSize;
|
||||
|
||||
// Build queries with proper parameter indexing
|
||||
const actionPattern = action ? `%${action}%` : null;
|
||||
const limitOffsetParams: unknown[] = [pageSize, offset];
|
||||
|
||||
let userQuery = "";
|
||||
let projectQuery = "";
|
||||
let queryParams: unknown[] = [];
|
||||
let userCountQuery = "";
|
||||
let projectCountQuery = "";
|
||||
let paramIdx = 1;
|
||||
let userParams: unknown[] = [];
|
||||
let projectParams: unknown[] = [];
|
||||
|
||||
// Build user_activity_log query
|
||||
if (source !== "project") {
|
||||
if (action) {
|
||||
userParams = [actionPattern, ...limitOffsetParams];
|
||||
userQuery = `SELECT 'user_activity' as source, id,
|
||||
COALESCE(user_uid::text, '') as actor_uid,
|
||||
action, NULL::text as resource,
|
||||
ip_address, user_agent, created_at::text as created_at
|
||||
FROM user_activity_log
|
||||
WHERE action ILIKE $${paramIdx}
|
||||
WHERE action ILIKE $1
|
||||
ORDER BY created_at DESC
|
||||
LIMIT $${paramIdx + 1} OFFSET $${paramIdx + 2}`;
|
||||
userCountQuery = `SELECT COUNT(*) FROM user_activity_log WHERE action ILIKE $${paramIdx}`;
|
||||
queryParams.push(`%${action}%`, pageSize, offset);
|
||||
paramIdx += 3;
|
||||
LIMIT $2 OFFSET $3`;
|
||||
} else {
|
||||
userParams = limitOffsetParams;
|
||||
userQuery = `SELECT 'user_activity' as source, id,
|
||||
COALESCE(user_uid::text, '') as actor_uid,
|
||||
action, NULL::text as resource,
|
||||
ip_address, user_agent, created_at::text as created_at
|
||||
FROM user_activity_log
|
||||
ORDER BY created_at DESC
|
||||
LIMIT $${paramIdx} OFFSET $${paramIdx + 1}`;
|
||||
userCountQuery = `SELECT COUNT(*) FROM user_activity_log`;
|
||||
queryParams.push(pageSize, offset);
|
||||
paramIdx += 2;
|
||||
LIMIT $1 OFFSET $2`;
|
||||
}
|
||||
}
|
||||
|
||||
// Build project_audit_log query
|
||||
if (source !== "user") {
|
||||
if (action) {
|
||||
projectParams = [actionPattern, ...limitOffsetParams];
|
||||
projectQuery = `SELECT 'project_audit' as source, id,
|
||||
COALESCE(actor::text, '') as actor_uid,
|
||||
action, details as resource, ip_address,
|
||||
NULL as user_agent, created_at::text as created_at
|
||||
FROM project_audit_log
|
||||
WHERE action ILIKE $${paramIdx}
|
||||
WHERE action ILIKE $1
|
||||
ORDER BY created_at DESC
|
||||
LIMIT $${paramIdx + 1} OFFSET $${paramIdx + 2}`;
|
||||
projectCountQuery = `SELECT COUNT(*) FROM project_audit_log WHERE action ILIKE $${paramIdx}`;
|
||||
queryParams.push(`%${action}%`, pageSize, offset);
|
||||
paramIdx += 3;
|
||||
LIMIT $2 OFFSET $3`;
|
||||
} else {
|
||||
projectParams = limitOffsetParams;
|
||||
projectQuery = `SELECT 'project_audit' as source, id,
|
||||
COALESCE(actor::text, '') as actor_uid,
|
||||
action, details as resource, ip_address,
|
||||
NULL as user_agent, created_at::text as created_at
|
||||
FROM project_audit_log
|
||||
ORDER BY created_at DESC
|
||||
LIMIT $${paramIdx} OFFSET $${paramIdx + 1}`;
|
||||
projectCountQuery = `SELECT COUNT(*) FROM project_audit_log`;
|
||||
queryParams.push(pageSize, offset);
|
||||
paramIdx += 2;
|
||||
LIMIT $1 OFFSET $2`;
|
||||
}
|
||||
}
|
||||
|
||||
const [userLogs, projectLogs] = await Promise.all([
|
||||
userQuery ? query<AuditLog>(userQuery, queryParams) : Promise.resolve({ rows: [] as AuditLog[] }),
|
||||
projectQuery ? query<AuditLog>(projectQuery, queryParams) : Promise.resolve({ rows: [] as AuditLog[] }),
|
||||
userQuery ? query<AuditLog>(userQuery, userParams) : 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([
|
||||
userCountQuery ? query<{ count: string }>(userCountQuery, action ? [`%${action}%`] : []) : Promise.resolve({ rows: [{ count: "0" }] }),
|
||||
projectCountQuery ? query<{ count: string }>(projectCountQuery, action ? [`%${action}%`] : []) : Promise.resolve({ rows: [{ count: "0" }] }),
|
||||
userCountQuery(userParams, action),
|
||||
projectCountQuery(projectParams, action),
|
||||
]);
|
||||
const total = parseInt(String(userCountRes.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 });
|
||||
}
|
||||
}
|
||||
|
||||
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