Skip to content

Commit a6a428c

Browse files
hariombalharadevin-ai-integration[bot]Udit-takkar
authored
feat: make actionSource required with ValidActionSource type across booking handlers (calcom#27869)
* feat: make actionSource required with ValidActionSource type across booking handlers Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: update getAuditActionSource to return ValidActionSource, default to SYSTEM Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: revert getAuditActionSource, handleSeats, RegularBookingService back to ActionSource Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com>
1 parent 217c6e6 commit a6a428c

11 files changed

Lines changed: 42 additions & 22 deletions

File tree

apps/api/v1/pages/api/bookings/[id]/_delete.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ async function handler(req: NextApiRequest) {
8282
return await handleCancelBooking({
8383
bookingData: { id, allRemainingBookings, cancellationReason, cancelledBy },
8484
userId: req.userId,
85+
actionSource: "API_V1",
8586
});
8687
}
8788

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ export class BookingsController_2024_04_15 {
279279
platformCancelUrl: bookingRequest.platformCancelUrl,
280280
platformRescheduleUrl: bookingRequest.platformRescheduleUrl,
281281
platformBookingUrl: bookingRequest.platformBookingUrl,
282+
actionSource: "API_V2",
282283
});
283284
if (!res.onlyRemovedAttendee && res.isPlatformManagedUserBooking) {
284285
void (await this.billingService.cancelUsageByBookingUid(res.bookingUid));

packages/features/bookings/lib/handleCancelBooking.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
makeGuestActor,
1111
makeUserActor,
1212
} from "@calcom/features/booking-audit/lib/makeActor";
13-
import type { ActionSource } from "@calcom/features/booking-audit/lib/types/actionSource";
13+
import type { ValidActionSource } from "@calcom/features/booking-audit/lib/types/actionSource";
1414
import { BookingReferenceRepository } from "@calcom/features/bookingReference/repositories/BookingReferenceRepository";
1515
import { getBookingEventHandlerService } from "@calcom/features/bookings/di/BookingEventHandlerService.container";
1616
import EventManager from "@calcom/features/bookings/lib/EventManager";
@@ -81,7 +81,7 @@ export type CancelBookingInput = {
8181
userId?: number;
8282
userUuid?: string;
8383
bookingData: z.infer<typeof bookingCancelInput>;
84-
actionSource?: ActionSource;
84+
actionSource: ValidActionSource;
8585
} & PlatformParams;
8686

8787
type Dependencies = {
@@ -173,17 +173,7 @@ async function handler(input: CancelBookingInput, dependencies?: Dependencies) {
173173

174174
const userUuid = input.userUuid ?? null;
175175

176-
// Extract action source once for reuse
177-
const actionSource = input.actionSource ?? "UNKNOWN";
178-
if (actionSource === "UNKNOWN") {
179-
log.warn(
180-
"Booking cancellation with unknown actionSource",
181-
safeStringify({
182-
bookingUid: bookingToDelete.uid,
183-
userUuid,
184-
})
185-
);
186-
}
176+
const actionSource = input.actionSource;
187177

188178
const actorToUse = getAuditActor({
189179
userUuid,
@@ -724,10 +714,15 @@ type BookingCancelServiceDependencies = {
724714
export class BookingCancelService implements IBookingCancelService {
725715
constructor(private readonly deps: BookingCancelServiceDependencies) {}
726716

727-
async cancelBooking(input: { bookingData: CancelRegularBookingData; bookingMeta?: CancelBookingMeta }) {
717+
async cancelBooking(input: {
718+
bookingData: CancelRegularBookingData;
719+
bookingMeta?: CancelBookingMeta;
720+
actionSource: ValidActionSource;
721+
}) {
728722
const cancelBookingInput: CancelBookingInput = {
729723
bookingData: input.bookingData,
730724
...(input.bookingMeta || {}),
725+
actionSource: input.actionSource,
731726
};
732727

733728
return handler(cancelBookingInput, this.deps);

packages/features/bookings/lib/handleCancelBooking/test/handleCancelBooking.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ describe("Cancel Booking", () => {
134134
cancelledBy: organizer.email,
135135
cancellationReason: "No reason",
136136
},
137+
actionSource: "WEBAPP",
137138
});
138139

139140
expectBookingCancelledWebhookToHaveBeenFired({
@@ -264,6 +265,7 @@ describe("Cancel Booking", () => {
264265
cancelledBy: organizer.email,
265266
cancellationReason: "No reason",
266267
},
268+
actionSource: "WEBAPP",
267269
});
268270

269271
expectBookingCancelledWebhookToHaveBeenFired({
@@ -411,6 +413,7 @@ describe("Cancel Booking", () => {
411413
cancelledBy: organizer.email,
412414
cancellationReason: "Testing round robin cancellation with host as attendee",
413415
},
416+
actionSource: "WEBAPP",
414417
});
415418

416419
expect(result.success).toBe(true);
@@ -563,6 +566,7 @@ describe("Cancel Booking", () => {
563566
cancelledBy: primaryHost.email,
564567
cancellationReason: "Testing EMAIL_HOST workflow sends to secondary host in round robin",
565568
},
569+
actionSource: "WEBAPP",
566570
});
567571

568572
expect(result.success).toBe(true);
@@ -640,6 +644,7 @@ describe("Cancel Booking", () => {
640644
cancelledBy: organizer.email,
641645
cancellationReason: "Testing past booking cancellation",
642646
},
647+
actionSource: "WEBAPP",
643648
})
644649
).rejects.toThrow("Cannot cancel a booking that has already ended");
645650
});
@@ -708,6 +713,7 @@ describe("Cancel Booking", () => {
708713
uid: uidOfBookingToBeCancelled,
709714
cancelledBy: organizer.email,
710715
},
716+
actionSource: "WEBAPP",
711717
})
712718
).rejects.toThrow("Cancellation reason is required when you are the host");
713719
});
@@ -819,6 +825,7 @@ describe("Cancel Booking", () => {
819825
cancelledBy: organizer.email,
820826
cancellationReason: "No reason",
821827
},
828+
actionSource: "WEBAPP",
822829
});
823830

