feat(repos): add guest-availability booking + user lookup methods#28908
feat(repos): add guest-availability booking + user lookup methods#28908bcornish1797 wants to merge 1 commit intocalcom:mainfrom
Conversation
Split of calcom#28636 (Part A of 3). Pure additive infra — no call sites change and no existing behaviour is altered. This layer is the data-access foundation that Parts B (frontend `rescheduledBy` plumbing) and C (slots/util.ts business logic) build on. BookingRepository: - findByUidIncludeAttendeeEmails(uid): fetches an original booking's attendee emails and the host user's email, used to detect who the reschedule initiator is and resolve attendees to Cal.com users. - findByUserIdsAndDateRange({ userIds, userEmails, dateFrom, dateTo, excludeUid? }): finds ACCEPTED/PENDING bookings overlapping a date range by userId or attendee email (case-insensitive), with an excludeUid parameter applied at the database level so the caller cannot accidentally include the very booking being rescheduled. UserRepository: - findByEmails({ emails }): resolves a list of emails to Cal.com users, checking both primary email and verified secondary emails, case-insensitively, with input deduplication before the query and output deduplication by user id. Uses Promise.all to fan out the two lookups concurrently. Tests cover: empty-input short-circuits, primary vs secondary lookup, dedup across both lookups, case-insensitive normalization, excludeUid, OR clause composition, and the select shape used downstream.
|
Welcome to Cal.diy, @bcornish1797! Thanks for opening this pull request. A few things to keep in mind:
A maintainer will review your PR soon. Thanks for contributing! |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3011eebafb
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| attendees: { select: { email: true } }, | ||
| user: { select: { email: true } }, | ||
| }, |
There was a problem hiding this comment.
Include host email fallback in booking lookup select
findByUidIncludeAttendeeEmails only selects user.email, so when a booking has no related user row (the Booking.userId relation is nullable and other codepaths already handle missing user data), this method returns no organizer email at all. That makes downstream reschedule logic unable to reliably determine host-vs-guest initiator for those bookings; selecting userPrimaryEmail (or an equivalent fallback) here would avoid dropping host identity.
Useful? React with 👍 / 👎.
| OR: [ | ||
| ...(userIds.length > 0 ? [{ userId: { in: userIds } }] : []), | ||
| ...(userEmails.length > 0 | ||
| ? [{ attendees: { some: { email: { in: userEmails, mode: "insensitive" as const } } } }] | ||
| : []), |
There was a problem hiding this comment.
Match bookings by host email when userId is missing
findByUserIdsAndDateRange only matches bookings via userId or attendee email, so organizer-owned bookings with userId = null but a populated userPrimaryEmail are skipped unless the organizer also appears as an attendee. In those cases, busy windows are undercounted and reschedule availability can include invalid slots; adding a userPrimaryEmail email-match branch to this OR would cover that data shape.
Useful? React with 👍 / 👎.
📝 WalkthroughWalkthroughThe PR adds two new query methods to 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning Review ran into problems🔥 ProblemsGit: Failed to clone repository. Please run 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 |
First of three PRs splitting #28636 for easier review.
Adds two lookup methods on BookingRepository (findByUidIncludeAttendeeEmails, findByUserIdsAndDateRange) and one on UserRepository (findByEmails). No call sites yet; this is pure data-access infra that Parts B (#28909) and C (#28911) build on. Together the three implement the guest-availability-on-host-reschedule scope from #16378.
Unit tests use vi.fn() mocks, follow the existing BookingRepository.test.ts / UserRepository.test.ts patterns. No schema or dependency changes.