Fix booking success page crash when a booking doesnt have newly added required fields response

pull/6560/head
Hariom Balhara 2023-02-16 13:23:18 +05:30
parent c5cd57d8f4
commit cdb5b4a9dd
4 changed files with 10 additions and 7 deletions

View File

@ -26,7 +26,7 @@ import {
SystemField, SystemField,
} from "@calcom/features/bookings/lib/getBookingFields"; } from "@calcom/features/bookings/lib/getBookingFields";
import getBookingResponsesSchema, { import getBookingResponsesSchema, {
getBookingResponsesQuerySchema, getBookingResponsesPartialSchema,
} from "@calcom/features/bookings/lib/getBookingResponsesSchema"; } from "@calcom/features/bookings/lib/getBookingResponsesSchema";
import { FormBuilderField } from "@calcom/features/form-builder/FormBuilder"; import { FormBuilderField } from "@calcom/features/form-builder/FormBuilder";
import CustomBranding from "@calcom/lib/CustomBranding"; import CustomBranding from "@calcom/lib/CustomBranding";
@ -261,7 +261,7 @@ const BookingPage = ({
const rescheduleUid = router.query.rescheduleUid as string; const rescheduleUid = router.query.rescheduleUid as string;
useTheme(profile.theme); useTheme(profile.theme);
const date = asStringOrNull(router.query.date); const date = asStringOrNull(router.query.date);
const querySchema = getBookingResponsesQuerySchema({ const querySchema = getBookingResponsesPartialSchema({
bookingFields: getBookingFieldsWithSystemFields(eventType), bookingFields: getBookingFieldsWithSystemFields(eventType),
}); });

View File

@ -1,7 +1,7 @@
import { Prisma, PrismaClient } from "@prisma/client"; import { Prisma, PrismaClient } from "@prisma/client";
import { z } from "zod"; import { z } from "zod";
import getBookingResponsesSchema from "@calcom/features/bookings/lib/getBookingResponsesSchema"; import { getBookingResponsesPartialSchema } from "@calcom/features/bookings/lib/getBookingResponsesSchema";
import slugify from "@calcom/lib/slugify"; import slugify from "@calcom/lib/slugify";
import { eventTypeBookingFields } from "@calcom/prisma/zod-utils"; import { eventTypeBookingFields } from "@calcom/prisma/zod-utils";
@ -72,7 +72,7 @@ async function getBooking(
} }
const booking = { const booking = {
...rawBooking, ...rawBooking,
responses: getBookingResponsesSchema({ responses: getBookingResponsesPartialSchema({
bookingFields, bookingFields,
}).parse(rawBooking.responses || getResponsesFromOldBooking(rawBooking)), }).parse(rawBooking.responses || getResponsesFromOldBooking(rawBooking)),
}; };

View File

@ -29,7 +29,7 @@ import {
SystemField, SystemField,
getBookingFieldsWithSystemFields, getBookingFieldsWithSystemFields,
} from "@calcom/features/bookings/lib/getBookingFields"; } from "@calcom/features/bookings/lib/getBookingFields";
import getBookingResponsesSchema from "@calcom/features/bookings/lib/getBookingResponsesSchema"; import { getBookingResponsesPartialSchema } from "@calcom/features/bookings/lib/getBookingResponsesSchema";
import { parseRecurringEvent } from "@calcom/lib"; import { parseRecurringEvent } from "@calcom/lib";
import CustomBranding from "@calcom/lib/CustomBranding"; import CustomBranding from "@calcom/lib/CustomBranding";
import { APP_NAME } from "@calcom/lib/constants"; import { APP_NAME } from "@calcom/lib/constants";
@ -1029,7 +1029,7 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
const bookingInfo = { const bookingInfo = {
...bookingInfoRaw, ...bookingInfoRaw,
responses: getBookingResponsesSchema(eventTypeRaw).parse(bookingInfoRaw.responses), responses: getBookingResponsesPartialSchema(eventTypeRaw).parse(bookingInfoRaw.responses),
}; };
// @NOTE: had to do this because Server side cant return [Object objects] // @NOTE: had to do this because Server side cant return [Object objects]

View File

@ -4,12 +4,15 @@ import z from "zod";
import { bookingResponses, eventTypeBookingFields } from "@calcom/prisma/zod-utils"; import { bookingResponses, eventTypeBookingFields } from "@calcom/prisma/zod-utils";
type EventType = Parameters<typeof preprocess>[0]["eventType"]; type EventType = Parameters<typeof preprocess>[0]["eventType"];
export const getBookingResponsesQuerySchema = (eventType: EventType) => { export const getBookingResponsesPartialSchema = (eventType: EventType) => {
const schema = bookingResponses.unwrap().partial().and(z.record(z.any())); const schema = bookingResponses.unwrap().partial().and(z.record(z.any()));
return preprocess({ schema, eventType, forQueryParsing: true }); return preprocess({ schema, eventType, forQueryParsing: true });
}; };
// Should be used when we know that not all fields responses are present
// - Can happen when we are parsing the prefill query string
// - Can happen when we are parsing a booking's responses (which was created before we added a new required field)
export default function getBookingResponsesSchema(eventType: EventType) { export default function getBookingResponsesSchema(eventType: EventType) {
const schema = bookingResponses.and(z.record(z.any())); const schema = bookingResponses.and(z.record(z.any()));
return preprocess({ schema, eventType, forQueryParsing: false }); return preprocess({ schema, eventType, forQueryParsing: false });