Skip to content

chore: [Booking Flow Refactor - 4] Add BookingDataPreparationService#24022

Closed
hariombalhara wants to merge 16 commits intomainfrom
hariom/pri-305-bookingcreateservice-validation-service
Closed

chore: [Booking Flow Refactor - 4] Add BookingDataPreparationService#24022
hariombalhara wants to merge 16 commits intomainfrom
hariom/pri-305-bookingcreateservice-validation-service

Conversation

@hariombalhara
Copy link
Copy Markdown
Member

@hariombalhara hariombalhara commented Sep 24, 2025

What does this PR do?

This PR extracts booking preparation logic into a dedicated BookingDataPreparationService as part 4 of the booking flow refactor series. The main changes include:

  • New BookingDataPreparationService: Centralizes all booking data preparation, validation, transformation, and enrichment logic into a single testable service class
  • Repository pattern adoption: Adds methods to BookingRepository and UserRepository to abstract Prisma queries used during preparation
  • Helper function refactoring: Updates checkIfBookerEmailIsBlocked and checkActiveBookingsLimitForBooker to use repository pattern instead of direct Prisma calls
  • DI integration: Full dependency injection setup with module, tokens, and container wiring
  • API v2 support: Wrapper service for API v2 with proper NestJS integration
  • Comprehensive test coverage: Adds 1000+ lines of unit tests for the preparation service

The service handles:

  • Enrichment: Fetches and enriches event type data
  • Validation: Time validation, email blocking, booking limits, email verification
  • Transformation: Converts raw booking data into structured format
  • Preparation: Organizes data into categories (booker, routing, meta, config, etc.)

This is a pure refactoring - no functional behavior changes, just improved code organization and testability.

Updates Since Last Revision

Latest changes (DI module + API v2 integration):

  • Created BookingDataPreparationService.module.ts with proper DI wiring using bindModuleToClassOnToken
  • Added DI tokens (BOOKING_DATA_PREPARATION_SERVICE, BOOKING_DATA_PREPARATION_SERVICE_MODULE) to tokens.ts
  • Updated RegularBookingService.module.ts to include bookingDataPreparationService dependency
  • Created API v2 wrapper service (apps/api/v2/src/lib/services/booking-data-preparation.service.ts)
  • Updated API v2 regular-booking.service.ts to inject and pass BookingDataPreparationService
  • Updated API v2 regular-booking.module.ts to include the new service provider
  • Exported BookingDataPreparationService from @calcom/platform-libraries/bookings
  • Updated BookingDataPreparationService to use Logger<unknown> type from tslog for API v2 compatibility

Previous changes (merge conflict resolution + interface update):

  • Resolved merge conflicts with main branch
  • Added bookingDataPreparationService: BookingDataPreparationService to IBookingServiceDependencies interface
  • Updated findActiveBookingsForEventType repository method to include bookingSeat selection for seated events

Repository Methods

BookingRepository:

  • countActiveBookingsForEventType: Counts active bookings for a booker by email
  • findActiveBookingsForEventType: Finds active bookings with ordering and includes bookingSeat for seated events

UserRepository:

  • findVerifiedUserByEmail: Finds verified users checking both primary and secondary emails

Visual Demo

N/A - This is a pure refactoring PR with no UI changes.

Mandatory Tasks (DO NOT REMOVE)

  • I have self-reviewed the code (A decent size PR without self-review might be rejected).
  • N/A - No documentation changes required (internal refactoring only)
  • I confirm automated tests are in place that prove my fix is effective or that my feature works.

How should this be tested?

Unit Tests

cd ~/repos/cal.com
TZ=UTC yarn test packages/features/bookings/lib/utils/BookingDataPreparationService.test.ts

Integration Testing

Run the full booking flow to ensure preparation still works:

  1. Create a booking on an event type
  2. Try booking with a blocked email (if configured)
  3. Try exceeding booking limits (if configured on event type)
  4. Try booking outside allowed time bounds
  5. Verify email verification flow (if enabled on event type)

