forked from calcom/cal.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: removed isNotAnApiCall (calcom#13568)
- Loading branch information
Showing
14 changed files
with
596 additions
and
643 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,20 @@ | ||
import { z } from "zod"; | ||
|
||
import { | ||
bookingCreateSchemaLegacyPropsForApi, | ||
bookingCreateBodySchemaForApi, | ||
extendedBookingCreateBody, | ||
} from "@calcom/prisma/zod-utils"; | ||
import { extendedBookingCreateBody } from "@calcom/prisma/zod-utils"; | ||
|
||
import type { getBookingFieldsWithSystemFields } from "./getBookingFields"; | ||
import getBookingResponsesSchema from "./getBookingResponsesSchema"; | ||
import type { getEventTypesFromDB } from "./handleNewBooking"; | ||
|
||
const getBookingDataSchema = ( | ||
rescheduleUid: string | undefined, | ||
isNotAnApiCall: boolean, | ||
eventType: Awaited<ReturnType<typeof getEventTypesFromDB>> | ||
) => { | ||
const responsesSchema = getBookingResponsesSchema({ | ||
eventType: { | ||
bookingFields: eventType.bookingFields, | ||
}, | ||
view: rescheduleUid ? "reschedule" : "booking", | ||
}); | ||
const bookingDataSchema = isNotAnApiCall | ||
? extendedBookingCreateBody.merge( | ||
z.object({ | ||
responses: responsesSchema, | ||
}) | ||
) | ||
: bookingCreateBodySchemaForApi | ||
.merge( | ||
z.object({ | ||
responses: responsesSchema.optional(), | ||
}) | ||
) | ||
.superRefine((val, ctx) => { | ||
if (val.responses && val.customInputs) { | ||
ctx.addIssue({ | ||
code: "custom", | ||
message: | ||
"Don't use both customInputs and responses. `customInputs` is only there for legacy support.", | ||
}); | ||
return; | ||
} | ||
const legacyProps = Object.keys(bookingCreateSchemaLegacyPropsForApi.shape); | ||
|
||
if (val.responses) { | ||
const unwantedProps: string[] = []; | ||
legacyProps.forEach((legacyProp) => { | ||
if (typeof val[legacyProp as keyof typeof val] !== "undefined") { | ||
console.error( | ||
`Deprecated: Unexpected falsy value for: ${unwantedProps.join( | ||
"," | ||
)}. They can't be used with \`responses\`. This will become a 400 error in the future.` | ||
); | ||
} | ||
if (val[legacyProp as keyof typeof val]) { | ||
unwantedProps.push(legacyProp); | ||
} | ||
}); | ||
if (unwantedProps.length) { | ||
ctx.addIssue({ | ||
code: "custom", | ||
message: `Legacy Props: ${unwantedProps.join(",")}. They can't be used with \`responses\``, | ||
}); | ||
return; | ||
} | ||
} else if (val.customInputs) { | ||
const { success } = bookingCreateSchemaLegacyPropsForApi.safeParse(val); | ||
if (!success) { | ||
ctx.addIssue({ | ||
code: "custom", | ||
message: `With \`customInputs\` you must specify legacy props ${legacyProps.join(",")}`, | ||
}); | ||
} | ||
} | ||
}); | ||
return bookingDataSchema; | ||
const getBookingDataSchema = ({ | ||
view = "booking", | ||
bookingFields, | ||
}: { | ||
view: "booking" | "reschedule"; | ||
bookingFields: Awaited<ReturnType<typeof getBookingFieldsWithSystemFields>>; | ||
}) => { | ||
return extendedBookingCreateBody.merge( | ||
z.object({ responses: getBookingResponsesSchema({ bookingFields, view }) }) | ||
); | ||
}; | ||
|
||
export default getBookingDataSchema; |
69 changes: 69 additions & 0 deletions
69
packages/features/bookings/lib/getBookingDataSchemaForApi.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { z } from "zod"; | ||
|
||
import { | ||
bookingCreateBodySchemaForApi, | ||
bookingCreateSchemaLegacyPropsForApi, | ||
} from "@calcom/prisma/zod-utils"; | ||
|
||
import type { getBookingFieldsWithSystemFields } from "./getBookingFields"; | ||
import getBookingResponsesSchema from "./getBookingResponsesSchema"; | ||
|
||
const getBookingDataSchemaForApi = ({ | ||
view = "booking", | ||
bookingFields, | ||
}: { | ||
view: "booking" | "reschedule"; | ||
bookingFields: Awaited<ReturnType<typeof getBookingFieldsWithSystemFields>>; | ||
}) => { | ||
const responsesSchema = getBookingResponsesSchema({ bookingFields, view }); | ||
return bookingCreateBodySchemaForApi | ||
.merge( | ||
z.object({ | ||
responses: responsesSchema.optional(), | ||
}) | ||
) | ||
.superRefine((val, ctx) => { | ||
if (val.responses && val.customInputs) { | ||
ctx.addIssue({ | ||
code: "custom", | ||
message: | ||
"Don't use both customInputs and responses. `customInputs` is only there for legacy support.", | ||
}); | ||
return; | ||
} | ||
const legacyProps = Object.keys(bookingCreateSchemaLegacyPropsForApi.shape); | ||
|
||
if (val.responses) { | ||
const unwantedProps: string[] = []; | ||
legacyProps.forEach((legacyProp) => { | ||
if (typeof val[legacyProp as keyof typeof val] !== "undefined") { | ||
console.error( | ||
`Deprecated: Unexpected falsy value for: ${unwantedProps.join( | ||
"," | ||
)}. They can't be used with \`responses\`. This will become a 400 error in the future.` | ||
); | ||
} | ||
if (val[legacyProp as keyof typeof val]) { | ||
unwantedProps.push(legacyProp); | ||
} | ||
}); | ||
if (unwantedProps.length) { | ||
ctx.addIssue({ | ||
code: "custom", | ||
message: `Legacy Props: ${unwantedProps.join(",")}. They can't be used with \`responses\``, | ||
}); | ||
return; | ||
} | ||
} else if (val.customInputs) { | ||
const { success } = bookingCreateSchemaLegacyPropsForApi.safeParse(val); | ||
if (!success) { | ||
ctx.addIssue({ | ||
code: "custom", | ||
message: `With \`customInputs\` you must specify legacy props ${legacyProps.join(",")}`, | ||
}); | ||
} | ||
} | ||
}); | ||
}; | ||
|
||
export default getBookingDataSchemaForApi; |
Oops, something went wrong.