Skip to content

Commit

Permalink
Fix TS issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hariombalhara committed Mar 20, 2023
1 parent 6d22f12 commit 98f36b3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
6 changes: 5 additions & 1 deletion packages/features/bookings/lib/getBookingResponsesSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ function preprocess<T extends z.ZodType>({
schema: T;
isPartialSchema: boolean;
eventType: {
bookingFields: z.infer<typeof eventTypeBookingFields> & z.BRAND<"HAS_SYSTEM_FIELDS">;
bookingFields: (z.infer<typeof eventTypeBookingFields> & z.BRAND<"HAS_SYSTEM_FIELDS">) | null;
};
view: View;
}): z.ZodType<z.infer<T>, z.infer<T>, z.infer<T>> {
const preprocessed = z.preprocess(
(responses) => {
const parsedResponses = z.record(z.any()).nullable().parse(responses) || {};
const newResponses = {} as typeof parsedResponses;
if (!eventType.bookingFields) return parsedResponses;
eventType.bookingFields.forEach((field) => {
const value = parsedResponses[field.name];
if (value === undefined) {
Expand Down Expand Up @@ -86,6 +87,9 @@ function preprocess<T extends z.ZodType>({
return newResponses;
},
schema.superRefine((responses, ctx) => {
if (!eventType.bookingFields) {
return;
}
eventType.bookingFields.forEach((bookingField) => {
const value = responses[bookingField.name];
const stringSchema = z.string();
Expand Down
4 changes: 2 additions & 2 deletions packages/features/bookings/lib/getCalEventResponses.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type z from "zod";

import { SystemField } from "@calcom/features/bookings/lib/getBookingFields";
import type getBookingResponsesSchema from "@calcom/features/bookings/lib/getBookingResponsesSchema";
import type { getBookingResponsesPartialSchema } from "@calcom/features/bookings/lib/getBookingResponsesSchema";
import type { eventTypeBookingFields } from "@calcom/prisma/zod-utils";
import type { CalendarEvent } from "@calcom/types/Calendar";

Expand All @@ -12,7 +12,7 @@ export const getCalEventResponses = ({
// If the eventType has been deleted and a booking is Accepted later on, then bookingFields will be null and we can't know the label of fields. So, we should store the label as well in the DB
// Also, it is no longer straightforward to identify if a field is system field or not
bookingFields: z.infer<typeof eventTypeBookingFields> | null;
responses: z.infer<ReturnType<typeof getBookingResponsesSchema>>;
responses: z.infer<ReturnType<typeof getBookingResponsesPartialSchema>>;
}) => {
const calEventUserFieldsResponses = {} as NonNullable<CalendarEvent["userFieldsResponses"]>;
const calEventResponses = {} as NonNullable<CalendarEvent["responses"]>;
Expand Down
13 changes: 12 additions & 1 deletion packages/trpc/server/routers/viewer/bookings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { deleteScheduledEmailReminder } from "@calcom/ee/workflows/lib/reminders
import { deleteScheduledSMSReminder } from "@calcom/ee/workflows/lib/reminders/smsReminderManager";
import { sendDeclinedEmails, sendLocationChangeEmails, sendRequestRescheduleEmail } from "@calcom/emails";
import { getBookingFieldsWithSystemFields } from "@calcom/features/bookings/lib/getBookingFields";
import { getBookingResponsesPartialSchema } from "@calcom/features/bookings/lib/getBookingResponsesSchema";
import { getCalEventResponses } from "@calcom/features/bookings/lib/getCalEventResponses";
import { handleConfirmation } from "@calcom/features/bookings/lib/handleConfirmation";
import getWebhooks from "@calcom/features/webhooks/lib/getWebhooks";
Expand Down Expand Up @@ -772,12 +773,22 @@ export const bookingsRouter = router({
scheduledJobs: true,
},
});
const bookingFields = bookingRaw.eventType
? getBookingFieldsWithSystemFields(bookingRaw.eventType)
: null;
const booking = {
...bookingRaw,
responses: getBookingResponsesPartialSchema({
eventType: {
bookingFields,
},
// An existing booking can have data from any number of views, so the schema should consider ALL_VIEWS
view: "ALL_VIEWS",
}),
eventType: bookingRaw.eventType
? {
...bookingRaw.eventType,
bookingFields: getBookingFieldsWithSystemFields(bookingRaw.eventType),
bookingFields,
}
: null,
};
Expand Down

0 comments on commit 98f36b3

Please sign in to comment.