refactor: v2 and atoms booking fields (#16685)
* refactor: define inputs and outputs by separating default and custom fields * refactor: event type api <-> internal transformers * refactor: v2 use refactored libraries to transform input and output booking fields * refactor: dont allow custom slugs with default reserved slugs * refactor: input service store only specific default system fields * refactor: atoms display only specific default system fields * refactor: delete unused system field constants * fix: tests * fix: old event types * fix: imports * fix: import * chore: release libraries and use v2 with them * fix: unit tests * refactor: push email in case of missing phone * refactor: satisfies check for event-type locations
This commit is contained in:
1 parent
2b43d9a716
commit
5b5a42e72d
34 files changed
+7108
-1187
No files matched your search
+32
-129
@@ -1,13 +1,20 @@
|
||||
import type { BookerProps } from "@calcom/features/bookings/Booker";
|
||||
import { getFieldIdentifier } from "@calcom/features/form-builder/utils/getFieldIdentifier";
|
||||
import { defaultEvents } from "@calcom/lib/defaultEvents";
|
||||
import type { UserField, SystemField } from "@calcom/lib/event-types/transformers";
|
||||
import type { CustomField, SystemField } from "@calcom/lib/event-types/transformers";
|
||||
import {
|
||||
transformApiEventTypeLocations,
|
||||
transformApiEventTypeBookingFields,
|
||||
transformLocationsApiToInternal,
|
||||
transformBookingFieldsApiToInternal,
|
||||
systemBeforeFieldName,
|
||||
systemBeforeFieldEmail,
|
||||
systemBeforeFieldLocation,
|
||||
systemAfterFieldRescheduleReason,
|
||||
} from "@calcom/lib/event-types/transformers";
|
||||
import { getBookerBaseUrlSync } from "@calcom/lib/getBookerUrl/client";
|
||||
import type { EventTypeOutput_2024_06_14, TeamEventTypeOutput_2024_06_14 } from "@calcom/platform-types";
|
||||
import type {
|
||||
CustomFieldOutput_2024_06_14,
|
||||
EventTypeOutput_2024_06_14,
|
||||
TeamEventTypeOutput_2024_06_14,
|
||||
} from "@calcom/platform-types";
|
||||
import {
|
||||
bookerLayoutOptions,
|
||||
BookerLayouts,
|
||||
@@ -184,7 +191,7 @@ function isDefaultEvent(eventSlug: string) {
|
||||
}
|
||||
|
||||
function getLocations(locations: EventTypeOutput_2024_06_14["locations"]) {
|
||||
const transformed = transformApiEventTypeLocations(locations);
|
||||
const transformed = transformLocationsApiToInternal(locations);
|
||||
|
||||
const withPrivateHidden = transformed.map((location) => {
|
||||
const { displayLocationPublicly, type } = location;
|
||||
@@ -210,133 +217,29 @@ function getLocations(locations: EventTypeOutput_2024_06_14["locations"]) {
|
||||
}
|
||||
|
||||
function getBookingFields(bookingFields: EventTypeOutput_2024_06_14["bookingFields"]) {
|
||||
const transformedBookingFields: (SystemField | UserField)[] =
|
||||
transformApiEventTypeBookingFields(bookingFields);
|
||||
|
||||
// These fields should be added before other user fields
|
||||
const systemBeforeFields: SystemField[] = [
|
||||
{
|
||||
type: "name",
|
||||
// This is the `name` of the main field
|
||||
name: "name",
|
||||
editable: "system",
|
||||
// This Label is used in Email only as of now.
|
||||
defaultLabel: "your_name",
|
||||
required: true,
|
||||
sources: [
|
||||
{
|
||||
label: "Default",
|
||||
id: "default",
|
||||
type: "default",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
defaultLabel: "email_address",
|
||||
type: "email",
|
||||
name: "email",
|
||||
required: true,
|
||||
editable: "system",
|
||||
sources: [
|
||||
{
|
||||
label: "Default",
|
||||
id: "default",
|
||||
type: "default",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
defaultLabel: "location",
|
||||
type: "radioInput",
|
||||
name: "location",
|
||||
editable: "system",
|
||||
hideWhenJustOneOption: true,
|
||||
required: false,
|
||||
getOptionsAt: "locations",
|
||||
optionsInputs: {
|
||||
attendeeInPerson: {
|
||||
type: "address",
|
||||
required: true,
|
||||
placeholder: "",
|
||||
},
|
||||
phone: {
|
||||
type: "phone",
|
||||
required: true,
|
||||
placeholder: "",
|
||||
},
|
||||
},
|
||||
sources: [
|
||||
{
|
||||
label: "Default",
|
||||
id: "default",
|
||||
type: "default",
|
||||
},
|
||||
],
|
||||
},
|
||||
systemBeforeFieldName,
|
||||
systemBeforeFieldEmail,
|
||||
systemBeforeFieldLocation,
|
||||
];
|
||||
|
||||
// These fields should be added after other user fields
|
||||
const systemAfterFields: SystemField[] = [
|
||||
{
|
||||
defaultLabel: "reason_for_reschedule",
|
||||
type: "textarea",
|
||||
editable: "system-but-optional",
|
||||
name: "rescheduleReason",
|
||||
defaultPlaceholder: "reschedule_placeholder",
|
||||
required: false,
|
||||
views: [
|
||||
{
|
||||
id: "reschedule",
|
||||
label: "Reschedule View",
|
||||
},
|
||||
],
|
||||
sources: [
|
||||
{
|
||||
label: "Default",
|
||||
id: "default",
|
||||
type: "default",
|
||||
},
|
||||
],
|
||||
},
|
||||
const transformedCustomFields: CustomField[] = transformBookingFieldsApiToInternal(
|
||||
bookingFields.filter((field) => isCustomField(field))
|
||||
);
|
||||
|
||||
const systemAfterFields: SystemField[] = [systemAfterFieldRescheduleReason];
|
||||
|
||||
const transformedBookingFields: (SystemField | CustomField)[] = [
|
||||
...systemBeforeFields,
|
||||
...transformedCustomFields,
|
||||
...systemAfterFields,
|
||||
];
|
||||
|
||||
const missingSystemBeforeFields: SystemField[] = [];
|
||||
|
||||
for (const field of systemBeforeFields) {
|
||||
const existingBookingFieldIndex = transformedBookingFields.findIndex(
|
||||
(f) => getFieldIdentifier(f.name) === getFieldIdentifier(field.name)
|
||||
);
|
||||
// Only do a push, we must not update existing system fields as user could have modified any property in it,
|
||||
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
|
||||
transformedBookingFields[existingBookingFieldIndex] = {
|
||||
...field,
|
||||
...transformedBookingFields[existingBookingFieldIndex],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
transformedBookingFields.push(...missingSystemBeforeFields);
|
||||
|
||||
const missingSystemAfterFields: SystemField[] = [];
|
||||
for (const field of systemAfterFields) {
|
||||
const existingBookingFieldIndex = transformedBookingFields.findIndex(
|
||||
(f) => getFieldIdentifier(f.name) === getFieldIdentifier(field.name)
|
||||
);
|
||||
// Only do a push, we must not update existing system fields as user could have modified any property in it,
|
||||
if (existingBookingFieldIndex === -1) {
|
||||
missingSystemAfterFields.push(field);
|
||||
} else {
|
||||
transformedBookingFields[existingBookingFieldIndex] = {
|
||||
// Adding the fields from Code first and then fields from DB. Allows, the code to push new properties to the field
|
||||
...field,
|
||||
...transformedBookingFields[existingBookingFieldIndex],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
transformedBookingFields.push(...missingSystemAfterFields);
|
||||
return eventTypeBookingFields.brand<"HAS_SYSTEM_FIELDS">().parse(transformedBookingFields);
|
||||
}
|
||||
|
||||
function isCustomField(
|
||||
field: EventTypeOutput_2024_06_14["bookingFields"][number]
|
||||
): field is CustomFieldOutput_2024_06_14 {
|
||||
return !field.isDefault;
|
||||
}
|
||||
@@ -1,3 +1,6 @@
|
||||
## 0.0.36
|
||||
Released to support PR https://github.com/calcom/cal.com/pull/16685
|
||||
|
||||
## 0.0.31
|
||||
PR https://github.com/calcom/cal.com/pull/16414 fixed issue of deleting and rescheduling recurring events.
|
||||
|
||||
|
||||
@@ -94,22 +94,31 @@ export { eventTypeBookingFields, eventTypeLocations } from "@calcom/prisma/zod-u
|
||||
export { EventTypeMetaDataSchema, userMetadata } from "@calcom/prisma/zod-utils";
|
||||
|
||||
export {
|
||||
transformApiEventTypeBookingFields,
|
||||
transformApiEventTypeIntervalLimits,
|
||||
transformApiEventTypeLocations,
|
||||
getResponseEventTypeLocations,
|
||||
getResponseEventTypeBookingFields,
|
||||
// note(Lauris): Api to internal
|
||||
transformBookingFieldsApiToInternal,
|
||||
transformLocationsApiToInternal,
|
||||
transformIntervalLimitsApiToInternal,
|
||||
transformFutureBookingLimitsApiToInternal,
|
||||
transformRecurrenceApiToInternal,
|
||||
// note(Lauris): Internal to api
|
||||
transformBookingFieldsInternalToApi,
|
||||
transformLocationsInternalToApi,
|
||||
transformIntervalLimitsInternalToApi,
|
||||
transformFutureBookingLimitsInternalToApi,
|
||||
transformRecurrenceInternalToApi,
|
||||
// note(Lauris): schemas
|
||||
TransformedLocationsSchema,
|
||||
BookingFieldsSchema,
|
||||
getResponseEventTypeIntervalLimits,
|
||||
getResponseEventTypeFutureBookingLimits,
|
||||
transformApiEventTypeFutureBookingLimits,
|
||||
transformApiEventTypeRecurrence,
|
||||
getResponseEventTypeRecurrence,
|
||||
// note(Lauris): constants
|
||||
systemBeforeFieldName,
|
||||
systemBeforeFieldEmail,
|
||||
systemBeforeFieldLocation,
|
||||
systemAfterFieldRescheduleReason,
|
||||
} from "@calcom/lib/event-types/transformers";
|
||||
|
||||
export type { SystemField, CustomField } from "@calcom/lib/event-types/transformers";
|
||||
|
||||
export { parseBookingLimit } from "@calcom/lib";
|
||||
export type { SystemField, UserField } from "@calcom/lib/event-types/transformers";
|
||||
|
||||
export { parseRecurringEvent } from "@calcom/lib/isRecurringEvent";
|
||||
export { dynamicEvent } from "@calcom/lib/defaultEvents";
|
||||
|
||||
+61
-102
@@ -5,9 +5,7 @@ import { IsString, IsBoolean, IsArray, IsIn, IsOptional } from "class-validator"
|
||||
import type { ValidationOptions, ValidatorConstraintInterface } from "class-validator";
|
||||
import { registerDecorator, validate, ValidatorConstraint } from "class-validator";
|
||||
|
||||
const bookingFields = [
|
||||
"name",
|
||||
"email",
|
||||
const inputBookingFieldTypes = [
|
||||
"phone",
|
||||
"address",
|
||||
"text",
|
||||
@@ -21,53 +19,8 @@ const bookingFields = [
|
||||
"boolean",
|
||||
] as const;
|
||||
|
||||
export class NameField_2024_06_14 {
|
||||
@IsIn(bookingFields)
|
||||
@DocsProperty()
|
||||
type!: "name";
|
||||
|
||||
@IsString()
|
||||
@DocsProperty()
|
||||
slug!: string;
|
||||
|
||||
@IsString()
|
||||
@DocsProperty()
|
||||
label!: string;
|
||||
|
||||
@IsBoolean()
|
||||
@DocsProperty()
|
||||
required!: boolean;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@DocsProperty()
|
||||
placeholder?: string;
|
||||
}
|
||||
export class EmailField_2024_06_14 {
|
||||
@IsIn(bookingFields)
|
||||
@DocsProperty()
|
||||
type!: "email";
|
||||
|
||||
@IsString()
|
||||
@DocsProperty()
|
||||
slug!: string;
|
||||
|
||||
@IsString()
|
||||
@DocsProperty()
|
||||
label!: string;
|
||||
|
||||
@IsBoolean()
|
||||
@DocsProperty()
|
||||
required!: boolean;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@DocsProperty()
|
||||
placeholder?: string;
|
||||
}
|
||||
|
||||
export class PhoneField_2024_06_14 {
|
||||
@IsIn(bookingFields)
|
||||
export class PhoneFieldInput_2024_06_14 {
|
||||
@IsIn(inputBookingFieldTypes)
|
||||
@DocsProperty()
|
||||
type!: "phone";
|
||||
|
||||
@@ -89,8 +42,8 @@ export class PhoneField_2024_06_14 {
|
||||
placeholder?: string;
|
||||
}
|
||||
|
||||
export class AddressField_2024_06_14 {
|
||||
@IsIn(bookingFields)
|
||||
export class AddressFieldInput_2024_06_14 {
|
||||
@IsIn(inputBookingFieldTypes)
|
||||
@DocsProperty()
|
||||
type!: "address";
|
||||
|
||||
@@ -113,8 +66,8 @@ export class AddressField_2024_06_14 {
|
||||
placeholder?: string;
|
||||
}
|
||||
|
||||
export class TextField_2024_06_14 {
|
||||
@IsIn(bookingFields)
|
||||
export class TextFieldInput_2024_06_14 {
|
||||
@IsIn(inputBookingFieldTypes)
|
||||
@DocsProperty()
|
||||
type!: "text";
|
||||
|
||||
@@ -137,8 +90,8 @@ export class TextField_2024_06_14 {
|
||||
placeholder?: string;
|
||||
}
|
||||
|
||||
export class NumberField_2024_06_14 {
|
||||
@IsIn(bookingFields)
|
||||
export class NumberFieldInput_2024_06_14 {
|
||||
@IsIn(inputBookingFieldTypes)
|
||||
@DocsProperty()
|
||||
type!: "number";
|
||||
|
||||
@@ -161,8 +114,8 @@ export class NumberField_2024_06_14 {
|
||||
placeholder?: string;
|
||||
}
|
||||
|
||||
export class TextAreaField_2024_06_14 {
|
||||
@IsIn(bookingFields)
|
||||
export class TextAreaFieldInput_2024_06_14 {
|
||||
@IsIn(inputBookingFieldTypes)
|
||||
@DocsProperty()
|
||||
type!: "textarea";
|
||||
|
||||
@@ -185,8 +138,8 @@ export class TextAreaField_2024_06_14 {
|
||||
placeholder?: string;
|
||||
}
|
||||
|
||||
export class SelectField_2024_06_14 {
|
||||
@IsIn(bookingFields)
|
||||
export class SelectFieldInput_2024_06_14 {
|
||||
@IsIn(inputBookingFieldTypes)
|
||||
@DocsProperty()
|
||||
type!: "select";
|
||||
|
||||
@@ -213,8 +166,8 @@ export class SelectField_2024_06_14 {
|
||||
options!: string[];
|
||||
}
|
||||
|
||||
export class MultiSelectField_2024_06_14 {
|
||||
@IsIn(bookingFields)
|
||||
export class MultiSelectFieldInput_2024_06_14 {
|
||||
@IsIn(inputBookingFieldTypes)
|
||||
@DocsProperty()
|
||||
type!: "multiselect";
|
||||
|
||||
@@ -235,8 +188,8 @@ export class MultiSelectField_2024_06_14 {
|
||||
options!: string[];
|
||||
}
|
||||
|
||||
export class MultiEmailField_2024_06_14 {
|
||||
@IsIn(bookingFields)
|
||||
export class MultiEmailFieldInput_2024_06_14 {
|
||||
@IsIn(inputBookingFieldTypes)
|
||||
@DocsProperty()
|
||||
type!: "multiemail";
|
||||
|
||||
@@ -259,8 +212,8 @@ export class MultiEmailField_2024_06_14 {
|
||||
placeholder?: string;
|
||||
}
|
||||
|
||||
export class CheckboxGroupField_2024_06_14 {
|
||||
@IsIn(bookingFields)
|
||||
export class CheckboxGroupFieldInput_2024_06_14 {
|
||||
@IsIn(inputBookingFieldTypes)
|
||||
@DocsProperty()
|
||||
type!: "checkbox";
|
||||
|
||||
@@ -281,8 +234,8 @@ export class CheckboxGroupField_2024_06_14 {
|
||||
options!: string[];
|
||||
}
|
||||
|
||||
export class RadioGroupField_2024_06_14 {
|
||||
@IsIn(bookingFields)
|
||||
export class RadioGroupFieldInput_2024_06_14 {
|
||||
@IsIn(inputBookingFieldTypes)
|
||||
@DocsProperty()
|
||||
type!: "radio";
|
||||
|
||||
@@ -303,8 +256,8 @@ export class RadioGroupField_2024_06_14 {
|
||||
options!: string[];
|
||||
}
|
||||
|
||||
export class BooleanField_2024_06_14 {
|
||||
@IsIn(bookingFields)
|
||||
export class BooleanFieldInput_2024_06_14 {
|
||||
@IsIn(inputBookingFieldTypes)
|
||||
@DocsProperty()
|
||||
type!: "boolean";
|
||||
|
||||
@@ -321,39 +274,37 @@ export class BooleanField_2024_06_14 {
|
||||
required!: boolean;
|
||||
}
|
||||
|
||||
export type BookingField_2024_06_14 =
|
||||
| NameField_2024_06_14
|
||||
| EmailField_2024_06_14
|
||||
| PhoneField_2024_06_14
|
||||
| AddressField_2024_06_14
|
||||
| TextField_2024_06_14
|
||||
| NumberField_2024_06_14
|
||||
| TextAreaField_2024_06_14
|
||||
| SelectField_2024_06_14
|
||||
| MultiSelectField_2024_06_14
|
||||
| MultiEmailField_2024_06_14
|
||||
| CheckboxGroupField_2024_06_14
|
||||
| RadioGroupField_2024_06_14
|
||||
| BooleanField_2024_06_14;
|
||||
export type InputBookingField_2024_06_14 =
|
||||
| PhoneFieldInput_2024_06_14
|
||||
| AddressFieldInput_2024_06_14
|
||||
| TextFieldInput_2024_06_14
|
||||
| NumberFieldInput_2024_06_14
|
||||
| TextAreaFieldInput_2024_06_14
|
||||
| SelectFieldInput_2024_06_14
|
||||
| MultiSelectFieldInput_2024_06_14
|
||||
| MultiEmailFieldInput_2024_06_14
|
||||
| CheckboxGroupFieldInput_2024_06_14
|
||||
| RadioGroupFieldInput_2024_06_14
|
||||
| BooleanFieldInput_2024_06_14;
|
||||
|
||||
@ValidatorConstraint({ async: true })
|
||||
class BookingFieldValidator_2024_06_14 implements ValidatorConstraintInterface {
|
||||
private classTypeMap: { [key: string]: new () => BookingField_2024_06_14 } = {
|
||||
name: NameField_2024_06_14,
|
||||
email: EmailField_2024_06_14,
|
||||
phone: PhoneField_2024_06_14,
|
||||
address: AddressField_2024_06_14,
|
||||
text: TextField_2024_06_14,
|
||||
number: NumberField_2024_06_14,
|
||||
textarea: TextAreaField_2024_06_14,
|
||||
select: SelectField_2024_06_14,
|
||||
multiselect: MultiSelectField_2024_06_14,
|
||||
multiemail: MultiEmailField_2024_06_14,
|
||||
checkbox: CheckboxGroupField_2024_06_14,
|
||||
radio: RadioGroupField_2024_06_14,
|
||||
boolean: BooleanField_2024_06_14,
|
||||
class InputBookingFieldValidator_2024_06_14 implements ValidatorConstraintInterface {
|
||||
private classTypeMap: { [key: string]: new () => InputBookingField_2024_06_14 } = {
|
||||
phone: PhoneFieldInput_2024_06_14,
|
||||
address: AddressFieldInput_2024_06_14,
|
||||
text: TextFieldInput_2024_06_14,
|
||||
number: NumberFieldInput_2024_06_14,
|
||||
textarea: TextAreaFieldInput_2024_06_14,
|
||||
select: SelectFieldInput_2024_06_14,
|
||||
multiselect: MultiSelectFieldInput_2024_06_14,
|
||||
multiemail: MultiEmailFieldInput_2024_06_14,
|
||||
checkbox: CheckboxGroupFieldInput_2024_06_14,
|
||||
radio: RadioGroupFieldInput_2024_06_14,
|
||||
boolean: BooleanFieldInput_2024_06_14,
|
||||
};
|
||||
|
||||
private reservedSystemSlugs = ["name", "email", "location", "rescheduleReason"];
|
||||
|
||||
async validate(bookingFields: { type: string; slug: string }[]) {
|
||||
if (!Array.isArray(bookingFields)) {
|
||||
throw new BadRequestException(`'bookingFields' must be an array.`);
|
||||
@@ -374,6 +325,14 @@ class BookingFieldValidator_2024_06_14 implements ValidatorConstraintInterface {
|
||||
throw new BadRequestException(`Each booking field must have a 'slug' property.`);
|
||||
}
|
||||
|
||||
if (this.reservedSystemSlugs.includes(slug)) {
|
||||
throw new BadRequestException(
|
||||
`The slug '${slug}' is reserved and cannot be used, because it is a slug of a default booking field. Reserved slugs are: ${this.reservedSystemSlugs.join(
|
||||
", "
|
||||
)}`
|
||||
);
|
||||
}
|
||||
|
||||
if (slugs.includes(slug)) {
|
||||
throw new BadRequestException(
|
||||
`Duplicate bookingFields slug '${slug}' found. All bookingFields slugs must be unique.`
|
||||
@@ -402,15 +361,15 @@ class BookingFieldValidator_2024_06_14 implements ValidatorConstraintInterface {
|
||||
}
|
||||
}
|
||||
|
||||
export function ValidateBookingFields_2024_06_14(validationOptions?: ValidationOptions) {
|
||||
export function ValidateInputBookingFields_2024_06_14(validationOptions?: ValidationOptions) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return function (object: any, propertyName: string) {
|
||||
registerDecorator({
|
||||
name: "ValidateBookingFields",
|
||||
name: "ValidateInputBookingFields",
|
||||
target: object.constructor,
|
||||
propertyName: propertyName,
|
||||
options: validationOptions,
|
||||
validator: new BookingFieldValidator_2024_06_14(),
|
||||
validator: new InputBookingFieldValidator_2024_06_14(),
|
||||
});
|
||||
};
|
||||
}
|
||||
+4
-4
@@ -13,8 +13,8 @@ import {
|
||||
|
||||
import { SchedulingType } from "@calcom/platform-enums";
|
||||
|
||||
import type { BookingField_2024_06_14 } from "./booking-fields.input";
|
||||
import { ValidateBookingFields_2024_06_14 } from "./booking-fields.input";
|
||||
import type { InputBookingField_2024_06_14 } from "./booking-fields.input";
|
||||
import { ValidateInputBookingFields_2024_06_14 } from "./booking-fields.input";
|
||||
import { BookingLimitsCount_2024_06_14, ValidateBookingLimistsCount } from "./booking-limits-count.input";
|
||||
import {
|
||||
BookingLimitsDuration_2024_06_14,
|
||||
@@ -54,8 +54,8 @@ export class CreateEventTypeInput_2024_06_14 {
|
||||
locations?: Location_2024_06_14[];
|
||||
|
||||
@IsOptional()
|
||||
@ValidateBookingFields_2024_06_14()
|
||||
bookingFields?: BookingField_2024_06_14[];
|
||||
@ValidateInputBookingFields_2024_06_14()
|
||||
bookingFields?: InputBookingField_2024_06_14[];
|
||||
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
|
||||
+6
-6
@@ -2,8 +2,8 @@ import { ApiProperty as DocsProperty } from "@nestjs/swagger";
|
||||
import { Type } from "class-transformer";
|
||||
import { IsString, IsInt, IsBoolean, IsOptional, Min, ValidateNested, IsArray } from "class-validator";
|
||||
|
||||
import type { BookingField_2024_06_14 } from "./booking-fields.input";
|
||||
import { ValidateBookingFields_2024_06_14 } from "./booking-fields.input";
|
||||
import type { InputBookingField_2024_06_14 } from "./booking-fields.input";
|
||||
import { ValidateInputBookingFields_2024_06_14 } from "./booking-fields.input";
|
||||
import { BookingLimitsCount_2024_06_14, ValidateBookingLimistsCount } from "./booking-limits-count.input";
|
||||
import {
|
||||
BookingLimitsDuration_2024_06_14,
|
||||
@@ -38,8 +38,8 @@ export class UpdateEventTypeInput_2024_06_14 {
|
||||
locations?: Location_2024_06_14[];
|
||||
|
||||
@IsOptional()
|
||||
@ValidateBookingFields_2024_06_14()
|
||||
bookingFields?: BookingField_2024_06_14[];
|
||||
@ValidateInputBookingFields_2024_06_14()
|
||||
bookingFields?: InputBookingField_2024_06_14[];
|
||||
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
@@ -122,9 +122,9 @@ export class UpdateTeamEventTypeInput_2024_06_14 {
|
||||
locations?: Location_2024_06_14[];
|
||||
|
||||
@IsOptional()
|
||||
@ValidateBookingFields_2024_06_14()
|
||||
@ValidateInputBookingFields_2024_06_14()
|
||||
@DocsProperty()
|
||||
bookingFields?: BookingField_2024_06_14[];
|
||||
bookingFields?: InputBookingField_2024_06_14[];
|
||||
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
|
||||
+309
@@ -0,0 +1,309 @@
|
||||
import { BadRequestException } from "@nestjs/common";
|
||||
import { plainToInstance } from "class-transformer";
|
||||
import { IsBoolean, IsString } from "class-validator";
|
||||
import type { ValidationOptions, ValidatorConstraintInterface } from "class-validator";
|
||||
import { registerDecorator, validate, ValidatorConstraint } from "class-validator";
|
||||
|
||||
import {
|
||||
PhoneFieldInput_2024_06_14,
|
||||
AddressFieldInput_2024_06_14,
|
||||
TextFieldInput_2024_06_14,
|
||||
NumberFieldInput_2024_06_14,
|
||||
TextAreaFieldInput_2024_06_14,
|
||||
SelectFieldInput_2024_06_14,
|
||||
MultiSelectFieldInput_2024_06_14,
|
||||
MultiEmailFieldInput_2024_06_14,
|
||||
CheckboxGroupFieldInput_2024_06_14,
|
||||
RadioGroupFieldInput_2024_06_14,
|
||||
BooleanFieldInput_2024_06_14,
|
||||
} from "../inputs";
|
||||
|
||||
class NameDefaultFieldOutput_2024_06_14 {
|
||||
@IsBoolean()
|
||||
isDefault = true;
|
||||
|
||||
@IsString()
|
||||
slug!: "name";
|
||||
|
||||
@IsString()
|
||||
type!: "name";
|
||||
|
||||
@IsBoolean()
|
||||
required!: true;
|
||||
}
|
||||
|
||||
class EmailDefaultFieldOutput_2024_06_14 {
|
||||
@IsBoolean()
|
||||
isDefault = true;
|
||||
|
||||
@IsString()
|
||||
slug!: "email";
|
||||
|
||||
@IsString()
|
||||
type!: "email";
|
||||
|
||||
@IsBoolean()
|
||||
required!: true;
|
||||
}
|
||||
|
||||
class LocationDefaultFieldOutput_2024_06_14 {
|
||||
@IsBoolean()
|
||||
isDefault = true;
|
||||
|
||||
@IsString()
|
||||
slug!: "location";
|
||||
|
||||
@IsString()
|
||||
type!: "radioInput";
|
||||
|
||||
@IsBoolean()
|
||||
required!: false;
|
||||
}
|
||||
|
||||
class RescheduleReasonDefaultFieldOutput_2024_06_14 {
|
||||
@IsBoolean()
|
||||
isDefault = true;
|
||||
|
||||
@IsString()
|
||||
slug!: "rescheduleReason";
|
||||
|
||||
@IsString()
|
||||
type!: "textarea";
|
||||
|
||||
@IsBoolean()
|
||||
required!: false;
|
||||
}
|
||||
|
||||
class TitleDefaultFieldOutput_2024_06_14 {
|
||||
@IsBoolean()
|
||||
isDefault = true;
|
||||
|
||||
@IsString()
|
||||
slug!: "title";
|
||||
|
||||
@IsString()
|
||||
type!: "text";
|
||||
|
||||
@IsBoolean()
|
||||
required!: true;
|
||||
}
|
||||
|
||||
class NotesDefaultFieldOutput_2024_06_14 {
|
||||
@IsBoolean()
|
||||
isDefault = true;
|
||||
|
||||
@IsString()
|
||||
slug!: "notes";
|
||||
|
||||
@IsString()
|
||||
type!: "textarea";
|
||||
|
||||
@IsBoolean()
|
||||
required!: false;
|
||||
}
|
||||
|
||||
class GuestsDefaultFieldOutput_2024_06_14 {
|
||||
@IsBoolean()
|
||||
isDefault = true;
|
||||
|
||||
@IsString()
|
||||
slug!: "guests";
|
||||
|
||||
@IsString()
|
||||
type!: "multiemail";
|
||||
|
||||
@IsBoolean()
|
||||
required!: false;
|
||||
}
|
||||
|
||||
class PhoneFieldOutput_2024_06_14 extends PhoneFieldInput_2024_06_14 {
|
||||
@IsBoolean()
|
||||
isDefault = false;
|
||||
}
|
||||
|
||||
class AddressFieldOutput_2024_06_14 extends AddressFieldInput_2024_06_14 {
|
||||
@IsBoolean()
|
||||
isDefault = false;
|
||||
}
|
||||
|
||||
class TextFieldOutput_2024_06_14 extends TextFieldInput_2024_06_14 {
|
||||
@IsBoolean()
|
||||
isDefault = false;
|
||||
}
|
||||
|
||||
class NumberFieldOutput_2024_06_14 extends NumberFieldInput_2024_06_14 {
|
||||
@IsBoolean()
|
||||
isDefault = false;
|
||||
}
|
||||
|
||||
class TextAreaFieldOutput_2024_06_14 extends TextAreaFieldInput_2024_06_14 {
|
||||
@IsBoolean()
|
||||
isDefault = false;
|
||||
}
|
||||
|
||||
class SelectFieldOutput_2024_06_14 extends SelectFieldInput_2024_06_14 {
|
||||
@IsBoolean()
|
||||
isDefault = false;
|
||||
}
|
||||
|
||||
class MultiSelectFieldOutput_2024_06_14 extends MultiSelectFieldInput_2024_06_14 {
|
||||
@IsBoolean()
|
||||
isDefault = false;
|
||||
}
|
||||
|
||||
class MultiEmailFieldOutput_2024_06_14 extends MultiEmailFieldInput_2024_06_14 {
|
||||
@IsBoolean()
|
||||
isDefault = false;
|
||||
}
|
||||
|
||||
class CheckboxGroupFieldOutput_2024_06_14 extends CheckboxGroupFieldInput_2024_06_14 {
|
||||
@IsBoolean()
|
||||
isDefault = false;
|
||||
}
|
||||
|
||||
class RadioGroupFieldOutput_2024_06_14 extends RadioGroupFieldInput_2024_06_14 {
|
||||
@IsBoolean()
|
||||
isDefault = false;
|
||||
}
|
||||
|
||||
class BooleanFieldOutput_2024_06_14 extends BooleanFieldInput_2024_06_14 {
|
||||
@IsBoolean()
|
||||
isDefault = false;
|
||||
}
|
||||
|
||||
export type DefaultFieldOutput_2024_06_14 =
|
||||
| NameDefaultFieldOutput_2024_06_14
|
||||
| EmailDefaultFieldOutput_2024_06_14
|
||||
| LocationDefaultFieldOutput_2024_06_14
|
||||
| RescheduleReasonDefaultFieldOutput_2024_06_14
|
||||
| TitleDefaultFieldOutput_2024_06_14
|
||||
| NotesDefaultFieldOutput_2024_06_14
|
||||
| GuestsDefaultFieldOutput_2024_06_14;
|
||||
|
||||
export type CustomFieldOutput_2024_06_14 =
|
||||
| PhoneFieldOutput_2024_06_14
|
||||
| AddressFieldOutput_2024_06_14
|
||||
| TextFieldOutput_2024_06_14
|
||||
| NumberFieldOutput_2024_06_14
|
||||
| TextAreaFieldOutput_2024_06_14
|
||||
| SelectFieldOutput_2024_06_14
|
||||
| MultiSelectFieldOutput_2024_06_14
|
||||
| MultiEmailFieldOutput_2024_06_14
|
||||
| CheckboxGroupFieldOutput_2024_06_14
|
||||
| RadioGroupFieldOutput_2024_06_14
|
||||
| BooleanFieldOutput_2024_06_14;
|
||||
|
||||
export type OutputBookingField_2024_06_14 = DefaultFieldOutput_2024_06_14 | CustomFieldOutput_2024_06_14;
|
||||
|
||||
@ValidatorConstraint({ async: true })
|
||||
class OutputBookingFieldValidator_2024_06_14 implements ValidatorConstraintInterface {
|
||||
private defaultOutputNameMap: { [key: string]: new () => DefaultFieldOutput_2024_06_14 } = {
|
||||
name: NameDefaultFieldOutput_2024_06_14,
|
||||
email: EmailDefaultFieldOutput_2024_06_14,
|
||||
location: LocationDefaultFieldOutput_2024_06_14,
|
||||
rescheduleReason: RescheduleReasonDefaultFieldOutput_2024_06_14,
|
||||
title: TitleDefaultFieldOutput_2024_06_14,
|
||||
notes: NotesDefaultFieldOutput_2024_06_14,
|
||||
guests: GuestsDefaultFieldOutput_2024_06_14,
|
||||
};
|
||||
|
||||
private customOutputTypeMap: { [key: string]: new () => CustomFieldOutput_2024_06_14 } = {
|
||||
phone: PhoneFieldOutput_2024_06_14,
|
||||
address: AddressFieldOutput_2024_06_14,
|
||||
text: TextFieldOutput_2024_06_14,
|
||||
number: NumberFieldOutput_2024_06_14,
|
||||
textarea: TextAreaFieldOutput_2024_06_14,
|
||||
select: SelectFieldOutput_2024_06_14,
|
||||
multiselect: MultiSelectFieldOutput_2024_06_14,
|
||||
multiemail: MultiEmailFieldOutput_2024_06_14,
|
||||
checkbox: CheckboxGroupFieldOutput_2024_06_14,
|
||||
radio: RadioGroupFieldOutput_2024_06_14,
|
||||
boolean: BooleanFieldOutput_2024_06_14,
|
||||
};
|
||||
|
||||
async validate(bookingFields: OutputBookingField_2024_06_14[]) {
|
||||
if (!Array.isArray(bookingFields)) {
|
||||
throw new BadRequestException(`'bookingFields' must be an array.`);
|
||||
}
|
||||
|
||||
if (!bookingFields.length) {
|
||||
throw new BadRequestException(`'bookingFields' must contain at least 1 booking field.`);
|
||||
}
|
||||
|
||||
const slugs: string[] = [];
|
||||
for (const field of bookingFields) {
|
||||
const { type, slug } = field;
|
||||
if (!type) {
|
||||
throw new BadRequestException(`Each booking field must have a 'type' property.`);
|
||||
}
|
||||
|
||||
if (!slug) {
|
||||
throw new BadRequestException(`Each booking field must have a 'slug' property.`);
|
||||
}
|
||||
|
||||
if (slugs.includes(slug)) {
|
||||
throw new BadRequestException(
|
||||
`Duplicate bookingFields slug '${slug}' found. All bookingFields slugs must be unique.`
|
||||
);
|
||||
}
|
||||
slugs.push(slug);
|
||||
|
||||
if (this.isDefaultField(field)) {
|
||||
await this.validateDefaultField(field);
|
||||
} else {
|
||||
await this.validateCustomField(field);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
isDefaultField(field: OutputBookingField_2024_06_14): field is DefaultFieldOutput_2024_06_14 {
|
||||
return field.isDefault === true;
|
||||
}
|
||||
|
||||
async validateDefaultField(field: DefaultFieldOutput_2024_06_14) {
|
||||
const ClassType = this.defaultOutputNameMap[field.slug];
|
||||
if (!ClassType) {
|
||||
throw new BadRequestException(`Unsupported booking field slgu '${field.slug}'.`);
|
||||
}
|
||||
|
||||
const instance = plainToInstance(ClassType, field);
|
||||
const errors = await validate(instance);
|
||||
if (errors.length > 0) {
|
||||
const message = errors.flatMap((error) => Object.values(error.constraints || {})).join(", ");
|
||||
throw new BadRequestException(`Validation failed for ${field.slug} booking field: ${message}`);
|
||||
}
|
||||
}
|
||||
|
||||
async validateCustomField(field: CustomFieldOutput_2024_06_14) {
|
||||
const ClassType = this.customOutputTypeMap[field.type];
|
||||
if (!ClassType) {
|
||||
throw new BadRequestException(`Unsupported booking field type '${field.type}'.`);
|
||||
}
|
||||
|
||||
const instance = plainToInstance(ClassType, field);
|
||||
const errors = await validate(instance);
|
||||
if (errors.length > 0) {
|
||||
const message = errors.flatMap((error) => Object.values(error.constraints || {})).join(", ");
|
||||
throw new BadRequestException(`Validation failed for ${field.type} booking field: ${message}`);
|
||||
}
|
||||
}
|
||||
|
||||
defaultMessage() {
|
||||
return `Validation failed for one or more booking fields.`;
|
||||
}
|
||||
}
|
||||
|
||||
export function ValidateOutputBookingFields_2024_06_14(validationOptions?: ValidationOptions) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return function (object: any, propertyName: string) {
|
||||
registerDecorator({
|
||||
name: "ValidateOutputBookingFields",
|
||||
target: object.constructor,
|
||||
propertyName: propertyName,
|
||||
options: validationOptions,
|
||||
validator: new OutputBookingFieldValidator_2024_06_14(),
|
||||
});
|
||||
};
|
||||
}
|
||||
+7
-6
@@ -11,12 +11,13 @@ import {
|
||||
ValidateNested,
|
||||
} from "class-validator";
|
||||
|
||||
import type { Location_2024_06_14, BookingField_2024_06_14, BookingWindow_2024_06_14 } from "../inputs";
|
||||
import type { Location_2024_06_14, BookingWindow_2024_06_14 } from "../inputs";
|
||||
import { Host as TeamEventTypeHostInput, BookingLimitsDuration_2024_06_14 } from "../inputs";
|
||||
import { Recurrence_2024_06_14 } from "../inputs";
|
||||
import { ValidateBookingFields_2024_06_14 } from "../inputs/booking-fields.input";
|
||||
import type { BookingLimitsCount_2024_06_14 } from "../inputs/booking-limits-count.input";
|
||||
import { ValidateLocations_2024_06_14 } from "../inputs/locations.input";
|
||||
import type { OutputBookingField_2024_06_14 } from "./booking-fields.output";
|
||||
import { ValidateOutputBookingFields_2024_06_14 } from "./booking-fields.output";
|
||||
|
||||
enum SchedulingTypeEnum {
|
||||
ROUND_ROBIN = "ROUND_ROBIN",
|
||||
@@ -75,8 +76,8 @@ export class EventTypeOutput_2024_06_14 {
|
||||
@ValidateLocations_2024_06_14()
|
||||
locations!: Location_2024_06_14[];
|
||||
|
||||
@ValidateBookingFields_2024_06_14()
|
||||
bookingFields!: BookingField_2024_06_14[];
|
||||
@ValidateOutputBookingFields_2024_06_14()
|
||||
bookingFields!: OutputBookingField_2024_06_14[];
|
||||
|
||||
@IsBoolean()
|
||||
disableGuests!: boolean;
|
||||
@@ -185,8 +186,8 @@ export class TeamEventTypeOutput_2024_06_14 {
|
||||
@ValidateLocations_2024_06_14()
|
||||
locations!: Location_2024_06_14[];
|
||||
|
||||
@ValidateBookingFields_2024_06_14()
|
||||
bookingFields!: BookingField_2024_06_14[];
|
||||
@ValidateOutputBookingFields_2024_06_14()
|
||||
bookingFields!: OutputBookingField_2024_06_14[];
|
||||
|
||||
@IsBoolean()
|
||||
disableGuests!: boolean;
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
export * from "./event-type.output";
|
||||
export * from "./booking-fields.output";
|
||||
Reference in new issue
Block a user