Allow rescheduleReason to be marked required (#7729)
This commit is contained in:
1 parent
325dc9805f
commit
7179b09d7b
10 files changed
+233
-205
No files matched your search
@@ -61,7 +61,7 @@ export const SystemFieldsEditability: Record<z.infer<typeof SystemField>, Fields
|
||||
location: "system",
|
||||
notes: "system-but-optional",
|
||||
guests: "system-but-optional",
|
||||
rescheduleReason: "system",
|
||||
rescheduleReason: "system-but-optional",
|
||||
smsReminderNumber: "system",
|
||||
};
|
||||
|
||||
@@ -252,6 +252,12 @@ export const ensureBookingInputsHaveSystemFields = ({
|
||||
name: "rescheduleReason",
|
||||
defaultPlaceholder: "reschedule_placeholder",
|
||||
required: false,
|
||||
views: [
|
||||
{
|
||||
id: "reschedule",
|
||||
label: "Reschedule View",
|
||||
},
|
||||
],
|
||||
sources: [
|
||||
{
|
||||
label: "Default",
|
||||
@@ -264,9 +270,16 @@ export const ensureBookingInputsHaveSystemFields = ({
|
||||
|
||||
const missingSystemBeforeFields = [];
|
||||
for (const field of systemBeforeFields) {
|
||||
const existingBookingFieldIndex = bookingFields.findIndex((f) => f.name === field.name);
|
||||
// Only do a push, we must not update existing system fields as user could have modified any property in it,
|
||||
if (!bookingFields.find((f) => f.name === field.name)) {
|
||||
if (existingBookingFieldIndex === -1) {
|
||||
missingSystemBeforeFields.push(field);
|
||||
} else {
|
||||
// Adding the fields from Code first and then fields from DB. Allows, the code to push new properties to the field
|
||||
bookingFields[existingBookingFieldIndex] = {
|
||||
...field,
|
||||
...bookingFields[existingBookingFieldIndex],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -310,9 +323,16 @@ export const ensureBookingInputsHaveSystemFields = ({
|
||||
|
||||
const missingSystemAfterFields = [];
|
||||
for (const field of systemAfterFields) {
|
||||
const existingBookingFieldIndex = bookingFields.findIndex((f) => f.name === field.name);
|
||||
// Only do a push, we must not update existing system fields as user could have modified any property in it,
|
||||
if (!bookingFields.find((f) => f.name === field.name)) {
|
||||
if (existingBookingFieldIndex === -1) {
|
||||
missingSystemAfterFields.push(field);
|
||||
} else {
|
||||
bookingFields[existingBookingFieldIndex] = {
|
||||
// Adding the fields from Code first and then fields from DB. Allows, the code to push new properties to the field
|
||||
...field,
|
||||
...bookingFields[existingBookingFieldIndex],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,22 +1,32 @@
|
||||
import { isValidPhoneNumber } from "libphonenumber-js";
|
||||
import z from "zod";
|
||||
|
||||
import type { ALL_VIEWS } from "@calcom/features/form-builder/FormBuilderFieldsSchema";
|
||||
import type { eventTypeBookingFields } from "@calcom/prisma/zod-utils";
|
||||
import { bookingResponses } from "@calcom/prisma/zod-utils";
|
||||
|
||||
type EventType = Parameters<typeof preprocess>[0]["eventType"];
|
||||
export const getBookingResponsesPartialSchema = (eventType: EventType) => {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
type View = ALL_VIEWS | (string & {});
|
||||
|
||||
export const getBookingResponsesPartialSchema = ({
|
||||
eventType,
|
||||
view,
|
||||
}: {
|
||||
eventType: EventType;
|
||||
view: View;
|
||||
}) => {
|
||||
const schema = bookingResponses.unwrap().partial().and(z.record(z.any()));
|
||||
|
||||
return preprocess({ schema, eventType, isPartialSchema: true });
|
||||
return preprocess({ schema, eventType, isPartialSchema: true, view });
|
||||
};
|
||||
|
||||
// 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, view }: { eventType: EventType; view: View }) {
|
||||
const schema = bookingResponses.and(z.record(z.any()));
|
||||
return preprocess({ schema, eventType, isPartialSchema: false });
|
||||
return preprocess({ schema, eventType, isPartialSchema: false, view });
|
||||
}
|
||||
|
||||
// TODO: Move preprocess of `booking.responses` to FormBuilder schema as that is going to parse the fields supported by FormBuilder
|
||||
@@ -25,12 +35,14 @@ function preprocess<T extends z.ZodType>({
|
||||
schema,
|
||||
eventType,
|
||||
isPartialSchema,
|
||||
view: currentView,
|
||||
}: {
|
||||
schema: T;
|
||||
isPartialSchema: boolean;
|
||||
eventType: {
|
||||
bookingFields: z.infer<typeof eventTypeBookingFields> & z.BRAND<"HAS_SYSTEM_FIELDS">;
|
||||
};
|
||||
view: View;
|
||||
}): z.ZodType<z.infer<T>, z.infer<T>, z.infer<T>> {
|
||||
const preprocessed = z.preprocess(
|
||||
(responses) => {
|
||||
@@ -42,8 +54,15 @@ function preprocess<T extends z.ZodType>({
|
||||
// If there is no response for the field, then we don't need to do any processing
|
||||
return;
|
||||
}
|
||||
// Turn a boolean in string to a real boolean
|
||||
const views = field.views;
|
||||
const isFieldApplicableToCurrentView =
|
||||
currentView === "ALL_VIEWS" ? true : views ? views.find((view) => view.id === currentView) : true;
|
||||
if (!isFieldApplicableToCurrentView) {
|
||||
// If the field is not applicable in the current view, then we don't need to do any processing
|
||||
return;
|
||||
}
|
||||
if (field.type === "boolean") {
|
||||
// Turn a boolean in string to a real boolean
|
||||
newResponses[field.name] = value === "true" || value === true;
|
||||
}
|
||||
// Make sure that the value is an array
|
||||
@@ -76,8 +95,16 @@ function preprocess<T extends z.ZodType>({
|
||||
: z.string().refine((val) => isValidPhoneNumber(val));
|
||||
// Tag the message with the input name so that the message can be shown at appropriate place
|
||||
const m = (message: string) => `{${bookingField.name}}${message}`;
|
||||
const views = bookingField.views;
|
||||
const isFieldApplicableToCurrentView =
|
||||
currentView === "ALL_VIEWS" ? true : views ? views.find((view) => view.id === currentView) : true;
|
||||
// If the field is hidden, then it can never be required
|
||||
const isRequired = bookingField.hidden ? false : bookingField.required;
|
||||
const isRequired = bookingField.hidden
|
||||
? false
|
||||
: isFieldApplicableToCurrentView
|
||||
? bookingField.required
|
||||
: false;
|
||||
|
||||
if ((isPartialSchema || !isRequired) && value === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -377,7 +377,10 @@ function getBookingData({
|
||||
? extendedBookingCreateBody.merge(
|
||||
z.object({
|
||||
responses: getBookingResponsesSchema({
|
||||
bookingFields: eventType.bookingFields,
|
||||
eventType: {
|
||||
bookingFields: eventType.bookingFields,
|
||||
},
|
||||
view: req.body.rescheduleUid ? "reschedule" : "booking",
|
||||
}),
|
||||
})
|
||||
)
|
||||
|
||||
@@ -335,6 +335,7 @@ export const FormBuilder = function FormBuilder({
|
||||
}}
|
||||
/>
|
||||
<Button
|
||||
data-testid="edit-field-action"
|
||||
color="secondary"
|
||||
onClick={() => {
|
||||
editField(index, field);
|
||||
@@ -378,7 +379,7 @@ export const FormBuilder = function FormBuilder({
|
||||
fieldIndex: -1,
|
||||
})
|
||||
}>
|
||||
<DialogContent enableOverflow>
|
||||
<DialogContent enableOverflow data-testid="edit-field-dialog">
|
||||
<DialogHeader title={t("add_a_booking_question")} subtitle={t("form_builder_field_add_subtitle")} />
|
||||
<div>
|
||||
<Form
|
||||
@@ -472,6 +473,7 @@ export const FormBuilder = function FormBuilder({
|
||||
render={({ field: { value, onChange } }) => {
|
||||
return (
|
||||
<BooleanToggleGroupField
|
||||
data-testid="field-required"
|
||||
disabled={fieldForm.getValues("editable") === "system"}
|
||||
value={value}
|
||||
onValueChange={(val) => {
|
||||
|
||||
@@ -23,7 +23,7 @@ export const EditableSchema = z.enum([
|
||||
"user", // Fully editable
|
||||
"user-readonly", // All fields are readOnly.
|
||||
]);
|
||||
|
||||
export type ALL_VIEWS = "ALL_VIEWS";
|
||||
const fieldSchema = z.object({
|
||||
name: z.string(),
|
||||
// TODO: We should make at least one of `defaultPlaceholder` and `placeholder` required. Do the same for label.
|
||||
@@ -35,7 +35,14 @@ const fieldSchema = z.object({
|
||||
*/
|
||||
defaultLabel: z.string().optional(),
|
||||
defaultPlaceholder: z.string().optional(),
|
||||
|
||||
views: z
|
||||
.object({
|
||||
label: z.string(),
|
||||
id: z.string(),
|
||||
description: z.string().optional(),
|
||||
})
|
||||
.array()
|
||||
.optional(),
|
||||
type: fieldTypeEnum,
|
||||
options: z.array(z.object({ label: z.string(), value: z.string() })).optional(),
|
||||
optionsInputs: z
|
||||
|
||||
Reference in new issue
Block a user