feat: v2 slots new version (#18758)

* refactor: version old slots

* feat: init new slots endpoints

* chore: range format

* fix: duratin

* fix: duratin

* test: slot releaseAt

* refactor: reserve slot response

* refactor variable name

* docs: have new slots controller in docs

* feat: crud for slots reservations

* refactor: use exclude all for response

* docs

* chore: slots input service

* refactor mini

* refactor: remove unused imports

* docs

* handle orgSlug for dynamic events

* refactor: correct name

* docs

* add optional organizationSlug to BySlug search

* refactor: slot output format

* refactor: return seated slot info

* docs

* rename functions

* refactor: slots seated response

* fix: handle same username in org and non org

* refactor: test format

* fix: allow reservationDuration only for authed requests

* chore: check if auth user has permissions to define custom reservation duration

* refactor: split e2e test files

* refactor: invalid request exception codes

* chore: old slots controller uses all old versions
This commit is contained in:
Lauris Skraucis authored and GitHub committed 2025-02-13 14:17:00 +00:00
1 parent 5a166ee430
commit 5dc81e8677
51 files changed
+13305 -1257

No files matched your search

+2
View File
@@ -57,12 +57,14 @@ export const VERSION_2024_06_14 = "2024-06-14";
export const VERSION_2024_06_11 = "2024-06-11";
export const VERSION_2024_04_15 = "2024-04-15";
export const VERSION_2024_08_13 = "2024-08-13";
export const VERSION_2024_09_04 = "2024-09-04";
export const API_VERSIONS = [
VERSION_2024_06_14,
VERSION_2024_06_11,
VERSION_2024_04_15,
VERSION_2024_08_13,
VERSION_2024_09_04,
] as const;
export type API_VERSIONS_ENUM = (typeof API_VERSIONS)[number];
+2
View File
@@ -0,0 +1,2 @@
export * from "./slots-2024-04-15";
export * from "./slots-2024-09-04";
@@ -0,0 +1 @@
export * from "./inputs";
@@ -14,7 +14,7 @@ import {
import { SlotFormat } from "@calcom/platform-enums";
export class GetAvailableSlotsInput {
export class GetAvailableSlotsInput_2024_04_15 {
@IsDateString()
@ApiProperty({
description: "Start date string starting from which to fetch slots in UTC timezone.",
@@ -131,7 +131,7 @@ export class GetAvailableSlotsInput {
teamMemberEmail?: string;
}
export class RemoveSelectedSlotInput {
export class RemoveSelectedSlotInput_2024_04_15 {
@IsString()
@IsOptional()
@ApiProperty({
@@ -142,7 +142,7 @@ export class RemoveSelectedSlotInput {
uid?: string;
}
export class ReserveSlotInput {
export class ReserveSlotInput_2024_04_15 {
@IsInt()
@ApiProperty({ description: "Event Type ID for which timeslot is being reserved.", example: 100 })
eventTypeId!: number;
@@ -0,0 +1,2 @@
export * from "./inputs";
export * from "./outputs";
@@ -0,0 +1,105 @@
import type { PipeTransform } from "@nestjs/common";
import { Injectable, BadRequestException } from "@nestjs/common";
import { plainToClass } from "class-transformer";
import type { ValidationError } from "class-validator";
import { validateSync } from "class-validator";
import { ById_2024_09_04, BySlug_2024_09_04 } from "./get-slots.input";
import { ByUsernames_2024_09_04 } from "./get-slots.input";
export type GetSlotsInput_2024_09_04 = ById_2024_09_04 | BySlug_2024_09_04 | ByUsernames_2024_09_04;
@Injectable()
export class GetSlotsInputPipe implements PipeTransform {
// note(Lauris): we need empty constructor otherwise v2 can't be started due to error:
// CreateBookingInputPipe is not a constructor
// eslint-disable-next-line @typescript-eslint/no-empty-function
constructor() {}
transform(value: GetSlotsInput_2024_09_04): GetSlotsInput_2024_09_04 {
if (!value) {
throw new BadRequestException("Body is required");
}
if (typeof value !== "object") {
throw new BadRequestException("Body should be an object");
}
if (this.isById(value)) {
return this.validateById(value);
}
if (this.isBySlug(value)) {
return this.validateBySlug(value);
}
return this.validateByUsernames(value);
}
validateById(value: ById_2024_09_04) {
const object = plainToClass(ById_2024_09_04, value);
const errors = validateSync(object, {
whitelist: true,
forbidNonWhitelisted: true,
skipMissingProperties: false,
});
if (errors.length > 0) {
throw new BadRequestException(this.formatErrors(errors));
}
return object;
}
validateBySlug(value: BySlug_2024_09_04) {
const object = plainToClass(BySlug_2024_09_04, value);
const errors = validateSync(object, {
whitelist: true,
forbidNonWhitelisted: true,
skipMissingProperties: false,
});
if (errors.length > 0) {
throw new BadRequestException(this.formatErrors(errors));
}
return object;
}
validateByUsernames(value: ByUsernames_2024_09_04) {
const object = plainToClass(ByUsernames_2024_09_04, value);
const errors = validateSync(object, {
whitelist: true,
forbidNonWhitelisted: true,
skipMissingProperties: false,
});
if (errors.length > 0) {
throw new BadRequestException(this.formatErrors(errors));
}
return object;
}
private formatErrors(errors: ValidationError[]): string {
return errors
.map((err) => {
const constraints = err.constraints ? Object.values(err.constraints).join(", ") : "";
const childrenErrors =
err.children && err.children.length > 0 ? `${this.formatErrors(err.children)}` : "";
return `${err.property} property is wrong,${constraints} ${childrenErrors}`;
})
.join(", ");
}
private isById(value: GetSlotsInput_2024_09_04): value is ById_2024_09_04 {
return value.hasOwnProperty("eventTypeId");
}
private isBySlug(value: GetSlotsInput_2024_09_04): value is BySlug_2024_09_04 {
return value.hasOwnProperty("eventTypeSlug");
}
}
@@ -0,0 +1,151 @@
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
import { Transform } from "class-transformer";
import {
IsDateString,
IsTimeZone,
IsOptional,
IsNumber,
IsString,
IsArray,
ArrayMinSize,
IsEnum,
} from "class-validator";
import { SlotFormat } from "@calcom/platform-enums";
class GetAvailableSlotsInput_2024_09_04 {
@IsDateString()
@ApiProperty({
type: String,
description: `
Time starting from which available slots should be checked.
Must be in UTC timezone as ISO 8601 datestring.
You can pass date without hours which defaults to start of day or specify hours:
2024-08-13 (will have hours 00:00:00 aka at very beginning of the date) or you can specify hours manually like 2024-08-13T09:00:00Z
`,
example: "2050-09-05",
})
start!: string;
@IsDateString()
@ApiProperty({
type: String,
description: `
Time until which available slots should be checked.
Must be in UTC timezone as ISO 8601 datestring.
You can pass date without hours which defaults to end of day or specify hours:
2024-08-20 (will have hours 23:59:59 aka at the very end of the date) or you can specify hours manually like 2024-08-20T18:00:00Z`,
example: "2050-09-06",
})
end!: string;
@IsTimeZone()
@IsOptional()
@ApiPropertyOptional({
type: String,
description: "Time zone in which the available slots should be returned. Defaults to UTC.",
example: "Europe/Rome",
})
timeZone?: string;
@Transform(({ value }: { value: string }) => value && parseInt(value))
@IsNumber()
@IsOptional()
@ApiPropertyOptional({
type: Number,
description:
"If event type has multiple possible durations then you can specify the desired duration here. Also, if you are fetching slots for a dynamic event then you can specify the duration her which defaults to 30, meaning that returned slots will be each 30 minutes long.",
example: "60",
})
duration?: number;
@IsString()
@IsEnum(SlotFormat, {
message: "slotFormat must be either 'range' or 'time'",
})
@Transform(({ value }) => {
if (!value) return undefined;
return value.toLowerCase();
})
@IsOptional()
@ApiPropertyOptional({
description: "Format of slot times in response. Use 'range' to get start and end times.",
example: "range",
enum: SlotFormat,
})
format?: SlotFormat;
}
export class ById_2024_09_04 extends GetAvailableSlotsInput_2024_09_04 {
@Transform(({ value }: { value: string }) => value && parseInt(value))
@IsNumber()
@ApiProperty({
type: Number,
description: "The ID of the event type for which available slots should be checked.",
example: "100",
})
eventTypeId!: number;
}
export class BySlug_2024_09_04 extends GetAvailableSlotsInput_2024_09_04 {
@IsString()
@ApiProperty({
type: String,
description: "The slug of the event type for which available slots should be checked.",
example: "event-type-slug",
})
eventTypeSlug!: string;
@IsString()
@ApiProperty({
type: String,
description:
"When searching by eventTypeSlug a username must be provided too aka username of the owner of the event type.",
example: "bob",
})
username!: string;
@IsString()
@IsOptional()
@ApiProperty({
type: String,
description:
"Organzation slug in which the slots of event type belonging to the specified username should be checked.",
example: "org-slug",
})
organizationSlug?: string;
}
export class ByUsernames_2024_09_04 extends GetAvailableSlotsInput_2024_09_04 {
@Transform(({ value }) => {
if (typeof value === "string") {
return value.split(",").map((username: string) => username.trim());
}
return value;
})
@IsArray()
@ArrayMinSize(2, { message: "The array must contain at least 2 elements." })
@IsString({ each: true })
@ApiProperty({
type: [String],
description: `The usernames for which available slots should be checked.
Checking slots by usernames is used mainly for dynamic event where there is no specific event but we just want to know when are 2 or more people available.
Must contain at least 2 usernames e.g. ?usernames=alice,bob`,
example: ["username1", "username2"],
})
usernames!: string[];
@IsString()
@ApiProperty({
type: String,
description: "Slug of the organization to which each user in the `usernames` array belongs.",
example: "org-slug",
})
organizationSlug!: string;
}
@@ -0,0 +1,3 @@
export * from "./reserve-slot.input";
export * from "./get-slots-input.pipe";
export * from "./get-slots.input";
@@ -0,0 +1,34 @@
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
import { IsInt, IsDateString, IsOptional } from "class-validator";
export class ReserveSlotInput_2024_09_04 {
@IsInt()
@ApiProperty({ example: 1, description: "The ID of the event type for which slot should be reserved." })
eventTypeId!: number;
@IsDateString()
@ApiProperty({
example: "2024-09-04T09:00:00Z",
description: "ISO 8601 datestring in UTC timezone representing available slot.",
})
slotStart!: string;
@IsInt()
@IsOptional()
@ApiPropertyOptional({
example: "30",
description:
"By default slot duration is equal to event type length, but if you want to reserve a slot for an event type that has a variable length you can specify it here as a number in minutes. If you don't have this set explicitly that event type can have one of many lengths you can omit this.",
})
slotDuration?: number;
@IsInt()
@IsOptional()
@ApiPropertyOptional({
example: 5,
description: `ONLY for authenticated requests with api key, access token or OAuth credentials (ID + secret).
For how many minutes the slot should be reserved - for this long time noone else can book this event type at \`start\` time. If not provided, defaults to 5 minutes.`,
})
reservationDuration?: number;
}
@@ -0,0 +1,50 @@
import { ApiProperty } from "@nestjs/swagger";
import { Expose } from "class-transformer";
import { IsDateString, IsString, IsInt } from "class-validator";
export class GetReservedSlotOutput_2024_09_04 {
@IsString()
@ApiProperty({
example: "e84be5a3-4696-49e3-acc7-b2f3999c3b94",
description: "The unique identifier of the reservation.",
})
@Expose()
reservationUid!: string;
@IsInt()
@ApiProperty({ example: 1, description: "The ID of the event type for which booking should be reserved." })
@Expose()
eventTypeId!: number;
@IsDateString()
@ApiProperty({
example: "2024-09-04T09:00:00Z",
description: "ISO 8601 datestring in UTC timezone representing available slot.",
})
@Expose()
slotStart!: string;
@IsDateString()
@ApiProperty({
example: "2024-09-04T10:00:00Z",
description: "ISO 8601 datestring in UTC timezone representing slot end.",
})
@Expose()
slotEnd!: string;
@IsInt()
@ApiProperty({
example: "30",
description: "Difference in minutes between slotStart and slotEnd.",
})
@Expose()
slotDuration!: number;
@IsDateString()
@ApiProperty({
example: "2023-09-04T10:00:00Z",
description: "ISO 8601 datestring in UTC timezone representing time until which the slot is reserved.",
})
@Expose()
reservationUntil!: string;
}
@@ -0,0 +1,3 @@
export * from "./slots.output";
export * from "./reserve-slot.output";
export * from "./get-reserved-slot.output";
@@ -0,0 +1,60 @@
import { ApiProperty } from "@nestjs/swagger";
import { Expose } from "class-transformer";
import { IsDateString, IsInt, IsString } from "class-validator";
export class ReserveSlotOutput_2024_09_04 {
@IsInt()
@ApiProperty({ example: 1, description: "The ID of the event type for which slot was reserved." })
@Expose()
eventTypeId!: number;
@IsDateString()
@ApiProperty({
example: "2024-09-04T09:00:00Z",
description: "ISO 8601 datestring in UTC timezone representing available slot.",
})
@Expose()
slotStart!: string;
@IsDateString()
@ApiProperty({
example: "2024-09-04T10:00:00Z",
description: "ISO 8601 datestring in UTC timezone representing slot end.",
})
@Expose()
slotEnd!: string;
@IsInt()
@ApiProperty({
example: "30",
description:
"By default slot duration is equal to event type length, but if you want to reserve a slot for an event type that has a variable length you can specify it here. If you don't have this set explicitly that event type can have one of many lengths you can omit this.",
})
@Expose()
slotDuration!: number;
@IsString()
@ApiProperty({
example: "e84be5a3-4696-49e3-acc7-b2f3999c3b94",
description: "The unique identifier of the reservation. Use it to update, get or delete the reservation.",
})
@Expose()
reservationUid!: string;
@IsInt()
@ApiProperty({
example: 5,
description:
"For how many minutes the slot is reserved - for this long time noone else can book this event type at `start` time.",
})
@Expose()
reservationDuration!: number;
@IsDateString()
@ApiProperty({
example: "2023-09-04T10:00:00Z",
description: "ISO 8601 datestring in UTC timezone representing time until which the slot is reserved.",
})
@Expose()
reservationUntil!: string;
}
@@ -0,0 +1,57 @@
import { ApiProperty } from "@nestjs/swagger";
import { IsDateString, IsInt, IsString } from "class-validator";
export class Slot_2024_09_04 {
@ApiProperty({ description: "Start time of slot." })
@IsDateString()
start!: string;
}
export class SeatedSlot_2024_09_04 extends Slot_2024_09_04 {
@ApiProperty({ description: "How many attendees are attending seated event at this slot." })
@IsInt()
seatsBooked!: number;
@ApiProperty({ description: "How many seats are remaining at this slot." })
@IsInt()
seatsRemaining!: number;
@ApiProperty({ description: "Total number of seats for the event type" })
@IsInt()
seatsTotal!: number;
@ApiProperty({ description: "Unique identifier of the booking of the seated event." })
@IsString()
bookingUid?: string;
}
export class SlotsOutput_2024_09_04 {
[key: string]: (Slot_2024_09_04 | SeatedSlot_2024_09_04)[];
}
export class RangeSlot_2024_09_04 extends Slot_2024_09_04 {
@ApiProperty({ description: "End time of slot." })
@IsDateString()
end!: string;
}
export class SeatedRangeSlot_2024_09_04 extends RangeSlot_2024_09_04 {
@ApiProperty({ description: "How many attendees are attending seated event at this slot." })
@IsInt()
seatsBooked!: number;
@ApiProperty({ description: "How many seats are remaining at this slot." })
@IsInt()
seatsRemaining!: number;
@ApiProperty({ description: "Total number of seats for the event type" })
@IsInt()
seatsTotal!: number;
@ApiProperty({ description: "Unique identifier of the booking of the seated event." })
@IsString()
bookingUid?: string;
}
export class RangeSlotsOutput_2024_09_04 {
[key: string]: (RangeSlot_2024_09_04 | SeatedRangeSlot_2024_09_04)[];
}