Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: ignore oAuthClientId for Booker Embed bookings #18314

Merged
merged 11 commits into from
Jan 6, 2025
Merged
2 changes: 2 additions & 0 deletions apps/api/v2/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
CAL_API_VERSION_HEADER,
X_CAL_CLIENT_ID,
X_CAL_SECRET_KEY,
X_CAL_PLATFORM_EMBED,
} from "@calcom/platform-constants";

import { TRPCExceptionFilter } from "./filters/trpc-exception.filter";
Expand Down Expand Up @@ -45,6 +46,7 @@ export const bootstrap = (app: NestExpressApplication): NestExpressApplication =
allowedHeaders: [
X_CAL_CLIENT_ID,
X_CAL_SECRET_KEY,
X_CAL_PLATFORM_EMBED,
CAL_API_VERSION_HEADER,
"Accept",
"Authorization",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { Request } from "express";
import { NextApiRequest } from "next/types";
import { v4 as uuidv4 } from "uuid";

import { X_CAL_CLIENT_ID } from "@calcom/platform-constants";
import { X_CAL_CLIENT_ID, X_CAL_PLATFORM_EMBED } from "@calcom/platform-constants";
import { BOOKING_READ, SUCCESS_STATUS, BOOKING_WRITE } from "@calcom/platform-constants";
import {
handleNewRecurringBooking,
Expand Down Expand Up @@ -161,14 +161,15 @@ export class BookingsController_2024_04_15 {
async createBooking(
@Req() req: BookingRequest,
@Body() body: CreateBookingInput_2024_04_15,
@Headers(X_CAL_CLIENT_ID) clientId?: string
@Headers(X_CAL_CLIENT_ID) clientId?: string,
@Headers(X_CAL_PLATFORM_EMBED) isEmbed?: string
): Promise<ApiResponse<Partial<BookingResponse>>> {
const oAuthClientId = clientId?.toString();
const { orgSlug, locationUrl } = body;
req.headers["x-cal-force-slug"] = orgSlug;
try {
const booking = await handleNewBooking(
await this.createNextApiBookingRequest(req, oAuthClientId, locationUrl)
await this.createNextApiBookingRequest(req, oAuthClientId, locationUrl, isEmbed)
);
if (booking.userId && booking.uid && booking.startTime) {
void (await this.billingService.increaseUsageByUserId(booking.userId, {
Expand All @@ -192,13 +193,16 @@ export class BookingsController_2024_04_15 {
@Req() req: BookingRequest,
@Param("bookingId") bookingId: string,
@Body() _: CancelBookingInput_2024_04_15,
@Headers(X_CAL_CLIENT_ID) clientId?: string
@Headers(X_CAL_CLIENT_ID) clientId?: string,
@Headers(X_CAL_PLATFORM_EMBED) isEmbed?: string
): Promise<ApiResponse<{ bookingId: number; bookingUid: string; onlyRemovedAttendee: boolean }>> {
const oAuthClientId = clientId?.toString();
if (bookingId) {
try {
req.body.id = parseInt(bookingId);
const res = await handleCancelBooking(await this.createNextApiBookingRequest(req, oAuthClientId));
const res = await handleCancelBooking(
await this.createNextApiBookingRequest(req, oAuthClientId, undefined, isEmbed)
);
if (!res.onlyRemovedAttendee) {
void (await this.billingService.cancelUsageByBookingUid(res.bookingUid));
}
Expand Down Expand Up @@ -246,7 +250,8 @@ export class BookingsController_2024_04_15 {
async createRecurringBooking(
@Req() req: BookingRequest,
@Body() _: CreateRecurringBookingInput_2024_04_15[],
@Headers(X_CAL_CLIENT_ID) clientId?: string
@Headers(X_CAL_CLIENT_ID) clientId?: string,
@Headers(X_CAL_PLATFORM_EMBED) isEmbed?: string
): Promise<ApiResponse<BookingResponse[]>> {
const oAuthClientId = clientId?.toString();
try {
Expand All @@ -258,7 +263,7 @@ export class BookingsController_2024_04_15 {
}

const createdBookings: BookingResponse[] = await handleNewRecurringBooking(
await this.createNextApiRecurringBookingRequest(req, oAuthClientId)
await this.createNextApiRecurringBookingRequest(req, oAuthClientId, undefined, isEmbed)
);

createdBookings.forEach(async (booking) => {
Expand All @@ -284,13 +289,14 @@ export class BookingsController_2024_04_15 {
async createInstantBooking(
@Req() req: BookingRequest,
@Body() _: CreateBookingInput_2024_04_15,
@Headers(X_CAL_CLIENT_ID) clientId?: string
@Headers(X_CAL_CLIENT_ID) clientId?: string,
@Headers(X_CAL_PLATFORM_EMBED) isEmbed?: string
): Promise<ApiResponse<Awaited<ReturnType<typeof handleInstantMeeting>>>> {
const oAuthClientId = clientId?.toString();
req.userId = (await this.getOwnerId(req)) ?? -1;
try {
const instantMeeting = await handleInstantMeeting(
await this.createNextApiBookingRequest(req, oAuthClientId)
await this.createNextApiBookingRequest(req, oAuthClientId, undefined, isEmbed)
);

if (instantMeeting.userId && instantMeeting.bookingUid) {
Expand Down Expand Up @@ -332,8 +338,14 @@ export class BookingsController_2024_04_15 {
}
}

private async getOAuthClientsParams(clientId: string): Promise<OAuthRequestParams> {
private async getOAuthClientsParams(clientId: string, isEmbed = false): Promise<OAuthRequestParams> {
const res = { ...DEFAULT_PLATFORM_PARAMS };

if (isEmbed) {
// embed should ignore oauth client settings and enable emails by default
return { ...res, arePlatformEmailsEnabled: true };
}

try {
const client = await this.oAuthClientRepository.getOAuthClient(clientId);
// fetch oAuthClient from db and use data stored in db to set these values
Expand All @@ -354,13 +366,14 @@ export class BookingsController_2024_04_15 {
private async createNextApiBookingRequest(
req: BookingRequest,
oAuthClientId?: string,
platformBookingLocation?: string
platformBookingLocation?: string,
isEmbed?: string
): Promise<NextApiRequest & { userId?: number } & OAuthRequestParams> {
const requestId = req.get("X-Request-Id");
const clone = { ...req };
const userId = (await this.getOwnerId(req)) ?? -1;
const oAuthParams = oAuthClientId
? await this.getOAuthClientsParams(oAuthClientId)
? await this.getOAuthClientsParams(oAuthClientId, this.transformToBoolean(isEmbed))
: DEFAULT_PLATFORM_PARAMS;
this.logger.log(`createNextApiBookingRequest_2024_04_15`, {
requestId,
Expand All @@ -377,12 +390,13 @@ export class BookingsController_2024_04_15 {
private async createNextApiRecurringBookingRequest(
req: BookingRequest,
oAuthClientId?: string,
platformBookingLocation?: string
platformBookingLocation?: string,
isEmbed?: string
): Promise<NextApiRequest & { userId?: number } & OAuthRequestParams> {
const clone = { ...req };
const userId = (await this.getOwnerId(req)) ?? -1;
const oAuthParams = oAuthClientId
? await this.getOAuthClientsParams(oAuthClientId)
? await this.getOAuthClientsParams(oAuthClientId, this.transformToBoolean(isEmbed))
: DEFAULT_PLATFORM_PARAMS;
const requestId = req.get("X-Request-Id");
this.logger.log(`createNextApiRecurringBookingRequest_2024_04_15`, {
Expand Down Expand Up @@ -424,4 +438,8 @@ export class BookingsController_2024_04_15 {

throw new InternalServerErrorException(errMsg);
}

private transformToBoolean(v?: string): boolean {
return v && typeof v === "string" ? v.toLowerCase() === "true" : false;
}
}
4 changes: 4 additions & 0 deletions packages/platform/atoms/cal-provider/CalProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ export function CalProvider({
}
}, [accessToken]);

useEffect(() => {
http.setPlatformEmbedHeader(isEmbed);
}, [isEmbed]);

return (
<QueryClientProvider client={queryClient}>
<BaseCalProvider
Expand Down
8 changes: 7 additions & 1 deletion packages/platform/atoms/lib/http.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from "axios";

import { CAL_API_VERSION_HEADER, X_CAL_CLIENT_ID } from "@calcom/platform-constants";
import { CAL_API_VERSION_HEADER, X_CAL_CLIENT_ID, X_CAL_PLATFORM_EMBED } from "@calcom/platform-constants";

// Immediately Invoked Function Expression to create simple singleton class like

Expand Down Expand Up @@ -42,6 +42,12 @@ const http = (function () {
getClientIdHeader: () => {
return instance.defaults.headers.common?.[X_CAL_CLIENT_ID]?.toString() ?? "";
},
setPlatformEmbedHeader: (isEmbed: boolean) => {
instance.defaults.headers.common[X_CAL_PLATFORM_EMBED] = isEmbed.toString();
},
getPlatformEmbedHeader: () => {
return instance.defaults.headers.common?.[X_CAL_PLATFORM_EMBED]?.toString() ?? "";
},
setVersionHeader: (clientId: string) => {
instance.defaults.headers.common[CAL_API_VERSION_HEADER] = clientId;
},
Expand Down
1 change: 1 addition & 0 deletions packages/platform/constants/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const API_ERROR_CODES = [
// Request headers
export const X_CAL_SECRET_KEY = "x-cal-secret-key";
export const X_CAL_CLIENT_ID = "x-cal-client-id";
export const X_CAL_PLATFORM_EMBED = "x-cal-platform-embed";

// HTTP status codes
export const HTTP_CODE_TOKEN_EXPIRED = 498;
Expand Down
Loading