Skip to content

Commit 0680f44

Browse files
committed
Integrate booking services
1 parent 788fbdb commit 0680f44

36 files changed

Lines changed: 528 additions & 308 deletions

apps/api/v1/pages/api/bookings/_post.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { NextApiRequest } from "next";
22

33
import getBookingDataSchemaForApi from "@calcom/features/bookings/lib/getBookingDataSchemaForApi";
4-
import handleNewBooking from "@calcom/features/bookings/lib/handleNewBooking";
4+
import { getRegularBookingService } from "@calcom/lib/di/bookings/containers/RegularBookingService.container";
55
import { ErrorCode } from "@calcom/lib/errorCodes";
66
import { HttpError } from "@calcom/lib/http-error";
77
import { defaultResponder } from "@calcom/lib/server/defaultResponder";
@@ -239,15 +239,17 @@ async function handler(req: NextApiRequest) {
239239
}
240240

241241
try {
242-
return await handleNewBooking(
243-
{
244-
bookingData: req.body,
242+
const regularBookingService = getRegularBookingService();
243+
244+
return await regularBookingService.createBookingForApiV1({
245+
bookingData: req.body,
246+
bookingMeta: {
245247
userId,
246248
hostname: req.headers.host || "",
247249
forcedSlug: req.headers["x-cal-force-slug"] as string | undefined,
248250
},
249-
getBookingDataSchemaForApi
250-
);
251+
bookingDataSchemaGetter: getBookingDataSchemaForApi,
252+
});
251253
} catch (error: unknown) {
252254
const knownError = error as Error;
253255
if (knownError?.message === ErrorCode.NoAvailableUsersFound) {

apps/api/v2/src/ee/bookings/2024-04-15/bookings.module.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import { CalendarsService } from "@/ee/calendars/services/calendars.service";
66
import { EventTypesModule_2024_04_15 } from "@/ee/event-types/event-types_2024_04_15/event-types.module";
77
import { EventTypesModule_2024_06_14 } from "@/ee/event-types/event-types_2024_06_14/event-types.module";
88
import { SchedulesModule_2024_04_15 } from "@/ee/schedules/schedules_2024_04_15/schedules.module";
9+
import { InstantBookingModule } from "@/lib/modules/instant-booking.module";
10+
import { RecurringBookingModule } from "@/lib/modules/recurring-booking.module";
11+
import { RegularBookingModule } from "@/lib/modules/regular-booking.module";
912
import { ApiKeysRepository } from "@/modules/api-keys/api-keys-repository";
1013
import { AppsRepository } from "@/modules/apps/apps.repository";
1114
import { BillingModule } from "@/modules/billing/billing.module";
@@ -35,6 +38,9 @@ import { Module } from "@nestjs/common";
3538
SchedulesModule_2024_04_15,
3639
EventTypesModule_2024_06_14,
3740
ProfilesModule,
41+
RegularBookingModule,
42+
RecurringBookingModule,
43+
InstantBookingModule,
3844
],
3945
providers: [
4046
TokensRepository,

apps/api/v2/src/ee/bookings/2024-04-15/controllers/bookings.controller.e2e-spec.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { randomString } from "test/utils/randomString";
2121
import { withApiAuth } from "test/utils/withApiAuth";
2222

2323
import { SUCCESS_STATUS } from "@calcom/platform-constants";
24-
import { handleNewBooking } from "@calcom/platform-libraries";
24+
import { type RegularBookingCreateResult } from "@calcom/platform-libraries/bookings";
2525
import type { ApiSuccessResponse, ApiErrorResponse } from "@calcom/platform-types";
2626
import type { User } from "@calcom/prisma/client";
2727

@@ -41,7 +41,7 @@ describe("Bookings Endpoints 2024-04-15", () => {
4141

4242
let eventTypeId: number;
4343

44-
let createdBooking: Awaited<ReturnType<typeof handleNewBooking>>;
44+
let createdBooking: RegularBookingCreateResult;
4545

4646
beforeAll(async () => {
4747
const moduleRef = await withApiAuth(
@@ -131,8 +131,7 @@ describe("Bookings Endpoints 2024-04-15", () => {
131131
.send(body)
132132
.expect(201)
133133
.then(async (response) => {
134-
const responseBody: ApiSuccessResponse<Awaited<ReturnType<typeof handleNewBooking>>> =
135-
response.body;
134+
const responseBody: ApiSuccessResponse<RegularBookingCreateResult> = response.body;
136135
expect(responseBody.status).toEqual(SUCCESS_STATUS);
137136
expect(responseBody.data).toBeDefined();
138137
expect(responseBody.data.userPrimaryEmail).toBeDefined();
@@ -231,8 +230,7 @@ describe("Bookings Endpoints 2024-04-15", () => {
231230
.set({ Authorization: `Bearer cal_test_${apiKeyString}` })
232231
.expect(201)
233232
.then(async (response) => {
234-
const responseBody: ApiSuccessResponse<Awaited<ReturnType<typeof handleNewBooking>>> =
235-
response.body;
233+
const responseBody: ApiSuccessResponse<RegularBookingCreateResult> = response.body;
236234
expect(responseBody.status).toEqual(SUCCESS_STATUS);
237235
expect(responseBody.data).toBeDefined();
238236
expect(responseBody.data.userPrimaryEmail).toBeDefined();
@@ -289,8 +287,7 @@ describe("Bookings Endpoints 2024-04-15", () => {
289287
.send(body)
290288
.expect(201)
291289
.then(async (response) => {
292-
const responseBody: ApiSuccessResponse<Awaited<ReturnType<typeof handleNewBooking>>> =
293-
response.body;
290+
const responseBody: ApiSuccessResponse<RegularBookingCreateResult> = response.body;
294291
expect(responseBody.status).toEqual(SUCCESS_STATUS);
295292
expect(responseBody.data).toBeDefined();
296293
expect(responseBody.data.userPrimaryEmail).toBeDefined();
@@ -387,8 +384,7 @@ describe("Bookings Endpoints 2024-04-15", () => {
387384
.send(body)
388385
.expect(201)
389386
.then(async (response) => {
390-
const responseBody: ApiSuccessResponse<Awaited<ReturnType<typeof handleNewBooking>>> =
391-
response.body;
387+
const responseBody: ApiSuccessResponse<RegularBookingCreateResult> = response.body;
392388
expect(responseBody.status).toEqual(SUCCESS_STATUS);
393389
expect(responseBody.data).toBeDefined();
394390
expect(responseBody.data.userPrimaryEmail).toBeDefined();

apps/api/v2/src/ee/bookings/2024-04-15/controllers/bookings.controller.ts

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { MarkNoShowOutput_2024_04_15 } from "@/ee/bookings/2024-04-15/outputs/ma
77
import { PlatformBookingsService } from "@/ee/bookings/shared/platform-bookings.service";
88
import { sha256Hash, isApiKey, stripApiKey } from "@/lib/api-key";
99
import { VERSION_2024_04_15, VERSION_2024_06_11, VERSION_2024_06_14 } from "@/lib/api-versions";
10+
import { InstantBookingCreateService } from "@/lib/services/instant-booking-create.service";
11+
import { RecurringBookingService } from "@/lib/services/recurring-booking.service";
12+
import { RegularBookingService } from "@/lib/services/regular-booking.service";
1013
import { ApiKeysRepository } from "@/modules/api-keys/api-keys-repository";
1114
import { GetUser } from "@/modules/auth/decorators/get-user/get-user.decorator";
1215
import { Permissions } from "@/modules/auth/decorators/permissions/permissions.decorator";
@@ -45,11 +48,8 @@ import { v4 as uuidv4 } from "uuid";
4548
import { X_CAL_CLIENT_ID, X_CAL_PLATFORM_EMBED } from "@calcom/platform-constants";
4649
import { BOOKING_READ, SUCCESS_STATUS, BOOKING_WRITE } from "@calcom/platform-constants";
4750
import {
48-
handleNewRecurringBooking,
49-
handleNewBooking,
5051
BookingResponse,
5152
HttpError,
52-
handleInstantMeeting,
5353
handleMarkNoShow,
5454
getAllUserBookings,
5555
getBookingInfo,
@@ -58,6 +58,7 @@ import {
5858
ErrorCode,
5959
} from "@calcom/platform-libraries";
6060
import { CreationSource } from "@calcom/platform-libraries";
61+
import { type InstantBookingCreateResult } from "@calcom/platform-libraries/bookings";
6162
import {
6263
GetBookingsInput_2024_04_15,
6364
CancelBookingInput_2024_04_15,
@@ -109,7 +110,10 @@ export class BookingsController_2024_04_15 {
109110
private readonly apiKeyRepository: ApiKeysRepository,
110111
private readonly platformBookingsService: PlatformBookingsService,
111112
private readonly usersRepository: UsersRepository,
112-
private readonly usersService: UsersService
113+
private readonly usersService: UsersService,
114+
private readonly regularBookingService: RegularBookingService,
115+
private readonly recurringBookingService: RecurringBookingService,
116+
private readonly instantBookingCreateService: InstantBookingCreateService
113117
) {}
114118

115119
@Get("/")
@@ -187,17 +191,19 @@ export class BookingsController_2024_04_15 {
187191
const { orgSlug, locationUrl } = body;
188192
try {
189193
const bookingRequest = await this.createNextApiBookingRequest(req, oAuthClientId, locationUrl, isEmbed);
190-
const booking = await handleNewBooking({
194+
const booking = await this.regularBookingService.createBooking({
191195
bookingData: bookingRequest.body,
192-
userId: bookingRequest.userId,
193-
hostname: bookingRequest.headers?.host || "",
194-
forcedSlug: orgSlug,
195-
platformClientId: bookingRequest.platformClientId,
196-
platformRescheduleUrl: bookingRequest.platformRescheduleUrl,
197-
platformCancelUrl: bookingRequest.platformCancelUrl,
198-
platformBookingUrl: bookingRequest.platformBookingUrl,
199-
platformBookingLocation: bookingRequest.platformBookingLocation,
200-
areCalendarEventsEnabled: bookingRequest.areCalendarEventsEnabled,
196+
bookingMeta: {
197+
userId: bookingRequest.userId,
198+
hostname: bookingRequest.headers?.host || "",
199+
forcedSlug: orgSlug,
200+
platformClientId: bookingRequest.platformClientId,
201+
platformRescheduleUrl: bookingRequest.platformRescheduleUrl,
202+
platformCancelUrl: bookingRequest.platformCancelUrl,
203+
platformBookingUrl: bookingRequest.platformBookingUrl,
204+
platformBookingLocation: bookingRequest.platformBookingLocation,
205+
areCalendarEventsEnabled: bookingRequest.areCalendarEventsEnabled,
206+
},
201207
});
202208
if (booking.userId && booking.uid && booking.startTime) {
203209
void (await this.billingService.increaseUsageByUserId(booking.userId, {
@@ -315,15 +321,17 @@ export class BookingsController_2024_04_15 {
315321

316322
const bookingRequest = await this.createNextApiBookingRequest(req, oAuthClientId, undefined, isEmbed);
317323

318-
const createdBookings: BookingResponse[] = await handleNewRecurringBooking({
324+
const createdBookings: BookingResponse[] = await this.recurringBookingService.createBooking({
319325
bookingData: bookingRequest.body,
320-
userId: bookingRequest.userId,
321-
hostname: bookingRequest.headers?.host || "",
322-
platformClientId: bookingRequest.platformClientId,
323-
platformRescheduleUrl: bookingRequest.platformRescheduleUrl,
324-
platformCancelUrl: bookingRequest.platformCancelUrl,
325-
platformBookingUrl: bookingRequest.platformBookingUrl,
326-
platformBookingLocation: bookingRequest.platformBookingLocation,
326+
bookingMeta: {
327+
userId: bookingRequest.userId,
328+
hostname: bookingRequest.headers?.host || "",
329+
platformClientId: bookingRequest.platformClientId,
330+
platformRescheduleUrl: bookingRequest.platformRescheduleUrl,
331+
platformCancelUrl: bookingRequest.platformCancelUrl,
332+
platformBookingUrl: bookingRequest.platformBookingUrl,
333+
platformBookingLocation: bookingRequest.platformBookingLocation,
334+
},
327335
});
328336

329337
createdBookings.forEach(async (booking) => {
@@ -351,14 +359,15 @@ export class BookingsController_2024_04_15 {
351359
@Body() body: CreateBookingInput_2024_04_15,
352360
@Headers(X_CAL_CLIENT_ID) clientId?: string,
353361
@Headers(X_CAL_PLATFORM_EMBED) isEmbed?: string
354-
): Promise<ApiResponse<Awaited<ReturnType<typeof handleInstantMeeting>>>> {
362+
): Promise<ApiResponse<InstantBookingCreateResult>> {
355363
const oAuthClientId =
356364
clientId?.toString() || (await this.getOAuthClientIdFromEventType(body.eventTypeId));
357365
req.userId = (await this.getOwnerId(req)) ?? -1;
358366
try {
359-
const instantMeeting = await handleInstantMeeting(
360-
await this.createNextApiBookingRequest(req, oAuthClientId, undefined, isEmbed)
361-
);
367+
const bookingReq = await this.createNextApiBookingRequest(req, oAuthClientId, undefined, isEmbed);
368+
const instantMeeting = await this.instantBookingCreateService.createBooking({
369+
bookingData: bookingReq.body,
370+
});
362371

363372
if (instantMeeting.userId && instantMeeting.bookingUid) {
364373
const now = new Date();

apps/api/v2/src/ee/bookings/2024-08-13/bookings.module.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import { EventTypesModule_2024_04_15 } from "@/ee/event-types/event-types_2024_0
1616
import { EventTypesModule_2024_06_14 } from "@/ee/event-types/event-types_2024_06_14/event-types.module";
1717
import { EventTypesRepository_2024_06_14 } from "@/ee/event-types/event-types_2024_06_14/event-types.repository";
1818
import { SchedulesModule_2024_04_15 } from "@/ee/schedules/schedules_2024_04_15/schedules.module";
19+
import { InstantBookingModule } from "@/lib/modules/instant-booking.module";
20+
import { RecurringBookingModule } from "@/lib/modules/recurring-booking.module";
21+
import { RegularBookingModule } from "@/lib/modules/regular-booking.module";
1922
import { ApiKeysRepository } from "@/modules/api-keys/api-keys-repository";
2023
import { AppsRepository } from "@/modules/apps/apps.repository";
2124
import { BillingModule } from "@/modules/billing/billing.module";
@@ -58,6 +61,9 @@ import { Module } from "@nestjs/common";
5861
TeamsEventTypesModule,
5962
MembershipsModule,
6063
ProfilesModule,
64+
RegularBookingModule,
65+
RecurringBookingModule,
66+
InstantBookingModule,
6167
],
6268
providers: [
6369
TokensRepository,

0 commit comments

Comments
 (0)