fix: use graceful filtering for previously hard failing blocked users in team events (#26446)

* init

* typefix

* --

* fix test

* update index

* cleanup

* rm unnecessary comments

* improvements

* add tests

* test fixes

* fixes

* fixes

* fixes

* add try catch to make booking fail-open

* Update packages/features/watchlist/operations/check-user-blocking.ts

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>

* fix type

---------

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
This commit is contained in:
Syed Ali Shahbazandcubic-dev-ai[bot] authored and GitHub committed 2026-01-08 08:30:54 -03:00
1 parent 27623926fc
commit 59fca85d92
20 files changed
+1573 -37

No files matched your search

@@ -1,6 +1,4 @@
import type { Logger } from "tslog";
import { v4 as uuid } from "uuid";
import process from "node:process";
import type { Dayjs } from "@calcom/dayjs";
import dayjs from "@calcom/dayjs";
import { orgDomainConfig } from "@calcom/ee/organizations/lib/orgDomains";
@@ -9,9 +7,9 @@ import type {
CurrentSeats,
EventType,
GetAvailabilityUser,
UserAvailabilityService,
IFromUser,
IToUser,
UserAvailabilityService,
} from "@calcom/features/availability/lib/getUserAvailability";
import type { CheckBookingLimitsService } from "@calcom/features/bookings/lib/checkBookingLimits";
import { checkForConflicts } from "@calcom/features/bookings/lib/conflictChecker/checkForConflicts";
@@ -33,6 +31,7 @@ import type { ISelectedSlotRepository } from "@calcom/features/selectedSlots/rep
import type { NoSlotsNotificationService } from "@calcom/features/slots/handleNotificationWhenNoSlots";
import type { UserRepository } from "@calcom/features/users/repositories/UserRepository";
import { withSelectedCalendars } from "@calcom/features/users/repositories/UserRepository";
import { filterBlockedHosts } from "@calcom/features/watchlist/operations/filter-blocked-hosts.controller";
import { shouldIgnoreContactOwner } from "@calcom/lib/bookings/routing/utils";
import { RESERVED_SUBDOMAINS } from "@calcom/lib/constants";
import { getUTCOffsetByTimezone } from "@calcom/lib/dayjs";
@@ -43,21 +42,21 @@ import { parseDurationLimit } from "@calcom/lib/intervalLimits/isDurationLimits"
import LimitManager from "@calcom/lib/intervalLimits/limitManager";
import { isBookingWithinPeriod } from "@calcom/lib/intervalLimits/utils";
import {
BookingDateInPastError,
calculatePeriodLimits,
isTimeOutOfBounds,
isTimeViolatingFutureLimit,
BookingDateInPastError,
} from "@calcom/lib/isOutOfBounds";
import logger from "@calcom/lib/logger";
import { safeStringify } from "@calcom/lib/safeStringify";
import { withReporting } from "@calcom/lib/sentryWrapper";
import type { RoutingFormResponseRepository } from "@calcom/lib/server/repository/formResponse";
import { SchedulingType, PeriodType } from "@calcom/prisma/enums";
import { PeriodType, SchedulingType } from "@calcom/prisma/enums";
import type { EventBusyDate, EventBusyDetails } from "@calcom/types/Calendar";
import type { CredentialForCalendarService } from "@calcom/types/Credential";
import { TRPCError } from "@trpc/server";
import type { Logger } from "tslog";
import { v4 as uuid } from "uuid";
import type { TGetScheduleInputSchema } from "./getSchedule.schema";
import type { GetScheduleOptions } from "./types";
@@ -200,8 +199,8 @@ export class AvailableSlotsService {
usernameList: Array.isArray(input.usernameList)
? input.usernameList
: input.usernameList
? [input.usernameList]
: [],
? [input.usernameList]
: [],
});
const usersWithOldSelectedCalendars = usersForDynamicEventType.map((user) => withSelectedCalendars(user));
@@ -280,7 +279,7 @@ export class AvailableSlotsService {
* cause slots from adjacent days to leak into the response.
*/
private _filterSlotsByRequestedDateRange<
T extends Record<string, { time: string; attendees?: number; bookingUid?: string }[]>
T extends Record<string, { time: string; attendees?: number; bookingUid?: string }[]>,
>({
slotsMappedToDate,
startTime,
@@ -871,7 +870,7 @@ export class AvailableSlotsService {
});
}
let busyTimesFromLimitsMap: Map<number, EventBusyDetails[]> | undefined = undefined;
let busyTimesFromLimitsMap: Map<number, EventBusyDetails[]> | undefined;
if (eventType && (bookingLimits || durationLimits)) {
const usersForLimits = usersWithCredentials.map((user) => ({ id: user.id, email: user.email }));
const eventTimeZone = eventType.schedule?.timeZone ?? usersWithCredentials[0]?.timeZone ?? "UTC";
@@ -894,7 +893,7 @@ export class AvailableSlotsService {
const teamBookingLimits = parseBookingLimit(teamForBookingLimits?.bookingLimits);
let teamBookingLimitsMap: Map<number, EventBusyDetails[]> | undefined = undefined;
let teamBookingLimitsMap: Map<number, EventBusyDetails[]> | undefined;
if (teamForBookingLimits && teamBookingLimits) {
const usersForTeamLimits = usersWithCredentials.map((user) => ({ id: user.id, email: user.email }));
const eventTimeZone = eventType.schedule?.timeZone ?? usersWithCredentials[0]?.timeZone ?? "UTC";
@@ -1097,11 +1096,32 @@ export class AvailableSlotsService {
rrHostSubsetIds: input.rrHostSubsetIds ?? undefined,
});
const allHosts = [...qualifiedRRHosts, ...fixedHosts];
// Filter out blocked hosts BEFORE calculating availability (batched - single DB query)
const organizationId = eventType.parent?.team?.parentId ?? eventType.team?.parentId ?? null;
const { eligibleHosts: eligibleQualifiedRRHosts } = await filterBlockedHosts(
qualifiedRRHosts,
organizationId
);
const { eligibleHosts: eligibleFixedHosts } = await filterBlockedHosts(fixedHosts, organizationId);
const { eligibleHosts: eligibleFallbackRRHosts } = allFallbackRRHosts
? await filterBlockedHosts(allFallbackRRHosts, organizationId)
: { eligibleHosts: [] };
const allHosts = [...eligibleQualifiedRRHosts, ...eligibleFixedHosts];
// If all hosts are blocked, return empty slots
if (allHosts.length === 0) {
loggerWithEventDetails.info("All hosts are blocked by watchlist, returning empty slots");
return {
slots: {},
};
}
const twoWeeksFromNow = dayjs().add(2, "week");
const hasFallbackRRHosts = allFallbackRRHosts && allFallbackRRHosts.length > qualifiedRRHosts.length;
const hasFallbackRRHosts =
eligibleFallbackRRHosts.length > 0 && eligibleFallbackRRHosts.length > eligibleQualifiedRRHosts.length;
let { allUsersAvailability, usersWithCredentials, currentSeats } =
await this.calculateHostsAndAvailabilities({
@@ -1142,7 +1162,7 @@ export class AvailableSlotsService {
const firstTwoWeeksAvailabilities = await this.calculateHostsAndAvailabilities({
input,
eventType,
hosts: [...qualifiedRRHosts, ...fixedHosts],
hosts: [...eligibleQualifiedRRHosts, ...eligibleFixedHosts],
loggerWithEventDetails,
startTime: dayjs(),
endTime: twoWeeksFromNow,
@@ -1165,8 +1185,9 @@ export class AvailableSlotsService {
loggerWithEventDetails.info({
email: input.email,
contactOwnerEmail,
qualifiedRRHosts: qualifiedRRHosts.map((host) => host.user.id),
fallbackRRHosts: allFallbackRRHosts.map((host) => host.user.id),
eligibleQualifiedRRHosts: eligibleQualifiedRRHosts.map((host) => host.user.id),
eligibleFallbackRRHosts: eligibleFallbackRRHosts.map((host) => host.user.id),
blockedHostsCount: qualifiedRRHosts.length - eligibleQualifiedRRHosts.length,
fallBackActive: diff > 0,
});
}
@@ -1177,7 +1198,7 @@ export class AvailableSlotsService {
await this.calculateHostsAndAvailabilities({
input,
eventType,
hosts: [...allFallbackRRHosts, ...fixedHosts],
hosts: [...eligibleFallbackRRHosts, ...eligibleFixedHosts],
loggerWithEventDetails,
startTime,
endTime,
@@ -1413,7 +1434,7 @@ export class AvailableSlotsService {
eventType.timeZone || eventType?.schedule?.timeZone || allUsersAvailability?.[0]?.timeZone;
const eventUtcOffset = getUTCOffsetByTimezone(eventTimeZone) ?? 0;
const bookerUtcOffset = input.timeZone ? getUTCOffsetByTimezone(input.timeZone) ?? 0 : 0;
const bookerUtcOffset = input.timeZone ? (getUTCOffsetByTimezone(input.timeZone) ?? 0) : 0;
const periodLimits = calculatePeriodLimits({
periodType: eventType.periodType,
periodDays: eventType.periodDays,