824831
expect(result.success).toBe(true);
@@ -945,6 +952,7 @@ describe("Cancel Booking", () => {
945952
cancelledBy: organizer.email,
946953
cancellationReason: "No reason",
947954
},
955+
actionSource: "WEBAPP",
948956
});
949957

950958
expect(result.success).toBe(true);
@@ -1062,6 +1070,7 @@ describe("Cancel Booking", () => {
10621070
cancellationReason: "Attendee cancelled within time threshold",
10631071
},
10641072
userId: 999,
1073+
actionSource: "WEBAPP",
10651074
});
10661075

10671076
expect(result.success).toBe(true);
@@ -1170,6 +1179,7 @@ describe("Cancel Booking", () => {
11701179
cancelledBy: organizer.email,
11711180
cancellationReason: "Organization booking cancellation test",
11721181
},
1182+
actionSource: "WEBAPP",
11731183
});
11741184

11751185
expectBookingCancelledWebhookToHaveBeenFired({
@@ -1292,6 +1302,7 @@ describe("Cancel Booking", () => {
12921302
cancellationReason: "Cancelling seated event",
12931303
},
12941304
userId: organizer.id,
1305+
actionSource: "WEBAPP",
12951306
});
12961307

12971308
expect(result.success).toBe(true);
@@ -1413,6 +1424,7 @@ describe("Cancel Booking", () => {
14131424
allRemainingBookings: true,
14141425
},
14151426
userId: organizer.id,
1427+
actionSource: "WEBAPP",
14161428
});
14171429

14181430
expect(result.success).toBe(true);
@@ -1534,6 +1546,7 @@ describe("Cancel Booking", () => {
15341546
cancelSubsequentBookings: true,
15351547
},
15361548
userId: organizer.id,
1549+
actionSource: "WEBAPP",
15371550
});
15381551

15391552
expect(result.success).toBe(true);
@@ -1647,6 +1660,7 @@ describe("Cancel Booking", () => {
16471660
cancellationReason: "Testing booking reference cleanup",
16481661
},
16491662
userId: organizer.id,
1663+
actionSource: "WEBAPP",
16501664
});
16511665

16521666
expect(result.success).toBe(true);

