Skip to content

Conversation

joeauyeung
Copy link
Contributor

@joeauyeung joeauyeung commented Jul 28, 2025

What does this PR do?

Adds a log statement to record the current availability of a booking organizer at the time of booking

Mandatory Tasks (DO NOT REMOVE)

  • I have self-reviewed the code (A decent size PR without self-review might be rejected).
  • I have updated the developer docs in /docs if this PR makes changes that would require a documentation change. If N/A, write N/A here and check the checkbox.
  • I confirm automated tests are in place that prove my fix is effective or that my feature works.

How should this be tested?

  • Are there environment variables that should be set?
  • What are the minimal test data to have?
  • What is expected (happy path) to have (input and output)?
  • Any other important info that could help to test that PR

Copy link
Contributor

coderabbitai bot commented Jul 28, 2025

Walkthrough

The changes update the booking handling logic to capture and log detailed user availability data at the time of booking creation. The availableUsers array is now scoped higher in the booking handler to allow later access. When a booking is created, the code attempts to find and log the organizer's availability snapshot. The process for determining available users is enhanced by storing the full availability data alongside each user. Type definitions are updated to include this new optional availabilityData field, and the type for user availability results is corrected to reflect asynchronous behavior.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6c93647 and ec0c7cc.

📒 Files selected for processing (4)
  • packages/features/bookings/lib/handleNewBooking.ts (2 hunks)
  • packages/features/bookings/lib/handleNewBooking/ensureAvailableUsers.ts (3 hunks)
  • packages/features/bookings/lib/handleNewBooking/types.ts (2 hunks)
  • packages/lib/getUserAvailability.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 use include, always use select
Ensure the credential.key field is never returned from tRPC endpoints or APIs

Files:

  • packages/features/bookings/lib/handleNewBooking/types.ts
  • packages/lib/getUserAvailability.ts
  • packages/features/bookings/lib/handleNewBooking/ensureAvailableUsers.ts
  • packages/features/bookings/lib/handleNewBooking.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/features/bookings/lib/handleNewBooking/types.ts
  • packages/lib/getUserAvailability.ts
  • packages/features/bookings/lib/handleNewBooking/ensureAvailableUsers.ts
  • packages/features/bookings/lib/handleNewBooking.ts
🧠 Learnings (4)
📓 Common learnings
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.
Learnt from: eunjae-lee
PR: calcom/cal.com#22106
File: packages/features/insights/components/FailedBookingsByField.tsx:65-71
Timestamp: 2025-07-15T12:59:34.389Z
Learning: In the FailedBookingsByField component (packages/features/insights/components/FailedBookingsByField.tsx), although routingFormId is typed as optional in useInsightsParameters, the system automatically enforces a routing form filter, so routingFormId is always present in practice. This means the data always contains only one entry, making the single-entry destructuring approach safe.
packages/features/bookings/lib/handleNewBooking/types.ts (2)

Learnt from: eunjae-lee
PR: #22106
File: packages/features/insights/components/FailedBookingsByField.tsx:65-71
Timestamp: 2025-07-15T12:59:34.389Z
Learning: In the FailedBookingsByField component (packages/features/insights/components/FailedBookingsByField.tsx), although routingFormId is typed as optional in useInsightsParameters, the system automatically enforces a routing form filter, so routingFormId is always present in practice. This means the data always contains only one entry, making the single-entry destructuring approach safe.

Learnt from: vijayraghav-io
PR: #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.

packages/features/bookings/lib/handleNewBooking/ensureAvailableUsers.ts (2)

Learnt from: eunjae-lee
PR: #22106
File: packages/features/insights/components/FailedBookingsByField.tsx:65-71
Timestamp: 2025-07-15T12:59:34.389Z
Learning: In the FailedBookingsByField component (packages/features/insights/components/FailedBookingsByField.tsx), although routingFormId is typed as optional in useInsightsParameters, the system automatically enforces a routing form filter, so routingFormId is always present in practice. This means the data always contains only one entry, making the single-entry destructuring approach safe.

Learnt from: eunjae-lee
PR: #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.

packages/features/bookings/lib/handleNewBooking.ts (3)

Learnt from: eunjae-lee
PR: #22106
File: packages/features/insights/components/FailedBookingsByField.tsx:65-71
Timestamp: 2025-07-15T12:59:34.389Z
Learning: In the FailedBookingsByField component (packages/features/insights/components/FailedBookingsByField.tsx), although routingFormId is typed as optional in useInsightsParameters, the system automatically enforces a routing form filter, so routingFormId is always present in practice. This means the data always contains only one entry, making the single-entry destructuring approach safe.

Learnt from: eunjae-lee
PR: #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.

Learnt from: CarinaWolli
PR: #22296
File: packages/lib/bookings/filterHostsBySameRoundRobinHost.ts:41-42
Timestamp: 2025-07-22T11:42:47.623Z
Learning: The filterHostsBySameRoundRobinHost function in packages/lib/bookings/filterHostsBySameRoundRobinHost.ts has a known limitation where it doesn't work correctly with fixed hosts or round robin groups. This is pre-existing technical debt that was already broken before the round robin groups feature. CarinaWolli has documented this in Linear issue CAL-6134 for future fix.

🧬 Code Graph Analysis (1)
packages/features/bookings/lib/handleNewBooking/types.ts (1)
packages/lib/getUserAvailability.ts (1)
  • GetUserAvailabilityResult (269-269)
🔇 Additional comments (8)
packages/lib/getUserAvailability.ts (1)

269-269: LGTM! Correct type fix for async function.

The change from ReturnType<typeof _getUserAvailability> to Awaited<ReturnType<typeof _getUserAvailability>> correctly represents the resolved value of the async function rather than the Promise wrapper.

packages/features/bookings/lib/handleNewBooking/types.ts (2)

7-7: LGTM! Correct import for availability type.

The import of GetUserAvailabilityResult type enables the enhanced availability tracking functionality.


44-44: LGTM! Appropriate optional property addition.

Adding the optional availabilityData property to IsFixedAwareUser allows storing detailed availability information alongside user data, which aligns with the PR objective of logging availability snapshots.

packages/features/bookings/lib/handleNewBooking.ts (2)

661-661: LGTM! Appropriate scope elevation for later access.

Moving the availableUsers declaration to a higher scope enables access after booking creation for availability logging, which aligns with the PR objective.


1394-1399: LGTM! Well-implemented availability snapshot logging.

The logging implementation correctly:

  • Finds the organizer user by matching booking.userId with the available users
  • Logs both the booking UID and the availability snapshot
  • Uses appropriate defensive programming with the booking?.userId check
  • Captures the availability data at the time of booking creation as intended

This successfully implements the PR objective of logging availability snapshots when bookings are created.

packages/features/bookings/lib/handleNewBooking/ensureAvailableUsers.ts (3)

102-102: LGTM! Parameter addition enables availability data capture.

The withSource: true parameter correctly enables the capture of detailed availability data that will be used for logging the organizer's availability snapshot when bookings are created.


214-215: LGTM! Clean refactor to accommodate enhanced availability data.

Moving the destructuring inside the loop body is appropriate given that userAvailability now contains additional data from the withSource: true parameter. The aliases are maintained for consistency with existing code.


244-244: LGTM! Availability data attachment enables snapshot logging.

The attachment of availabilityData to available user objects correctly implements the feature requirement. This preserves the full availability information needed for logging organizer availability snapshots during booking creation.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch record-availability-snapshot

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@keithwillcode keithwillcode added core area: core, team members only enterprise area: enterprise, audit log, organisation, SAML, SSO labels Jul 28, 2025
Copy link

vercel bot commented Jul 28, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
cal ⬜️ Ignored (Inspect) Jul 28, 2025 5:39pm
cal-eu ⬜️ Ignored (Inspect) Jul 28, 2025 5:39pm

@joeauyeung joeauyeung marked this pull request as ready for review July 28, 2025 17:38
@joeauyeung joeauyeung requested a review from a team as a code owner July 28, 2025 17:38
@graphite-app graphite-app bot requested a review from a team July 28, 2025 17:39
Copy link

graphite-app bot commented Jul 28, 2025

Graphite Automations

"Add consumer team as reviewer" took an action on this PR • (07/28/25)

1 reviewer was added to this PR based on Keith Williams's automation.

@dosubot dosubot bot added the bookings area: bookings, availability, timezones, double booking label Jul 28, 2025
@joeauyeung joeauyeung merged commit 4e92b6e into main Jul 29, 2025
82 of 86 checks passed
@joeauyeung joeauyeung deleted the record-availability-snapshot branch July 29, 2025 16:10
emrysal pushed a commit that referenced this pull request Jul 29, 2025
* Add userAvailability type

* Add user availability to return value of getUsersAvailability

* Log availabilitySnapshot when booking is created

* Type fix
zomars pushed a commit that referenced this pull request Jul 29, 2025
* Add userAvailability type

* Add user availability to return value of getUsersAvailability

* Log availabilitySnapshot when booking is created

* Type fix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bookings area: bookings, availability, timezones, double booking core area: core, team members only enterprise area: enterprise, audit log, organisation, SAML, SSO ready-for-e2e
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants