Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 65 additions & 1 deletion apps/web/modules/bookings/columns/filterColumns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ const FILTER_COLUMN_IDS = [
"attendeeEmail",
"dateRange",
"bookingUid",
"utmSource",
"utmMedium",
"utmCampaign",
"utmTerm",
"utmContent",
] as const;

/**
Expand Down Expand Up @@ -128,5 +133,64 @@ export function buildFilterColumns({ t, permissions, status }: BuildFilterColumn
},
},
}),
columnHelper.accessor((row) => (row.type === "data" ? row.booking.utmSource : null), {
id: "utmSource",
header: t("utm_source"),
enableColumnFilter: true,
enableSorting: false,
cell: () => null,
meta: {
filter: {
type: ColumnFilterType.TEXT,
},
},
}),
columnHelper.accessor((row) => (row.type === "data" ? row.booking.utmMedium : null), {
id: "utmMedium",
header: t("utm_medium"),
enableColumnFilter: true,
enableSorting: false,
cell: () => null,
meta: {
filter: {
type: ColumnFilterType.TEXT,
},
},
}),
columnHelper.accessor((row) => (row.type === "data" ? row.booking.utmCampaign : null), {
id: "utmCampaign",
header: t("utm_campaign"),
enableColumnFilter: true,
enableSorting: false,
cell: () => null,
meta: {
filter: {
type: ColumnFilterType.TEXT,
},
},
}),
columnHelper.accessor((row) => (row.type === "data" ? row.booking.utmTerm : null), {
id: "utmTerm",
header: t("utm_term"),
enableColumnFilter: true,
enableSorting: false,
cell: () => null,
meta: {
filter: {
type: ColumnFilterType.TEXT,
},
},
}),
columnHelper.accessor((row) => (row.type === "data" ? row.booking.utmContent : null), {
id: "utmContent",
header: t("utm_content"),
enableColumnFilter: true,
enableSorting: false,
cell: () => null,
meta: {
filter: {
type: ColumnFilterType.TEXT,
},
},
}),
];
}
31 changes: 29 additions & 2 deletions apps/web/modules/bookings/components/BookingListContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ function BookingListInner({
attendeeEmail: false,
dateRange: false,
bookingUid: false,
utmSource: false,
utmMedium: false,
utmCampaign: false,
utmTerm: false,
utmContent: false,
},
},
getCoreRowModel: getCoreRowModel(),
Expand Down Expand Up @@ -233,8 +238,20 @@ function BookingListInner({

export function BookingListContainer(props: BookingListContainerProps) {
const { limit, offset, isValidatorPending } = useDataTable();
const { eventTypeIds, teamIds, userIds, dateRange, attendeeName, attendeeEmail, bookingUid } =
useBookingFilters();
const {
eventTypeIds,
teamIds,
userIds,
dateRange,
attendeeName,
attendeeEmail,
bookingUid,
utmSource,
utmMedium,
utmCampaign,
utmTerm,
utmContent,
} = useBookingFilters();

const { resolvedTabStatus, isResolvingTabStatus, preSelectedBooking } = useSwitchToCorrectStatusTab({
defaultStatus: props.status,
Expand All @@ -253,6 +270,11 @@ export function BookingListContainer(props: BookingListContainerProps) {
attendeeName,
attendeeEmail,
bookingUid,
utmSource,
utmMedium,
utmCampaign,
utmTerm,
utmContent,
afterStartDate: dateRange?.startDate
? dayjs(dateRange?.startDate).startOf("day").toISOString()
: undefined,
Expand All @@ -269,6 +291,11 @@ export function BookingListContainer(props: BookingListContainerProps) {
attendeeName,
attendeeEmail,
bookingUid,
utmSource,
utmMedium,
utmCampaign,
utmTerm,
utmContent,
dateRange,
]
);
Expand Down
10 changes: 10 additions & 0 deletions apps/web/modules/bookings/hooks/useBookingFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export function useBookingFilters() {
const attendeeName = useFilterValue("attendeeName", ZTextFilterValue);
const attendeeEmail = useFilterValue("attendeeEmail", ZTextFilterValue);
const bookingUid = useFilterValue("bookingUid", ZTextFilterValue)?.data?.operand as string | undefined;
const utmSource = useFilterValue("utmSource", ZTextFilterValue)?.data?.operand as string | undefined;
const utmMedium = useFilterValue("utmMedium", ZTextFilterValue)?.data?.operand as string | undefined;
const utmCampaign = useFilterValue("utmCampaign", ZTextFilterValue)?.data?.operand as string | undefined;
const utmTerm = useFilterValue("utmTerm", ZTextFilterValue)?.data?.operand as string | undefined;
const utmContent = useFilterValue("utmContent", ZTextFilterValue)?.data?.operand as string | undefined;

return {
eventTypeIds,
Expand All @@ -18,5 +23,10 @@ export function useBookingFilters() {
attendeeName,
attendeeEmail,
bookingUid,
utmSource,
utmMedium,
utmCampaign,
utmTerm,
utmContent,
};
}
225 changes: 225 additions & 0 deletions packages/features/ee/round-robin/RoundRobinHostLimitsService.ts
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) {
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;
Loading
Loading