-
Notifications
You must be signed in to change notification settings - Fork 10.2k
fix: resolve circular dependency in getUserAvailability by extracting getPeriodStartDatesBetween utility #22913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: resolve circular dependency in getUserAvailability by extracting getPeriodStartDatesBetween utility #22913
Conversation
… getPeriodStartDatesBetween utility - Extract getPeriodStartDatesBetween into standalone utility function - Remove dependency on getUserAvailabilityService from getBusyTimesFromLimits.ts - Update UserAvailabilityService to delegate to utility function - Breaks circular dependency chain while preserving all existing functionality - Fixes ReferenceError during fast refresh in development mode Co-Authored-By: zomars@cal.com <zomars@me.com>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
WalkthroughThe changes refactor how the calculation of period start dates between two dates is handled across the codebase. The internal method for generating these dates within the Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes 📜 Recent review detailsConfiguration used: CodeRabbit UI ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (1)
✅ Files skipped from review due to trivial changes (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
✨ Finishing Touches🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @calcom/foundation this is causing issues with local development
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
packages/lib/intervalLimits/utils/getPeriodStartDatesBetween.ts (1)
7-22
: Consider optimizing Day.js usage in the loop for performance.The while loop creates new Day.js objects on each iteration (line 19:
startDate.add(1, period)
), which could impact performance in hot paths. Per coding guidelines, excessive Day.js use should be flagged in performance-critical code.However, given the complexity of timezone-aware period calculations and the need for
.startOf()
,.endOf()
, and.add()
operations, the current Day.js approach may be necessary for correctness.Consider profiling this function if it becomes a bottleneck, especially for large date ranges or frequent calls.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
packages/lib/getUserAvailability.ts
(2 hunks)packages/lib/intervalLimits/server/getBusyTimesFromLimits.ts
(3 hunks)packages/lib/intervalLimits/utils/getPeriodStartDatesBetween.ts
(1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.ts
📄 CodeRabbit Inference Engine (.cursor/rules/review.mdc)
**/*.ts
: For Prisma queries, only select data you need; never useinclude
, always useselect
Ensure thecredential.key
field is never returned from tRPC endpoints or APIs
Files:
packages/lib/intervalLimits/server/getBusyTimesFromLimits.ts
packages/lib/intervalLimits/utils/getPeriodStartDatesBetween.ts
packages/lib/getUserAvailability.ts
**/*.{ts,tsx}
📄 CodeRabbit Inference Engine (.cursor/rules/review.mdc)
Flag excessive Day.js use in performance-critical code; prefer native Date or Day.js
.utc()
in hot paths like loops
Files:
packages/lib/intervalLimits/server/getBusyTimesFromLimits.ts
packages/lib/intervalLimits/utils/getPeriodStartDatesBetween.ts
packages/lib/getUserAvailability.ts
🧠 Learnings (5)
📓 Common learnings
Learnt from: din-prajapati
PR: calcom/cal.com#21854
File: packages/app-store/office365calendar/__tests__/unit_tests/SubscriptionManager.test.ts:0-0
Timestamp: 2025-08-05T12:04:29.037Z
Learning: In packages/app-store/office365calendar/lib/CalendarService.ts, the fetcher method in Office365CalendarService class is public, not private. It was specifically changed from private to public in this PR to support proper testing and external access patterns.
📚 Learning: in packages/app-store/office365calendar/lib/calendarservice.ts, the fetcher method in office365calen...
Learnt from: din-prajapati
PR: calcom/cal.com#21854
File: packages/app-store/office365calendar/__tests__/unit_tests/SubscriptionManager.test.ts:0-0
Timestamp: 2025-08-05T12:04:29.037Z
Learning: In packages/app-store/office365calendar/lib/CalendarService.ts, the fetcher method in Office365CalendarService class is public, not private. It was specifically changed from private to public in this PR to support proper testing and external access patterns.
Applied to files:
packages/lib/intervalLimits/server/getBusyTimesFromLimits.ts
packages/lib/getUserAvailability.ts
📚 Learning: applies to **/*.{ts,tsx} : flag excessive day.js use in performance-critical code; prefer native dat...
Learnt from: CR
PR: calcom/cal.com#0
File: .cursor/rules/review.mdc:0-0
Timestamp: 2025-07-28T11:50:23.946Z
Learning: Applies to **/*.{ts,tsx} : Flag excessive Day.js use in performance-critical code; prefer native Date or Day.js `.utc()` in hot paths like loops
Applied to files:
packages/lib/intervalLimits/server/getBusyTimesFromLimits.ts
packages/lib/intervalLimits/utils/getPeriodStartDatesBetween.ts
packages/lib/getUserAvailability.ts
📚 Learning: the office365calendar webhook handler in packages/app-store/office365calendar/api/webhook.ts is spec...
Learnt from: vijayraghav-io
PR: calcom/cal.com#21072
File: packages/app-store/office365calendar/api/webhook.ts:120-123
Timestamp: 2025-07-18T17:57:16.395Z
Learning: The office365calendar webhook handler in packages/app-store/office365calendar/api/webhook.ts is specifically designed for Office365 calendar integration, not as a generic webhook handler. Therefore, it's safe to assume that fetchAvailabilityAndSetCache method will be implemented in the Office365CalendarService, making explicit validation checks unnecessary.
Applied to files:
packages/lib/intervalLimits/server/getBusyTimesFromLimits.ts
📚 Learning: in the insightsbookingservice (packages/lib/server/service/insightsbooking.ts), the constructor stor...
Learnt from: eunjae-lee
PR: calcom/cal.com#22702
File: packages/lib/server/service/insightsBooking.ts:120-124
Timestamp: 2025-07-24T08:39:06.185Z
Learning: In the InsightsBookingService (packages/lib/server/service/insightsBooking.ts), the constructor stores null for invalid options or filters but this is handled safely through null checks in buildFilterConditions() and buildAuthorizationConditions() methods. The service uses defensive programming to return safe fallback conditions (null or NOTHING_CONDITION) rather than throwing errors on invalid inputs.
Applied to files:
packages/lib/intervalLimits/server/getBusyTimesFromLimits.ts
🧬 Code Graph Analysis (1)
packages/lib/intervalLimits/server/getBusyTimesFromLimits.ts (1)
packages/lib/intervalLimits/utils/getPeriodStartDatesBetween.ts (1)
getPeriodStartDatesBetween
(24-24)
🔇 Additional comments (6)
packages/lib/intervalLimits/utils/getPeriodStartDatesBetween.ts (1)
24-24
: LGTM! Proper error reporting integration.The utility function is correctly wrapped with Sentry error reporting, maintaining consistency with the existing codebase patterns.
packages/lib/intervalLimits/server/getBusyTimesFromLimits.ts (3)
6-6
: LGTM! Proper import of the new utility.The import correctly references the extracted utility function, which will break the circular dependency.
108-108
: LGTM! Service dependency successfully removed.The direct call to
getPeriodStartDatesBetween
utility replaces the previous dependency onuserAvailabilityService.getPeriodStartDatesBetween
, effectively breaking the circular dependency while maintaining the same functionality.
170-170
: LGTM! Consistent replacement in duration limits function.The utility function is used consistently across both booking limits and duration limits functions, ensuring uniform behavior.
packages/lib/getUserAvailability.ts (2)
40-40
: LGTM! Proper import with alias to avoid naming conflicts.The import uses a clear alias
getPeriodStartDatesBetweenUtil
to distinguish from the class method, preventing any naming confusion.
599-603
: LGTM! Clean delegation preserves existing API.The public method now properly delegates to the shared utility while maintaining:
- The same method signature and parameter types
- Sentry error reporting wrapper consistency
- Backward compatibility for existing callers
This completes the circular dependency resolution by having the service use the shared utility instead of maintaining its own implementation.
E2E results are ready! |
…-user-availability
The latest updates on your projects. Learn more about Vercel for Git ↗︎ |
… getPeriodStartDatesBetween utility (calcom#22913) Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
fix: resolve circular dependency in getUserAvailability by extracting getPeriodStartDatesBetween utility
Summary
This PR fixes a circular dependency issue that was causing
ReferenceError: Cannot access 'getUserAvailabilityModule' before initialization
during fast refresh in development mode.The Problem:
A circular dependency chain existed between:
packages/lib/di/containers/get-user-availability.ts
→ importsgetUserAvailabilityModule
packages/lib/intervalLimits/server/getBusyTimesFromLimits.ts
→ importsgetUserAvailabilityService
packages/lib/getUserAvailability.ts
→ importsgetBusyTimesFromLimits
This caused module initialization order issues during fast refresh, breaking the development experience.
The Solution:
getPeriodStartDatesBetween
into a standalone utility function atpackages/lib/intervalLimits/utils/getPeriodStartDatesBetween.ts
getBusyTimesFromLimits.ts
to import the utility directly instead of calling through the serviceUserAvailabilityService
to delegate to the utility function, preserving the existing APIReview & Testing Checklist for Human
ReferenceError: Cannot access 'getUserAvailabilityModule' before initialization
no longer occursRecommended Test Plan:
yarn dev
packages/lib/getUserAvailability.ts
andpackages/lib/intervalLimits/server/getBusyTimesFromLimits.ts
Diagram
Notes
withReporting
wrapperSession Details: