Skip to content

Comments

fix: allow org admin to accept/reject booking#23164

Merged
ibex088 merged 3 commits intomainfrom
lauris/cal-6272-feat-confirm-decline-booking-as-org-admin-managed-user
Aug 19, 2025
Merged

fix: allow org admin to accept/reject booking#23164
ibex088 merged 3 commits intomainfrom
lauris/cal-6272-feat-confirm-decline-booking-as-org-admin-managed-user

Conversation

@supalarry
Copy link
Contributor

Linear CAL-6272

@supalarry supalarry requested a review from a team as a code owner August 18, 2025 14:40
@supalarry supalarry requested a review from a team August 18, 2025 14:40
@linear
Copy link

linear bot commented Aug 18, 2025

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 18, 2025

Walkthrough

Adds organization-admin authorization to the booking confirm/decline flow by extending confirm.handler.ts with checks that allow a logged-in user who is OWNER or ADMIN of an organization (root org or its sub-teams) to confirm/decline bookings for users belonging to that org or its teams. Adds helper Prisma queries to resolve org IDs where the actor is admin and to verify booking-user membership. Expands e2e tests to create an org with billing, provision an org-admin managed user, create an event type requiring confirmation, and verify accept/decline behavior and rejection for non-org users.

Assessment against linked issues

Objective Addressed Explanation
Add org-admin/owner authorization in confirm.handler.ts to confirm/decline bookings for users in same org or its teams [CAL-6272]
E2E: Create managed user who is platform org admin; verify accept/decline of managed user’s booking via admin token; ensure failure for non-org user booking [CAL-6272]

Out-of-scope changes

Code Change Explanation
Dependency bump of @calcom/platform-libraries in apps/api/v2/package.json This dependency version change is unrelated to the authorization or e2e test logic specified in CAL-6272 and does not affect the confirm/decline implementation.

Possibly related PRs

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.


📜 Recent review details

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

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 608fc2a and dc871b6.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (1)
  • apps/api/v2/package.json (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • apps/api/v2/package.json
⏰ 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)
  • GitHub Check: Atoms E2E Tests
✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch lauris/cal-6272-feat-confirm-decline-booking-as-org-admin-managed-user

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.
    • 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.
  • 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 the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

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

CodeRabbit Commands (Invoked using PR/Issue comments)

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

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

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • 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.

@graphite-app graphite-app bot requested a review from a team August 18, 2025 14:40
@keithwillcode keithwillcode added core area: core, team members only platform Anything related to our platform plan labels Aug 18, 2025
@dosubot dosubot bot added bookings area: bookings, availability, timezones, double booking 🐛 bug Something isn't working labels Aug 18, 2025
@vercel
Copy link

vercel bot commented Aug 18, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

2 Skipped Deployments
Project Deployment Preview Comments Updated (UTC)
cal Ignored Ignored Aug 18, 2025 2:49pm
cal-eu Ignored Ignored Aug 18, 2025 2:49pm

@graphite-app
Copy link

graphite-app bot commented Aug 18, 2025

Graphite Automations

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

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

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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 (5)
packages/trpc/server/routers/viewer/bookings/confirm.handler.ts (2)

450-483: Reduce DB round-trips and align Prisma usage with minimal selects

You can collapse the two membership existence checks into a single query and avoid fetching unnecessary columns by using select: { id: true }. Also, if available on the Team model, filtering org roots via isOrganization: true increases correctness (prevents personal teams from being treated as org roots).

Apply this refactor:

-async function isLoggedInUserOrgAdminOfBookingUser(loggedInUserId: number, bookingUserId: number) {
+async function isLoggedInUserOrgAdminOfBookingUser(
+  loggedInUserId: number,
+  bookingUserId: number
+): Promise<boolean> {
   const orgIdsWhereLoggedInUserAdmin = await getOrgIdsWhereAdmin(loggedInUserId);

   if (orgIdsWhereLoggedInUserAdmin.length === 0) {
     return false;
   }

-  const bookingUserOrgMembership = await prisma.membership.findFirst({
-    where: {
-      userId: bookingUserId,
-      teamId: {
-        in: orgIdsWhereLoggedInUserAdmin,
-      },
-      team: {
-        parentId: null,
-      },
-    },
-  });
-
-  if (bookingUserOrgMembership) return true;
-
-  const bookingUserOrgTeamMembership = await prisma.membership.findFirst({
-    where: {
-      userId: bookingUserId,
-      team: {
-        parentId: {
-          in: orgIdsWhereLoggedInUserAdmin,
-        },
-      },
-    },
-  });
-
-  return !!bookingUserOrgTeamMembership;
+  const membership = await prisma.membership.findFirst({
+    where: {
+      userId: bookingUserId,
+      OR: [
+        // Booking user belongs to the same root org
+        {
+          teamId: { in: orgIdsWhereLoggedInUserAdmin },
+          team: { parentId: null, isOrganization: true },
+        },
+        // Booking user belongs to a direct sub-team of one of those orgs
+        {
+          team: { parentId: { in: orgIdsWhereLoggedInUserAdmin } },
+        },
+      ],
+    },
+    select: { id: true },
+  });
+
+  return !!membership;
 }

485-502: Constrain “admin orgs” to actual organizations and annotate return type

Good use of select to return only teamId. Consider also constraining to org roots explicitly (if supported by schema) and annotate the return type for clarity.

-async function getOrgIdsWhereAdmin(loggedInUserId: number) {
+async function getOrgIdsWhereAdmin(loggedInUserId: number): Promise<number[]> {
   const loggedInUserOrgMemberships = await prisma.membership.findMany({
     where: {
       userId: loggedInUserId,
       role: {
         in: [MembershipRole.OWNER, MembershipRole.ADMIN],
       },
       team: {
-        parentId: null,
+        parentId: null,
+        isOrganization: true,
       },
     },
     select: {
       teamId: true,
     },
   });

   return loggedInUserOrgMemberships.map((m) => m.teamId);
 }
apps/api/v2/src/ee/bookings/2024-08-13/controllers/e2e/managed-user-bookings.e2e-spec.ts (3)

298-309: Event type requiring confirmation setup: consider asserting the flag

You create an event type with requiresConfirmation: true but don’t assert it. A tiny assertion would make this intent explicit and protect against fixture changes.

Example addition after creation:

  • expect(eventTypeRequiresConfirmation.requiresConfirmation).toBe(true)

716-778: Negative path: consider adding decline-unauthorized case as well

You assert 401 for confirm against a non-org regular user. Consider mirroring for decline to fully cover both endpoints’ authorization behavior.

Would you like me to add a concise test that posts to /v2/bookings/:uid/decline with the org-admin token and expects 401?


780-789: Optional: clean up the repo-created bookings to keep DB tidy across runs

The tests create bookings directly via the repository but don’t delete them. While teardown likely cascades via team/user cleanup, explicit deletion reduces coupling and potential leftovers.

Options:

  • Delete each created booking at the end of its test:
    • await bookingsRepositoryFixture.deleteById(bookingRequiringConfirmation.id)
  • Or keep track of created booking IDs and delete them here in afterAll.

If you want, I can draft a small helper to collect created booking IDs and clean them up centrally.

📜 Review details

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

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between e18b790 and 608fc2a.

📒 Files selected for processing (2)
  • apps/api/v2/src/ee/bookings/2024-08-13/controllers/e2e/managed-user-bookings.e2e-spec.ts (7 hunks)
  • packages/trpc/server/routers/viewer/bookings/confirm.handler.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/trpc/server/routers/viewer/bookings/confirm.handler.ts
  • apps/api/v2/src/ee/bookings/2024-08-13/controllers/e2e/managed-user-bookings.e2e-spec.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/trpc/server/routers/viewer/bookings/confirm.handler.ts
  • apps/api/v2/src/ee/bookings/2024-08-13/controllers/e2e/managed-user-bookings.e2e-spec.ts
🧬 Code Graph Analysis (2)
packages/trpc/server/routers/viewer/bookings/confirm.handler.ts (1)
packages/platform/libraries/index.ts (2)
  • TRPCError (56-56)
  • MembershipRole (98-98)
apps/api/v2/src/ee/bookings/2024-08-13/controllers/e2e/managed-user-bookings.e2e-spec.ts (6)
apps/api/v2/test/fixtures/repository/bookings.repository.fixture.ts (1)
  • BookingsRepositoryFixture (8-44)
apps/api/v2/test/utils/randomString.ts (1)
  • randomString (3-6)
apps/api/v2/src/modules/oauth-clients/controllers/oauth-client-users/outputs/create-managed-user.output.ts (2)
  • CreateManagedUserData (9-16)
  • CreateManagedUserOutput (18-31)
apps/api/v2/src/modules/users/inputs/create-managed-user.input.ts (1)
  • CreateManagedUserInput (10-79)
packages/platform/constants/api.ts (3)
  • SUCCESS_STATUS (9-9)
  • CAL_API_VERSION_HEADER (72-72)
  • VERSION_2024_08_13 (59-59)
packages/platform/types/bookings/2024-08-13/outputs/booking.output.ts (1)
  • BookingOutput_2024_08_13 (280-306)
⏰ 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). (2)
  • GitHub Check: Install dependencies / Yarn install & cache
  • GitHub Check: Atoms E2E Tests
🔇 Additional comments (8)
packages/trpc/server/routers/viewer/bookings/confirm.handler.ts (1)

443-445: Org-admin authorization early-exit looks correct and safely placed

Good addition. This grants access when the logged-in user is an OWNER/ADMIN of the organization (or its direct sub-team) the booking user belongs to, while preserving existing auth paths and error messaging.

apps/api/v2/src/ee/bookings/2024-08-13/controllers/e2e/managed-user-bookings.e2e-spec.ts (7)

18-18: Repository fixture import is appropriate for direct booking setup

Switching to BookingsRepositoryFixture enables precise test setup for confirm/decline flows. Looks good.


56-57: Fixture wiring LGTM

Instantiating BookingsRepositoryFixture alongside the others follows the existing testing pattern.


103-110: Org billing provisioning in fixtures is fine

Provisioning platformBilling as part of org setup keeps tests realistic for platform orgs. No issues.


73-75: Org-admin managed user test identity: good separation

Defining a dedicated org-admin managed user keeps test responsibilities clear and isolated from platform admin. LGTM.


258-297: Org-admin managed user creation and membership assignment look correct

  • Managed user creation via OAuth flow is validated properly.
  • Assigning ADMIN role to the organization establishes the required authorization state.

611-663: Positive path: org-admin can confirm managed user’s booking

End-to-end flow is comprehensive:

  • Creates a PENDING booking directly.
  • Confirms via API using the org-admin managed user token.
  • Asserts API response and persistence layer status.

Nice coverage.


664-715: Positive path: org-admin can reject managed user’s booking

Mirrors the confirm case and validates both transport and DB state transitions to REJECTED. Solid.

@github-actions
Copy link
Contributor

E2E results are ready!

Copy link
Contributor

@ThyMinimalDev ThyMinimalDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

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 🐛 bug Something isn't working core area: core, team members only platform Anything related to our platform plan ready-for-e2e

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants