Fix/minor tech debt#29791
Conversation
- Replaces flawed containment logic with strict interval intersection math - Centralizes Prisma where-clause generation into prismaIntervalOverlap.ts - Migrates getBusyTimes, BookingRepository, and HolidayRepository to new utility - Adds comprehensive behavioral and unit test coverage for overlapping scenarios
fix: resolve busy time overlap logic and centralize interval queries
|
Welcome to Cal.diy, @Ashid332! Thanks for opening this pull request. A few things to keep in mind:
A maintainer will review your PR soon. Thanks for contributing! |
|
Hey there and thank you for opening this pull request! 👋🏼 We require pull request titles to follow the Conventional Commits specification and it looks like your proposed title needs to be adjusted. Details: |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe change adds a shared Prisma interval-overlap helper with strict and inclusive boundary modes and input validation. Booking, busy-time, and holiday queries now use the helper, with tests covering overlap boundaries and helper behavior. User deletion now uses a new 🚥 Pre-merge checks | ✅ 2 | ❌ 3❌ Failed checks (1 warning, 2 inconclusive)
✅ Passed checks (2 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/features/busyTimes/services/getBusyTimes.test.ts`:
- Around line 407-420: Update the mock filtering logic around where,
startTimeCondition, and endTimeCondition to remove the broad as any cast and use
narrow, type-safe casts for the specific where properties. Replace the Day.js
comparisons with native Date comparisons using booking.startTime/endTime and the
lt/gt values, while preserving the existing optional-condition behavior.
In `@packages/features/users/lib/deleteUser.ts`:
- Around line 9-10: Update the repository construction in the delete-user flow
to pass the module’s existing Prisma client into UserRepository. Change the
UserRepository instantiation near the delete call to use prisma, while
preserving the existing delete({ id: user.id }) behavior.
In `@packages/lib/prismaIntervalOverlap.ts`:
- Around line 31-41: Replace the generic Error throws in the date validation
checks with ErrorWithCode imported from `@calcom/lib/errors`. Update each
validation failure in the interval validation logic to use the appropriate
ErrorWithCode construction while preserving the existing messages and validation
behavior.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: e7367634-6f8f-4c6a-9f3a-bafc2f5f8e82
📒 Files selected for processing (9)
packages/features/bookings/repositories/BookingRepository.tspackages/features/busyTimes/services/getBusyTimes.test.tspackages/features/busyTimes/services/getBusyTimes.tspackages/features/holidays/repositories/HolidayRepository.tspackages/features/profile/lib/checkUsername.tspackages/features/users/lib/deleteUser.tspackages/features/users/repositories/UserRepository.tspackages/lib/prismaIntervalOverlap.test.tspackages/lib/prismaIntervalOverlap.ts
| const where = args?.where as any; | ||
| const startTimeCondition = where?.startTime; | ||
| const endTimeCondition = where?.endTime; | ||
|
|
||
| return existingBookings.filter((booking) => { | ||
| // Emulate Prisma's lt/gt logic | ||
| const startsBeforeEnd = startTimeCondition?.lt | ||
| ? dayjs(booking.startTime).isBefore(dayjs(startTimeCondition.lt)) | ||
| : true; | ||
| const endsAfterStart = endTimeCondition?.gt | ||
| ? dayjs(booking.endTime).isAfter(dayjs(endTimeCondition.gt)) | ||
| : true; | ||
| return startsBeforeEnd && endsAfterStart; | ||
| }); |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Remove as any and use native Date comparisons.
As per coding guidelines:
- Never use
as any— use proper type-safe solutions instead. - Use native
Dateinstead of Day.js when timezone awareness isn't needed.
You can safely cast the specific where properties without bypassing the entire type system using any. Additionally, native Date comparisons (<, >) are more performant and idiomatic here than instantiating Day.js objects for simple before/after checks.
♻️ Proposed refactor
- const where = args?.where as any;
- const startTimeCondition = where?.startTime;
- const endTimeCondition = where?.endTime;
+ const where = args?.where;
+ const startTimeCondition = where?.startTime as { lt?: Date } | undefined;
+ const endTimeCondition = where?.endTime as { gt?: Date } | undefined;
return existingBookings.filter((booking) => {
// Emulate Prisma's lt/gt logic
const startsBeforeEnd = startTimeCondition?.lt
- ? dayjs(booking.startTime).isBefore(dayjs(startTimeCondition.lt))
+ ? booking.startTime < startTimeCondition.lt
: true;
const endsAfterStart = endTimeCondition?.gt
- ? dayjs(booking.endTime).isAfter(dayjs(endTimeCondition.gt))
+ ? booking.endTime > endTimeCondition.gt
: true;
return startsBeforeEnd && endsAfterStart;
});📝 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.
| const where = args?.where as any; | |
| const startTimeCondition = where?.startTime; | |
| const endTimeCondition = where?.endTime; | |
| return existingBookings.filter((booking) => { | |
| // Emulate Prisma's lt/gt logic | |
| const startsBeforeEnd = startTimeCondition?.lt | |
| ? dayjs(booking.startTime).isBefore(dayjs(startTimeCondition.lt)) | |
| : true; | |
| const endsAfterStart = endTimeCondition?.gt | |
| ? dayjs(booking.endTime).isAfter(dayjs(endTimeCondition.gt)) | |
| : true; | |
| return startsBeforeEnd && endsAfterStart; | |
| }); | |
| const where = args?.where; | |
| const startTimeCondition = where?.startTime as { lt?: Date } | undefined; | |
| const endTimeCondition = where?.endTime as { gt?: Date } | undefined; | |
| return existingBookings.filter((booking) => { | |
| // Emulate Prisma's lt/gt logic | |
| const startsBeforeEnd = startTimeCondition?.lt | |
| ? booking.startTime < startTimeCondition.lt | |
| : true; | |
| const endsAfterStart = endTimeCondition?.gt | |
| ? booking.endTime > endTimeCondition.gt | |
| : true; | |
| return startsBeforeEnd && endsAfterStart; | |
| }); |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@packages/features/busyTimes/services/getBusyTimes.test.ts` around lines 407 -
420, Update the mock filtering logic around where, startTimeCondition, and
endTimeCondition to remove the broad as any cast and use narrow, type-safe casts
for the specific where properties. Replace the Day.js comparisons with native
Date comparisons using booking.startTime/endTime and the lt/gt values, while
preserving the existing optional-condition behavior.
Source: Coding guidelines
What does this PR do?
Visual Demo (For contributors especially)
A visual demonstration is strongly recommended, for both the original and new change (video / image - any one).
Video Demo (if applicable):
Image Demo (if applicable):
Mandatory Tasks (DO NOT REMOVE)
How should this be tested?
Checklist