Skip to content

Commit 22bb747

Browse files
committed
fix(audit-log): lazily resolve actor name/email when missing
1 parent 3c470ab commit 22bb747

File tree

1 file changed

+44
-22
lines changed

1 file changed

+44
-22
lines changed

apps/sim/lib/audit/log.ts

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { auditLog, db } from '@sim/db'
2+
import { user } from '@sim/db/schema'
23
import { createLogger } from '@sim/logger'
4+
import { eq } from 'drizzle-orm'
35
import { nanoid } from 'nanoid'
46

57
const logger = createLogger('AuditLog')
@@ -185,6 +187,7 @@ interface AuditLogParams {
185187

186188
/**
187189
* Records an audit log entry. Fire-and-forget — never throws or blocks the caller.
190+
* If actorName/actorEmail are not provided, resolves them from the user table.
188191
*/
189192
export function recordAudit(params: AuditLogParams): void {
190193
try {
@@ -194,31 +197,50 @@ export function recordAudit(params: AuditLogParams): void {
194197
undefined
195198
const userAgent = params.request?.headers.get('user-agent') ?? undefined
196199

197-
db.insert(auditLog)
198-
.values({
199-
id: nanoid(),
200-
workspaceId: params.workspaceId || null,
201-
actorId: params.actorId,
202-
action: params.action,
203-
resourceType: params.resourceType,
204-
resourceId: params.resourceId,
205-
actorName: params.actorName ?? undefined,
206-
actorEmail: params.actorEmail ?? undefined,
207-
resourceName: params.resourceName,
208-
description: params.description,
209-
metadata: params.metadata ?? {},
210-
ipAddress,
211-
userAgent,
212-
})
213-
.then(() => {
214-
logger.debug('Audit log recorded', {
200+
const needsLookup = params.actorId && !params.actorName && !params.actorEmail
201+
202+
const insertAudit = (actorName?: string | null, actorEmail?: string | null) => {
203+
db.insert(auditLog)
204+
.values({
205+
id: nanoid(),
206+
workspaceId: params.workspaceId || null,
207+
actorId: params.actorId,
215208
action: params.action,
216209
resourceType: params.resourceType,
210+
resourceId: params.resourceId,
211+
actorName: actorName ?? params.actorName ?? undefined,
212+
actorEmail: actorEmail ?? params.actorEmail ?? undefined,
213+
resourceName: params.resourceName,
214+
description: params.description,
215+
metadata: params.metadata ?? {},
216+
ipAddress,
217+
userAgent,
218+
})
219+
.then(() => {
220+
logger.debug('Audit log recorded', {
221+
action: params.action,
222+
resourceType: params.resourceType,
223+
})
224+
})
225+
.catch((error) => {
226+
logger.error('Failed to record audit log', { error, action: params.action })
227+
})
228+
}
229+
230+
if (needsLookup) {
231+
db.select({ name: user.name, email: user.email })
232+
.from(user)
233+
.where(eq(user.id, params.actorId))
234+
.limit(1)
235+
.then(([row]) => {
236+
insertAudit(row?.name, row?.email)
237+
})
238+
.catch(() => {
239+
insertAudit()
217240
})
218-
})
219-
.catch((error) => {
220-
logger.error('Failed to record audit log', { error, action: params.action })
221-
})
241+
} else {
242+
insertAudit()
243+
}
222244
} catch (error) {
223245
logger.error('Failed to initiate audit log', { error, action: params.action })
224246
}

0 commit comments

Comments
 (0)