feat: api v2 event type settings - booker bookings limit, emails, round robin reschedule (#24169)
* feat: event type bookerActiveBookingsLimit * test: booking with bookerActiveBookingsLimit * fix: typecheck * feat: org team event type emailSettings & rescheduleWithSameRoundRobinHost * docs: regenerate swagger * chore: default booker name and email in examples app * refactor: email settings description * refactor: rename email setting properties * refactor: function to add email settings to metadata * fix: try to fix e2e * fix: dont return input bookerActiveBookingsLimit in transformed object * fix: dont allow recurrence and bookerActiveBookingsLimit * fix: output service return bookerActiveBookingsLimit
This commit is contained in:
1 parent
1aac14056e
commit
bb9d99c3e1
21 files changed
+938
-69
No files matched your search
@@ -1,6 +1,5 @@
|
||||
import { Navbar } from "@/components/Navbar";
|
||||
import { Inter } from "next/font/google";
|
||||
// eslint-disable-next-line @calcom/eslint/deprecated-imports-next-router
|
||||
import { useRouter } from "next/router";
|
||||
import { useState } from "react";
|
||||
|
||||
@@ -95,6 +94,7 @@ export default function Bookings(props: { calUsername: string; calEmail: string
|
||||
// timeZones={["Europe/London", "Asia/Kolkata"]}
|
||||
// isBookingDryRun={true}
|
||||
// roundRobinHideOrgAndTeam={true}
|
||||
defaultFormValues={{ name: "Bob the booker", email: "[email protected]" }}
|
||||
bannerUrl="https://i0.wp.com/mahala.co.uk/wp-content/uploads/2014/12/img_banner-thin_mountains.jpg?fit=800%2C258&ssl=1"
|
||||
eventSlug={eventTypeSlug}
|
||||
onCreateBookingSuccess={(data) => {
|
||||
|
||||
@@ -18,6 +18,8 @@ export {
|
||||
eventTypeLocations,
|
||||
} from "@calcom/prisma/zod-utils";
|
||||
|
||||
export type { EventTypeMetadata } from "@calcom/prisma/zod-utils";
|
||||
|
||||
export { validateCustomEventName } from "@calcom/features/eventtypes/lib/eventNaming";
|
||||
export { EventManager };
|
||||
export { getEventTypeById } from "@calcom/features/eventtypes/lib/getEventTypeById";
|
||||
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
import { ApiPropertyOptional } from "@nestjs/swagger";
|
||||
import { IsBoolean, IsInt, IsOptional, Min } from "class-validator";
|
||||
|
||||
export class BookerActiveBookingsLimit_2024_06_14 {
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
@ApiPropertyOptional({
|
||||
description: "The maximum number of active bookings a booker can have for this event type.",
|
||||
example: 3,
|
||||
})
|
||||
maximumActiveBookings?: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
@ApiPropertyOptional({
|
||||
description:
|
||||
"Whether to offer rescheduling the last active booking to the chosen time slot when limit is reached.",
|
||||
})
|
||||
offerReschedule?: boolean;
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
disabled?: boolean = false;
|
||||
}
|
||||
+50
-4
@@ -19,9 +19,10 @@ import {
|
||||
ArrayUnique,
|
||||
} from "class-validator";
|
||||
|
||||
|
||||
import { SchedulingType } from "@calcom/platform-enums";
|
||||
|
||||
import { RequiresAtLeastOnePropertyWhenNotDisabled } from "../../../utils/RequiresOneOfPropertiesWhenNotDisabled";
|
||||
import { BookerActiveBookingsLimit_2024_06_14 } from "./booker-active-booking-limit.input";
|
||||
import { BookerLayouts_2024_06_14 } from "./booker-layouts.input";
|
||||
import {
|
||||
AddressFieldInput_2024_06_14,
|
||||
@@ -64,6 +65,7 @@ import type { ConfirmationPolicy_2024_06_14 } from "./confirmation-policy.input"
|
||||
import { BaseConfirmationPolicy_2024_06_14, ValidateConfirmationPolicy } from "./confirmation-policy.input";
|
||||
import { DestinationCalendar_2024_06_14 } from "./destination-calendar.input";
|
||||
import { Disabled_2024_06_14 } from "./disabled.input";
|
||||
import { EmailSettings_2024_06_14 } from "./email-settings.input";
|
||||
import { EventTypeColor_2024_06_14 } from "./event-type-color.input";
|
||||
import {
|
||||
InputAddressLocation_2024_06_14,
|
||||
@@ -80,6 +82,7 @@ import {
|
||||
import type { InputLocation_2024_06_14, InputTeamLocation_2024_06_14 } from "./locations.input";
|
||||
import { Recurrence_2024_06_14 } from "./recurrence.input";
|
||||
import { Seats_2024_06_14 } from "./seats.input";
|
||||
import { CantHaveRecurrenceAndBookerActiveBookingsLimit } from "./validators/CantHaveRecurrenceAndBookerActiveBookingsLimit";
|
||||
|
||||
export const CREATE_EVENT_LENGTH_EXAMPLE = 60;
|
||||
export const CREATE_EVENT_TITLE_EXAMPLE = "Learn the secrets of masterchief!";
|
||||
@@ -108,6 +111,7 @@ export const CREATE_EVENT_SLUG_EXAMPLE = "learn-the-secrets-of-masterchief";
|
||||
CalendarDaysWindow_2024_06_14,
|
||||
RangeWindow_2024_06_14,
|
||||
BaseBookingLimitsCount_2024_06_14,
|
||||
BookerActiveBookingsLimit_2024_06_14,
|
||||
Disabled_2024_06_14,
|
||||
BaseBookingLimitsDuration_2024_06_14,
|
||||
Recurrence_2024_06_14,
|
||||
@@ -123,7 +127,8 @@ export const CREATE_EVENT_SLUG_EXAMPLE = "learn-the-secrets-of-masterchief";
|
||||
NotesDefaultFieldInput_2024_06_14,
|
||||
GuestsDefaultFieldInput_2024_06_14,
|
||||
RescheduleReasonDefaultFieldInput_2024_06_14,
|
||||
InputOrganizersDefaultApp_2024_06_14
|
||||
InputOrganizersDefaultApp_2024_06_14,
|
||||
EmailSettings_2024_06_14
|
||||
)
|
||||
export class CalVideoSettings {
|
||||
@IsOptional()
|
||||
@@ -176,7 +181,8 @@ export class CalVideoSettings {
|
||||
disableTranscriptionForOrganizer?: boolean;
|
||||
}
|
||||
|
||||
class BaseCreateEventTypeInput {
|
||||
@CantHaveRecurrenceAndBookerActiveBookingsLimit()
|
||||
export class BaseCreateEventTypeInput {
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
@DocsProperty({ example: CREATE_EVENT_LENGTH_EXAMPLE })
|
||||
@@ -297,6 +303,29 @@ class BaseCreateEventTypeInput {
|
||||
@Type(() => Object)
|
||||
bookingLimitsCount?: BookingLimitsCount_2024_06_14;
|
||||
|
||||
@IsOptional()
|
||||
@RequiresAtLeastOnePropertyWhenNotDisabled()
|
||||
@Transform(({ value }) => {
|
||||
if (value && typeof value === "object") {
|
||||
if ("disabled" in value && value.disabled) {
|
||||
return Object.assign(new Disabled_2024_06_14(), value);
|
||||
} else {
|
||||
return Object.assign(new BookerActiveBookingsLimit_2024_06_14(), value);
|
||||
}
|
||||
}
|
||||
return value;
|
||||
})
|
||||
@ValidateNested()
|
||||
@DocsPropertyOptional({
|
||||
description: "Limit the number of active bookings a booker can make for this event type.",
|
||||
oneOf: [
|
||||
{ $ref: getSchemaPath(BookerActiveBookingsLimit_2024_06_14) },
|
||||
{ $ref: getSchemaPath(Disabled_2024_06_14) },
|
||||
],
|
||||
})
|
||||
@Type(() => Object)
|
||||
bookerActiveBookingsLimit?: BookerActiveBookingsLimit_2024_06_14 | Disabled_2024_06_14;
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
@DocsPropertyOptional({
|
||||
@@ -568,7 +597,8 @@ export class CreateTeamEventTypeInput_2024_06_14 extends BaseCreateEventTypeInpu
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
@DocsPropertyOptional({
|
||||
description: "If true, all current and future team members will be assigned to this event type. Provide either assignAllTeamMembers or hosts but not both",
|
||||
description:
|
||||
"If true, all current and future team members will be assigned to this event type. Provide either assignAllTeamMembers or hosts but not both",
|
||||
})
|
||||
assignAllTeamMembers?: boolean;
|
||||
|
||||
@@ -591,4 +621,20 @@ export class CreateTeamEventTypeInput_2024_06_14 extends BaseCreateEventTypeInpu
|
||||
})
|
||||
@Type(() => Object)
|
||||
locations?: InputTeamLocation_2024_06_14[];
|
||||
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@DocsPropertyOptional({
|
||||
description: "Email settings for this event type - only available for organization team event types.",
|
||||
type: () => EmailSettings_2024_06_14,
|
||||
})
|
||||
@Type(() => EmailSettings_2024_06_14)
|
||||
emailSettings?: EmailSettings_2024_06_14;
|
||||
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
@DocsPropertyOptional({
|
||||
description: "Rescheduled events will be assigned to the same host as initially scheduled.",
|
||||
})
|
||||
rescheduleWithSameRoundRobinHost?: boolean;
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
import { ApiPropertyOptional } from "@nestjs/swagger";
|
||||
import { IsBoolean, IsOptional } from "class-validator";
|
||||
|
||||
export class EmailSettings_2024_06_14 {
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
@ApiPropertyOptional({
|
||||
description:
|
||||
"Disables all email communication to attendees for this event type, including booking confirmations, reminders, and cancellations. This DOES NOT include emails sent by custom email workflows.",
|
||||
})
|
||||
disableEmailsToAttendees?: boolean;
|
||||
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
@ApiPropertyOptional({
|
||||
description:
|
||||
"Disables all email communication to hosts for this event type, including booking confirmations, reminders, and cancellations. This DOES NOT include emails sent by custom email workflows.",
|
||||
})
|
||||
disableEmailsToHosts?: boolean;
|
||||
}
|
||||
@@ -9,6 +9,7 @@ export * from "./update-event-type.input";
|
||||
export * from "./get-event-types-query.input";
|
||||
export * from "./booker-layouts.input";
|
||||
export * from "./confirmation-policy.input";
|
||||
export * from "./email-settings.input";
|
||||
export * from "./event-type-color.input";
|
||||
export * from "./seats.input";
|
||||
export * from "./destination-calendar.input";
|
||||
|
||||
+53
-10
@@ -1,9 +1,4 @@
|
||||
import {
|
||||
ApiProperty as DocsProperty,
|
||||
ApiPropertyOptional as DocsPropertyOptional,
|
||||
getSchemaPath,
|
||||
ApiExtraModels,
|
||||
} from "@nestjs/swagger";
|
||||
import { ApiPropertyOptional as DocsPropertyOptional, getSchemaPath, ApiExtraModels } from "@nestjs/swagger";
|
||||
import { Type, Transform } from "class-transformer";
|
||||
import {
|
||||
IsString,
|
||||
@@ -18,6 +13,8 @@ import {
|
||||
IsUrl,
|
||||
} from "class-validator";
|
||||
|
||||
import { RequiresAtLeastOnePropertyWhenNotDisabled } from "../../../utils/RequiresOneOfPropertiesWhenNotDisabled";
|
||||
import { BookerActiveBookingsLimit_2024_06_14 } from "./booker-active-booking-limit.input";
|
||||
import { BookerLayouts_2024_06_14 } from "./booker-layouts.input";
|
||||
import type { InputBookingField_2024_06_14 } from "./booking-fields.input";
|
||||
import {
|
||||
@@ -67,6 +64,7 @@ import {
|
||||
} from "./create-event-type.input";
|
||||
import { DestinationCalendar_2024_06_14 } from "./destination-calendar.input";
|
||||
import { Disabled_2024_06_14 } from "./disabled.input";
|
||||
import { EmailSettings_2024_06_14 } from "./email-settings.input";
|
||||
import { EventTypeColor_2024_06_14 } from "./event-type-color.input";
|
||||
import {
|
||||
InputAddressLocation_2024_06_14,
|
||||
@@ -83,6 +81,7 @@ import {
|
||||
import type { InputLocation_2024_06_14, InputTeamLocation_2024_06_14 } from "./locations.input";
|
||||
import { Recurrence_2024_06_14 } from "./recurrence.input";
|
||||
import { Seats_2024_06_14 } from "./seats.input";
|
||||
import { CantHaveRecurrenceAndBookerActiveBookingsLimit } from "./validators/CantHaveRecurrenceAndBookerActiveBookingsLimit";
|
||||
|
||||
@ApiExtraModels(
|
||||
InputAddressLocation_2024_06_14,
|
||||
@@ -118,8 +117,11 @@ import { Seats_2024_06_14 } from "./seats.input";
|
||||
LocationDefaultFieldInput_2024_06_14,
|
||||
NotesDefaultFieldInput_2024_06_14,
|
||||
GuestsDefaultFieldInput_2024_06_14,
|
||||
RescheduleReasonDefaultFieldInput_2024_06_14
|
||||
RescheduleReasonDefaultFieldInput_2024_06_14,
|
||||
BookerActiveBookingsLimit_2024_06_14,
|
||||
EmailSettings_2024_06_14
|
||||
)
|
||||
@CantHaveRecurrenceAndBookerActiveBookingsLimit()
|
||||
class BaseUpdateEventTypeInput {
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
@@ -243,6 +245,29 @@ class BaseUpdateEventTypeInput {
|
||||
@Type(() => Object)
|
||||
bookingLimitsCount?: BookingLimitsCount_2024_06_14;
|
||||
|
||||
@IsOptional()
|
||||
@Transform(({ value }) => {
|
||||
if (value && typeof value === "object") {
|
||||
if ("disabled" in value && value.disabled) {
|
||||
return Object.assign(new Disabled_2024_06_14(), value);
|
||||
} else {
|
||||
return Object.assign(new BookerActiveBookingsLimit_2024_06_14(), value);
|
||||
}
|
||||
}
|
||||
return value;
|
||||
})
|
||||
@ValidateNested()
|
||||
@RequiresAtLeastOnePropertyWhenNotDisabled()
|
||||
@DocsPropertyOptional({
|
||||
description: "Limit the number of active bookings a booker can make for this event type.",
|
||||
oneOf: [
|
||||
{ $ref: getSchemaPath(BookerActiveBookingsLimit_2024_06_14) },
|
||||
{ $ref: getSchemaPath(Disabled_2024_06_14) },
|
||||
],
|
||||
})
|
||||
@Type(() => Object)
|
||||
bookerActiveBookingsLimit?: BookerActiveBookingsLimit_2024_06_14 | Disabled_2024_06_14;
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
@DocsPropertyOptional({
|
||||
@@ -455,16 +480,18 @@ export class UpdateTeamEventTypeInput_2024_06_14 extends BaseUpdateEventTypeInpu
|
||||
@Type(() => Host)
|
||||
@IsArray()
|
||||
@IsOptional()
|
||||
@DocsPropertyOptional({ type: [Host],
|
||||
@DocsPropertyOptional({
|
||||
type: [Host],
|
||||
description:
|
||||
"Hosts contain specific team members you want to assign to this event type, but if you want to assign all team members, use `assignAllTeamMembers: true` instead and omit this field. For platform customers the hosts can include userIds only of managed users. Provide either hosts or assignAllTeamMembers but not both",
|
||||
})
|
||||
})
|
||||
hosts?: Host[];
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
@DocsPropertyOptional({
|
||||
description: "If true, all current and future team members will be assigned to this event type. Provide either assignAllTeamMembers or hosts but not both",
|
||||
description:
|
||||
"If true, all current and future team members will be assigned to this event type. Provide either assignAllTeamMembers or hosts but not both",
|
||||
})
|
||||
assignAllTeamMembers?: boolean;
|
||||
|
||||
@@ -487,4 +514,20 @@ export class UpdateTeamEventTypeInput_2024_06_14 extends BaseUpdateEventTypeInpu
|
||||
})
|
||||
@Type(() => Object)
|
||||
locations?: InputTeamLocation_2024_06_14[];
|
||||
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@DocsPropertyOptional({
|
||||
description: "Email settings for this event type. Only available for organization team event types.",
|
||||
type: () => EmailSettings_2024_06_14,
|
||||
})
|
||||
@Type(() => EmailSettings_2024_06_14)
|
||||
emailSettings?: EmailSettings_2024_06_14;
|
||||
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
@DocsPropertyOptional({
|
||||
description: "Rescheduled events will be assigned to the same host as initially scheduled.",
|
||||
})
|
||||
rescheduleWithSameRoundRobinHost?: boolean;
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
import { BadRequestException } from "@nestjs/common";
|
||||
import { ValidationOptions, registerDecorator, ValidationArguments } from "class-validator";
|
||||
|
||||
import type { BaseCreateEventTypeInput } from "../create-event-type.input";
|
||||
|
||||
export const FAILED_RECURRING_EVENT_TYPE_WITH_BOOKER_LIMITS_ERROR_MESSAGE =
|
||||
"Can't have `recurrence` and `bookerActiveBookingsLimit` enabled at the same time - recurring events do not support maximum active bookings limit setting.";
|
||||
|
||||
export function CantHaveRecurrenceAndBookerActiveBookingsLimit(validationOptions?: ValidationOptions) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return function (object: any) {
|
||||
registerDecorator({
|
||||
name: "cantHaveRecurrenceAndBookerActiveBookingsLimit",
|
||||
target: object,
|
||||
propertyName: "recurrence or bookerActiveBookingsLimit",
|
||||
options: validationOptions,
|
||||
constraints: [],
|
||||
validator: {
|
||||
validate(_: unknown, args: ValidationArguments) {
|
||||
const obj = args.object as BaseCreateEventTypeInput;
|
||||
|
||||
const hasActiveRecurrence = obj?.recurrence && "interval" in obj.recurrence;
|
||||
|
||||
const hasActiveBookerLimit =
|
||||
obj?.bookerActiveBookingsLimit &&
|
||||
("maximumActiveBookings" in obj.bookerActiveBookingsLimit ||
|
||||
"offerReschedule" in obj.bookerActiveBookingsLimit);
|
||||
|
||||
if (hasActiveRecurrence && hasActiveBookerLimit) {
|
||||
throw new BadRequestException(FAILED_RECURRING_EVENT_TYPE_WITH_BOOKER_LIMITS_ERROR_MESSAGE);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
defaultMessage(): string {
|
||||
return FAILED_RECURRING_EVENT_TYPE_WITH_BOOKER_LIMITS_ERROR_MESSAGE;
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
import { ApiPropertyOptional } from "@nestjs/swagger";
|
||||
import { IsBoolean, IsInt, IsOptional, Min } from "class-validator";
|
||||
|
||||
export class BookerActiveBookingsLimitOutput_2024_06_14 {
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
@ApiPropertyOptional({
|
||||
description: "The maximum number of active bookings a booker can have for this event type.",
|
||||
example: 3,
|
||||
})
|
||||
maximumActiveBookings?: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
@ApiPropertyOptional({
|
||||
description:
|
||||
"Whether to offer rescheduling the last active booking to the chosen time slot when limit is reached.",
|
||||
})
|
||||
offerReschedule?: boolean;
|
||||
}
|
||||
+25
-1
@@ -35,6 +35,7 @@ import type { BookingLimitsCount_2024_06_14 } from "../inputs/booking-limits-cou
|
||||
import type { ConfirmationPolicy_2024_06_14 } from "../inputs/confirmation-policy.input";
|
||||
import { DestinationCalendar_2024_06_14 } from "../inputs/destination-calendar.input";
|
||||
import type { Disabled_2024_06_14 } from "../inputs/disabled.input";
|
||||
import { EmailSettings_2024_06_14 } from "../inputs/email-settings.input";
|
||||
import {
|
||||
EmailDefaultFieldOutput_2024_06_14,
|
||||
NameDefaultFieldOutput_2024_06_14,
|
||||
@@ -56,6 +57,7 @@ import {
|
||||
TextFieldOutput_2024_06_14,
|
||||
UrlFieldOutput_2024_06_14,
|
||||
} from "../outputs/booking-fields.output";
|
||||
import { BookerActiveBookingsLimitOutput_2024_06_14 } from "./booker-active-bookings-limit.output";
|
||||
import type { OutputBookingField_2024_06_14 } from "./booking-fields.output";
|
||||
import { ValidateOutputBookingFields_2024_06_14 } from "./booking-fields.output";
|
||||
import type { OutputLocation_2024_06_14 } from "./locations.output";
|
||||
@@ -178,7 +180,8 @@ class EventTypeTeam {
|
||||
BaseBookingLimitsDuration_2024_06_14,
|
||||
BusinessDaysWindow_2024_06_14,
|
||||
CalendarDaysWindow_2024_06_14,
|
||||
RangeWindow_2024_06_14
|
||||
RangeWindow_2024_06_14,
|
||||
EmailSettings_2024_06_14
|
||||
)
|
||||
class BaseEventTypeOutput_2024_06_14 {
|
||||
@IsInt()
|
||||
@@ -346,6 +349,11 @@ class BaseEventTypeOutput_2024_06_14 {
|
||||
@ApiPropertyOptional()
|
||||
bookingLimitsCount?: BookingLimitsCount_2024_06_14;
|
||||
|
||||
@IsOptional()
|
||||
@Type(() => BookerActiveBookingsLimitOutput_2024_06_14)
|
||||
@ApiPropertyOptional({ type: BookerActiveBookingsLimitOutput_2024_06_14 })
|
||||
bookerActiveBookingsLimit?: BookerActiveBookingsLimitOutput_2024_06_14;
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
@ApiPropertyOptional()
|
||||
@@ -526,4 +534,20 @@ export class TeamEventTypeOutput_2024_06_14 extends BaseEventTypeOutput_2024_06_
|
||||
@Type(() => EventTypeTeam)
|
||||
@DocsProperty()
|
||||
team!: EventTypeTeam;
|
||||
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@Type(() => EmailSettings_2024_06_14)
|
||||
@ApiPropertyOptional({
|
||||
description: "Email settings for this event type. Only available for organization team event types.",
|
||||
type: () => EmailSettings_2024_06_14,
|
||||
})
|
||||
emailSettings?: EmailSettings_2024_06_14;
|
||||
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
@ApiPropertyOptional({
|
||||
description: "Rescheduled events will be assigned to the same host as initially scheduled.",
|
||||
})
|
||||
rescheduleWithSameRoundRobinHost?: boolean;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import { BadRequestException } from "@nestjs/common";
|
||||
import { ValidationOptions, registerDecorator } from "class-validator";
|
||||
|
||||
/**
|
||||
* note(Lauris): See example in event-types_2024_06_14/inputs/create-event-type.input.ts file bookerActiveBookingsLimit property.
|
||||
* This validator is used if type can be SomeClass | Disabled_2024_06_14 to ensure that if {disabled: false} is passed
|
||||
* meaning that the feature is enabled that other properties are provided too.
|
||||
*/
|
||||
|
||||
export const REQUIRES_AT_LEAST_ONE_PROPERTY_ERROR =
|
||||
"When disabled is false {disabled: false} at least one other property must be provided, because it means that the property is enabled but then other properties must be provided too.";
|
||||
|
||||
export function RequiresAtLeastOnePropertyWhenNotDisabled(validationOptions?: ValidationOptions) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
return function (object: any, propertyName: string) {
|
||||
registerDecorator({
|
||||
name: "requiresAtLeastOnePropertyWhenNotDisabled",
|
||||
target: object.constructor,
|
||||
propertyName: propertyName,
|
||||
options: validationOptions,
|
||||
constraints: [],
|
||||
validator: {
|
||||
validate(value: unknown) {
|
||||
if (!value || typeof value !== "object") {
|
||||
return true;
|
||||
}
|
||||
|
||||
const obj = value as Record<string, unknown>;
|
||||
|
||||
if (obj.disabled !== false) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const otherProperties = Object.keys(obj).filter((key) => key !== "disabled");
|
||||
const hasAtLeastOneOtherProperty = otherProperties.length > 0;
|
||||
|
||||
if (!hasAtLeastOneOtherProperty) {
|
||||
throw new BadRequestException(REQUIRES_AT_LEAST_ONE_PROPERTY_ERROR);
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
defaultMessage(): string {
|
||||
return REQUIRES_AT_LEAST_ONE_PROPERTY_ERROR;
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user