From d0a3d8d144549b7dc3dfe417ae8e2883f948d044 Mon Sep 17 00:00:00 2001 From: zomars Date: Thu, 22 Dec 2022 10:52:50 -0700 Subject: [PATCH] Link fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit commit 56c1608deaf1921a80dc71bcbd107e772dd656f4 Author: Omar López Date: Wed Dec 21 19:26:04 2022 -0700 Update link.ts commit eab43d833515cabc7acf2f973c2d2837ac1a0332 Author: zomars Date: Wed Dec 21 19:03:02 2022 -0700 Prevents 500 on TRPC error commit 3a6970d5e5fab54136b379e1b2ccd7acf515a809 Author: zomars Date: Wed Dec 21 18:15:26 2022 -0700 Encoding fixes --- apps/web/pages/api/link.ts | 30 ++++++++++++------- .../src/templates/OrganizerRequestEmail.tsx | 2 +- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/apps/web/pages/api/link.ts b/apps/web/pages/api/link.ts index a25c3958d6..9b692d95de 100644 --- a/apps/web/pages/api/link.ts +++ b/apps/web/pages/api/link.ts @@ -5,6 +5,7 @@ import { z } from "zod"; import { symmetricDecrypt } from "@calcom/lib/crypto"; import { defaultResponder } from "@calcom/lib/server"; import prisma from "@calcom/prisma"; +import { TRPCError } from "@calcom/trpc/server"; import { createContext } from "@calcom/trpc/server/createContext"; import { viewerRouter } from "@calcom/trpc/server/routers/viewer"; @@ -27,7 +28,7 @@ const decryptedSchema = z.object({ async function handler(req: NextApiRequest, res: NextApiResponse) { const { action, token, reason } = querySchema.parse(req.query); const { bookingUid, userId } = decryptedSchema.parse( - JSON.parse(symmetricDecrypt(token, process.env.CALENDSO_ENCRYPTION_KEY || "")) + JSON.parse(symmetricDecrypt(decodeURIComponent(token), process.env.CALENDSO_ENCRYPTION_KEY || "")) ); /** We shape the session as required by tRPC router */ @@ -47,17 +48,24 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { where: { uid: bookingUid }, }); - /** @see https://trpc.io/docs/server-side-calls */ - const ctx = await createContext({ req, res }, sessionGetter); - const caller = viewerRouter.createCaller(ctx); - await caller.bookings.confirm({ - bookingId: booking.id, - recurringEventId: booking.recurringEventId || undefined, - confirmed: action === DirectAction.ACCEPT, - reason, - }); + try { + /** @see https://trpc.io/docs/server-side-calls */ + const ctx = await createContext({ req, res }, sessionGetter); + const caller = viewerRouter.createCaller(ctx); + await caller.bookings.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.message; + res.redirect(`/booking/${bookingUid}?error=${encodeURIComponent(message)}`); + return; + } - return res.redirect(`/booking/${bookingUid}`); + res.redirect(`/booking/${bookingUid}`); } export default defaultResponder(handler); diff --git a/packages/emails/src/templates/OrganizerRequestEmail.tsx b/packages/emails/src/templates/OrganizerRequestEmail.tsx index 7029681212..ab441b27dc 100644 --- a/packages/emails/src/templates/OrganizerRequestEmail.tsx +++ b/packages/emails/src/templates/OrganizerRequestEmail.tsx @@ -7,7 +7,7 @@ import { OrganizerScheduledEmail } from "./OrganizerScheduledEmail"; export const OrganizerRequestEmail = (props: React.ComponentProps) => { const seedData = { bookingUid: props.calEvent.uid, userId: props.calEvent.organizer.id }; const token = symmetricEncrypt(JSON.stringify(seedData), process.env.CALENDSO_ENCRYPTION_KEY || ""); - const actionHref = `${WEBAPP_URL}/api/link/?token=${token}`; + const actionHref = `${WEBAPP_URL}/api/link/?token=${encodeURIComponent(token)}`; return (