refactor: v2 calendars busy-times (#18244)
* refactor: make dateFrom and dateTo required * refactor: make dateFrom and dateTo required * refactor: useCalendarsBusyTimes enabled only if dateFrom & dateTo passed
This commit is contained in:
@@ -86,18 +86,16 @@ export class CalendarsController {
|
||||
|
||||
@UseGuards(ApiAuthGuard)
|
||||
@Get("/busy-times")
|
||||
@ApiOperation({ summary: "Get busy times" })
|
||||
@ApiOperation({
|
||||
summary: "Get busy times",
|
||||
description:
|
||||
"Get busy times from a calendar. Example request URL is `https://api.cal.com/v2/calendars/busy-times?loggedInUsersTz=Europe%2FMadrid&dateFrom=2024-12-18&dateTo=2024-12-18&calendarsToLoad[0][credentialId]=135&calendarsToLoad[0][externalId]=skrauciz%40gmail.com`",
|
||||
})
|
||||
async getBusyTimes(
|
||||
@Query() queryParams: CalendarBusyTimesInput,
|
||||
@GetUser() user: UserWithProfile
|
||||
): Promise<GetBusyTimesOutput> {
|
||||
const { loggedInUsersTz, dateFrom, dateTo, calendarsToLoad } = queryParams;
|
||||
if (!dateFrom || !dateTo) {
|
||||
return {
|
||||
status: SUCCESS_STATUS,
|
||||
data: [],
|
||||
};
|
||||
}
|
||||
|
||||
const busyTimes = await this.calendarsService.getBusyTimes(
|
||||
calendarsToLoad,
|
||||
|
||||
@@ -3592,6 +3592,7 @@
|
||||
"get": {
|
||||
"operationId": "CalendarsController_getBusyTimes",
|
||||
"summary": "Get busy times",
|
||||
"description": "Get busy times from a calendar. Example request URL is `https://api.cal.com/v2/calendars/busy-times?loggedInUsersTz=Europe%2FMadrid&dateFrom=2024-12-18&dateTo=2024-12-18&calendarsToLoad[0][credentialId]=135&calendarsToLoad[0][externalId]=skrauciz%40gmail.com`",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "loggedInUsersTz",
|
||||
@@ -3603,6 +3604,26 @@
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "dateFrom",
|
||||
"required": false,
|
||||
"in": "query",
|
||||
"description": "The starting date for the busy times query",
|
||||
"example": "2023-10-01",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "dateTo",
|
||||
"required": false,
|
||||
"in": "query",
|
||||
"description": "The ending date for the busy times query",
|
||||
"example": "2023-10-31",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "credentialId",
|
||||
"in": "query",
|
||||
|
||||
@@ -3408,6 +3408,7 @@
|
||||
"get": {
|
||||
"operationId": "CalendarsController_getBusyTimes",
|
||||
"summary": "Get busy times",
|
||||
"description": "Get busy times from a calendar. Example request URL is `https://api.cal.com/v2/calendars/busy-times?loggedInUsersTz=Europe%2FMadrid&dateFrom=2024-12-18&dateTo=2024-12-18&calendarsToLoad[0][credentialId]=135&calendarsToLoad[0][externalId]=skrauciz%40gmail.com`",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "loggedInUsersTz",
|
||||
@@ -3419,6 +3420,26 @@
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "dateFrom",
|
||||
"required": false,
|
||||
"in": "query",
|
||||
"description": "The starting date for the busy times query",
|
||||
"example": "2023-10-01",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "dateTo",
|
||||
"required": false,
|
||||
"in": "query",
|
||||
"description": "The ending date for the busy times query",
|
||||
"example": "2023-10-31",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "credentialId",
|
||||
"in": "query",
|
||||
|
||||
@@ -8,7 +8,10 @@ import http from "../lib/http";
|
||||
|
||||
export const QUERY_KEY = "get-calendars-busy-times";
|
||||
|
||||
type UseCalendarsBusyTimesProps = CalendarBusyTimesInput & { onError?: () => void; enabled: boolean };
|
||||
type UseCalendarsBusyTimesProps = Omit<CalendarBusyTimesInput, "dateFrom" | "dateTo"> & {
|
||||
dateFrom: string | null;
|
||||
dateTo: string | null;
|
||||
} & { onError?: () => void; enabled: boolean };
|
||||
|
||||
export const useCalendarsBusyTimes = ({ onError, enabled, ...rest }: UseCalendarsBusyTimesProps) => {
|
||||
const availableSlots = useQuery({
|
||||
@@ -32,7 +35,7 @@ export const useCalendarsBusyTimes = ({ onError, enabled, ...rest }: UseCalendar
|
||||
throw new Error(res.data.error.message);
|
||||
});
|
||||
},
|
||||
enabled,
|
||||
enabled: enabled && !!rest.dateFrom && !!rest.dateTo,
|
||||
});
|
||||
return availableSlots;
|
||||
};
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { Type } from "class-transformer";
|
||||
import { Transform } from "class-transformer";
|
||||
import { IsNumber, IsString, IsOptional, IsArray, ValidateNested, Validate } from "class-validator";
|
||||
|
||||
import { IsYearMonthDays } from "./validators/isYearMonthDays";
|
||||
import { IsNumber, IsString, IsArray, ValidateNested, IsDateString } from "class-validator";
|
||||
|
||||
export class Calendar {
|
||||
@Transform(({ value }: { value: string }) => value && parseInt(value))
|
||||
@@ -29,23 +27,19 @@ export class CalendarBusyTimesInput {
|
||||
required: false,
|
||||
description: "The starting date for the busy times query",
|
||||
example: "2023-10-01",
|
||||
nullable: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@Validate(IsYearMonthDays)
|
||||
dateFrom?: string | null;
|
||||
@IsDateString()
|
||||
dateFrom!: string;
|
||||
|
||||
@ApiProperty({
|
||||
required: false,
|
||||
description: "The ending date for the busy times query",
|
||||
example: "2023-10-31",
|
||||
nullable: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@Validate(IsYearMonthDays)
|
||||
dateTo?: string | null;
|
||||
@IsDateString()
|
||||
dateTo!: string;
|
||||
|
||||
@ApiProperty({
|
||||
type: [Calendar],
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
import type { ValidatorConstraintInterface } from "class-validator";
|
||||
import { ValidatorConstraint } from "class-validator";
|
||||
import { DateTime } from "luxon";
|
||||
|
||||
@ValidatorConstraint({ name: "IsYearMonthDays", async: false })
|
||||
export class IsYearMonthDays implements ValidatorConstraintInterface {
|
||||
validate(dateString: string): boolean {
|
||||
try {
|
||||
// Attempt to parse the date string
|
||||
const dateTime = DateTime.fromFormat(dateString, "yyyy-MM-dd");
|
||||
|
||||
// Check if the parsed date is valid
|
||||
return dateTime.isValid;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
defaultMessage() {
|
||||
return "time format must be YEAR-MONTH-DAYS (e.g. 2022-01-01)";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user