Skip to content

Commit 670e63c

Browse files
authored
fix(schedules): restore enabling/disabling of schedules, fix premature cron validation (#1807)
1 parent e62a635 commit 670e63c

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export async function PUT(request: NextRequest, { params }: { params: Promise<{
117117
}
118118

119119
const [workflowRecord] = await db
120-
.select({ userId: workflow.userId })
120+
.select({ userId: workflow.userId, workspaceId: workflow.workspaceId })
121121
.from(workflow)
122122
.where(eq(workflow.id, schedule.workflowId))
123123
.limit(1)
@@ -127,7 +127,18 @@ export async function PUT(request: NextRequest, { params }: { params: Promise<{
127127
return NextResponse.json({ error: 'Workflow not found' }, { status: 404 })
128128
}
129129

130-
if (workflowRecord.userId !== session.user.id) {
130+
let isAuthorized = workflowRecord.userId === session.user.id
131+
132+
if (!isAuthorized && workflowRecord.workspaceId) {
133+
const userPermission = await getUserEntityPermissions(
134+
session.user.id,
135+
'workspace',
136+
workflowRecord.workspaceId
137+
)
138+
isAuthorized = userPermission === 'write' || userPermission === 'admin'
139+
}
140+
141+
if (!isAuthorized) {
131142
logger.warn(`[${requestId}] User not authorized to modify this schedule: ${scheduleId}`)
132143
return NextResponse.json({ error: 'Not authorized to modify this schedule' }, { status: 403 })
133144
}

apps/sim/app/api/schedules/route.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export async function GET(req: NextRequest) {
121121
.limit(1)
122122

123123
const headers = new Headers()
124-
headers.set('Cache-Control', 'max-age=30')
124+
headers.set('Cache-Control', 'no-store, max-age=0')
125125

126126
if (schedule.length === 0) {
127127
return NextResponse.json({ schedule: null }, { headers })
@@ -301,9 +301,13 @@ export async function POST(req: NextRequest) {
301301
time: scheduleTime || 'not specified',
302302
})
303303

304-
cronExpression = generateCronExpression(defaultScheduleType, scheduleValues)
304+
const sanitizedScheduleValues =
305+
defaultScheduleType !== 'custom'
306+
? { ...scheduleValues, cronExpression: null }
307+
: scheduleValues
308+
309+
cronExpression = generateCronExpression(defaultScheduleType, sanitizedScheduleValues)
305310

306-
// Always validate the generated cron expression
307311
if (cronExpression) {
308312
const validation = validateCronExpression(cronExpression, timezone)
309313
if (!validation.isValid) {
@@ -318,7 +322,7 @@ export async function POST(req: NextRequest) {
318322
}
319323
}
320324

321-
nextRunAt = calculateNextRunTime(defaultScheduleType, scheduleValues)
325+
nextRunAt = calculateNextRunTime(defaultScheduleType, sanitizedScheduleValues)
322326

323327
logger.debug(
324328
`[${requestId}] Generated cron: ${cronExpression}, next run at: ${nextRunAt.toISOString()}`

apps/sim/lib/schedules/utils.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,6 @@ export function getScheduleTimeValues(starterBlock: BlockState): {
142142

143143
const cronExpression = getSubBlockValue(starterBlock, 'cronExpression') || null
144144

145-
// Validate cron expression if provided
146-
if (cronExpression) {
147-
const validation = validateCronExpression(cronExpression)
148-
if (!validation.isValid) {
149-
throw new Error(`Invalid cron expression: ${validation.error}`)
150-
}
151-
}
152-
153145
return {
154146
scheduleTime,
155147
scheduleStartAt,

0 commit comments

Comments
 (0)