Human Review Checklist

Critical areas to review:

  1. API v2 Logger type cast (HIGH PRIORITY):

    • apps/api/v2/src/lib/services/booking-data-preparation.service.ts uses bridgeLogger as unknown as TsLogger<unknown>
    • Verify the Logger bridge implements all methods used by BookingDataPreparationService (currently warn, info)
  2. Repository pattern migration (HIGH PRIORITY):

    • checkIfBookerEmailIsBlocked now requires userRepository parameter - verify all callers are updated
    • checkActiveBookingsLimitForBooker now requires bookingRepository parameter - verify all callers are updated
    • These are breaking changes to the function signatures
  3. Repository query equivalence (HIGH PRIORITY):

    • BookingRepository.countActiveBookingsForEventType - Does it correctly count active bookings?
    • BookingRepository.findActiveBookingsForEventType - Does ordering, limit, and bookingSeat selection work correctly?
    • UserRepository.findVerifiedUserByEmail - Does it check both primary and secondary emails correctly?
  4. DI wiring completeness:

    • Verify BookingDataPreparationService.module.ts correctly wires all dependencies (log, bookingRepository, userRepository)
    • Verify RegularBookingService.module.ts includes the new bookingDataPreparationService dependency

Notes

  • This is PR Unable to login if running on custom port #4 in a stacked series (depends on other booking flow refactor PRs)
  • Part of broader effort to improve booking flow architecture
  • No functional changes expected - pure refactoring for better code organization
  • Full integration with RegularBookingService.handler() will come in a follow-up PR

Link to Devin run: https://app.devin.ai/sessions/1f56530a5ce1411894caca3c387d5e64
Requested by: hariom@cal.com (@hariombalhara)

@linear
Copy link
Copy Markdown

linear bot commented Sep 24, 2025

PRI-305

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Sep 24, 2025

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch hariom/pri-305-bookingcreateservice-validation-service

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown
Contributor

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 release type found in pull request title "Add BookingValidationService". Add a prefix to indicate what kind of release this pull request corresponds to. For reference, see https://www.conventionalcommits.org/

Available types:
 - feat: A new feature
 - fix: A bug fix
 - docs: Documentation only changes
 - style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
 - refactor: A code change that neither fixes a bug nor adds a feature
 - perf: A code change that improves performance
 - test: Adding missing tests or correcting existing tests
 - build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
 - ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)
 - chore: Other changes that don't modify src or test files
 - revert: Reverts a previous commit

@hariombalhara hariombalhara mentioned this pull request Sep 24, 2025
3 tasks
@hariombalhara hariombalhara changed the title Add BookingValidationService feat: [Booking Flow Refactor - 4] Add BookingValidationService Sep 24, 2025
@hariombalhara hariombalhara changed the base branch from hariom/pri-305-bookingcreateservice-rename-handlers-to-services to graphite-base/24022 September 24, 2025 07:49
@hariombalhara hariombalhara changed the title feat: [Booking Flow Refactor - 4] Add BookingValidationService feat: [Booking Flow Refactor - 4] Add BookingValidationService[Ready for Review but Stacked PR] Oct 7, 2025
@hariombalhara hariombalhara force-pushed the hariom/pri-305-bookingcreateservice-validation-service branch from 3edada1 to bdc1064 Compare October 7, 2025 05:35
@vercel
Copy link
Copy Markdown

vercel bot commented Oct 7, 2025

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

3 Skipped Deployments
Project Deployment Review Updated (UTC)
cal Ignored Ignored Dec 19, 2025 10:01am
cal-companion Ignored Ignored Preview Dec 19, 2025 10:01am
cal-eu Ignored Ignored Dec 19, 2025 10:01am

