Skip to content

Commit 88be375

Browse files
committed
fix(db): fix duplicate db query
1 parent b4d94b3 commit 88be375

2 files changed

Lines changed: 11 additions & 33 deletions

File tree

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

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,7 @@ const logger = createLogger('ScheduleAPI')
2727

2828
export const dynamic = 'force-dynamic'
2929

30-
type ScheduleRow = {
31-
id: string
32-
workflowId: string | null
33-
status: string
34-
cronExpression: string | null
35-
timezone: string | null
36-
sourceType: string | null
37-
sourceWorkspaceId: string | null
38-
jobTitle: string | null
39-
}
30+
type ScheduleRow = typeof workflowSchedule.$inferSelect
4031

4132
async function fetchAndAuthorize(
4233
requestId: string,
@@ -45,16 +36,7 @@ async function fetchAndAuthorize(
4536
action: 'read' | 'write'
4637
): Promise<{ schedule: ScheduleRow; workspaceId: string | null } | NextResponse> {
4738
const [schedule] = await db
48-
.select({
49-
id: workflowSchedule.id,
50-
workflowId: workflowSchedule.workflowId,
51-
status: workflowSchedule.status,
52-
cronExpression: workflowSchedule.cronExpression,
53-
timezone: workflowSchedule.timezone,
54-
sourceType: workflowSchedule.sourceType,
55-
sourceWorkspaceId: workflowSchedule.sourceWorkspaceId,
56-
jobTitle: workflowSchedule.jobTitle,
57-
})
39+
.select()
5840
.from(workflowSchedule)
5941
.where(and(eq(workflowSchedule.id, scheduleId), isNull(workflowSchedule.archivedAt)))
6042
.limit(1)
@@ -121,20 +103,12 @@ export const GET = withRouteHandler(
121103

122104
const { id: scheduleId } = parsed.data.params
123105

106+
// fetchAndAuthorize already loads the full row (and 404s if missing), so
107+
// return it directly — no second query.
124108
const authResult = await fetchAndAuthorize(requestId, scheduleId, session.user.id, 'read')
125109
if (authResult instanceof NextResponse) return authResult
126110

127-
const [row] = await db
128-
.select()
129-
.from(workflowSchedule)
130-
.where(and(eq(workflowSchedule.id, scheduleId), isNull(workflowSchedule.archivedAt)))
131-
.limit(1)
132-
133-
if (!row) {
134-
return NextResponse.json({ error: 'Schedule not found' }, { status: 404 })
135-
}
136-
137-
return NextResponse.json({ schedule: row })
111+
return NextResponse.json({ schedule: authResult.schedule })
138112
} catch (error) {
139113
logger.error(`[${requestId}] Failed to get schedule`, { error })
140114
return NextResponse.json({ error: 'Failed to get schedule' }, { status: 500 })

apps/sim/lib/copilot/chat/process-contents.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
authorizeWorkflowByWorkspacePermission,
66
getActiveWorkflowRecord,
77
} from '@sim/workflow-authz'
8-
import { and, eq, isNull } from 'drizzle-orm'
8+
import { and, eq, isNull, ne } from 'drizzle-orm'
99
import { normalizeVfsSegment } from '@/lib/copilot/vfs/normalize-segment'
1010
import {
1111
buildVfsFolderPathMap,
@@ -744,7 +744,11 @@ async function resolveScheduledTaskResource(
744744
eq(workflowSchedule.id, scheduleId),
745745
eq(workflowSchedule.sourceWorkspaceId, workspaceId),
746746
eq(workflowSchedule.sourceType, 'job'),
747-
isNull(workflowSchedule.archivedAt)
747+
isNull(workflowSchedule.archivedAt),
748+
// Mirror the VFS materializer (workspace-vfs `materializeJobs`), which
749+
// excludes completed jobs — otherwise we'd point at a meta.json it never
750+
// wrote and the agent's read would dangle.
751+
ne(workflowSchedule.status, 'completed')
748752
)
749753
)
750754
.limit(1)

0 commit comments

Comments
 (0)