Skip to content

Commit f93a946

Browse files
committed
consolidated permissions utils
1 parent f2950c7 commit f93a946

File tree

12 files changed

+377
-201
lines changed

12 files changed

+377
-201
lines changed

apps/sim/app/api/a2a/agents/[agentId]/route.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ export async function GET(request: NextRequest, { params }: { params: Promise<Ro
4545
}
4646

4747
if (!agent.agent.isPublished) {
48-
// Check if requester has access (for preview)
4948
const auth = await checkHybridAuth(request, { requireWorkflowId: false })
5049
if (!auth.success) {
5150
return NextResponse.json({ error: 'Agent not published' }, { status: 404 })
@@ -104,7 +103,6 @@ export async function PUT(request: NextRequest, { params }: { params: Promise<Ro
104103

105104
const body = await request.json()
106105

107-
// Update agent
108106
const [updatedAgent] = await db
109107
.update(a2aAgent)
110108
.set({
@@ -191,7 +189,6 @@ export async function POST(request: NextRequest, { params }: { params: Promise<R
191189
const action = body.action as 'publish' | 'unpublish' | 'refresh'
192190

193191
if (action === 'publish') {
194-
// Verify workflow is deployed
195192
const [wf] = await db
196193
.select({ isDeployed: workflow.isDeployed })
197194
.from(workflow)
@@ -232,7 +229,6 @@ export async function POST(request: NextRequest, { params }: { params: Promise<R
232229
}
233230

234231
if (action === 'refresh') {
235-
// Refresh skills from workflow
236232
const workflowData = await loadWorkflowFromNormalizedTables(existingAgent.workflowId)
237233
if (!workflowData) {
238234
return NextResponse.json({ error: 'Failed to load workflow' }, { status: 500 })

apps/sim/app/api/a2a/agents/route.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import { db } from '@sim/db'
8-
import { a2aAgent, workflow, workspace } from '@sim/db/schema'
8+
import { a2aAgent, workflow } from '@sim/db/schema'
99
import { createLogger } from '@sim/logger'
1010
import { and, eq, sql } from 'drizzle-orm'
1111
import { type NextRequest, NextResponse } from 'next/server'
@@ -16,6 +16,7 @@ import { sanitizeAgentName } from '@/lib/a2a/utils'
1616
import { checkHybridAuth } from '@/lib/auth/hybrid'
1717
import { loadWorkflowFromNormalizedTables } from '@/lib/workflows/persistence/utils'
1818
import { hasValidStartBlockInState } from '@/lib/workflows/triggers/trigger-utils'
19+
import { getWorkspaceById } from '@/lib/workspaces/permissions/utils'
1920

2021
const logger = createLogger('A2AAgentsAPI')
2122

@@ -38,18 +39,11 @@ export async function GET(request: NextRequest) {
3839
return NextResponse.json({ error: 'workspaceId is required' }, { status: 400 })
3940
}
4041

41-
// Verify workspace access
42-
const [ws] = await db
43-
.select({ id: workspace.id })
44-
.from(workspace)
45-
.where(eq(workspace.id, workspaceId))
46-
.limit(1)
47-
42+
const ws = await getWorkspaceById(workspaceId)
4843
if (!ws) {
4944
return NextResponse.json({ error: 'Workspace not found' }, { status: 404 })
5045
}
5146

52-
// Get agents with workflow info
5347
const agents = await db
5448
.select({
5549
id: a2aAgent.id,
@@ -108,7 +102,6 @@ export async function POST(request: NextRequest) {
108102
)
109103
}
110104

111-
// Verify workflow exists and belongs to workspace
112105
const [wf] = await db
113106
.select({
114107
id: workflow.id,
@@ -128,7 +121,6 @@ export async function POST(request: NextRequest) {
128121
)
129122
}
130123

131-
// Check if agent already exists for this workflow
132124
const [existing] = await db
133125
.select({ id: a2aAgent.id })
134126
.from(a2aAgent)
@@ -142,7 +134,6 @@ export async function POST(request: NextRequest) {
142134
)
143135
}
144136

145-
// Verify workflow has a start block
146137
const workflowData = await loadWorkflowFromNormalizedTables(workflowId)
147138
if (!workflowData || !hasValidStartBlockInState(workflowData)) {
148139
return NextResponse.json(
@@ -151,10 +142,8 @@ export async function POST(request: NextRequest) {
151142
)
152143
}
153144

154-
// Generate skills from workflow
155145
const skills = generateSkillsFromWorkflow(name || wf.name, description || wf.description)
156146

157-
// Create agent
158147
const agentId = uuidv4()
159148
const agentName = name || sanitizeAgentName(wf.name)
160149

0 commit comments

Comments
 (0)