Fix: #8528 Moved the data checks to superRefine (#8749)

* 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 <zomars@me.com>
This commit is contained in:
Shivang Sharma 2023-05-13 09:41:12 +05:30 committed by GitHub
parent 82d3c4370d
commit b552bf6956
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 11 deletions

View File

@ -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;

View File

@ -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<typeof ZStripeCheckoutSessionInputSchema>;