feat: extend api/v2/event-types (#16128)

* feat: extended api/v2/event-types to add more attributes

- additional attributes added
* bookingLimits
* onlyShowFirstAvailableSlot
* durationLimits
* offsetStart
* bookingWindow

* add swagger docs

* type fix

* extend api/v2/teams/[teamId]/event-types

* added tests

* extend api/v2/orgs/[orgId]/teams/[teamid]/event-types

* update: added tests to get and update event-type as well

* rename variables

- bookingLimits -> bookingLimitsCount
- durationLimits -> bookingLimitsDuration

* refactor: rename bookingLimits and durationLimits, add descriptions

- Renamed `bookingLimits` to `bookingLimitsCount`
- Renamed `durationLimits` to `bookingLimitsDuration`
- Added descriptions with examples to `bookingLimitsCount` and `bookingLimitsDuration`

* feat: add unit tests for transformation logic in api-request and api-response

- Added tests for `transformApiEventTypeIntervalLimits`
- Added tests for `transformApiEventTypeFutureBookingLimits`

* fix: type errors

* feat: added custom validators for BookingLimitsCount and BookingLimitsDuration

* fix: unit-tests

* Update CHANGELOG.md

* refactor: update imports to use platform-libraries from @calcom/platform-libraries-1.2.3

* fix: prevent double transformation of bookingWindow and bookingFields

- resolving e2e test failures

---------

Co-authored-by: Morgan <[email protected]>
This commit is contained in:
Somay ChauhanandMorgan authored and GitHub committed 2024-08-16 09:25:34 +00:00
1 parent 894c0fc8a7
commit 8fbc76d59a
28 files changed
+1261 -5956

No files matched your search

@@ -1,12 +1,19 @@
import { describe, expect, it } from "vitest";
import type { BookingField_2024_06_14, Location_2024_06_14 } from "@calcom/platform-types";
import type {
BookingField_2024_06_14,
Location_2024_06_14,
BookingLimitsCount_2024_06_14,
BookingWindow_2024_06_14,
} from "@calcom/platform-types";
import type { UserField, OptionsField } from "./api-request";
import type { UserField } from "./api-request";
import {
transformApiEventTypeLocations,
transformApiEventTypeBookingFields,
transformSelectOptions,
transformApiEventTypeIntervalLimits,
transformApiEventTypeFutureBookingLimits,
} from "./api-request";
describe("transformApiEventTypeLocations", () => {
@@ -336,7 +343,7 @@ describe("transformApiEventTypeBookingFields", () => {
const input: BookingField_2024_06_14[] = [bookingField];
const expectedOutput: OptionsField[] = [
const expectedOutput: UserField[] = [
{
name: bookingField.slug,
type: bookingField.type,
@@ -372,7 +379,7 @@ describe("transformApiEventTypeBookingFields", () => {
const input: BookingField_2024_06_14[] = [bookingField];
const expectedOutput: OptionsField[] = [
const expectedOutput: UserField[] = [
{
name: bookingField.slug,
type: bookingField.type,
@@ -443,7 +450,7 @@ describe("transformApiEventTypeBookingFields", () => {
const input: BookingField_2024_06_14[] = [bookingField];
const expectedOutput: OptionsField[] = [
const expectedOutput: UserField[] = [
{
name: bookingField.slug,
type: bookingField.type,
@@ -479,7 +486,7 @@ describe("transformApiEventTypeBookingFields", () => {
const input: BookingField_2024_06_14[] = [bookingField];
const expectedOutput: OptionsField[] = [
const expectedOutput: UserField[] = [
{
name: bookingField.slug,
type: bookingField.type,
@@ -538,3 +545,111 @@ describe("transformApiEventTypeBookingFields", () => {
expect(result).toEqual(expectedOutput);
});
});
describe("transformApiEventTypeIntervalLimits", () => {
it("should transform booking limits count or booking limits duration", () => {
const input: BookingLimitsCount_2024_06_14 = {
day: 2,
week: 11,
month: 22,
year: 33,
};
const expectedOutput = {
PER_DAY: 2,
PER_WEEK: 11,
PER_MONTH: 22,
PER_YEAR: 33,
};
const result = transformApiEventTypeIntervalLimits(input);
expect(result).toEqual(expectedOutput);
});
});
describe("transformApiEventTypeFutureBookingLimits", () => {
it("should transform range type", () => {
const input: BookingWindow_2024_06_14 = {
type: "range",
value: ["2024-08-06", "2024-08-28"],
};
const expectedOutput = {
periodType: "RANGE",
periodStartDate: new Date("2024-08-06"),
periodEndDate: new Date("2024-08-28"),
};
const result = transformApiEventTypeFutureBookingLimits(input);
expect(result).toEqual(expectedOutput);
});
it("should transform calendar days", () => {
const input: BookingWindow_2024_06_14 = {
type: "calendarDays",
value: 30,
rolling: false,
};
const expectedOutput = {
periodType: "ROLLING",
periodDays: 30,
periodCountCalendarDays: true,
};
const result = transformApiEventTypeFutureBookingLimits(input);
expect(result).toEqual(expectedOutput);
});
it("should transform calendar days rolling-window", () => {
const input: BookingWindow_2024_06_14 = {
type: "calendarDays",
value: 30,
rolling: true,
};
const expectedOutput = {
periodType: "ROLLING_WINDOW",
periodDays: 30,
periodCountCalendarDays: true,
};
const result = transformApiEventTypeFutureBookingLimits(input);
expect(result).toEqual(expectedOutput);
});
it("should transform Business days", () => {
const input: BookingWindow_2024_06_14 = {
type: "businessDays",
value: 30,
rolling: false,
};
const expectedOutput = {
periodType: "ROLLING",
periodDays: 30,
periodCountCalendarDays: false,
};
const result = transformApiEventTypeFutureBookingLimits(input);
expect(result).toEqual(expectedOutput);
});
it("should transform Business days rolling-window", () => {
const input: BookingWindow_2024_06_14 = {
type: "businessDays",
value: 30,
rolling: true,
};
const expectedOutput = {
periodType: "ROLLING_WINDOW",
periodDays: 30,
periodCountCalendarDays: false,
};
const result = transformApiEventTypeFutureBookingLimits(input);
expect(result).toEqual(expectedOutput);
});
});
@@ -1,6 +1,21 @@
import { z } from "zod";
import type { CreateEventTypeInput_2024_06_14, Integration_2024_06_14 } from "@calcom/platform-types";
import type {
BookingLimitsKeyOutputType_2024_06_14,
TransformBookingLimitsSchema_2024_06_14,
} from "@calcom/platform-types";
import {
type CreateEventTypeInput_2024_06_14,
type Integration_2024_06_14,
type BusinessDaysWindow_2024_06_14,
type RangeWindow_2024_06_14,
type TransformFutureBookingsLimitSchema_2024_06_14,
} from "@calcom/platform-types";
import {
BookingWindowPeriodInputTypeEnum_2024_06_14,
BookingWindowPeriodOutputTypeEnum_2024_06_14,
} from "@calcom/platform-types/event-types/event-types_2024_06_14/inputs/enums/booking-window.enum";
import { BookingLimitsEnum_2024_06_14 } from "@calcom/platform-types/event-types/event-types_2024_06_14/inputs/enums/interval-limits.enum";
const integrationsMapping: Record<Integration_2024_06_14, string> = {
"cal-video": "integrations:daily",
@@ -102,6 +117,51 @@ function transformApiEventTypeBookingFields(
return customBookingFields;
}
function transformApiEventTypeIntervalLimits(
inputBookingLimits: CreateEventTypeInput_2024_06_14["bookingLimitsCount"]
) {
const res: TransformBookingLimitsSchema_2024_06_14 = {};
inputBookingLimits &&
Object.entries(inputBookingLimits).map(([key, value]) => {
const outputKey: BookingLimitsKeyOutputType_2024_06_14 = BookingLimitsEnum_2024_06_14[
key as keyof typeof BookingLimitsEnum_2024_06_14
] as BookingLimitsKeyOutputType_2024_06_14;
res[outputKey] = value;
});
return res;
}
function transformApiEventTypeFutureBookingLimits(
inputBookingLimits: CreateEventTypeInput_2024_06_14["bookingWindow"]
): TransformFutureBookingsLimitSchema_2024_06_14 | undefined {
switch (inputBookingLimits?.type) {
case BookingWindowPeriodInputTypeEnum_2024_06_14.businessDays:
return {
periodDays: (inputBookingLimits as BusinessDaysWindow_2024_06_14).value,
periodType: !!(inputBookingLimits as BusinessDaysWindow_2024_06_14).rolling
? BookingWindowPeriodOutputTypeEnum_2024_06_14.ROLLING_WINDOW
: BookingWindowPeriodOutputTypeEnum_2024_06_14.ROLLING,
periodCountCalendarDays: false,
};
case BookingWindowPeriodInputTypeEnum_2024_06_14.calendarDays:
return {
periodDays: (inputBookingLimits as BusinessDaysWindow_2024_06_14).value,
periodType: !!(inputBookingLimits as BusinessDaysWindow_2024_06_14).rolling
? BookingWindowPeriodOutputTypeEnum_2024_06_14.ROLLING_WINDOW
: BookingWindowPeriodOutputTypeEnum_2024_06_14.ROLLING,
periodCountCalendarDays: true,
};
case BookingWindowPeriodInputTypeEnum_2024_06_14.range:
return {
periodType: BookingWindowPeriodOutputTypeEnum_2024_06_14.RANGE,
periodStartDate: new Date((inputBookingLimits as RangeWindow_2024_06_14).value[0]),
periodEndDate: new Date((inputBookingLimits as RangeWindow_2024_06_14).value[1]),
};
default:
return undefined;
}
}
export function transformSelectOptions(options: string[]) {
return options.map((option) => ({
label: option,
@@ -199,4 +259,9 @@ export type UserField = z.infer<typeof UserFieldsSchema>;
export const BookingFieldsSchema = z.array(z.union([UserFieldsSchema, SystemFieldsSchema]));
export { transformApiEventTypeLocations, transformApiEventTypeBookingFields };
export {
transformApiEventTypeLocations,
transformApiEventTypeBookingFields,
transformApiEventTypeIntervalLimits,
transformApiEventTypeFutureBookingLimits,
};
@@ -5,10 +5,17 @@ import type {
LinkLocation_2024_06_14,
PhoneLocation_2024_06_14,
IntegrationLocation_2024_06_14,
TransformBookingLimitsSchema_2024_06_14,
TransformFutureBookingsLimitSchema_2024_06_14,
} from "@calcom/platform-types";
import type { UserField, OptionsField } from "./api-request";
import { getResponseEventTypeLocations, getResponseEventTypeBookingFields } from "./api-response";
import type { UserField } from "./api-request";
import {
getResponseEventTypeLocations,
getResponseEventTypeBookingFields,
getResponseEventTypeIntervalLimits,
getResponseEventTypeFutureBookingLimits,
} from "./api-response";
describe("getResponseEventTypeLocations", () => {
it("should reverse transform address location", () => {
@@ -344,7 +351,7 @@ describe("getResponseEventTypeBookingFields", () => {
});
it("should reverse transform select field", () => {
const transformedField: OptionsField[] = [
const transformedField: UserField[] = [
{
name: "your-select",
type: "select",
@@ -384,7 +391,7 @@ describe("getResponseEventTypeBookingFields", () => {
});
it("should reverse transform multiselect field", () => {
const transformedField: OptionsField[] = [
const transformedField: UserField[] = [
{
name: "your-multiselect",
type: "multiselect",
@@ -457,7 +464,7 @@ describe("getResponseEventTypeBookingFields", () => {
});
it("should reverse transform checkbox field", () => {
const transformedField: OptionsField[] = [
const transformedField: UserField[] = [
{
name: "your-checkbox",
type: "checkbox",
@@ -495,7 +502,7 @@ describe("getResponseEventTypeBookingFields", () => {
});
it("should reverse transform radio field", () => {
const transformedField: OptionsField[] = [
const transformedField: UserField[] = [
{
name: "your-radio",
type: "radio",
@@ -566,3 +573,109 @@ describe("getResponseEventTypeBookingFields", () => {
expect(result).toEqual(expectedOutput);
});
});
describe("transformApiEventTypeIntervalLimits", () => {
it("should reverse transform booking limits count or booking limits duration", () => {
const transformedField: TransformBookingLimitsSchema_2024_06_14 = {
PER_DAY: 2,
PER_WEEK: 11,
PER_MONTH: 22,
PER_YEAR: 33,
};
const expectedOutput = {
day: 2,
week: 11,
month: 22,
year: 33,
};
const result = getResponseEventTypeIntervalLimits(transformedField);
expect(result).toEqual(expectedOutput);
});
});
describe("transformApiEventTypeFutureBookingLimits", () => {
it("should reverse transform range type", () => {
const transformedField: TransformFutureBookingsLimitSchema_2024_06_14 = {
periodType: "RANGE",
periodStartDate: new Date("2024-08-06T09:14:30.000Z"),
periodEndDate: new Date("2024-08-28T18:30:00.000Z"),
};
const expectedOutput = {
type: "range",
value: ["2024-08-06", "2024-08-28"],
};
const result = getResponseEventTypeFutureBookingLimits(transformedField);
expect(result).toEqual(expectedOutput);
});
it("should reverse transform calendar days", () => {
const transformedField: TransformFutureBookingsLimitSchema_2024_06_14 = {
periodType: "ROLLING",
periodDays: 30,
periodCountCalendarDays: true,
};
const expectedOutput = {
type: "calendarDays",
value: 30,
rolling: false,
};
const result = getResponseEventTypeFutureBookingLimits(transformedField);
expect(result).toEqual(expectedOutput);
});
it("should reverse transform calendar days rolling-window", () => {
const transformedField: TransformFutureBookingsLimitSchema_2024_06_14 = {
periodType: "ROLLING_WINDOW",
periodDays: 30,
periodCountCalendarDays: true,
};
const expectedOutput = {
type: "calendarDays",
value: 30,
rolling: true,
};
const result = getResponseEventTypeFutureBookingLimits(transformedField);
expect(result).toEqual(expectedOutput);
});
it("should reverse transform Business days", () => {
const transformedField: TransformFutureBookingsLimitSchema_2024_06_14 = {
periodType: "ROLLING",
periodDays: 30,
periodCountCalendarDays: false,
};
const expectedOutput = {
type: "businessDays",
value: 30,
rolling: false,
};
const result = getResponseEventTypeFutureBookingLimits(transformedField);
expect(result).toEqual(expectedOutput);
});
it("should reverse transform Business days rolling-window", () => {
const transformedField: TransformFutureBookingsLimitSchema_2024_06_14 = {
periodType: "ROLLING_WINDOW",
periodDays: 30,
periodCountCalendarDays: false,
};
const expectedOutput = {
type: "businessDays",
value: 30,
rolling: true,
};
const result = getResponseEventTypeFutureBookingLimits(transformedField);
expect(result).toEqual(expectedOutput);
});
});
@@ -4,8 +4,20 @@ import type {
LinkLocation_2024_06_14,
PhoneLocation_2024_06_14,
Integration_2024_06_14,
BookingLimitsKeysInputType,
TransformBookingLimitsSchema_2024_06_14,
TransformFutureBookingsLimitSchema_2024_06_14,
BookingWindow_2024_06_14,
RangeWindow_2024_06_14,
CalendarDaysWindow_2024_06_14,
BusinessDaysWindow_2024_06_14,
BookingField_2024_06_14,
} from "@calcom/platform-types";
import {
BookingWindowPeriodInputTypeEnum_2024_06_14,
BookingWindowPeriodOutputTypeEnum_2024_06_14,
} from "@calcom/platform-types/event-types/event-types_2024_06_14/inputs/enums/booking-window.enum";
import { BookingLimitsEnum_2024_06_14 } from "@calcom/platform-types/event-types/event-types_2024_06_14/inputs/enums/interval-limits.enum";
import type { transformApiEventTypeBookingFields, transformApiEventTypeLocations } from "./api-request";
@@ -189,4 +201,63 @@ function getResponseEventTypeBookingFields(
});
}
export { getResponseEventTypeLocations, getResponseEventTypeBookingFields };
function getResponseEventTypeIntervalLimits(
transformedBookingFields: TransformBookingLimitsSchema_2024_06_14 | null
) {
if (!transformedBookingFields) {
return undefined;
}
const res: { [K in BookingLimitsKeysInputType]?: number } = {};
transformedBookingFields &&
Object.entries(transformedBookingFields).map(([key, value]) => {
const outputKey: BookingLimitsKeysInputType | undefined = Object.keys(
BookingLimitsEnum_2024_06_14
).find(
(item) => BookingLimitsEnum_2024_06_14[item as keyof typeof BookingLimitsEnum_2024_06_14] === key
) as BookingLimitsKeysInputType;
if (outputKey) {
res[outputKey] = value as number;
}
});
return res;
}
function getResponseEventTypeFutureBookingLimits(
transformedFutureBookingsLimitsFields: TransformFutureBookingsLimitSchema_2024_06_14
): BookingWindow_2024_06_14 | undefined {
switch (transformedFutureBookingsLimitsFields?.periodType) {
case BookingWindowPeriodOutputTypeEnum_2024_06_14.RANGE:
return {
type: BookingWindowPeriodInputTypeEnum_2024_06_14.range,
value: [
transformedFutureBookingsLimitsFields?.periodStartDate?.toISOString().split("T")[0],
transformedFutureBookingsLimitsFields?.periodEndDate?.toISOString().split("T")[0],
],
} as RangeWindow_2024_06_14;
case BookingWindowPeriodOutputTypeEnum_2024_06_14.ROLLING_WINDOW:
return {
type: transformedFutureBookingsLimitsFields.periodCountCalendarDays
? BookingWindowPeriodInputTypeEnum_2024_06_14.calendarDays
: BookingWindowPeriodInputTypeEnum_2024_06_14.businessDays,
value: transformedFutureBookingsLimitsFields.periodDays,
rolling: true,
} as CalendarDaysWindow_2024_06_14 | BusinessDaysWindow_2024_06_14;
case BookingWindowPeriodOutputTypeEnum_2024_06_14.ROLLING:
return {
type: transformedFutureBookingsLimitsFields.periodCountCalendarDays
? BookingWindowPeriodInputTypeEnum_2024_06_14.calendarDays
: BookingWindowPeriodInputTypeEnum_2024_06_14.businessDays,
value: transformedFutureBookingsLimitsFields.periodDays,
rolling: false,
} as CalendarDaysWindow_2024_06_14 | BusinessDaysWindow_2024_06_14;
default:
return undefined;
}
}
export {
getResponseEventTypeLocations,
getResponseEventTypeBookingFields,
getResponseEventTypeIntervalLimits,
getResponseEventTypeFutureBookingLimits,
};