feat: add pagination to get bookings endpoints [v2] (#21173)

* feat: add pagination to get bookings endpoints [v2]

* add decorators for pagination

* fix: ensure correct values

* code review comment

* fixup! code review comment
This commit is contained in:
Morgan authored and GitHub committed 2025-05-12 10:09:56 +02:00
1 parent 1277f5e9f7
commit 77054d92df
10 files changed
+242 -13

No files matched your search

@@ -258,7 +258,7 @@ export class GetBookingsInput_2024_08_13 {
sortUpdatedAt?: SortOrderType;
// note(Lauris): pagination
@ApiProperty({ required: false, description: "The number of items to return", example: 10 })
@ApiProperty({ required: false, description: "The number of items to return", example: 10, default: 100 })
@Transform(({ value }: { value: string }) => value && parseInt(value))
@IsNumber()
@Min(1)
@@ -266,7 +266,7 @@ export class GetBookingsInput_2024_08_13 {
@IsOptional()
take?: number;
@ApiProperty({ required: false, description: "The number of items to skip", example: 0 })
@ApiProperty({ required: false, description: "The number of items to skip", example: 0, default: 0 })
@Transform(({ value }: { value: string }) => value && parseInt(value))
@IsNumber()
@Min(0)
@@ -1,5 +1,6 @@
import { ApiProperty, ApiExtraModels, getSchemaPath } from "@nestjs/swagger";
import { IsEnum, ValidateNested } from "class-validator";
import { Type } from "class-transformer";
import { IsBoolean, IsEnum, IsInt, Min, ValidateNested } from "class-validator";
import { SUCCESS_STATUS, ERROR_STATUS } from "@calcom/platform-constants";
import {
@@ -9,6 +10,68 @@ import {
RecurringBookingOutput_2024_08_13,
} from "@calcom/platform-types";
export class PaginationMetaDto {
@ApiProperty({
description: "The total number of items available across all pages, matching the query criteria.",
example: 123,
minimum: 0,
})
@IsInt()
@Min(0)
totalItems!: number;
@ApiProperty({
description:
"The number of items remaining to be fetched *after* the current page. Calculated as: `totalItems - (skip + itemsPerPage)`.",
example: 103, // e.g., if totalItems=123, skip=10, itemsPerPage=10 -> 123 - (10 + 10) = 103
minimum: 0,
})
@IsInt()
@Min(0)
remainingItems!: number;
@ApiProperty({
description: "The maximum number of items requested per page.",
example: 10,
minimum: 1,
})
@IsInt()
@Min(1) // Typically, you request at least 1 item per page
itemsPerPage!: number;
@ApiProperty({
description: "The current page number being returned.",
example: 2, // e.g., if skip=10, itemsPerPage=10 -> page 2
minimum: 1,
})
@IsInt()
@Min(1) // Page numbers usually start from 1
currentPage!: number;
@ApiProperty({
description: "The total number of pages available.",
example: 13, // e.g., if totalItems=123, itemsPerPage=10 -> 13 pages
minimum: 0, // Can be 0 if totalItems is 0
})
@IsInt()
@Min(0)
totalPages!: number;
@ApiProperty({
description: "Indicates if there is a subsequent page available after the current one.",
example: true,
})
@IsBoolean()
hasNextPage!: boolean;
@ApiProperty({
description: "Indicates if there is a preceding page available before the current one.",
example: true,
})
@IsBoolean()
hasPreviousPage!: boolean;
}
@ApiExtraModels(
BookingOutput_2024_08_13,
RecurringBookingOutput_2024_08_13,
@@ -33,13 +96,16 @@ export class GetBookingsOutput_2024_08_13 {
description:
"Array of booking data, which can contain either BookingOutput objects or RecurringBookingOutput objects",
})
@ValidateNested({ each: true })
data!: (
| BookingOutput_2024_08_13
| RecurringBookingOutput_2024_08_13
| GetSeatedBookingOutput_2024_08_13
| GetRecurringSeatedBookingOutput_2024_08_13
)[];
error?: Error;
@ApiProperty({ type: () => PaginationMetaDto }) // Crucial for Swagger
@Type(() => PaginationMetaDto)
@ValidateNested()
pagination!: PaginationMetaDto;
}