-
Notifications
You must be signed in to change notification settings - Fork 12.7k
refactor(bookings): add round-robin host effective-limits foundation #28760
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
Closed
deepshekhardas
wants to merge
2
commits into
calcom:main
from
deepshekhardas:fix/round-robin-limits-28715
Closed
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
225 changes: 225 additions & 0 deletions
225
packages/features/ee/round-robin/RoundRobinHostLimitsService.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,225 @@ | ||||||
| import type { PrismaClient } from "@calcom/prisma"; | ||||||
| import dayjs from "@calcom/dayjs"; | ||||||
|
|
||||||
| export interface HostLimitConfig { | ||||||
| userId: number; | ||||||
| eventTypeId: number; | ||||||
| limit: number | null; | ||||||
| window: "day" | "week" | "month" | null; | ||||||
| } | ||||||
|
|
||||||
| export interface EffectiveLimitsResult { | ||||||
| userId: number; | ||||||
| currentCount: number; | ||||||
| limit: number | null; | ||||||
| window: string | null; | ||||||
| isWithinLimit: boolean; | ||||||
| remainingSlots: number | null; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Service for managing round-robin host effective limits | ||||||
| * This is the foundation for per-member round-robin limit settings | ||||||
| */ | ||||||
| export class RoundRobinHostLimitsService { | ||||||
| constructor(private prisma: PrismaClient) {} | ||||||
|
|
||||||
| /** | ||||||
| * Get effective limits for hosts in an event type | ||||||
| * This is a foundation method that prepares the structure for limit checks | ||||||
| * without changing current booking behavior | ||||||
| */ | ||||||
| async getEffectiveLimits({ | ||||||
| eventTypeId, | ||||||
| hostIds, | ||||||
| limitConfig, | ||||||
| }: { | ||||||
| eventTypeId: number; | ||||||
| hostIds: number[]; | ||||||
| limitConfig?: Map<number, { limit: number | null; window: "day" | "week" | "month" | null }>; | ||||||
| }): Promise<EffectiveLimitsResult[]> { | ||||||
| const results: EffectiveLimitsResult[] = []; | ||||||
|
|
||||||
| for (const userId of hostIds) { | ||||||
| const config = limitConfig?.get(userId); | ||||||
| const limit = config?.limit ?? null; | ||||||
| const window = config?.window ?? null; | ||||||
|
|
||||||
| // If no limit is set, host has unlimited capacity | ||||||
| if (!limit || !window) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Falsely treating Prompt for AI agents
Suggested change
|
||||||
| results.push({ | ||||||
| userId, | ||||||
| currentCount: 0, | ||||||
| limit: null, | ||||||
| window: null, | ||||||
| isWithinLimit: true, | ||||||
| remainingSlots: null, | ||||||
| }); | ||||||
| continue; | ||||||
| } | ||||||
|
|
||||||
| // Calculate window boundaries | ||||||
| const now = dayjs(); | ||||||
| let windowStart: Date; | ||||||
|
|
||||||
| switch (window) { | ||||||
| case "day": | ||||||
| windowStart = now.startOf("day").toDate(); | ||||||
| break; | ||||||
| case "week": | ||||||
| windowStart = now.startOf("week").toDate(); | ||||||
| break; | ||||||
| case "month": | ||||||
| windowStart = now.startOf("month").toDate(); | ||||||
| break; | ||||||
| default: | ||||||
| windowStart = now.startOf("day").toDate(); | ||||||
| } | ||||||
|
|
||||||
| // Count bookings for this host in the current window | ||||||
| const currentCount = await this.prisma.booking.count({ | ||||||
| where: { | ||||||
| userId, | ||||||
| eventTypeId, | ||||||
| createdAt: { | ||||||
| gte: windowStart, | ||||||
| }, | ||||||
| status: { | ||||||
| notIn: ["CANCELLED"], | ||||||
| }, | ||||||
| }, | ||||||
| }); | ||||||
|
|
||||||
| results.push({ | ||||||
| userId, | ||||||
| currentCount, | ||||||
| limit, | ||||||
| window, | ||||||
| isWithinLimit: currentCount < limit, | ||||||
| remainingSlots: Math.max(0, limit - currentCount), | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| return results; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Filter hosts by effective limits | ||||||
| * Returns only hosts that are within their booking limits | ||||||
| * This is a foundation method - currently allows all hosts (no limit enforcement) | ||||||
| * but provides the structure for future limit enforcement | ||||||
| */ | ||||||
| async filterHostsByLimits({ | ||||||
| eventTypeId, | ||||||
| hosts, | ||||||
| limitConfig, | ||||||
| }: { | ||||||
| eventTypeId: number; | ||||||
| hosts: { userId: number; isFixed: boolean }[]; | ||||||
| limitConfig?: Map<number, { limit: number | null; window: "day" | "week" | "month" | null }>; | ||||||
| }): Promise<{ userId: number; isFixed: boolean }[]> { | ||||||
| // For now, return all hosts (no limit enforcement) | ||||||
| // This is the foundation - limits will be enforced in a future iteration | ||||||
| const hostIds = hosts.filter((h) => !h.isFixed).map((h) => h.userId); | ||||||
|
|
||||||
| if (hostIds.length === 0 || !limitConfig) { | ||||||
| return hosts; | ||||||
| } | ||||||
|
|
||||||
| const effectiveLimits = await this.getEffectiveLimits({ | ||||||
| eventTypeId, | ||||||
| hostIds, | ||||||
| limitConfig, | ||||||
| }); | ||||||
|
|
||||||
| // Create a map of userId to limit status | ||||||
| const limitStatusMap = new Map( | ||||||
| effectiveLimits.map((r) => [ | ||||||
| r.userId, | ||||||
| { | ||||||
| isWithinLimit: r.isWithinLimit, | ||||||
| remainingSlots: r.remainingSlots, | ||||||
| }, | ||||||
| ]) | ||||||
| ); | ||||||
|
|
||||||
| // Filter hosts - for now we keep all hosts but the structure is ready | ||||||
| // In the future, this will filter out hosts that have exceeded their limits | ||||||
| return hosts.filter((host) => { | ||||||
| if (host.isFixed) return true; // Fixed hosts are not affected by RR limits | ||||||
|
|
||||||
| const status = limitStatusMap.get(host.userId); | ||||||
| if (!status) return true; | ||||||
|
|
||||||
| // Foundation: Currently allows all hosts | ||||||
| // TODO: In future PR, change to: return status.isWithinLimit; | ||||||
| return true; | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Get limit status for a specific host | ||||||
| * Useful for UI indicators showing how many bookings a host has left | ||||||
| */ | ||||||
| async getHostLimitStatus({ | ||||||
| userId, | ||||||
| eventTypeId, | ||||||
| limit, | ||||||
| window, | ||||||
| }: { | ||||||
| userId: number; | ||||||
| eventTypeId: number; | ||||||
| limit: number | null; | ||||||
| window: "day" | "week" | "month" | null; | ||||||
| }): Promise<Omit<EffectiveLimitsResult, "userId"> | null> { | ||||||
| if (!limit || !window) { | ||||||
| return { | ||||||
| currentCount: 0, | ||||||
| limit: null, | ||||||
| window: null, | ||||||
| isWithinLimit: true, | ||||||
| remainingSlots: null, | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| const now = dayjs(); | ||||||
| let windowStart: Date; | ||||||
|
|
||||||
| switch (window) { | ||||||
| case "day": | ||||||
| windowStart = now.startOf("day").toDate(); | ||||||
| break; | ||||||
| case "week": | ||||||
| windowStart = now.startOf("week").toDate(); | ||||||
| break; | ||||||
| case "month": | ||||||
| windowStart = now.startOf("month").toDate(); | ||||||
| break; | ||||||
| default: | ||||||
| windowStart = now.startOf("day").toDate(); | ||||||
| } | ||||||
|
|
||||||
| const currentCount = await this.prisma.booking.count({ | ||||||
| where: { | ||||||
| userId, | ||||||
| eventTypeId, | ||||||
| createdAt: { | ||||||
| gte: windowStart, | ||||||
| }, | ||||||
| status: { | ||||||
| notIn: ["CANCELLED"], | ||||||
| }, | ||||||
| }, | ||||||
| }); | ||||||
|
|
||||||
| return { | ||||||
| currentCount, | ||||||
| limit, | ||||||
| window, | ||||||
| isWithinLimit: currentCount < limit, | ||||||
| remainingSlots: Math.max(0, limit - currentCount), | ||||||
| }; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| export default RoundRobinHostLimitsService; | ||||||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
P2: Newly added UTM header translation keys are referenced but missing in locale resources, causing fallback/raw key rendering in the UI.
Prompt for AI agents