Allow schedule cron inputs to use a user-selected timezone (#1228)#2180
Allow schedule cron inputs to use a user-selected timezone (#1228)#2180lancepioch wants to merge 3 commits intomainfrom
Conversation
Add a timezone dropdown (defaulting to the user's profile timezone) to the schedule form so cron inputs are interpreted in the chosen timezone. The selected timezone is used to compute next_run_at in UTC at save time with no database schema changes required. Resolves #1228
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 Fix all issues with AI agents
In `@app/Filament/Server/Resources/Schedules/Pages/CreateSchedule.php`:
- Around line 42-53: The timezone assignment may throw a TypeError if user()
returns null; update the assignment in CreateSchedule (the $timezone line) to
use the null-safe operator on user() so it reads the same fallback order but
accesses user()->timezone safely (i.e., use user()?->timezone), leaving the rest
of the logic and the call to ScheduleResource::getNextRun unchanged.
In `@app/Filament/Server/Resources/Schedules/Pages/EditSchedule.php`:
- Around line 40-50: The timezone resolution uses a direct user() call; change
it to use the null-safe operator so missing user doesn't raise an error: when
computing $timezone before calling ScheduleResource::getNextRun, replace the
user()->timezone usage with user()?->timezone so the expression reads
$data['timezone'] ?? user()?->timezone ?? 'UTC' and then leave subsequent
unset($data['timezone']) and the ScheduleResource::getNextRun call unchanged.
In `@app/Filament/Server/Resources/Schedules/ScheduleResource.php`:
- Around line 103-106: The current assignment uses user()->timezone which can
throw if user() is null; update the null-coalescing to use null-safe access
(e.g., user()?->timezone) so $timezone = $get('timezone') ?? user()?->timezone
?? 'UTC'; locate this in ScheduleResource (the line that sets $timezone) and
replace the direct property access with the null-safe operator to prevent
TypeError during unauthenticated requests.
- Around line 300-306: The Select field for 'timezone' uses user()->timezone in
->default(...) which can throw if user() is null; change the default call in the
Select::make('timezone') chain to use the null-safe operator (user()?->timezone
?? 'UTC') so it safely falls back to 'UTC' when no user exists, keeping the rest
of the chain (options, searchable, live, hiddenOn) unchanged.
🧹 Nitpick comments (2)
app/Http/Controllers/Api/Client/Servers/ScheduleController.php (2)
78-78: Consider using null-safe operator for defensive coding.While
StoreScheduleRequestlikely enforces authentication, using the null-safe operator maintains consistency with defensive patterns elsewhere in the codebase.🛡️ Proposed fix
- 'next_run_at' => $this->getNextRunAt($request, $request->user()->timezone ?? 'UTC'), + 'next_run_at' => $this->getNextRunAt($request, $request->user()?->timezone ?? 'UTC'),
134-134: Same null-safe pattern recommended here.🛡️ Proposed fix
- 'next_run_at' => $this->getNextRunAt($request, $request->user()->timezone ?? 'UTC'), + 'next_run_at' => $this->getNextRunAt($request, $request->user()?->timezone ?? 'UTC'),
| $timezone = $data['timezone'] ?? user()->timezone ?? 'UTC'; | ||
| unset($data['timezone']); | ||
|
|
||
| if (!isset($data['next_run_at'])) { | ||
| $data['next_run_at'] = ScheduleResource::getNextRun( | ||
| $data['cron_minute'], | ||
| $data['cron_hour'], | ||
| $data['cron_day_of_month'], | ||
| $data['cron_month'], | ||
| $data['cron_day_of_week'] | ||
| $data['cron_day_of_week'], | ||
| $timezone | ||
| ); |
There was a problem hiding this comment.
Apply null-safe operator for user() call.
Consistent with the same pattern used elsewhere in this PR, use the null-safe operator to prevent potential TypeError.
🛡️ Proposed fix
- $timezone = $data['timezone'] ?? user()->timezone ?? 'UTC';
+ $timezone = $data['timezone'] ?? user()?->timezone ?? 'UTC';📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| $timezone = $data['timezone'] ?? user()->timezone ?? 'UTC'; | |
| unset($data['timezone']); | |
| if (!isset($data['next_run_at'])) { | |
| $data['next_run_at'] = ScheduleResource::getNextRun( | |
| $data['cron_minute'], | |
| $data['cron_hour'], | |
| $data['cron_day_of_month'], | |
| $data['cron_month'], | |
| $data['cron_day_of_week'] | |
| $data['cron_day_of_week'], | |
| $timezone | |
| ); | |
| $timezone = $data['timezone'] ?? user()?->timezone ?? 'UTC'; | |
| unset($data['timezone']); | |
| if (!isset($data['next_run_at'])) { | |
| $data['next_run_at'] = ScheduleResource::getNextRun( | |
| $data['cron_minute'], | |
| $data['cron_hour'], | |
| $data['cron_day_of_month'], | |
| $data['cron_month'], | |
| $data['cron_day_of_week'], | |
| $timezone | |
| ); |
🤖 Prompt for AI Agents
In `@app/Filament/Server/Resources/Schedules/Pages/CreateSchedule.php` around
lines 42 - 53, The timezone assignment may throw a TypeError if user() returns
null; update the assignment in CreateSchedule (the $timezone line) to use the
null-safe operator on user() so it reads the same fallback order but accesses
user()->timezone safely (i.e., use user()?->timezone), leaving the rest of the
logic and the call to ScheduleResource::getNextRun unchanged.
| $timezone = $data['timezone'] ?? user()->timezone ?? 'UTC'; | ||
| unset($data['timezone']); | ||
|
|
||
| $data['next_run_at'] = ScheduleResource::getNextRun( | ||
| $data['cron_minute'], | ||
| $data['cron_hour'], | ||
| $data['cron_day_of_month'], | ||
| $data['cron_month'], | ||
| $data['cron_day_of_week'] | ||
| $data['cron_day_of_week'], | ||
| $timezone | ||
| ); |
There was a problem hiding this comment.
Apply null-safe operator for user() call.
Same pattern as CreateSchedule.php - use the null-safe operator for consistency.
🛡️ Proposed fix
- $timezone = $data['timezone'] ?? user()->timezone ?? 'UTC';
+ $timezone = $data['timezone'] ?? user()?->timezone ?? 'UTC';📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| $timezone = $data['timezone'] ?? user()->timezone ?? 'UTC'; | |
| unset($data['timezone']); | |
| $data['next_run_at'] = ScheduleResource::getNextRun( | |
| $data['cron_minute'], | |
| $data['cron_hour'], | |
| $data['cron_day_of_month'], | |
| $data['cron_month'], | |
| $data['cron_day_of_week'] | |
| $data['cron_day_of_week'], | |
| $timezone | |
| ); | |
| $timezone = $data['timezone'] ?? user()?->timezone ?? 'UTC'; | |
| unset($data['timezone']); | |
| $data['next_run_at'] = ScheduleResource::getNextRun( | |
| $data['cron_minute'], | |
| $data['cron_hour'], | |
| $data['cron_day_of_month'], | |
| $data['cron_month'], | |
| $data['cron_day_of_week'], | |
| $timezone | |
| ); |
🤖 Prompt for AI Agents
In `@app/Filament/Server/Resources/Schedules/Pages/EditSchedule.php` around lines
40 - 50, The timezone resolution uses a direct user() call; change it to use the
null-safe operator so missing user doesn't raise an error: when computing
$timezone before calling ScheduleResource::getNextRun, replace the
user()->timezone usage with user()?->timezone so the expression reads
$data['timezone'] ?? user()?->timezone ?? 'UTC' and then leave subsequent
unset($data['timezone']) and the ScheduleResource::getNextRun call unchanged.
| $timezone = $get('timezone') ?? user()->timezone ?? 'UTC'; | ||
|
|
||
| try { | ||
| $nextRun = Utilities::getScheduleNextRunDate($get('cron_minute'), $get('cron_hour'), $get('cron_day_of_month'), $get('cron_month'), $get('cron_day_of_week'))->timezone(user()->timezone ?? 'UTC'); | ||
| $nextRun = Utilities::getScheduleNextRunDate($get('cron_minute'), $get('cron_hour'), $get('cron_day_of_month'), $get('cron_month'), $get('cron_day_of_week'), $timezone)->timezone($timezone); |
There was a problem hiding this comment.
Null safety issue: user() may return null.
The user() helper can return null when no user is authenticated. Accessing ->timezone on null will throw a TypeError. While this form likely requires authentication, it's safer to use null-safe access.
🛡️ Proposed fix using null-safe operator
- $timezone = $get('timezone') ?? user()->timezone ?? 'UTC';
+ $timezone = $get('timezone') ?? user()?->timezone ?? 'UTC';📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| $timezone = $get('timezone') ?? user()->timezone ?? 'UTC'; | |
| try { | |
| $nextRun = Utilities::getScheduleNextRunDate($get('cron_minute'), $get('cron_hour'), $get('cron_day_of_month'), $get('cron_month'), $get('cron_day_of_week'))->timezone(user()->timezone ?? 'UTC'); | |
| $nextRun = Utilities::getScheduleNextRunDate($get('cron_minute'), $get('cron_hour'), $get('cron_day_of_month'), $get('cron_month'), $get('cron_day_of_week'), $timezone)->timezone($timezone); | |
| $timezone = $get('timezone') ?? user()?->timezone ?? 'UTC'; | |
| try { | |
| $nextRun = Utilities::getScheduleNextRunDate($get('cron_minute'), $get('cron_hour'), $get('cron_day_of_month'), $get('cron_month'), $get('cron_day_of_week'), $timezone)->timezone($timezone); |
🤖 Prompt for AI Agents
In `@app/Filament/Server/Resources/Schedules/ScheduleResource.php` around lines
103 - 106, The current assignment uses user()->timezone which can throw if
user() is null; update the null-coalescing to use null-safe access (e.g.,
user()?->timezone) so $timezone = $get('timezone') ?? user()?->timezone ??
'UTC'; locate this in ScheduleResource (the line that sets $timezone) and
replace the direct property access with the null-safe operator to prevent
TypeError during unauthenticated requests.
| Select::make('timezone') | ||
| ->label(trans('server/schedule.timezone')) | ||
| ->options(fn () => array_combine(timezone_identifiers_list(), timezone_identifiers_list())) | ||
| ->default(user()->timezone ?? 'UTC') | ||
| ->searchable() | ||
| ->live() | ||
| ->hiddenOn('view'), |
There was a problem hiding this comment.
Apply the same null-safe fix to the default value.
Same issue as above - use the null-safe operator for consistency and safety.
🛡️ Proposed fix
- ->default(user()->timezone ?? 'UTC')
+ ->default(user()?->timezone ?? 'UTC')🤖 Prompt for AI Agents
In `@app/Filament/Server/Resources/Schedules/ScheduleResource.php` around lines
300 - 306, The Select field for 'timezone' uses user()->timezone in
->default(...) which can throw if user() is null; change the default call in the
Select::make('timezone') chain to use the null-safe operator (user()?->timezone
?? 'UTC') so it safely falls back to 'UTC' when no user exists, keeping the rest
of the chain (options, searchable, live, hiddenOn) unchanged.
Add a timezone dropdown (defaulting to the user's profile timezone) to the schedule form so cron inputs are interpreted in the chosen timezone. The selected timezone is used to compute next_run_at in UTC at save time with no database schema changes required.
Resolves #1228