Skip to content

Commit f2a4847

Browse files
author
test
committed
fix: align workflow audit metadata helpers
Use the shared audit actor helper consistently so workflow deletion matches deploy behavior and remove the redundant deploy wrapper raised in review.
1 parent aab58cb commit f2a4847

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

apps/sim/app/api/workflows/[id]/deploy/route.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { db, workflow, workflowDeploymentVersion } from '@sim/db'
22
import { createLogger } from '@sim/logger'
33
import { and, desc, eq } from 'drizzle-orm'
44
import type { NextRequest } from 'next/server'
5-
import type { AuthResult } from '@/lib/auth/hybrid'
65
import { getAuditActorMetadata } from '@/lib/audit/actor-metadata'
76
import { AuditAction, AuditResourceType, recordAudit } from '@/lib/audit/log'
7+
import type { AuthResult } from '@/lib/auth/hybrid'
88
import { generateRequestId } from '@/lib/core/utils/request'
99
import { removeMcpToolsForWorkflow, syncMcpToolsForWorkflow } from '@/lib/mcp/workflow-mcp-sync'
1010
import {
@@ -61,10 +61,6 @@ async function validateLifecycleAdminAccess(
6161
}
6262
}
6363

64-
function getLifecycleAuditActor(auth: AuthResult | null | undefined) {
65-
return getAuditActorMetadata(auth)
66-
}
67-
6864
export async function GET(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
6965
const requestId = generateRequestId()
7066
const { id } = await params
@@ -294,7 +290,7 @@ export async function POST(request: NextRequest, { params }: { params: Promise<{
294290
// Sync MCP tools with the latest parameter schema
295291
await syncMcpToolsForWorkflow({ workflowId: id, requestId, context: 'deploy' })
296292

297-
const { actorName, actorEmail } = getLifecycleAuditActor(auth)
293+
const { actorName, actorEmail } = getAuditActorMetadata(auth)
298294

299295
recordAudit({
300296
workspaceId: workflowData?.workspaceId || null,
@@ -420,7 +416,7 @@ export async function DELETE(
420416
// Silently fail
421417
}
422418

423-
const { actorName, actorEmail } = getLifecycleAuditActor(auth)
419+
const { actorName, actorEmail } = getAuditActorMetadata(auth)
424420

425421
recordAudit({
426422
workspaceId: workflowData?.workspaceId || null,

apps/sim/app/api/workflows/[id]/route.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,13 @@ describe('Workflow By ID API Route', () => {
380380

381381
mockValidateWorkflowAccess.mockResolvedValue({
382382
workflow: mockWorkflow,
383-
auth: { success: true, userId: 'api-user-1', authType: 'api_key' },
383+
auth: {
384+
success: true,
385+
userId: 'api-user-1',
386+
authType: 'api_key',
387+
userName: 'API Key Actor',
388+
userEmail: null,
389+
},
384390
})
385391
mockGetWorkflowById.mockResolvedValue(mockWorkflow)
386392
mockAuthorizeWorkflowByWorkspacePermission.mockResolvedValue({
@@ -409,6 +415,13 @@ describe('Workflow By ID API Route', () => {
409415

410416
expect(response.status).toBe(200)
411417
expect(mockAuthorizeWorkflowByWorkspacePermission).not.toHaveBeenCalled()
418+
expect(auditMock.recordAudit).toHaveBeenCalledWith(
419+
expect.objectContaining({
420+
actorId: 'api-user-1',
421+
actorName: undefined,
422+
actorEmail: undefined,
423+
})
424+
)
412425
})
413426

414427
it('should prevent deletion of the last workflow in workspace', async () => {

apps/sim/app/api/workflows/[id]/route.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { createLogger } from '@sim/logger'
44
import { and, eq, isNull, ne } from 'drizzle-orm'
55
import { type NextRequest, NextResponse } from 'next/server'
66
import { z } from 'zod'
7+
import { getAuditActorMetadata } from '@/lib/audit/actor-metadata'
78
import { AuditAction, AuditResourceType, recordAudit } from '@/lib/audit/log'
89
import { AuthType, checkHybridAuth } from '@/lib/auth/hybrid'
910
import { env } from '@/lib/core/config/env'
@@ -153,7 +154,10 @@ export async function DELETE(
153154
})
154155
if (validation.error) {
155156
logger.warn(`[${requestId}] Unauthorized deletion attempt for workflow ${workflowId}`)
156-
return NextResponse.json({ error: validation.error.message }, { status: validation.error.status })
157+
return NextResponse.json(
158+
{ error: validation.error.message },
159+
{ status: validation.error.status }
160+
)
157161
}
158162

159163
const auth = validation.auth
@@ -323,11 +327,13 @@ export async function DELETE(
323327
// Don't fail the deletion if Socket.IO notification fails
324328
}
325329

330+
const { actorName, actorEmail } = getAuditActorMetadata(auth)
331+
326332
recordAudit({
327333
workspaceId: workflowData.workspaceId || null,
328334
actorId: userId,
329-
actorName: auth.userName,
330-
actorEmail: auth.userEmail,
335+
actorName,
336+
actorEmail,
331337
action: AuditAction.WORKFLOW_DELETED,
332338
resourceType: AuditResourceType.WORKFLOW,
333339
resourceId: workflowId,

0 commit comments

Comments
 (0)