forked from calcom/cal.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Enhance booking slot validation and timezone handling #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ShashankFC
wants to merge
2
commits into
main
Choose a base branch
from
feature/enhance-booking-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
packages/features/bookings/Booker/utils/validateBookingSlot.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import dayjs from "@calcom/dayjs"; | ||
|
|
||
| interface SlotValidationResult { | ||
| isValid: boolean; | ||
| reason?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Validates if a booking slot is within acceptable time boundaries | ||
| * @param slotTime - ISO format time string | ||
| * @param maxAdvanceBookingDays - Maximum days in advance allowed for booking | ||
| * @returns Validation result with reason if invalid | ||
| */ | ||
| export function validateSlotTimeRange( | ||
| slotTime: string, | ||
| maxAdvanceBookingDays: number = 365 | ||
| ): SlotValidationResult { | ||
| if (!slotTime) { | ||
| return { isValid: false, reason: "Slot time is required" }; | ||
| } | ||
|
|
||
| const slotDate = dayjs(slotTime); | ||
| const now = dayjs(); | ||
|
|
||
| if (!slotDate.isValid()) { | ||
| return { isValid: false, reason: "Invalid slot time format" }; | ||
| } | ||
|
|
||
| if (slotDate.isBefore(now)) { | ||
| return { isValid: false, reason: "Slot is in the past" }; | ||
| } | ||
|
|
||
| const maxDate = now.add(maxAdvanceBookingDays, "day"); | ||
| if (slotDate.isAfter(maxDate)) { | ||
| return { isValid: false, reason: `Slot exceeds maximum advance booking period of ${maxAdvanceBookingDays} days` }; | ||
| } | ||
|
|
||
| return { isValid: true }; | ||
| } | ||
|
|
||
| /** | ||
| * Validates slot duration against minimum and maximum constraints | ||
| * @param startTime - ISO format start time | ||
| * @param endTime - ISO format end time | ||
| * @param minDuration - Minimum duration in minutes | ||
| * @param maxDuration - Maximum duration in minutes | ||
| */ | ||
| export function validateSlotDuration( | ||
| startTime: string, | ||
| endTime: string, | ||
| minDuration: number = 5, | ||
| maxDuration: number = 480 | ||
| ): SlotValidationResult { | ||
| const start = dayjs(startTime); | ||
| const end = dayjs(endTime); | ||
|
|
||
| if (!start.isValid() || !end.isValid()) { | ||
| return { isValid: false, reason: "Invalid time format" }; | ||
| } | ||
|
|
||
| const duration = end.diff(start, "minute"); | ||
|
|
||
| if (duration < minDuration) { | ||
| return { isValid: false, reason: `Duration must be at least ${minDuration} minutes` }; | ||
| } | ||
|
|
||
| if (duration >= maxDuration) { | ||
| return { isValid: false, reason: `Duration must be less than ${maxDuration} minutes` }; | ||
| } | ||
|
|
||
| return { isValid: true }; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a slot falls within business hours | ||
| * @param slotTime - ISO format time string | ||
| * @param businessHoursStart - Start hour (0-23) | ||
| * @param businessHoursEnd - End hour (0-23) | ||
| */ | ||
| export function validateBusinessHours( | ||
| slotTime: string, | ||
| businessHoursStart: number = 0, | ||
| businessHoursEnd: number = 24 | ||
| ): SlotValidationResult { | ||
| const slot = dayjs(slotTime); | ||
|
|
||
| if (!slot.isValid()) { | ||
| return { isValid: false, reason: "Invalid slot time format" }; | ||
| } | ||
|
|
||
| const hour = slot.hour(); | ||
|
|
||
| if (hour < businessHoursStart || hour > businessHoursEnd) { | ||
| return { isValid: false, reason: `Slot must be between ${businessHoursStart}:00 and ${businessHoursEnd}:00` }; | ||
| } | ||
|
|
||
| return { isValid: true }; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
correctness: 🟢 [Standard Reviewer]
setIntervalinterval calculation is incorrect: it uses seconds instead of milliseconds, causing the reservation refresh to occur every ~16 hours instead of every 14 minutes.🤖 AI Agent Prompt for Cursor/Windsurf
📝 Committable Code Suggestion