From b552bf69569ac6adf07cf04e6fcf6d262c2cfbfa Mon Sep 17 00:00:00 2001 From: Shivang Sharma <70644519+shivang-sharma@users.noreply.github.com> Date: Sat, 13 May 2023 09:41:12 +0530 Subject: [PATCH] Fix: #8528 Moved the data checks to superRefine (#8749) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix: #8528 Moved the data checks to superRefine * Resolved lint isseu * Updated to parse from safeParse * added new line --------- Co-authored-by: Omar López --- .../stripeCheckoutSession.handler.ts | 10 +++----- .../stripeCheckoutSession.schema.ts | 23 +++++++++++++++---- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/packages/trpc/server/routers/publicViewer/stripeCheckoutSession.handler.ts b/packages/trpc/server/routers/publicViewer/stripeCheckoutSession.handler.ts index 8e65440eed..fcaf642d0b 100644 --- a/packages/trpc/server/routers/publicViewer/stripeCheckoutSession.handler.ts +++ b/packages/trpc/server/routers/publicViewer/stripeCheckoutSession.handler.ts @@ -1,6 +1,7 @@ import stripe from "@calcom/app-store/stripepayment/lib/server"; import type { TStripeCheckoutSessionInputSchema } from "./stripeCheckoutSession.schema"; +import { ZStripeCheckoutSessionInputSchema } from "./stripeCheckoutSession.schema"; type StripeCheckoutSessionOptions = { input: TStripeCheckoutSessionInputSchema; @@ -9,14 +10,9 @@ type StripeCheckoutSessionOptions = { export const stripeCheckoutSessionHandler = async ({ input }: StripeCheckoutSessionOptions) => { const { checkoutSessionId, stripeCustomerId } = input; - // TODO: Move the following data checks to superRefine - if (!checkoutSessionId && !stripeCustomerId) { - throw new Error("Missing checkoutSessionId or stripeCustomerId"); - } + // Moved the following data checks to superRefine + const validationResult = ZStripeCheckoutSessionInputSchema.parse(input); - if (checkoutSessionId && stripeCustomerId) { - throw new Error("Both checkoutSessionId and stripeCustomerId provided"); - } let customerId: string; let isPremiumUsername = false; let hasPaymentFailed = false; diff --git a/packages/trpc/server/routers/publicViewer/stripeCheckoutSession.schema.ts b/packages/trpc/server/routers/publicViewer/stripeCheckoutSession.schema.ts index 5e307112f2..db054fe2ab 100644 --- a/packages/trpc/server/routers/publicViewer/stripeCheckoutSession.schema.ts +++ b/packages/trpc/server/routers/publicViewer/stripeCheckoutSession.schema.ts @@ -1,8 +1,23 @@ import { z } from "zod"; -export const ZStripeCheckoutSessionInputSchema = z.object({ - stripeCustomerId: z.string().optional(), - checkoutSessionId: z.string().optional(), -}); +export const ZStripeCheckoutSessionInputSchema = z + .object({ + stripeCustomerId: z.string().optional(), + checkoutSessionId: z.string().optional(), + }) + .superRefine((arg, ctx) => { + if (!arg.checkoutSessionId && !arg.stripeCustomerId) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: "Missing checkoutSessionId or stripeCustomerId", + }); + } + if (arg.checkoutSessionId && arg.stripeCustomerId) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: "Both checkoutSessionId and stripeCustomerId provided", + }); + } + }); export type TStripeCheckoutSessionInputSchema = z.infer;