| 2 | * Converts schedule options to a cron expression |
| 3 | */ |
| 4 | export function convertScheduleOptionsToCron( |
| 5 | scheduleType: string, |
| 6 | options: Record<string, string> |
| 7 | ): string { |
| 8 | switch (scheduleType) { |
| 9 | case 'minutes': { |
| 10 | const interval = options.minutesInterval || '15' |
| 11 | // For example, if options.minutesStartingAt is provided, use that as the start minute. |
| 12 | return `*/${interval} * * * *` |
| 13 | } |
| 14 | case 'hourly': { |
| 15 | // When scheduling hourly, take the specified minute offset |
| 16 | return `${options.hourlyMinute || '00'} * * * *` |
| 17 | } |
| 18 | case 'daily': { |
| 19 | // Expected dailyTime in HH:MM |
| 20 | const [minute, hour] = (options.dailyTime || '00:09').split(':') |
| 21 | return `${minute || '00'} ${hour || '09'} * * *` |
| 22 | } |
| 23 | case 'weekly': { |
| 24 | // Expected weeklyDay as MON, TUE, etc. and weeklyDayTime in HH:MM |
| 25 | const dayMap: Record<string, number> = { |
| 26 | MON: 1, |
| 27 | TUE: 2, |
| 28 | WED: 3, |
| 29 | THU: 4, |
| 30 | FRI: 5, |
| 31 | SAT: 6, |
| 32 | SUN: 0, |
| 33 | } |
| 34 | const day = dayMap[options.weeklyDay || 'MON'] |
| 35 | const [minute, hour] = (options.weeklyDayTime || '00:09').split(':') |
| 36 | return `${minute || '00'} ${hour || '09'} * * ${day}` |
| 37 | } |
| 38 | case 'monthly': { |
| 39 | // Expected monthlyDay and monthlyTime in HH:MM |
| 40 | const day = options.monthlyDay || '1' |
| 41 | const [minute, hour] = (options.monthlyTime || '00:09').split(':') |
| 42 | return `${minute || '00'} ${hour || '09'} ${day} * *` |
| 43 | } |
| 44 | case 'custom': { |
| 45 | // Use the provided cron expression directly |
| 46 | return options.cronExpression |
| 47 | } |
| 48 | default: |
| 49 | throw new Error('Unsupported schedule type') |
| 50 | } |
| 51 | } |