1+ import {
2+ BOOKING_READ ,
3+ BOOKING_WRITE ,
4+ SUCCESS_STATUS ,
5+ X_CAL_CLIENT_ID ,
6+ X_CAL_PLATFORM_EMBED ,
7+ } from "@calcom/platform-constants" ;
8+ import {
9+ BookingResponse ,
10+ CreationSource ,
11+ getAllUserBookings ,
12+ getBookingForReschedule ,
13+ getBookingInfo ,
14+ handleCancelBooking ,
15+ handleMarkNoShow ,
16+ } from "@calcom/platform-libraries" ;
17+ import { type InstantBookingCreateResult } from "@calcom/platform-libraries/bookings" ;
18+ import { ErrorCode , HttpError } from "@calcom/platform-libraries/errors" ;
19+ import type { ApiResponse } from "@calcom/platform-types" ;
20+ import {
21+ CancelBookingInput_2024_04_15 ,
22+ GetBookingsInput_2024_04_15 ,
23+ Status_2024_04_15 ,
24+ } from "@calcom/platform-types" ;
25+ import type { PrismaClient } from "@calcom/prisma" ;
26+ import {
27+ BadRequestException ,
28+ Body ,
29+ Controller ,
30+ ForbiddenException ,
31+ Get ,
32+ Headers ,
33+ HttpException ,
34+ InternalServerErrorException ,
35+ Logger ,
36+ NotFoundException ,
37+ Param ,
38+ Post ,
39+ Query ,
40+ Req ,
41+ UnauthorizedException ,
42+ UseGuards ,
43+ } from "@nestjs/common" ;
44+ import { ConfigService } from "@nestjs/config" ;
45+ import { ApiQuery , ApiExcludeController as DocsExcludeController } from "@nestjs/swagger" ;
46+ import { Request } from "express" ;
47+ import { NextApiRequest } from "next/types" ;
48+ import { v4 as uuidv4 } from "uuid" ;
149import { CreateBookingInput_2024_04_15 } from "@/ee/bookings/2024-04-15/inputs/create-booking.input" ;
250import { CreateRecurringBookingInput_2024_04_15 } from "@/ee/bookings/2024-04-15/inputs/create-recurring-booking.input" ;
351import { MarkNoShowInput_2024_04_15 } from "@/ee/bookings/2024-04-15/inputs/mark-no-show.input" ;
452import { GetBookingOutput_2024_04_15 } from "@/ee/bookings/2024-04-15/outputs/get-booking.output" ;
553import { GetBookingsOutput_2024_04_15 } from "@/ee/bookings/2024-04-15/outputs/get-bookings.output" ;
654import { MarkNoShowOutput_2024_04_15 } from "@/ee/bookings/2024-04-15/outputs/mark-no-show.output" ;
755import { PlatformBookingsService } from "@/ee/bookings/shared/platform-bookings.service" ;
8- import { sha256Hash , isApiKey , stripApiKey } from "@/lib/api-key" ;
56+ import { isApiKey , sha256Hash , stripApiKey } from "@/lib/api-key" ;
957import { VERSION_2024_04_15 , VERSION_2024_06_11 , VERSION_2024_06_14 } from "@/lib/api-versions" ;
1058import { PrismaEventTypeRepository } from "@/lib/repositories/prisma-event-type.repository" ;
1159import { PrismaTeamRepository } from "@/lib/repositories/prisma-team.repository" ;
@@ -30,50 +78,6 @@ import { OAuthFlowService } from "@/modules/oauth-clients/services/oauth-flow.se
3078import { PrismaReadService } from "@/modules/prisma/prisma-read.service" ;
3179import { UsersService } from "@/modules/users/services/users.service" ;
3280import { UsersRepository , UserWithProfile } from "@/modules/users/users.repository" ;
33- import {
34- Controller ,
35- Post ,
36- Logger ,
37- Req ,
38- InternalServerErrorException ,
39- Body ,
40- Headers ,
41- HttpException ,
42- Param ,
43- Get ,
44- Query ,
45- NotFoundException ,
46- UseGuards ,
47- BadRequestException ,
48- UnauthorizedException ,
49- ForbiddenException ,
50- } from "@nestjs/common" ;
51- import { ConfigService } from "@nestjs/config" ;
52- import { ApiQuery , ApiExcludeController as DocsExcludeController } from "@nestjs/swagger" ;
53- import { Request } from "express" ;
54- import { NextApiRequest } from "next/types" ;
55- import { v4 as uuidv4 } from "uuid" ;
56-
57- import { X_CAL_CLIENT_ID , X_CAL_PLATFORM_EMBED } from "@calcom/platform-constants" ;
58- import { BOOKING_READ , SUCCESS_STATUS , BOOKING_WRITE } from "@calcom/platform-constants" ;
59- import {
60- BookingResponse ,
61- handleMarkNoShow ,
62- getAllUserBookings ,
63- getBookingInfo ,
64- handleCancelBooking ,
65- getBookingForReschedule ,
66- } from "@calcom/platform-libraries" ;
67- import { CreationSource } from "@calcom/platform-libraries" ;
68- import { type InstantBookingCreateResult } from "@calcom/platform-libraries/bookings" ;
69- import { HttpError , ErrorCode } from "@calcom/platform-libraries/errors" ;
70- import {
71- GetBookingsInput_2024_04_15 ,
72- CancelBookingInput_2024_04_15 ,
73- Status_2024_04_15 ,
74- } from "@calcom/platform-types" ;
75- import type { ApiResponse } from "@calcom/platform-types" ;
76- import type { PrismaClient } from "@calcom/prisma" ;
7781
7882type BookingRequest = Request & {
7983 userId ?: number ;
@@ -137,7 +141,7 @@ export class BookingsController_2024_04_15 {
137141 @Query ( ) queryParams : GetBookingsInput_2024_04_15
138142 ) : Promise < GetBookingsOutput_2024_04_15 > {
139143 const { filters, cursor, limit } = queryParams ;
140- const bookingListingByStatus = filters ?. status ?? Status_2024_04_15 [ " upcoming" ] ;
144+ const bookingListingByStatus = filters ?. status ?? Status_2024_04_15 . upcoming ;
141145 const profile = this . usersService . getUserMainProfile ( user ) ;
142146 const bookings = await getAllUserBookings ( {
143147 bookingListingByStatus : [ bookingListingByStatus ] ,
@@ -221,7 +225,7 @@ export class BookingsController_2024_04_15 {
221225 areCalendarEventsEnabled : bookingRequest . areCalendarEventsEnabled ,
222226 } ,
223227 } ) ;
224- if ( booking . userId && booking . uid && booking . startTime ) {
228+ if ( booking . userId && booking . uid && booking . startTime && booking . user ?. isPlatformManaged ) {
225229 void ( await this . billingService . increaseUsageByUserId ( booking . userId , {
226230 uid : booking . uid ,
227231 startTime : booking . startTime ,
@@ -242,12 +246,12 @@ export class BookingsController_2024_04_15 {
242246 async cancelBooking (
243247 @Req ( ) req : BookingRequest ,
244248 @Param ( "bookingUid" ) bookingUid : string ,
245- @Body ( ) body : CancelBookingInput_2024_04_15 ,
249+ @Body ( ) _body : CancelBookingInput_2024_04_15 ,
246250 @Headers ( X_CAL_CLIENT_ID ) clientId ?: string ,
247251 @Headers ( X_CAL_PLATFORM_EMBED ) isEmbed ?: string
248252 ) : Promise < ApiResponse < { bookingId : number ; bookingUid : string ; onlyRemovedAttendee : boolean } > > {
249253 const oAuthClientId = clientId ?. toString ( ) ;
250- const isUidNumber = ! isNaN ( Number ( bookingUid ) ) ;
254+ const isUidNumber = ! Number . isNaN ( Number ( bookingUid ) ) ;
251255
252256 if ( isUidNumber ) {
253257 throw new BadRequestException ( "Please provide booking uid instead of booking id." ) ;
@@ -276,7 +280,7 @@ export class BookingsController_2024_04_15 {
276280 platformRescheduleUrl : bookingRequest . platformRescheduleUrl ,
277281 platformBookingUrl : bookingRequest . platformBookingUrl ,
278282 } ) ;
279- if ( ! res . onlyRemovedAttendee ) {
283+ if ( ! res . onlyRemovedAttendee && res . isPlatformManagedUserBooking ) {
280284 void ( await this . billingService . cancelUsageByBookingUid ( res . bookingUid ) ) ;
281285 }
282286 return {
@@ -353,7 +357,7 @@ export class BookingsController_2024_04_15 {
353357 } ) ;
354358
355359 createdBookings . forEach ( async ( booking ) => {
356- if ( booking . userId && booking . uid && booking . startTime ) {
360+ if ( booking . userId && booking . uid && booking . startTime && booking . user . isPlatformManaged ) {
357361 void ( await this . billingService . increaseUsageByUserId ( booking . userId , {
358362 uid : booking . uid ,
359363 startTime : booking . startTime ,
@@ -623,58 +627,22 @@ export class BookingsController_2024_04_15 {
623627 oAuthClientId
624628 ) ;
625629 }
626- if ( requestBody ?. responses ?. guests && requestBody ?. responses ?. guests . length ) {
630+ if ( requestBody ?. responses ?. guests ? .length ) {
627631 requestBody . responses . guests = await this . platformBookingsService . getPlatformAttendeesEmails (
628632 requestBody . responses . guests ,
629633 oAuthClientId
630634 ) ;
631635 }
632636 }
633637
634- private async createNextApiRecurringBookingRequest (
635- req : BookingRequest ,
636- oAuthClientId ?: string ,
637- platformBookingLocation ?: string ,
638- isEmbed ?: string
639- ) : Promise < NextApiRequest & { userId ?: number ; userUuid ?: string } & OAuthRequestParams > {
640- const clone = { ...req } ;
641- const owner = await this . getOwner ( req ) ;
642- const userId = owner ?. id ?? - 1 ;
643- const userUuid = owner ?. uuid ;
644-
645- const oAuthParams = oAuthClientId
646- ? await this . getOAuthClientsParams ( oAuthClientId , this . transformToBoolean ( isEmbed ) )
647- : DEFAULT_PLATFORM_PARAMS ;
648- const requestId = req . get ( "X-Request-Id" ) ;
649- this . logger . log ( `createNextApiRecurringBookingRequest_2024_04_15` , {
650- requestId,
651- ownerId : userId ,
652- platformBookingLocation,
653- oAuthClientId,
654- ...oAuthParams ,
655- } ) ;
656- Object . assign ( clone , {
657- userId,
658- userUuid,
659- ...oAuthParams ,
660- platformBookingLocation,
661- noEmail : ! oAuthParams . arePlatformEmailsEnabled ,
662- creationSource : CreationSource . API_V2 ,
663- } ) ;
664- if ( oAuthClientId ) {
665- await this . setPlatformAttendeesEmails ( clone . body , oAuthClientId ) ;
666- }
667- return clone as unknown as NextApiRequest & { userId ?: number ; userUuid ?: string } & OAuthRequestParams ;
668- }
669-
670638 private handleBookingErrors (
671639 err : Error | HttpError | unknown ,
672640 type ?: "recurring" | `instant` | "no-show"
673641 ) : void {
674642 const errMsg =
675643 type === "no-show"
676644 ? `Error while marking no-show.`
677- : `Error while creating ${ type ? type + " " : "" } booking.` ;
645+ : `Error while creating ${ type ? ` ${ type } ` : "" } booking.` ;
678646 if ( err instanceof HttpError ) {
679647 const httpError = err as HttpError ;
680648 throw new HttpException ( httpError ?. message ?? errMsg , httpError ?. statusCode ?? 500 ) ;
0 commit comments