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

@@ -62,10 +62,6 @@ export type BookerPlatformWrapperAtomProps = Omit<
notes?: string;
rescheduleReason?: string;
} & Record<string, string | string[]>;
readOnlyFormValues?: {
name?: boolean;
email?: boolean;
};
handleCreateBooking?: (input: UseCreateBookingInput) => void;
onCreateBookingSuccess?: (data: ApiSuccessResponse<BookingResponse>) => void;
onCreateBookingError?: (data: ApiErrorResponse | Error) => void;
@@ -153,7 +149,7 @@ export const BookerPlatformWrapper = (
isPending: isTeamPending,
data:
teamEventTypeData && teamEventTypeData.length > 0
? transformApiTeamEventTypeForAtom(teamEventTypeData[0], props.entity, props.readOnlyFormValues)
? transformApiTeamEventTypeForAtom(teamEventTypeData[0], props.entity, props.defaultFormValues)
: undefined,
};
}
@@ -164,7 +160,7 @@ export const BookerPlatformWrapper = (
isPending,
data:
data && data.length > 0
? transformApiEventTypeForAtom(data[0], props.entity, props.readOnlyFormValues)
? transformApiEventTypeForAtom(data[0], props.entity, props.defaultFormValues)
: undefined,
};
}, [
@@ -113,7 +113,7 @@ export function BaseCalProvider({
let translation =
labels?.[resolvedKey as keyof typeof labels] ?? String(getTranslation(resolvedKey, language) ?? "");
if (!translation) {
return "";
return key;
}
if (values) {
const valueKeys = Object.keys(values) as (keyof typeof values)[];
@@ -8,13 +8,13 @@ import {
systemBeforeFieldLocation,
systemAfterFieldRescheduleReason,
transformRecurrenceApiToInternal,
systemBeforeFieldNameReadOnly,
systemBeforeFieldEmailReadOnly,
} from "@calcom/lib/event-types/transformers";
import { getBookerBaseUrlSync } from "@calcom/lib/getBookerUrl/client";
import type {
CustomFieldOutput_2024_06_14,
EmailDefaultFieldOutput_2024_06_14,
EventTypeOutput_2024_06_14,
NameDefaultFieldOutput_2024_06_14,
TeamEventTypeOutput_2024_06_14,
} from "@calcom/platform-types";
import {
@@ -30,7 +30,7 @@ import type { BookerPlatformWrapperAtomProps } from "../../booker/BookerPlatform
export function transformApiEventTypeForAtom(
eventType: Omit<EventTypeOutput_2024_06_14, "ownerId">,
entity: BookerPlatformWrapperAtomProps["entity"] | undefined,
readOnlyFormValues: BookerPlatformWrapperAtomProps["readOnlyFormValues"] | undefined
defaultFormValues: BookerPlatformWrapperAtomProps["defaultFormValues"] | undefined
) {
const { lengthInMinutes, locations, bookingFields, users, recurrence, ...rest } = eventType;
@@ -50,7 +50,7 @@ export function transformApiEventTypeForAtom(
...rest,
length: lengthInMinutes,
locations: getLocations(locations),
bookingFields: getBookingFields(bookingFields, readOnlyFormValues),
bookingFields: getBookingFields(bookingFields, defaultFormValues),
isDefault,
isDynamic: false,
profile: {
@@ -107,7 +107,7 @@ export function transformApiEventTypeForAtom(
export function transformApiTeamEventTypeForAtom(
eventType: TeamEventTypeOutput_2024_06_14,
entity: BookerPlatformWrapperAtomProps["entity"] | undefined,
readOnlyFormValues: BookerPlatformWrapperAtomProps["readOnlyFormValues"] | undefined
defaultFormValues: BookerPlatformWrapperAtomProps["defaultFormValues"] | undefined
) {
const { lengthInMinutes, locations, hosts, bookingFields, recurrence, ...rest } = eventType;
@@ -126,7 +126,7 @@ export function transformApiTeamEventTypeForAtom(
...rest,
length: lengthInMinutes,
locations: getLocations(locations),
bookingFields: getBookingFields(bookingFields, readOnlyFormValues),
bookingFields: getBookingFields(bookingFields, defaultFormValues),
isDefault,
isDynamic: false,
profile: {
@@ -226,26 +226,48 @@ function getLocations(locations: EventTypeOutput_2024_06_14["locations"]) {
function getBookingFields(
bookingFields: EventTypeOutput_2024_06_14["bookingFields"],
readOnlyFormValues: BookerPlatformWrapperAtomProps["readOnlyFormValues"] | undefined
defaultFormValues: BookerPlatformWrapperAtomProps["defaultFormValues"] | undefined
) {
// note(Lauris): the peculiar thing about returning atom booking fields using v2 event type is that v2 event type has more possible
// booking field outputs than inputs due to default system fields that cant be passed as inputs, which is why we take v2 from response
// only the custom fields and default editable fields aka fields that can be passed as inputs for event type booking fields.
const customFields: (SystemField | CustomField)[] = bookingFields
? transformBookingFieldsApiToInternal(
bookingFields.filter((field) => isCustomField(field) || isDefaultEditableField(field))
)
: [];
const customFieldsWithoutNameEmail = customFields.filter(
(field) => field.type !== "name" && field.type !== "email"
);
const customNameField = customFields?.find((field) => field.type === "name");
const customEmailField = customFields?.find((field) => field.type === "email");
const systemBeforeFields: SystemField[] = [
readOnlyFormValues?.name ? systemBeforeFieldNameReadOnly : systemBeforeFieldName,
readOnlyFormValues?.email ? systemBeforeFieldEmailReadOnly : systemBeforeFieldEmail,
customNameField || systemBeforeFieldName,
customEmailField || systemBeforeFieldEmail,
systemBeforeFieldLocation,
];
const transformedCustomFields: CustomField[] = transformBookingFieldsApiToInternal(
bookingFields.filter((field) => isCustomField(field))
);
const systemAfterFields: SystemField[] = [systemAfterFieldRescheduleReason];
const transformedBookingFields: (SystemField | CustomField)[] = [
...systemBeforeFields,
...transformedCustomFields,
...customFieldsWithoutNameEmail,
...systemAfterFields,
];
// note(Lauris): in web app booking form values can be passed as url query params, but booker atom does not accept booking field values via url,
// so defaultFormValues act as a way to prefill booking form fields, and if the field in database has disableOnPrefill=true and value passed then its read only.
const defaultFormValuesKeys = defaultFormValues ? Object.keys(defaultFormValues) : [];
if (defaultFormValuesKeys.length) {
for (const field of transformedBookingFields) {
if (defaultFormValuesKeys.includes(field.name) && field.disableOnPrefill) {
field.editable = "user-readonly";
}
}
}
return eventTypeBookingFields.brand<"HAS_SYSTEM_FIELDS">().parse(transformedBookingFields);
}
@@ -254,3 +276,9 @@ function isCustomField(
): field is CustomFieldOutput_2024_06_14 {
return !field.isDefault;
}
function isDefaultEditableField(
field: EventTypeOutput_2024_06_14["bookingFields"][number]
): field is NameDefaultFieldOutput_2024_06_14 | EmailDefaultFieldOutput_2024_06_14 {
return field.type === "name" || field.type === "email";
}
+6 -1
View File
@@ -125,7 +125,12 @@ export {
systemAfterFieldRescheduleReason,
} from "@calcom/lib/event-types/transformers";
export type { SystemField, CustomField } from "@calcom/lib/event-types/transformers";
export type {
SystemField,
CustomField,
NameSystemField,
EmailSystemField,
} from "@calcom/lib/event-types/transformers";
export { parseBookingLimit, parseEventTypeColor } from "@calcom/lib";
@@ -1,11 +1,13 @@
import { BadRequestException } from "@nestjs/common";
import { ApiProperty as DocsProperty } from "@nestjs/swagger";
import { ApiProperty as DocsProperty, ApiPropertyOptional as DocsPropertyOptional } from "@nestjs/swagger";
import { plainToInstance } from "class-transformer";
import { IsString, IsBoolean, IsArray, IsIn, IsOptional } from "class-validator";
import type { ValidationOptions, ValidatorConstraintInterface } from "class-validator";
import { registerDecorator, validate, ValidatorConstraint } from "class-validator";
const inputBookingFieldTypes = [
"name",
"email",
"phone",
"address",
"text",
@@ -19,6 +21,59 @@ const inputBookingFieldTypes = [
"boolean",
] as const;
export class NameFieldInput_2024_06_14 {
@IsIn(inputBookingFieldTypes)
@DocsProperty({ example: "name", description: "only allowed value for type is `name`" })
type!: "name";
@IsString()
@IsOptional()
@DocsProperty()
label?: string;
@IsString()
@IsOptional()
@DocsProperty()
placeholder?: string;
@IsBoolean()
@IsOptional()
@DocsPropertyOptional({
type: Boolean,
description:
"Disable this booking field if the URL contains query parameter with key equal to the slug and prefill it with the provided value.\
For example, if URL contains query parameter `&name=bob`,\
the name field will be prefilled with this value and disabled.",
})
disableOnPrefill?: boolean;
}
export class EmailFieldInput_2024_06_14 {
@IsIn(inputBookingFieldTypes)
@DocsProperty({ example: "email", description: "only allowed value for type is `email`" })
type!: "email";
@IsString()
@IsOptional()
@DocsProperty()
label?: string;
@IsString()
@IsOptional()
@DocsProperty()
placeholder?: string;
@IsBoolean()
@IsOptional()
@DocsPropertyOptional({
type: Boolean,
description:
"Disable this booking field if the URL contains query parameter with key equal to the slug and prefill it with the provided value.\
For example, if URL contains query parameter `&[email protected]`,\
the email field will be prefilled with this value and disabled.",
})
disableOnPrefill?: boolean;
}
export class PhoneFieldInput_2024_06_14 {
@IsIn(inputBookingFieldTypes)
@DocsProperty({ example: "phone", description: "only allowed value for type is `phone`" })
@@ -44,6 +99,17 @@ export class PhoneFieldInput_2024_06_14 {
@IsOptional()
@DocsProperty()
placeholder?: string;
@IsBoolean()
@IsOptional()
@DocsPropertyOptional({
type: Boolean,
description:
"Disable this booking field if the URL contains query parameter with key equal to the slug and prefill it with the provided value.\
For example, if the slug is `phone` and the URL contains query parameter `&phone=1234567890`,\
the phone field will be prefilled with this value and disabled.",
})
disableOnPrefill?: boolean;
}
export class AddressFieldInput_2024_06_14 {
@@ -72,6 +138,17 @@ export class AddressFieldInput_2024_06_14 {
@DocsProperty()
@DocsProperty({ example: "e.g., 1234 Main St" })
placeholder?: string;
@IsBoolean()
@IsOptional()
@DocsPropertyOptional({
type: Boolean,
description:
"Disable this booking field if the URL contains query parameter with key equal to the slug and prefill it with the provided value.\
For example, if the slug is `address` and the URL contains query parameter `&address=1234 Main St, London`,\
the address field will be prefilled with this value and disabled.",
})
disableOnPrefill?: boolean;
}
export class TextFieldInput_2024_06_14 {
@@ -100,6 +177,17 @@ export class TextFieldInput_2024_06_14 {
@IsOptional()
@DocsProperty()
placeholder?: string;
@IsBoolean()
@IsOptional()
@DocsPropertyOptional({
type: Boolean,
description:
"Disable this booking field if the URL contains query parameter with key equal to the slug and prefill it with the provided value.\
For example, if the slug is `who-referred-you` and the URL contains query parameter `&who-referred-you=bob`,\
the text field will be prefilled with this value and disabled.",
})
disableOnPrefill?: boolean;
}
export class NumberFieldInput_2024_06_14 {
@@ -128,6 +216,17 @@ export class NumberFieldInput_2024_06_14 {
@IsOptional()
@DocsProperty()
placeholder?: string;
@IsBoolean()
@IsOptional()
@DocsPropertyOptional({
type: Boolean,
description:
"Disable this booking field if the URL contains query parameter with key equal to the slug and prefill it with the provided value.\
For example, if the slug is `calories-per-day` and the URL contains query parameter `&calories-per-day=3000`,\
the number field will be prefilled with this value and disabled.",
})
disableOnPrefill?: boolean;
}
export class TextAreaFieldInput_2024_06_14 {
@@ -156,6 +255,17 @@ export class TextAreaFieldInput_2024_06_14 {
@IsOptional()
@DocsProperty()
placeholder?: string;
@IsBoolean()
@IsOptional()
@DocsPropertyOptional({
type: Boolean,
description:
"Disable this booking field if the URL contains query parameter with key equal to the slug and prefill it with the provided value.\
For example, if the slug is `dear-diary` and the URL contains query parameter `&dear-diary=Today I shipped a feature`,\
the text area will be prefilled with this value and disabled.",
})
disableOnPrefill?: boolean;
}
export class SelectFieldInput_2024_06_14 {
@@ -188,6 +298,17 @@ export class SelectFieldInput_2024_06_14 {
@IsArray()
@DocsProperty({ type: [String], example: ["Option 1", "Option 2"] })
options!: string[];
@IsBoolean()
@IsOptional()
@DocsPropertyOptional({
type: Boolean,
description:
"Disable this booking field if the URL contains query parameter with key equal to the slug and prefill it with the provided value.\
For example, if the slug is `language` and options of this select field are ['english', 'italian'] and the URL contains query parameter `&language=italian`,\
the 'italian' will be selected and the select field will be disabled.",
})
disableOnPrefill?: boolean;
}
export class MultiSelectFieldInput_2024_06_14 {
@@ -214,6 +335,17 @@ export class MultiSelectFieldInput_2024_06_14 {
@IsArray()
@DocsProperty({ type: [String], example: ["Option 1", "Option 2"] })
options!: string[];
@IsBoolean()
@IsOptional()
@DocsPropertyOptional({
type: Boolean,
description:
"Disable this booking field if the URL contains query parameter with key equal to the slug and prefill it with the provided value.\
For example, if the slug is `consultants` and the URL contains query parameter `&consultants=en&language=it`,\
the 'en' and 'it' will be selected and the select field will be disabled.",
})
disableOnPrefill?: boolean;
}
export class MultiEmailFieldInput_2024_06_14 {
@@ -242,6 +374,17 @@ export class MultiEmailFieldInput_2024_06_14 {
@IsOptional()
@DocsProperty()
placeholder?: string;
@IsBoolean()
@IsOptional()
@DocsPropertyOptional({
type: Boolean,
description:
"Disable this booking field if the URL contains query parameter with key equal to the slug and prefill it with the provided value.\
For example, if the slug is `consultants` and the URL contains query parameter `&[email protected]&[email protected]`,\
the these emails will be added and none more can be added.",
})
disableOnPrefill?: boolean;
}
export class CheckboxGroupFieldInput_2024_06_14 {
@@ -268,6 +411,17 @@ export class CheckboxGroupFieldInput_2024_06_14 {
@IsArray()
@DocsProperty({ type: [String], example: ["Checkbox 1", "Checkbox 2"] })
options!: string[];
@IsBoolean()
@IsOptional()
@DocsPropertyOptional({
type: Boolean,
description:
"Disable this booking field if the URL contains query parameter with key equal to the slug and prefill it with the provided value.\
For example, if the slug is `notify-me` and the URL contains query parameter `&notify-me=true`,\
the checkbox will be selected and the checkbox field will be disabled.",
})
disableOnPrefill?: boolean;
}
export class RadioGroupFieldInput_2024_06_14 {
@@ -294,6 +448,17 @@ export class RadioGroupFieldInput_2024_06_14 {
@IsArray()
@DocsProperty({ type: [String], example: ["Radio 1", "Radio 2"] })
options!: string[];
@IsBoolean()
@IsOptional()
@DocsPropertyOptional({
type: Boolean,
description:
"Disable this booking field if the URL contains query parameter with key equal to the slug and prefill it with the provided value.\
For example, if the slug is `language` and options of this select field are ['english', 'italian'] and the URL contains query parameter `&language=italian`,\
the 'italian' radio buttom will be selected and the select field will be disabled.",
})
disableOnPrefill?: boolean;
}
export class BooleanFieldInput_2024_06_14 {
@@ -316,9 +481,16 @@ export class BooleanFieldInput_2024_06_14 {
@IsBoolean()
@DocsProperty()
required!: boolean;
@IsBoolean()
@IsOptional()
@DocsPropertyOptional({ type: Boolean })
disableOnPrefill?: boolean;
}
export type InputBookingField_2024_06_14 =
| NameFieldInput_2024_06_14
| EmailFieldInput_2024_06_14
| PhoneFieldInput_2024_06_14
| AddressFieldInput_2024_06_14
| TextFieldInput_2024_06_14
@@ -334,6 +506,8 @@ export type InputBookingField_2024_06_14 =
@ValidatorConstraint({ async: true })
class InputBookingFieldValidator_2024_06_14 implements ValidatorConstraintInterface {
private classTypeMap: { [key: string]: new () => InputBookingField_2024_06_14 } = {
name: NameFieldInput_2024_06_14,
email: EmailFieldInput_2024_06_14,
phone: PhoneFieldInput_2024_06_14,
address: AddressFieldInput_2024_06_14,
text: TextFieldInput_2024_06_14,
@@ -347,7 +521,7 @@ class InputBookingFieldValidator_2024_06_14 implements ValidatorConstraintInterf
boolean: BooleanFieldInput_2024_06_14,
};
private reservedSystemSlugs = ["name", "email", "location", "rescheduleReason"];
private reservedSystemSlugs = ["location", "rescheduleReason"];
async validate(bookingFields: { type: string; slug: string }[]) {
if (!Array.isArray(bookingFields)) {
@@ -365,7 +539,8 @@ class InputBookingFieldValidator_2024_06_14 implements ValidatorConstraintInterf
throw new BadRequestException(`Each booking field must have a 'type' property.`);
}
if (!slug) {
const notSystemEditableField = type !== "name" && type !== "email";
if (notSystemEditableField && !slug) {
throw new BadRequestException(`Each booking field must have a 'slug' property.`);
}
@@ -382,7 +557,9 @@ class InputBookingFieldValidator_2024_06_14 implements ValidatorConstraintInterf
`Duplicate bookingFields slug '${slug}' found. All bookingFields slugs must be unique.`
);
}
slugs.push(slug);
if (notSystemEditableField) {
slugs.push(slug);
}
const ClassType = this.classTypeMap[type];
if (!ClassType) {
@@ -17,9 +17,11 @@ import {
CheckboxGroupFieldInput_2024_06_14,
RadioGroupFieldInput_2024_06_14,
BooleanFieldInput_2024_06_14,
NameFieldInput_2024_06_14,
EmailFieldInput_2024_06_14,
} from "../inputs";
export class NameDefaultFieldOutput_2024_06_14 {
export class NameDefaultFieldOutput_2024_06_14 extends NameFieldInput_2024_06_14 {
@IsBoolean()
@DocsProperty({
description: "This property is always true because it's a default field",
@@ -45,7 +47,7 @@ export class NameDefaultFieldOutput_2024_06_14 {
required!: true;
}
export class EmailDefaultFieldOutput_2024_06_14 {
export class EmailDefaultFieldOutput_2024_06_14 extends EmailFieldInput_2024_06_14 {
@IsBoolean()
@DocsProperty({
description: "This property is always true because it's a default field",