refactor: v2 and booker atom booking fields (#17168)

* handle storing disableOnPrefill

* feat: allow overriding properties of default name and email fields

* feat: allow overriding properties of default name and email fields

* feat: output booking fields

* fix: event type test

* refactor: booker atom booking fields handling

* tests

* fix: boolean booking field

* update platform libraries

* fix: unit tests
This commit is contained in:
Lauris Skraucis authored and GitHub committed 2024-10-21 13:45:30 +03:00
1 parent b7792d0d0a
commit 4e90b65884
16 files changed
+1155 -1292

No files matched your search

@@ -21,7 +21,12 @@ import type {
SeatOptionsDisabledSchema,
} from "@calcom/platform-types";
import type { CustomField } from "../internal-to-api/booking-fields";
import {
systemBeforeFieldEmail,
systemBeforeFieldName,
type CustomField,
type SystemField,
} from "../internal-to-api/booking-fields";
import {
transformLocationsApiToInternal,
transformBookingFieldsApiToInternal,
@@ -105,6 +110,59 @@ describe("transformLocationsApiToInternal", () => {
});
describe("transformBookingFieldsApiToInternal", () => {
it("should transform name field", () => {
const bookingField: InputBookingField_2024_06_14 = {
type: "name",
label: "Your name number",
placeholder: "123456789",
disableOnPrefill: true,
};
const input: InputBookingField_2024_06_14[] = [bookingField];
const expectedField = {
...systemBeforeFieldName,
label: "Your name number",
placeholder: "123456789",
disableOnPrefill: true,
};
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expectedField.variantsConfig.variants.fullName.fields[0].label = "Your name number";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expectedField.variantsConfig.variants.fullName.fields[0].placeholder = "123456789";
const expectedOutput: SystemField[] = [expectedField];
const result = transformBookingFieldsApiToInternal(input);
expect(result).toEqual(expectedOutput);
});
it("should transform email field", () => {
const bookingField: InputBookingField_2024_06_14 = {
type: "email",
label: "Your email",
placeholder: "[email protected]",
disableOnPrefill: true,
};
const input: InputBookingField_2024_06_14[] = [bookingField];
const expectedOutput: SystemField[] = [
{
...systemBeforeFieldEmail,
label: "Your email",
placeholder: "[email protected]",
disableOnPrefill: true,
},
];
const result = transformBookingFieldsApiToInternal(input);
expect(result).toEqual(expectedOutput);
});
it("should transform phone field", () => {
const bookingField: InputBookingField_2024_06_14 = {
type: "phone",
@@ -470,6 +528,44 @@ describe("transformBookingFieldsApiToInternal", () => {
const input: InputBookingField_2024_06_14[] = [bookingField];
const expectedOutput: CustomField[] = [
{
name: bookingField.slug,
type: bookingField.type,
label: bookingField.label,
labelAsSafeHtml: `<p>${bookingField.label}</p>\n`,
sources: [
{
id: "user",
type: "user",
label: "User",
fieldRequired: true,
},
],
editable: "user",
required: bookingField.required,
},
];
const result = transformBookingFieldsApiToInternal(input);
expect(result).toEqual(expectedOutput);
});
it("should keep disableOnPrefill property", () => {
const disableOnPrefill = true;
const bookingField: InputBookingField_2024_06_14 = {
type: "radio",
slug: "radio",
label: "Your radio buttons",
required: true,
options: ["Radio 1", "Radio 2"],
disableOnPrefill,
};
const input: InputBookingField_2024_06_14[] = [bookingField];
const expectedOutput: CustomField[] = [
{
name: bookingField.slug,
@@ -486,6 +582,8 @@ describe("transformBookingFieldsApiToInternal", () => {
editable: "user",
required: bookingField.required,
placeholder: "",
options: transformSelectOptionsApiToInternal(bookingField.options),
disableOnPrefill,
},
];
@@ -1,19 +1,70 @@
import { type CreateEventTypeInput_2024_06_14 } from "@calcom/platform-types";
import type { InputBookingField_2024_06_14 } from "@calcom/platform-types";
import type { CustomField } from "../internal-to-api";
import {
systemBeforeFieldEmail,
systemBeforeFieldName,
type CustomField,
type SystemField,
} from "../internal-to-api";
export function transformBookingFieldsApiToInternal(
inputBookingFields: CreateEventTypeInput_2024_06_14["bookingFields"]
) {
if (!inputBookingFields) {
return [];
inputBookingFields: InputBookingField_2024_06_14[]
): (SystemField | CustomField)[] {
const customBookingFields = inputBookingFields.map((field) => {
const baseProperties = getBaseProperties(field);
const options =
"options" in field && field.options ? transformSelectOptionsApiToInternal(field.options) : undefined;
if (!options) {
return baseProperties;
}
return {
...baseProperties,
options,
};
});
return customBookingFields;
}
function getBaseProperties(field: InputBookingField_2024_06_14): SystemField | CustomField {
if (field.type === "name") {
const systemName = { ...systemBeforeFieldName };
if (systemName.variantsConfig?.variants?.fullName?.fields?.[0]) {
systemName.variantsConfig.variants.fullName.fields[0].label = field.label;
}
if (systemName.variantsConfig?.variants?.fullName?.fields?.[0]) {
systemName.variantsConfig.variants.fullName.fields[0].placeholder = field.placeholder;
}
// note(Lauris): we attach top level label and placeholder for easier access when converting database event type
// to v2 response event type even though form builder uses label and placeholder from variantsConfig.
systemName.label = field.label;
systemName.placeholder = field.placeholder;
return {
...systemName,
disableOnPrefill: field.disableOnPrefill,
};
}
const customBookingFields = inputBookingFields.map((field) => {
const commonFields: CustomField = {
if (field.type === "email") {
return {
...systemBeforeFieldEmail,
label: field.label,
placeholder: field.placeholder,
disableOnPrefill: field.disableOnPrefill,
};
}
if (field.type === "boolean") {
return {
name: field.slug,
type: field.type,
label: field.label,
labelAsSafeHtml: `<p>${field.label}</p>\n`,
sources: [
{
id: "user",
@@ -24,23 +75,27 @@ export function transformBookingFieldsApiToInternal(
],
editable: "user",
required: field.required,
placeholder: "placeholder" in field && field.placeholder ? field.placeholder : "",
disableOnPrefill: field.disableOnPrefill,
};
}
const options =
"options" in field && field.options ? transformSelectOptionsApiToInternal(field.options) : undefined;
if (!options) {
return commonFields;
}
return {
...commonFields,
options,
};
});
return customBookingFields;
return {
name: field.slug,
type: field.type,
label: field.label,
sources: [
{
id: "user",
type: "user",
label: "User",
fieldRequired: true,
},
],
editable: "user",
required: field.required,
placeholder: "placeholder" in field && field.placeholder ? field.placeholder : "",
disableOnPrefill: field.disableOnPrefill,
};
}
export function transformSelectOptionsApiToInternal(options: string[]) {
@@ -25,6 +25,9 @@ export function transformBookingFieldsInternalToApi(
type: field.type,
slug: field.name,
required: field.required,
label: field.label,
placeholder: field.placeholder,
disableOnPrefill: field.disableOnPrefill,
};
case "email":
return {
@@ -32,6 +35,9 @@ export function transformBookingFieldsInternalToApi(
type: field.type,
slug: field.name,
required: field.required,
label: field.label,
placeholder: field.placeholder,
disableOnPrefill: field.disableOnPrefill,
};
case "location":
return {
@@ -85,6 +91,7 @@ export function transformBookingFieldsInternalToApi(
label: field.label,
required: field.required,
placeholder: field.placeholder,
disableOnPrefill: field.disableOnPrefill,
};
case "address":
return {
@@ -94,6 +101,7 @@ export function transformBookingFieldsInternalToApi(
label: field.label,
required: field.required,
placeholder: field.placeholder,
disableOnPrefill: field.disableOnPrefill,
};
case "text":
return {
@@ -103,6 +111,7 @@ export function transformBookingFieldsInternalToApi(
label: field.label,
required: field.required,
placeholder: field.placeholder,
disableOnPrefill: field.disableOnPrefill,
};
case "number":
return {
@@ -112,6 +121,7 @@ export function transformBookingFieldsInternalToApi(
label: field.label,
required: field.required,
placeholder: field.placeholder,
disableOnPrefill: field.disableOnPrefill,
};
case "textarea":
return {
@@ -121,6 +131,7 @@ export function transformBookingFieldsInternalToApi(
label: field.label,
required: field.required,
placeholder: field.placeholder,
disableOnPrefill: field.disableOnPrefill,
};
case "multiemail":
return {
@@ -130,6 +141,7 @@ export function transformBookingFieldsInternalToApi(
label: field.label,
required: field.required,
placeholder: field.placeholder,
disableOnPrefill: field.disableOnPrefill,
};
case "boolean":
return {
@@ -138,6 +150,7 @@ export function transformBookingFieldsInternalToApi(
slug: field.name,
label: field.label,
required: field.required,
disableOnPrefill: field.disableOnPrefill,
};
case "select":
return {
@@ -148,6 +161,7 @@ export function transformBookingFieldsInternalToApi(
required: field.required,
placeholder: field.placeholder,
options: field.options ? field.options.map((option) => option.value) : [],
disableOnPrefill: field.disableOnPrefill,
};
case "multiselect":
return {
@@ -157,6 +171,7 @@ export function transformBookingFieldsInternalToApi(
label: field.label,
required: field.required,
options: field.options ? field.options?.map((option) => option.value) : [],
disableOnPrefill: field.disableOnPrefill,
};
case "checkbox":
return {
@@ -166,6 +181,7 @@ export function transformBookingFieldsInternalToApi(
label: field.label,
required: field.required,
options: field.options ? field.options?.map((option) => option.value) : [],
disableOnPrefill: field.disableOnPrefill,
};
case "radio":
return {
@@ -175,6 +191,7 @@ export function transformBookingFieldsInternalToApi(
label: field.label,
required: field.required,
options: field.options ? field.options?.map((option) => option.value) : [],
disableOnPrefill: field.disableOnPrefill,
};
default:
throw new Error(`Unsupported booking field type '${field.type}'.`);
@@ -203,6 +220,7 @@ const CustomFieldsSchema = z.object({
name: z.string(),
type: CustomFieldTypeEnum,
label: z.string(),
labelAsSafeHtml: z.string().optional(),
sources: z.array(
z.object({
id: z.literal("user"),
@@ -211,7 +229,7 @@ const CustomFieldsSchema = z.object({
fieldRequired: z.literal(true),
})
),
editable: z.literal("user"),
editable: z.enum(["user", "user-readonly"]),
required: z.boolean(),
placeholder: z.string().optional(),
options: z
@@ -222,6 +240,7 @@ const CustomFieldsSchema = z.object({
})
)
.optional(),
disableOnPrefill: z.boolean().optional(),
});
const SystemFieldSchema = z.object({
@@ -244,6 +263,7 @@ const SystemFieldSchema = z.object({
)
.optional(),
defaultPlaceholder: z.enum(["", "share_additional_notes", "email", "reschedule_placeholder"]).optional(),
placeholder: z.string().optional(),
hidden: z.boolean().optional(),
required: z.boolean(),
hideWhenJustOneOption: z.boolean().optional(),
@@ -262,12 +282,31 @@ const SystemFieldSchema = z.object({
}),
})
.optional(),
disableOnPrefill: z.boolean().optional(),
});
const NameSystemFieldSchema = SystemFieldSchema.extend({
name: z.literal("name"),
type: z.literal("name"),
required: z.literal(true),
variant: z.literal("fullName").optional(),
variantsConfig: z
.object({
variants: z.object({
fullName: z.object({
fields: z.array(
z.object({
name: z.literal("fullName"),
type: z.literal("text"),
label: z.string().optional(),
required: z.literal(true),
placeholder: z.string().optional(),
})
),
}),
}),
})
.optional(),
});
const EmailSystemFieldSchema = SystemFieldSchema.extend({
@@ -306,8 +345,8 @@ const GuestsSystemFieldSchema = SystemFieldSchema.extend({
required: z.literal(false),
});
type NameSystemField = z.infer<typeof NameSystemFieldSchema>;
type EmailSystemField = z.infer<typeof EmailSystemFieldSchema>;
export type NameSystemField = z.infer<typeof NameSystemFieldSchema>;
export type EmailSystemField = z.infer<typeof EmailSystemFieldSchema>;
type RescheduleReasonSystemField = z.infer<typeof RescheduleReasonSystemFieldSchema>;
type LocationReasonSystemField = z.infer<typeof LocationReasonSystemFieldSchema>;
type TitleSystemField = z.infer<typeof TitleSystemFieldSchema>;
@@ -343,6 +382,7 @@ export const systemBeforeFieldName: NameSystemField = {
editable: "system",
defaultLabel: "your_name",
required: true,
variant: "fullName",
sources: [
{
label: "Default",
@@ -350,21 +390,19 @@ export const systemBeforeFieldName: NameSystemField = {
type: "default",
},
],
};
export const systemBeforeFieldNameReadOnly: NameSystemField = {
type: "name",
name: "name",
editable: "user-readonly",
defaultLabel: "your_name",
required: true,
sources: [
{
label: "Default",
id: "default",
type: "default",
variantsConfig: {
variants: {
fullName: {
fields: [
{
name: "fullName",
type: "text",
required: true,
},
],
},
},
],
},
};
export const systemBeforeFieldEmail: EmailSystemField = {
@@ -382,21 +420,6 @@ export const systemBeforeFieldEmail: EmailSystemField = {
],
};
export const systemBeforeFieldEmailReadOnly: EmailSystemField = {
defaultLabel: "email_address",
type: "email",
name: "email",
required: true,
editable: "user-readonly",
sources: [
{
label: "Default",
id: "default",
type: "default",
},
],
};
export const systemBeforeFieldLocation: LocationReasonSystemField = {
defaultLabel: "location",
type: "radioInput",
@@ -31,13 +31,18 @@ import {
transformEventTypeColorsInternalToApi,
transformSeatsInternalToApi,
} from ".";
import type { CustomField } from "./booking-fields";
import {
systemBeforeFieldEmail,
systemBeforeFieldName,
type CustomField,
type SystemField,
} from "./booking-fields";
describe("transformLocationsInternalToApi", () => {
it("should reverse transform address location", () => {
const transformedLocation = [
{
type: "inPerson",
type: "inPerson" as const,
address: "1234 Main St",
displayLocationPublicly: true,
},
@@ -59,7 +64,7 @@ describe("transformLocationsInternalToApi", () => {
it("should reverse transform link location", () => {
const transformedLocation = [
{
type: "link",
type: "link" as const,
link: "https://example.com",
displayLocationPublicly: true,
},
@@ -81,7 +86,7 @@ describe("transformLocationsInternalToApi", () => {
it("should reverse transform phone location", () => {
const transformedLocation = [
{
type: "userPhone",
type: "userPhone" as const,
hostPhoneNumber: "123456789",
displayLocationPublicly: true,
},
@@ -103,7 +108,7 @@ describe("transformLocationsInternalToApi", () => {
it("should reverse transform integration location", () => {
const transformedLocation = [
{
type: "integrations:daily",
type: "integrations:daily" as const,
},
];
@@ -121,6 +126,109 @@ describe("transformLocationsInternalToApi", () => {
});
describe("transformBookingFieldsInternalToApi", () => {
it("should reverse transform not modified name default field", () => {
const transformedField: SystemField[] = [systemBeforeFieldName];
const expectedOutput = [
{
type: "name",
slug: "name",
isDefault: true,
required: true,
},
];
const result = transformBookingFieldsInternalToApi(transformedField);
expect(result).toEqual(expectedOutput);
});
it("should reverse transform modified name default field", () => {
const nameField = {
...systemBeforeFieldName,
placeholder: "custom placeholder",
disableOnPrefill: true,
label: "custom label",
variantsConfig: {
variants: {
fullName: {
fields: [
{
name: "fullName",
label: "custom label",
placeholder: "custom placeholder",
type: "text",
required: true,
},
],
},
},
},
};
const transformedField: SystemField[] = [nameField];
const expectedOutput = [
{
type: "name",
slug: "name",
isDefault: true,
required: true,
placeholder: "custom placeholder",
disableOnPrefill: true,
label: "custom label",
},
];
const result = transformBookingFieldsInternalToApi(transformedField);
expect(result).toEqual(expectedOutput);
});
it("should reverse transform not modified email default field", () => {
const transformedField: SystemField[] = [systemBeforeFieldEmail];
const expectedOutput = [
{
type: "email",
slug: "email",
isDefault: true,
required: true,
},
];
const result = transformBookingFieldsInternalToApi(transformedField);
expect(result).toEqual(expectedOutput);
});
it("should reverse transform modified email default field", () => {
const transformedField: SystemField[] = [
{
...systemBeforeFieldEmail,
placeholder: "custom placeholder",
disableOnPrefill: true,
label: "custom label",
},
];
const expectedOutput = [
{
type: "email",
slug: "email",
isDefault: true,
required: true,
placeholder: "custom placeholder",
disableOnPrefill: true,
label: "custom label",
},
];
const result = transformBookingFieldsInternalToApi(transformedField);
expect(result).toEqual(expectedOutput);
});
it("should reverse transform phone field", () => {
const transformedField: CustomField[] = [
{
@@ -511,7 +619,6 @@ describe("transformBookingFieldsInternalToApi", () => {
],
editable: "user",
required: true,
placeholder: "",
},
];
@@ -733,7 +840,7 @@ describe("transformRequiresConfirmationInternalToApi", () => {
};
const result = transformRequiresConfirmationInternalToApi(
transformedField.requiresConfirmation,
!!undefined,
false,
undefined
);