update
This commit is contained in:
@@ -14,9 +14,13 @@ export class AuditController {
|
||||
// 解析查询参数
|
||||
const limit = parseInt(req.query.limit as string || '50', 10);
|
||||
const offset = parseInt(req.query.offset as string || '0', 10);
|
||||
const actionType = req.query.actionType as AuditLogActionType | undefined;
|
||||
// 修正:从 req.query 中读取 action_type (snake_case)
|
||||
const actionType = req.query.action_type as AuditLogActionType | undefined;
|
||||
const startDate = req.query.startDate ? parseInt(req.query.startDate as string, 10) : undefined;
|
||||
const endDate = req.query.endDate ? parseInt(req.query.endDate as string, 10) : undefined;
|
||||
// 解析 searchTerm 参数
|
||||
const searchTerm = req.query.search as string | undefined;
|
||||
|
||||
|
||||
// 输入验证 (基本)
|
||||
if (isNaN(limit) || limit <= 0) {
|
||||
@@ -37,7 +41,8 @@ export class AuditController {
|
||||
}
|
||||
// TODO: 可以添加对 actionType 是否有效的验证
|
||||
|
||||
const result = await auditLogService.getLogs(limit, offset, actionType, startDate, endDate);
|
||||
// 将 searchTerm 传递给 service
|
||||
const result = await auditLogService.getLogs(limit, offset, actionType, startDate, endDate, searchTerm);
|
||||
|
||||
// 解析 details 字段从 JSON 字符串到对象(如果需要)
|
||||
const logsWithParsedDetails = result.logs.map(log => {
|
||||
|
||||
@@ -50,23 +50,40 @@ export class AuditLogRepository {
|
||||
* @param actionType 可选的操作类型过滤
|
||||
* @param startDate 可选的开始时间戳 (秒)
|
||||
* @param endDate 可选的结束时间戳 (秒)
|
||||
* @param searchTerm 可选的搜索关键词 (模糊匹配 details)
|
||||
*/
|
||||
async getLogs(
|
||||
limit: number = 50,
|
||||
offset: number = 0,
|
||||
actionType?: AuditLogActionType,
|
||||
startDate?: number,
|
||||
endDate?: number
|
||||
endDate?: number,
|
||||
searchTerm?: string // 添加 searchTerm 参数
|
||||
): Promise<{ logs: AuditLogEntry[], total: number }> {
|
||||
console.log(`[Audit Repo] getLogs called with: actionType=${actionType}, searchTerm=${searchTerm}`); // 添加日志
|
||||
|
||||
let baseSql = 'SELECT * FROM audit_logs';
|
||||
let countSql = 'SELECT COUNT(*) as total FROM audit_logs';
|
||||
const whereClauses: string[] = [];
|
||||
const params: (string | number)[] = [];
|
||||
const countParams: (string | number)[] = [];
|
||||
|
||||
if (actionType) { whereClauses.push('action_type = ?'); params.push(actionType); countParams.push(actionType); }
|
||||
if (startDate) { whereClauses.push('timestamp >= ?'); params.push(startDate); countParams.push(startDate); }
|
||||
if (endDate) { whereClauses.push('timestamp <= ?'); params.push(endDate); countParams.push(endDate); }
|
||||
if (actionType) {
|
||||
console.log(`[Audit Repo] Filtering by actionType: ${actionType}`); // 添加日志
|
||||
whereClauses.push('action_type = ?');
|
||||
params.push(actionType);
|
||||
countParams.push(actionType);
|
||||
}
|
||||
// 添加 searchTerm 的过滤逻辑
|
||||
if (searchTerm) {
|
||||
console.log(`[Audit Repo] Filtering by searchTerm: ${searchTerm}`); // 添加日志
|
||||
// 搜索 details 字段,使用 LIKE 进行模糊匹配
|
||||
whereClauses.push('details LIKE ?');
|
||||
const searchTermLike = `%${searchTerm}%`;
|
||||
params.push(searchTermLike);
|
||||
countParams.push(searchTermLike);
|
||||
}
|
||||
|
||||
|
||||
if (whereClauses.length > 0) {
|
||||
const whereSql = ` WHERE ${whereClauses.join(' AND ')}`;
|
||||
@@ -77,6 +94,9 @@ export class AuditLogRepository {
|
||||
baseSql += ' ORDER BY timestamp DESC LIMIT ? OFFSET ?';
|
||||
params.push(limit, offset);
|
||||
|
||||
console.log(`[Audit Repo] Executing count SQL: ${countSql} with params:`, countParams); // 添加日志
|
||||
console.log(`[Audit Repo] Executing base SQL: ${baseSql} with params:`, params); // 添加日志
|
||||
|
||||
try {
|
||||
const db = await getDbInstance();
|
||||
// First get the total count
|
||||
|
||||
@@ -41,9 +41,11 @@ export class AuditLogService {
|
||||
offset: number = 0,
|
||||
actionType?: AuditLogActionType,
|
||||
startDate?: number,
|
||||
endDate?: number
|
||||
endDate?: number,
|
||||
searchTerm?: string // 添加 searchTerm 参数
|
||||
): Promise<{ logs: AuditLogEntry[], total: number }> {
|
||||
return this.repository.getLogs(limit, offset, actionType, startDate, endDate);
|
||||
// 将 searchTerm 传递给 repository
|
||||
return this.repository.getLogs(limit, offset, actionType, startDate, endDate, searchTerm);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user