Skip to content

Fix schedule creation rejecting today and timezone-aware dates - #3774

Open
SajalDevX wants to merge 3 commits into
ohcnetwork:developfrom
SajalDevX:issues/3773/schedule-valid-from-today
Open

SajalDevX wants to merge 3 commits into
ohcnetwork:developfrom
SajalDevX:issues/3773/schedule-valid-from-today

Conversation

@SajalDevX

@SajalDevX SajalDevX commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Proposed Changes

  • ScheduleCreateSpec.validate_dates now compares local calendar dates instead of comparing against a naive UTC "now". I treat values without a timezone (like the plain date the frontend sends) as local time.
  • A schedule can now start today, and timezone-aware valid_from / valid_to no longer cause a 500.
  • Added tests for both cases: starting today with date-only values (time frozen so the result doesn't depend on when the tests run), and timezone-aware datetimes.
  • I changed the two existing "date less than current date" tests to use yesterday instead of 30 minutes ago. The check is date-based now, which matches its error message and the availability exception check.

Associated Issue

Fixes #3773

The frontend lets users pick today and sends it as YYYY-MM-DD. That parsed to midnight, which is earlier than the current UTC time, so the request was rejected. Aware datetimes hit TypeError: can't compare offset-naive and offset-aware datetimes. Comparing timezone.localdate(value) with timezone.localdate(care_now()) fixes both.

Merge Checklist

  • Tests added/fixed
  • Update docs in /docs
  • Linting Complete
  • Any other necessary step

I ran test_schedule_api, test_booking_api and test_token_api locally and all 141 pass. Without the fix, the two new tests fail: one gets a 400 and the other a TypeError.

I used Claude to help track this down and write the tests; I reviewed the change and ran everything locally.

Only PR's with test cases included and passing lint and test pipelines will be reviewed

@ohcnetwork/care-backend-maintainers @ohcnetwork/care-backend-admins

Summary by CodeRabbit

  • Bug Fixes
    • Schedule dates are now validated against the current local calendar date. Schedules starting today are accepted, including date-only and timezone-aware inputs, while dates before today remain invalid.

The valid_from/valid_to check compared the input against a naive UTC
"now". A plain date for today (what the frontend sends) became midnight
and was rejected as being in the past, and a timezone-aware datetime
raised a TypeError (500).

Compare local calendar dates instead, treating naive values as local
time, which matches the error message and the availability exception
check.

Fixes ohcnetwork#3773
The check is now date-based, so the two "starts in the past" tests use
yesterday instead of 30 minutes ago.
@SajalDevX
SajalDevX requested a review from a team as a code owner September 27, 2026 07:18
@coderabbitai

coderabbitai Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: 70639602-3f23-43f0-aca3-6bcec0e6e3c6

📥 Commits

Reviewing files that changed from the base of the PR and between c7b5d59 and 4e22de6.

📒 Files selected for processing (2)
  • care/emr/resources/scheduling/schedule/spec.py
  • care/emr/tests/test_schedule_api.py

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.


📝 Walkthrough

Walkthrough

Schedule creation now validates valid_from and valid_to using local calendar dates. The API tests cover past dates, a date-only start date set to today, and timezone-aware date values.

Changes

Schedule date validation

Layer / File(s) Summary
Local calendar date validation
care/emr/resources/scheduling/schedule/spec.py, care/emr/tests/test_schedule_api.py
ScheduleCreateSpec.validate_dates makes naive values timezone-aware and rejects values whose local calendar date is before today. Tests cover past dates, a date-only start date set to today, and timezone-aware dates.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: vigneshhari

Merge Risk: ⚪ Minimal · up to 9f687

The date validation change is ready to merge after normal checks; no remaining issue was identified.

Security Architecture Review

Security architecture risk: 🔵 Low · up to 4e22d

Same-day and timezone-aware schedules can now be created, while the existing facility-scoped permission check remains in place. No new security vulnerability was established, but the intended timezone authority for every facility has not been confirmed.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The effective change is limited to which validity dates an authorized caller can submit for a facility-scoped resource. Accepted schedules can subsequently affect that resource’s availability and booking slots; no new cross-service or privilege dependency is evidenced.

Trust Boundaries and Controls

  • observed — Creation checks can_write_schedule for the facility-scoped resource before persisting a schedule. Resource resolution can create a resource before permission denial, but that hook predates this change and was already reachable with future-dated requests.

Resilience and Maintainability Implications

  • observed — Schedule and availability persistence is transactionally grouped, and downstream slot generation excludes slots whose end time is past. The inspected paths do not establish a create-time idempotency contract.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main fix: schedule creation now accepts today and timezone-aware dates.
Description check ✅ Passed The description explains the changes, linked issue, technical cause, tests, and checklist status. The optional architecture section is omitted appropriately, and the documentation item is marked as no…
Linked Issues check ✅ Passed The change satisfies the coding objectives in [#3773]. ScheduleCreateSpec.validate_dates converts naive values to aware local values and compares local calendar dates. This accepts a date-only value…
Out of Scope Changes check ✅ Passed The changes remain within [#3773]. The implementation changes schedule date validation, and the test updates cover the reported regressions and preserve past-date behavior. No unrelated product behavi…
Docstring Coverage ✅ Passed Docstring coverage is 85.71% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 2 files.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Creating a schedule that starts today fails, and timezone-aware dates return 500

1 participant