packages/features/bookings/lib/handleConfirmation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { eventTypeAppMetadataOptionalSchema } from "@calcom/app-store/zod-utils"
22
import { scheduleMandatoryReminder } from "@calcom/ee/workflows/lib/reminders/scheduleMandatoryReminder";
33
import { sendScheduledEmailsAndSMS } from "@calcom/emails/email-manager";
44
import type { Actor } from "@calcom/features/booking-audit/lib/dto/types";
5-
import type { ActionSource } from "@calcom/features/booking-audit/lib/types/actionSource";
5+
import type { ValidActionSource } from "@calcom/features/booking-audit/lib/types/actionSource";
66
import { getBookingEventHandlerService } from "@calcom/features/bookings/di/BookingEventHandlerService.container";
77
import type { EventManagerUser } from "@calcom/features/bookings/lib/EventManager";
88
import EventManager, { placeholderCreatedEvent } from "@calcom/features/bookings/lib/EventManager";
@@ -48,7 +48,7 @@ async function fireBookingAcceptedEvent({
4848
}: {
4949
actor: Actor;
5050
organizationId: number | null;
51-
actionSource: ActionSource;
51+
actionSource: ValidActionSource;
5252
acceptedBookings: {
5353
uid: string;
5454
oldStatus: BookingStatus;
@@ -133,7 +133,7 @@ export async function handleConfirmation(args: {
133133
emailsEnabled?: boolean;
134134
platformClientParams?: PlatformClientParams;
135135
traceContext: TraceContext;
136-
actionSource: ActionSource;
136+
actionSource: ValidActionSource;
137137
actor: Actor;
138138
}) {
139139
const {

packages/features/bookings/lib/handleSeats/test/handleSeats.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,6 +1976,7 @@ describe("handleSeats", () => {
19761976
cancellationReason: "test cancellation reason",
19771977
},
19781978
userId: organizer.id,
1979+
actionSource: "WEBAPP",
19791980
});
19801981

19811982
// Ensure that the booking has been cancelled
@@ -2115,6 +2116,7 @@ describe("handleSeats", () => {
21152116
cancellationReason: "test cancellation reason",
21162117
},
21172118
userId: organizer.id,
2119+
actionSource: "WEBAPP",
21182120
});
21192121

21202122
// Ensure that the booking has been cancelled
@@ -2967,6 +2969,7 @@ describe("handleSeats", () => {
29672969
cancellationReason: "test cancellation reason",
29682970
},
29692971
userId: organizer.id,
2972+
actionSource: "WEBAPP",
29702973
});
29712974

29722975
// Ensure that the booking has been cancelled

packages/features/bookings/lib/interfaces/IBookingCancelService.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import type {
33
CancelBookingMeta,
44
HandleCancelBookingResponse,
55
} from "../dto/BookingCancel";
6+
import type { ValidActionSource } from "@calcom/features/booking-audit/lib/types/actionSource";
67

78
export interface IBookingCancelService {
89
cancelBooking(input: {
910
bookingData: CancelRegularBookingData;
1011
bookingMeta?: CancelBookingMeta;
12+
actionSource: ValidActionSource;
1113
}): Promise<HandleCancelBookingResponse>;
1214
}

packages/trpc/server/routers/viewer/bookings/_router.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ export const bookingsRouter = router({
121121
return reportBookingHandler({
122122
ctx,
123123
input,
124+
actionSource: "WEBAPP",
124125
});
125126
}),
126127
reportWrongAssignment: authedProcedure

packages/trpc/server/routers/viewer/bookings/addGuests.handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getUsersCredentialsIncludeServiceAccountKey } from "@calcom/app-store/d
22
import { eventTypeMetaDataSchemaWithTypedApps } from "@calcom/app-store/zod-utils";
33
import dayjs from "@calcom/dayjs";
44
import { makeUserActor } from "@calcom/features/booking-audit/lib/makeActor";
5-
import type { ActionSource } from "@calcom/features/booking-audit/lib/types/actionSource";
5+
import type { ValidActionSource } from "@calcom/features/booking-audit/lib/types/actionSource";
66
import { getBookingEventHandlerService } from "@calcom/features/bookings/di/BookingEventHandlerService.container";
77
import { BookingEmailSmsHandler } from "@calcom/features/bookings/lib/BookingEmailSmsHandler";
88
import EventManager from "@calcom/features/bookings/lib/EventManager";
@@ -35,7 +35,7 @@ type AddGuestsOptions = {
3535
};
3636
input: TAddGuestsInputSchema;
3737
emailsEnabled?: boolean;
38-
actionSource: ActionSource;
38+
actionSource: ValidActionSource;
3939
};
4040

4141
export type Booking = NonNullable<

packages/trpc/server/routers/viewer/bookings/reportBooking.handler.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { PrismaBookingReportRepository } from "@calcom/features/bookingReport/repositories/PrismaBookingReportRepository";
2+
import type { ValidActionSource } from "@calcom/features/booking-audit/lib/types/actionSource";
23
import handleCancelBooking from "@calcom/features/bookings/lib/handleCancelBooking";
34
import { BookingRepository } from "@calcom/features/bookings/repositories/BookingRepository";
45
import { BookingAccessService } from "@calcom/features/bookings/services/BookingAccessService";
@@ -16,11 +17,12 @@ type ReportBookingOptions = {
1617
user: NonNullable<TrpcSessionUser>;
1718
};
1819
input: TReportBookingInputSchema;
20+
actionSource: ValidActionSource;
1921
};
2022

2123
const log = logger.getSubLogger({ prefix: ["reportBookingHandler"] });
2224

23-
export const reportBookingHandler = async ({ ctx, input }: ReportBookingOptions) => {
25+
export const reportBookingHandler = async ({ ctx, input, actionSource }: ReportBookingOptions) => {
2426
const { user } = ctx;
2527
const { bookingUid, reason, description } = input;
2628

@@ -90,6 +92,7 @@ export const reportBookingHandler = async ({ ctx, input }: ReportBookingOptions)
9092
...(seatReferenceUid ? { seatReferenceUid } : {}),
9193
},
9294
userId: user.id,
95+
actionSource,
9396
});
9497
didCancel = true;
9598
} catch (error) {

0 commit comments

Comments
 (0)