refactor: replace loggedInUsersTz with timeZone property in busy-times API (#25440)
* refactor: replace loggedInUsersTz with timeZone in busy-times API - Add new timeZone parameter to CalendarBusyTimesInput - Mark loggedInUsersTz as deprecated - Update getBusyTimes endpoint to prioritize timeZone parameter - Maintain backward compatibility with loggedInUsersTz - Add validation to ensure at least one timezone parameter is provided - Update API documentation with new parameter example Fixes #25423 * fix: improve timezone validation in CalendarBusyTimesInput - Use custom ValidatorConstraint instead of problematic ValidateIf - Properly validate that at least one timezone parameter is provided - Improve code quality and clarity * fix: update DTOs for getBusyTimes * cleanup * fix: abstract normalize timezone logic into platform types util function * chore: update e2e tests * fixup * fixup --------- Co-authored-by: Ryukemeister <[email protected]> Co-authored-by: Rajiv Sahal <[email protected]>
This commit is contained in:
6 files changed
+164
-36
No files matched your search
@@ -1,10 +1,45 @@
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { Type } from "class-transformer";
|
||||
import { Transform } from "class-transformer";
|
||||
import { IsNumber, IsString, IsArray, ValidateNested, IsDateString } from "class-validator";
|
||||
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
|
||||
import { Transform, Type } from "class-transformer";
|
||||
import {
|
||||
IsArray,
|
||||
IsDateString,
|
||||
IsNumber,
|
||||
IsOptional,
|
||||
IsString,
|
||||
IsTimeZone,
|
||||
registerDecorator,
|
||||
ValidateNested,
|
||||
type ValidationArguments,
|
||||
type ValidationOptions,
|
||||
} from "class-validator";
|
||||
|
||||
import { normalizeTimezone } from "../../utils/normalizeTimezone";
|
||||
|
||||
function ValidateTimezoneRequired(
|
||||
validationOptions?: ValidationOptions
|
||||
): (target: new (...args: unknown[]) => unknown) => void {
|
||||
return (target: new (...args: unknown[]) => unknown) => {
|
||||
registerDecorator({
|
||||
name: "validateTimezoneRequired",
|
||||
target: target as new (...args: unknown[]) => unknown,
|
||||
propertyName: "timeZone",
|
||||
options: validationOptions,
|
||||
constraints: [],
|
||||
validator: {
|
||||
validate(_: unknown, args: ValidationArguments): boolean {
|
||||
const obj = args.object as CalendarBusyTimesInput;
|
||||
return !!obj.timeZone || !!obj.loggedInUsersTz;
|
||||
},
|
||||
defaultMessage(): string {
|
||||
return "Either timeZone or loggedInUsersTz must be provided";
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export class Calendar {
|
||||
@Transform(({ value }: { value: string }) => value && parseInt(value))
|
||||
@Transform(({ value }: { value: string }) => value && parseInt(value, 10))
|
||||
@IsNumber()
|
||||
@ApiProperty()
|
||||
credentialId!: number;
|
||||
@@ -14,14 +49,28 @@ export class Calendar {
|
||||
externalId!: string;
|
||||
}
|
||||
|
||||
@ValidateTimezoneRequired()
|
||||
export class CalendarBusyTimesInput {
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: "The timezone of the logged in user represented as a string",
|
||||
required: false,
|
||||
deprecated: true,
|
||||
description: "Deprecated: Use timeZone instead. The timezone of the user represented as a string",
|
||||
example: "America/New_York",
|
||||
})
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
loggedInUsersTz!: string;
|
||||
loggedInUsersTz?: string;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
required: false,
|
||||
description: "The timezone for the busy times query represented as a string",
|
||||
example: "America/New_York",
|
||||
})
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@IsTimeZone()
|
||||
@Transform(({ value }) => normalizeTimezone(value))
|
||||
timeZone?: string;
|
||||
|
||||
@ApiProperty({
|
||||
required: false,
|
||||
|
||||
@@ -16,3 +16,4 @@ export * from "./routing-forms";
|
||||
export * from "./me";
|
||||
export * from "./pagination/pagination.input";
|
||||
export * from "./pagination/pagination.output";
|
||||
export * from "./utils/normalizeTimezone";
|
||||
@@ -0,0 +1,24 @@
|
||||
export function normalizeTimezone(value: unknown): string | unknown {
|
||||
if (typeof value === "string") {
|
||||
const parts = value.split("/");
|
||||
const normalizedParts = parts.map((part) =>
|
||||
part
|
||||
.split("_")
|
||||
.map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
|
||||
.join("_")
|
||||
);
|
||||
let normalizedTimeZone = normalizedParts.join("/");
|
||||
|
||||
// note(Lauris): regex matching GMT, EST, UTC at the start, end, or surrounded by non-letters and capitalizing them
|
||||
const specialCases = ["GMT", "EST", "UTC"];
|
||||
specialCases.forEach((specialCase) => {
|
||||
const regex = new RegExp(`(^|[^a-zA-Z])(${specialCase})([^a-zA-Z]|$)`, "gi");
|
||||
normalizedTimeZone = normalizedTimeZone.replace(regex, (match, p1, p2, p3) => {
|
||||
return `${p1}${specialCase}${p3}`;
|
||||
});
|
||||
});
|
||||
|
||||
return normalizedTimeZone;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
Reference in new issue
Block a user