* version and verify-booking-token * username route * api/me * logo route * render email in dev * link route * future * nope and geo location * referal link and daily * intercom route and dynamic variables * intercom route and dynamic variables * scim 2.0 and helpscout route * app credentials * api/book events * fix daily import path in tests * fix buildLegacyRequest generation * fix type errors * migrate the /teams/ routes * move cron jobs * fix daily import path in tests * fix buildLegacyRequest generation * fix type errors * migrate the /teams/ routes * move cron jobs * Revert "api/book events" This reverts commit 607a32fb5b754cad090c2d0cbf64a68f990e220e. * mock next server NextResponse to fix daily video * add default responder to teams api * fix search params * uses nextUrl.searchParams instead of new url * uses nextUrl.searchParams instead of new url * remove outdated api config * remove app dir version of inbound dynamic variables * restore pages version of inbound variables * fix missing code from stupid cursor --------- Co-authored-by: Benny Joo <sldisek783@gmail.com>
113 lines
3.4 KiB
TypeScript
113 lines
3.4 KiB
TypeScript
import { cookies, headers } from "next/headers";
|
|
import type { NextRequest } from "next/server";
|
|
import { NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
|
|
import { symmetricDecrypt } from "@calcom/lib/crypto";
|
|
import prisma from "@calcom/prisma";
|
|
import { UserPermissionRole } from "@calcom/prisma/enums";
|
|
import { createContext } from "@calcom/trpc/server/createContext";
|
|
import { bookingsRouter } from "@calcom/trpc/server/routers/viewer/bookings/_router";
|
|
import type { UserProfile } from "@calcom/types/UserProfile";
|
|
|
|
import { buildLegacyRequest } from "@lib/buildLegacyCtx";
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
enum DirectAction {
|
|
ACCEPT = "accept",
|
|
REJECT = "reject",
|
|
}
|
|
|
|
const querySchema = z.object({
|
|
action: z.nativeEnum(DirectAction),
|
|
token: z.string(),
|
|
reason: z.string().optional(),
|
|
});
|
|
|
|
const decryptedSchema = z.object({
|
|
bookingUid: z.string(),
|
|
userId: z.number().int(),
|
|
});
|
|
|
|
// Move the sessionGetter function outside the GET function
|
|
const createSessionGetter = (userId: number) => async () => {
|
|
return {
|
|
user: {
|
|
id: userId,
|
|
username: "" /* Not used in this context */,
|
|
role: UserPermissionRole.USER,
|
|
/* Not used in this context */
|
|
profile: {
|
|
id: null,
|
|
organizationId: null,
|
|
organization: null,
|
|
username: "",
|
|
upId: "",
|
|
} satisfies UserProfile,
|
|
},
|
|
upId: "",
|
|
hasValidLicense: true,
|
|
expires: "" /* Not used in this context */,
|
|
};
|
|
};
|
|
|
|
export async function GET(request: NextRequest) {
|
|
const url = new URL(request.url);
|
|
const searchParams = request.nextUrl.searchParams;
|
|
|
|
try {
|
|
const { action, token, reason } = querySchema.parse(Object.fromEntries(searchParams.entries()));
|
|
|
|
const decryptedData = JSON.parse(
|
|
symmetricDecrypt(decodeURIComponent(token), process.env.CALENDSO_ENCRYPTION_KEY || "")
|
|
);
|
|
|
|
const { bookingUid, userId } = decryptedSchema.parse(decryptedData);
|
|
|
|
const booking = await prisma.booking.findUniqueOrThrow({
|
|
where: { uid: bookingUid },
|
|
});
|
|
|
|
const user = await prisma.user.findUniqueOrThrow({
|
|
where: { id: userId },
|
|
});
|
|
|
|
// Use the factory function instead of declaring inside the block
|
|
const sessionGetter = createSessionGetter(userId);
|
|
|
|
try {
|
|
/** @see https://trpc.io/docs/server-side-calls */
|
|
// Create a legacy request object for compatibility
|
|
const legacyReq = buildLegacyRequest(headers(), cookies());
|
|
const res = {} as any; // Response is still mocked as it's not used in this context
|
|
|
|
const ctx = await createContext({ req: legacyReq, res }, sessionGetter);
|
|
const caller = bookingsRouter.createCaller({
|
|
...ctx,
|
|
req: legacyReq,
|
|
res,
|
|
user: { ...user, locale: user?.locale ?? "en" },
|
|
});
|
|
|
|
await caller.confirm({
|
|
bookingId: booking.id,
|
|
recurringEventId: booking.recurringEventId || undefined,
|
|
confirmed: action === DirectAction.ACCEPT,
|
|
reason,
|
|
});
|
|
} catch (e) {
|
|
let message = "Error confirming booking";
|
|
if (e instanceof TRPCError) message = (e as TRPCError).message;
|
|
return NextResponse.redirect(
|
|
`${url.origin}/booking/${bookingUid}?error=${encodeURIComponent(message)}`
|
|
);
|
|
}
|
|
|
|
return NextResponse.redirect(`${url.origin}/booking/${bookingUid}`);
|
|
} catch (error) {
|
|
console.error("Error processing link request:", error);
|
|
return NextResponse.json({ error: "Invalid request" }, { status: 400 });
|
|
}
|
|
}
|