11import short , { uuid } from "short-uuid" ;
22import { v5 as uuidv5 } from "uuid" ;
3-
3+ import { ProfileRepository } from "@calcom/features/profile/repositories/ProfileRepository" ;
44import processExternalId from "@calcom/app-store/_utils/calendars/processExternalId" ;
55import { getPaymentAppData } from "@calcom/app-store/_utils/payments/getPaymentAppData" ;
66import {
@@ -23,6 +23,7 @@ import { scheduleMandatoryReminder } from "@calcom/ee/workflows/lib/reminders/sc
2323import getICalUID from "@calcom/emails/lib/getICalUID" ;
2424import { CalendarEventBuilder } from "@calcom/features/CalendarEventBuilder" ;
2525import EventManager , { placeholderCreatedEvent } from "@calcom/features/bookings/lib/EventManager" ;
26+ import type { CheckBookingLimitsService } from "@calcom/features/bookings/lib/checkBookingLimits" ;
2627import type { BookingDataSchemaGetter } from "@calcom/features/bookings/lib/dto/types" ;
2728import type {
2829 CreateRegularBookingData ,
@@ -35,12 +36,15 @@ import { handleWebhookTrigger } from "@calcom/features/bookings/lib/handleWebhoo
3536import { isEventTypeLoggingEnabled } from "@calcom/features/bookings/lib/isEventTypeLoggingEnabled" ;
3637import type { CacheService } from "@calcom/features/calendar-cache/lib/getShouldServeCache" ;
3738import { getSpamCheckService } from "@calcom/features/di/watchlist/containers/SpamCheckService.container" ;
39+ import { getBookerBaseUrl } from "@calcom/features/ee/organizations/lib/getBookerUrlServer" ;
3840import AssignmentReasonRecorder from "@calcom/features/ee/round-robin/assignmentReason/AssignmentReasonRecorder" ;
41+ import { WorkflowRepository } from "@calcom/features/ee/workflows/repositories/WorkflowRepository" ;
3942import { getUsernameList } from "@calcom/features/eventtypes/lib/defaultEvents" ;
4043import { getEventName , updateHostInEventName } from "@calcom/features/eventtypes/lib/eventNaming" ;
4144import type { FeaturesRepository } from "@calcom/features/flags/features.repository" ;
4245import { getFullName } from "@calcom/features/form-builder/utils" ;
4346import { handleAnalyticsEvents } from "@calcom/features/tasker/tasks/analytics/handleAnalyticsEvents" ;
47+ import type { UserRepository } from "@calcom/features/users/repositories/UserRepository" ;
4448import { UsersRepository } from "@calcom/features/users/users.repository" ;
4549import type { GetSubscriberOptions } from "@calcom/features/webhooks/lib/getWebhooks" ;
4650import getWebhooks from "@calcom/features/webhooks/lib/getWebhooks" ;
@@ -56,21 +60,16 @@ import { DEFAULT_GROUP_ID } from "@calcom/lib/constants";
5660import { ErrorCode } from "@calcom/lib/errorCodes" ;
5761import { getErrorFromUnknown } from "@calcom/lib/errors" ;
5862import { extractBaseEmail } from "@calcom/lib/extract-base-email" ;
59- import { getBookerBaseUrl } from "@calcom/features/ee/organizations/lib/getBookerUrlServer" ;
6063import getOrgIdFromMemberOrTeamId from "@calcom/lib/getOrgIdFromMemberOrTeamId" ;
6164import { getTeamIdFromEventType } from "@calcom/lib/getTeamIdFromEventType" ;
6265import { HttpError } from "@calcom/lib/http-error" ;
63- import type { CheckBookingLimitsService } from "@calcom/features/bookings/lib/checkBookingLimits" ;
6466import logger from "@calcom/lib/logger" ;
6567import { getPiiFreeCalendarEvent , getPiiFreeEventType } from "@calcom/lib/piiFreeData" ;
6668import { safeStringify } from "@calcom/lib/safeStringify" ;
6769import { getTranslation } from "@calcom/lib/server/i18n" ;
6870import type { PrismaAttributeRepository as AttributeRepository } from "@calcom/lib/server/repository/PrismaAttributeRepository" ;
69- import type { BookingRepository } from "../repositories/BookingRepository" ;
7071import type { HostRepository } from "@calcom/lib/server/repository/host" ;
7172import type { PrismaOOORepository as OooRepository } from "@calcom/lib/server/repository/ooo" ;
72- import type { UserRepository } from "@calcom/features/users/repositories/UserRepository" ;
73- import { WorkflowRepository } from "@calcom/features/ee/workflows/repositories/WorkflowRepository" ;
7473import { HashedLinkService } from "@calcom/lib/server/service/hashedLinkService" ;
7574import { WorkflowService } from "@calcom/lib/server/service/workflows" ;
7675import { getTimeFormatStringFromUserTimeFormat } from "@calcom/lib/timeFormat" ;
@@ -96,6 +95,7 @@ import type { CredentialForCalendarService } from "@calcom/types/Credential";
9695import type { EventResult , PartialReference } from "@calcom/types/EventManager" ;
9796
9897import type { EventPayloadType , EventTypeInfo } from "../../webhooks/lib/sendPayload" ;
98+ import type { BookingRepository } from "../repositories/BookingRepository" ;
9999import { BookingActionMap , BookingEmailSmsHandler } from "./BookingEmailSmsHandler" ;
100100import { getAllCredentialsIncludeServiceAccountKey } from "./getAllCredentialsForUsersOnEvent/getAllCredentials" ;
101101import { refreshCredentials } from "./getAllCredentialsForUsersOnEvent/refreshCredentials" ;
@@ -427,6 +427,45 @@ export interface IBookingServiceDependencies {
427427 attributeRepository : AttributeRepository ;
428428}
429429
430+ /**
431+ * TODO: Ideally we should send organizationId directly to handleNewBooking.
432+ * webapp can derive from domain and API V2 knows it already through its endpoint URL
433+ */
434+ async function getEventOrganizationId ( {
435+ eventType,
436+ } : {
437+ eventType : {
438+ userId : number | null ;
439+ team : {
440+ parentId : number | null ;
441+ } | null ;
442+ parent : {
443+ team : {
444+ parentId : number | null ;
445+ } | null ;
446+ } | null ;
447+ } ;
448+ } ) {
449+ let eventOrganizationId : number | null = null ;
450+ const team = eventType . team ?? eventType . parent ?. team ?? null ;
451+ eventOrganizationId = team ?. parentId ?? null ;
452+
453+ if ( eventOrganizationId ) {
454+ return eventOrganizationId ;
455+ }
456+
457+ if ( eventType . userId ) {
458+ // TODO: Moving it to instance based access through DI in a followup
459+ const profile = await ProfileRepository . findFirstForUserId ( {
460+ userId : eventType . userId ,
461+ } ) ;
462+ eventOrganizationId = profile ?. organizationId ?? null ;
463+ return eventOrganizationId ;
464+ }
465+
466+ return eventOrganizationId ;
467+ }
468+
430469async function handler (
431470 input : BookingHandlerInput ,
432471 deps : IBookingServiceDependencies ,
@@ -509,9 +548,10 @@ async function handler(
509548 await checkIfBookerEmailIsBlocked ( { loggedInUserId : userId , bookerEmail } ) ;
510549
511550 const spamCheckService = getSpamCheckService ( ) ;
512- // Either it is a team event or a managed child event of a managed event
513- const team = eventType . team ?? eventType . parent ?. team ?? null ;
514- const eventOrganizationId = team ?. parentId ?? null ;
551+ const eventOrganizationId = await getEventOrganizationId ( {
552+ eventType,
553+ } ) ;
554+
515555 spamCheckService . startCheck ( { email : bookerEmail , organizationId : eventOrganizationId } ) ;
516556
517557 if ( ! rawBookingData . rescheduleUid ) {
@@ -1492,7 +1532,7 @@ async function handler(
14921532 paymentId : undefined ,
14931533 seatReferenceUid : undefined ,
14941534 isShortCircuitedBooking : true , // Renamed from isSpamDecoy to avoid exposing spam detection to blocked users
1495- }
1535+ } ;
14961536 }
14971537
14981538 // For seats, if the booking already exists then we want to add the new attendee to the existing booking
0 commit comments