| 36 | } |
| 37 | |
| 38 | export function buildFilterConditions(params: AuditLogFilterParams): SQL<unknown>[] { |
| 39 | const conditions: SQL<unknown>[] = [] |
| 40 | |
| 41 | if (params.action) conditions.push(eq(auditLog.action, params.action)) |
| 42 | if (params.resourceType) { |
| 43 | const types = params.resourceType.split(',').filter(Boolean) |
| 44 | if (types.length === 1) conditions.push(eq(auditLog.resourceType, types[0])) |
| 45 | else if (types.length > 1) conditions.push(inArray(auditLog.resourceType, types)) |
| 46 | } |
| 47 | if (params.resourceId) conditions.push(eq(auditLog.resourceId, params.resourceId)) |
| 48 | if (params.workspaceId) conditions.push(eq(auditLog.workspaceId, params.workspaceId)) |
| 49 | if (params.actorId) conditions.push(eq(auditLog.actorId, params.actorId)) |
| 50 | if (params.actorEmail) conditions.push(eq(auditLog.actorEmail, params.actorEmail)) |
| 51 | |
| 52 | if (params.search) { |
| 53 | const escaped = params.search.replace(/[%_\\]/g, '\\$&') |
| 54 | const searchTerm = `%${escaped}%` |
| 55 | conditions.push( |
| 56 | or( |
| 57 | ilike(auditLog.action, searchTerm), |
| 58 | ilike(auditLog.actorEmail, searchTerm), |
| 59 | ilike(auditLog.actorName, searchTerm), |
| 60 | ilike(auditLog.resourceName, searchTerm), |
| 61 | ilike(auditLog.description, searchTerm) |
| 62 | )! |
| 63 | ) |
| 64 | } |
| 65 | |
| 66 | if (params.startDate) conditions.push(gte(auditLog.createdAt, new Date(params.startDate))) |
| 67 | if (params.endDate) conditions.push(lte(auditLog.createdAt, new Date(params.endDate))) |
| 68 | |
| 69 | return conditions |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns the IDs of all workspaces attached to the organization. |