@hariombalhara hariombalhara changed the base branch from graphite-base/24022 to hariom/pri-305-bookingcreateservice-rename-handlers-to-services October 7, 2025 05:35
@hariombalhara hariombalhara changed the base branch from hariom/pri-305-bookingcreateservice-rename-handlers-to-services to graphite-base/24022 October 13, 2025 13:28
@hariombalhara hariombalhara force-pushed the hariom/pri-305-bookingcreateservice-validation-service branch from bdc1064 to 7500777 Compare October 13, 2025 13:37
@hariombalhara hariombalhara changed the base branch from graphite-base/24022 to hariom/pri-305-bookingcreateservice-rename-handlers-to-services October 13, 2025 13:38
@hariombalhara hariombalhara changed the base branch from hariom/pri-305-bookingcreateservice-rename-handlers-to-services to graphite-base/24022 October 16, 2025 05:56
@hariombalhara hariombalhara force-pushed the hariom/pri-305-bookingcreateservice-validation-service branch from 7500777 to eeb29c8 Compare October 16, 2025 10:46
…ataPreparationService

- Reset BookingRepository.ts to main version and add countActiveBookingsForEventType and findActiveBookingsForEventType methods
- Reset UserRepository.ts to main version and add findVerifiedUserByEmail method
- Update checkIfBookerEmailIsBlocked.ts to use userRepository instead of direct Prisma calls
- Update checkActiveBookingsLimitForBooker.ts to use bookingRepository instead of direct Prisma calls

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>
…e and RegularBookingService

- Fix verifyCodeUnAuthenticated import path in BookingDataPreparationService.ts
- Add missing isReschedule parameter to checkIfBookerEmailIsBlocked call
- Fix useCacheIfEnabled to use default value instead of non-existent _shouldServeCache property
- Fix CacheService import to use CalendarCacheService from calendar-cache-sql
- Fix getAllWorkflowsFromEventType import path to use @calcom/features/ee/workflows/lib

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>
The BookingDataPreparationService integration into RegularBookingService.ts
will be done in a follow-up PR. This PR focuses on:
- Adding the BookingDataPreparationService class and tests
- Adding repository methods for booking data preparation
- Updating helper functions to use repository pattern

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>
The BookingDataPreparationService can work independently without
modifying the existing booking flow types. The integration into
RegularBookingService.ts will be done in a follow-up PR.

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>
Reset API v2 files, test files, and other files that were incorrectly
modified in this PR. The PR should only contain BookingDataPreparationService
and its supporting repository methods.

Also fixed lint warnings in handleSeats.test.ts by prefixing unused
variables with underscore.

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>
- Reverted booking-validations.integration-test.ts back to booking-validations.test.ts
- Reset editLocation.handler.ts to main version (removed console.log change)

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>
- Update checkIfBookerEmailIsBlocked to use UserRepository instead of direct Prisma
- Update checkActiveBookingsLimitForBooker to use BookingRepository instead of direct Prisma
- Update RegularBookingService to pass repositories to helper functions
- Add BookingDataPreparationService as dependency in IBookingServiceDependencies
- Extend BookingFlowConfig type with additional properties

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>
Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>
…ncies (not yet integrated)

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>
@github-actions
Copy link
Copy Markdown
Contributor

Devin AI is resolving merge conflicts

This PR has merge conflicts with the main branch. A Devin session has been created to automatically resolve them.

View Devin Session

Devin will:

  1. Merge the latest main into this branch
  2. Resolve any conflicts intelligently
  3. Run lint/type checks to ensure validity
  4. Push the resolved changes

If you prefer to resolve conflicts manually, you can close the Devin session and handle it yourself.

@github-actions github-actions bot added the ❗️ .env changes contains changes to env variables label Jan 11, 2026
@vercel vercel bot temporarily deployed to Preview – cal-companion January 13, 2026 08:45 Inactive
@vercel vercel bot temporarily deployed to Preview – dev January 13, 2026 08:56 Inactive
devin-ai-integration bot and others added 6 commits January 23, 2026 14:56
…-service

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>
Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>
Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>
…larBookingService

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>
Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>
… for API v2 compatibility

Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core area: core, team members only devin-conflict-resolution enterprise area: enterprise, audit log, organisation, SAML, SSO ❗️ .env changes contains changes to env variables ready-for-e2e size/XXL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants