Skip to content

Commit b539adf

Browse files
joeauyeungdevin-ai-integration[bot]Udit-takkar
authored
perf: add paginated host endpoints and repository methods for large teams (calcom#28156)
* perf: add paginated host endpoints and delta-based host updates for event type editor - New cursor-paginated tRPC endpoints: getHostsForAssignment, getHostsForAvailability, searchTeamMembers, getChildrenForAssignment, exportHostsForWeights, getHostsWithLocationOptions - Delta-based host update support in update.handler.ts (pendingHostChanges, pendingChildrenChanges) - Repository additions: EventTypeRepository.findChildrenByParentId, HostRepository pagination, MembershipRepository.searchMembers, UserRepository.findByIdsWithPagination - Remove teamMembers from getEventTypeById initial load - Shared types: PendingHostChangesInput, PendingChildrenChangesInput, HostUpdateInput Co-Authored-By: unknown <> * fix: revert getTranslation import path to @calcom/i18n/server Co-Authored-By: unknown <> * fix: guard findChildrenByParentId to only run when pendingChildrenChanges exists Co-Authored-By: unknown <> * refactor: remove delta-based saving logic from backend PR Move pendingHostChanges/pendingChildrenChanges processing out of backend PR. These changes belong in the frontend PR since they are tightly coupled to the new frontend delta tracking components. Backend PR now contains only read-side optimizations: - Paginated host/children/member endpoints - Repository methods - getEventTypeById optimizations Co-Authored-By: unknown <> * refactor: move getEventTypeById changes to frontend PR for type safety Reverts getEventTypeById.ts, eventTypeRepository.ts, API v2 atom service, and platform libraries to main. The backend PR now only adds new infrastructure (paginated endpoints, repository methods, findChildrenByParentId) without changing existing return types. The getEventTypeById optimizations will be in the frontend PR instead. Co-Authored-By: unknown <> * refactor: move findTeamMembersMatchingAttributeLogic pagination to frontend PR The handler's return type change (adding nextCursor/total) breaks frontend files on main that expect the old shape. Moving these changes to the frontend PR keeps the backend PR purely additive. Co-Authored-By: unknown <> * fix: address Cubic review comments - empty array filter, stable total count, Set lookup Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * Fix inifnite pagination loop Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix: derive teamId from event type to prevent cross-team enumeration in exportHostsForWeights Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * fix: restore doc comment to correct method hasAnyTeamMembershipByUserId Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * fix: use memberUserIds?.length to handle empty array filter in findHostsForAssignmentPaginated Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * fix: use explicit undefined check for memberUserIds to preserve empty array semantics Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * refactor: rename findChildrenByParentId to findChildrenByParentIdIncludeOwner The method selects owner with user profile data, so the name should reflect the included relation per Cal.com repository naming conventions. Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * refactor: rename host repository methods to follow naming conventions - findHostsForAvailabilityPaginated -> findHostsPaginatedIncludeUser - findHostsForAssignmentPaginated -> findHostsPaginatedIncludeUserForAssignment Repository methods should not be named after use-cases (Availability/Assignment) but should describe what data they include, per Cal.com conventions. Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * refactor: standardize slice(0, limit) across all pagination methods Replace slice(0, -1) with slice(0, limit) in all HostRepository pagination methods for consistency. slice(0, limit) is clearer about intent since it directly references the limit parameter. Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * perf: only run total count query on first page in findByIdsWithPagination Wrap the count query in a !cursor guard so it only runs on the first page request, avoiding an extra database query on every scroll. Consistent with the hasFixedHosts optimization in HostRepository. Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * test: add integration tests for paginated host endpoints Tests cover getHostsForAvailability and getHostsForAssignment handlers: - Basic host retrieval - Cursor-based pagination across multiple pages - Host data fields (isFixed, priority, weight, name, email) - Search filtering by name - memberUserIds filtering (including empty array returning zero results) - hasFixedHosts only present on first page Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * refactor: extract EventTypeHostService and make TRPC handlers thin - Create EventTypeHostService at packages/features/host/services/ with all DTO types and business logic for 5 event-type-host endpoints - Refactor all 5 handlers to delegate to the service (thin handlers) - Add 17 unit tests covering DTO mapping, authorization, segment filtering, default values, and pagination pass-through Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * docs: add PR review context comments to EventTypeHostService Reference key review decisions from PR calcom#28156 as code comments: - searchTeamMembers: membership check + repository delegation per @eunjae-lee - exportHostsForWeights: cross-team enumeration security fix per @hariombalhara - exportHostsForWeights: repository method instead of direct Prisma per @hariombalhara Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * Revert "docs: add PR review context comments to EventTypeHostService" This reverts commit 1a1596e. * fix: use explicit undefined/null check for memberUserIds in searchMembers Fixes empty array semantics so memberUserIds: [] correctly returns zero results instead of all members. Now consistent with HostRepository pattern which uses 'memberUserIds !== undefined' instead of 'memberUserIds?.length'. Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * refactor: replace TRPCError with ErrorWithCode in EventTypeHostService Per AGENTS.md rules, services in packages/features/ should use ErrorWithCode instead of TRPCError. The errorConversionMiddleware will automatically convert it to the appropriate TRPCError at the router layer. Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * Remove comment Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix: remove unused teamId from exportHostsForWeights schema teamId was originally accepted by the schema when the handler used it directly. After the security fix to derive teamId server-side from the event type, the field became dead code. Removing it to keep the API contract accurate. Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * Abstract types * Update imports --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com>
1 parent 5993889 commit b539adf

24 files changed

Lines changed: 1880 additions & 18 deletions

packages/features/eventtypes/lib/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type { RecurringEvent } from "@calcom/types/Calendar";
2020
import type { UserProfile } from "@calcom/types/UserProfile";
2121
import type { z } from "zod";
2222
import type { EventType } from "./getEventTypeById";
23+
2324
export type CustomInputParsed = typeof customInputSchema._output;
2425

2526
export type AvailabilityOption = {
@@ -51,6 +52,7 @@ export type Host = {
5152
groupId: string | null;
5253
location?: HostLocation | null;
5354
};
55+
5456
export type TeamMember = {
5557
value: string;
5658
label: string;

packages/features/eventtypes/repositories/eventTypeRepository.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,24 @@ export class EventTypeRepository implements IEventTypesRepository {
17431743
};
17441744
}
17451745

1746+
async findChildrenByParentIdIncludeOwner(parentId: number) {
1747+
return this.prismaClient.eventType.findMany({
1748+
where: { parentId },
1749+
select: {
1750+
hidden: true,
1751+
slug: true,
1752+
owner: {
1753+
select: {
1754+
id: true,
1755+
name: true,
1756+
email: true,
1757+
eventTypes: { select: { slug: true } },
1758+
},
1759+
},
1760+
},
1761+
});
1762+
}
1763+
17461764
async findByIdWithParentAndUserId(eventTypeId: number) {
17471765
return this.prismaClient.eventType.findUnique({
17481766
where: { id: eventTypeId },

packages/features/host/repositories/HostRepository.ts

Lines changed: 184 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { AppCategories } from "@calcom/prisma/enums";
21
import type { PrismaClient } from "@calcom/prisma";
2+
import { AppCategories } from "@calcom/prisma/enums";
33
import { safeCredentialSelect } from "@calcom/prisma/selects/credential";
44

55
export class HostRepository {
@@ -136,9 +136,191 @@ export class HostRepository {
136136
});
137137

138138
const hasMore = hosts.length > limit;
139-
const items = hasMore ? hosts.slice(0, -1) : hosts;
139+
const items = hasMore ? hosts.slice(0, limit) : hosts;
140+
const nextCursor = hasMore ? items[items.length - 1].userId : undefined;
141+
142+
return { items, nextCursor, hasMore };
143+
}
144+
145+
async findHostsPaginatedIncludeUser({
146+
eventTypeId,
147+
cursor,
148+
limit = 20,
149+
search,
150+
}: {
151+
eventTypeId: number;
152+
cursor?: number;
153+
limit?: number;
154+
search?: string;
155+
}) {
156+
const hosts = await this.prismaClient.host.findMany({
157+
where: {
158+
eventTypeId,
159+
...(cursor && { userId: { gt: cursor } }),
160+
...(search && {
161+
OR: [
162+
{ user: { name: { contains: search, mode: "insensitive" as const } } },
163+
{ user: { email: { contains: search, mode: "insensitive" as const } } },
164+
],
165+
}),
166+
},
167+
take: limit + 1,
168+
select: {
169+
userId: true,
170+
isFixed: true,
171+
priority: true,
172+
weight: true,
173+
scheduleId: true,
174+
groupId: true,
175+
user: {
176+
select: {
177+
name: true,
178+
avatarUrl: true,
179+
timeZone: true,
180+
},
181+
},
182+
},
183+
orderBy: [{ userId: "asc" }],
184+
});
185+
186+
const hasMore = hosts.length > limit;
187+
const items = hasMore ? hosts.slice(0, limit) : hosts;
188+
const nextCursor = hasMore ? items[items.length - 1].userId : undefined;
189+
190+
return { items, nextCursor, hasMore };
191+
}
192+
193+
async findHostsPaginatedIncludeUserForAssignment({
194+
eventTypeId,
195+
cursor,
196+
limit = 20,
197+
search,
198+
memberUserIds,
199+
}: {
200+
eventTypeId: number;
201+
cursor?: number;
202+
limit?: number;
203+
search?: string;
204+
memberUserIds?: number[];
205+
}) {
206+
const userIdFilter = memberUserIds !== undefined
207+
? cursor
208+
? { in: memberUserIds, gt: cursor }
209+
: { in: memberUserIds }
210+
: cursor
211+
? { gt: cursor }
212+
: undefined;
213+
214+
const hosts = await this.prismaClient.host.findMany({
215+
where: {
216+
eventTypeId,
217+
...(userIdFilter && { userId: userIdFilter }),
218+
...(search && {
219+
OR: [
220+
{ user: { name: { contains: search, mode: "insensitive" as const } } },
221+
{ user: { email: { contains: search, mode: "insensitive" as const } } },
222+
],
223+
}),
224+
},
225+
take: limit + 1,
226+
select: {
227+
userId: true,
228+
isFixed: true,
229+
priority: true,
230+
weight: true,
231+
scheduleId: true,
232+
groupId: true,
233+
user: {
234+
select: {
235+
name: true,
236+
email: true,
237+
avatarUrl: true,
238+
},
239+
},
240+
},
241+
orderBy: [{ userId: "asc" }],
242+
});
243+
244+
const hasMore = hosts.length > limit;
245+
const items = hasMore ? hosts.slice(0, limit) : hosts;
140246
const nextCursor = hasMore ? items[items.length - 1].userId : undefined;
141247

248+
// Only check on the first page to avoid an extra query on every scroll
249+
const hasFixedHosts = !cursor
250+
? (await this.prismaClient.host.count({
251+
where: { eventTypeId, isFixed: true },
252+
take: 1,
253+
})) > 0
254+
: undefined;
255+
256+
return { items, nextCursor, hasMore, hasFixedHosts };
257+
}
258+
259+
async findAllRoundRobinHosts({ eventTypeId }: { eventTypeId: number }) {
260+
return this.prismaClient.host.findMany({
261+
where: {
262+
eventTypeId,
263+
isFixed: false,
264+
},
265+
select: {
266+
userId: true,
267+
weight: true,
268+
user: {
269+
select: {
270+
name: true,
271+
email: true,
272+
avatarUrl: true,
273+
},
274+
},
275+
},
276+
orderBy: [{ userId: "asc" }],
277+
});
278+
}
279+
280+
async findChildrenForAssignmentPaginated({
281+
eventTypeId,
282+
cursor,
283+
limit = 20,
284+
search,
285+
}: {
286+
eventTypeId: number;
287+
cursor?: number;
288+
limit?: number;
289+
search?: string;
290+
}) {
291+
const children = await this.prismaClient.eventType.findMany({
292+
where: {
293+
parentId: eventTypeId,
294+
...(cursor && { id: { gt: cursor } }),
295+
...(search && {
296+
OR: [
297+
{ owner: { name: { contains: search, mode: "insensitive" as const } } },
298+
{ owner: { email: { contains: search, mode: "insensitive" as const } } },
299+
],
300+
}),
301+
},
302+
take: limit + 1,
303+
select: {
304+
id: true,
305+
slug: true,
306+
hidden: true,
307+
owner: {
308+
select: {
309+
id: true,
310+
name: true,
311+
email: true,
312+
username: true,
313+
avatarUrl: true,
314+
},
315+
},
316+
},
317+
orderBy: [{ id: "asc" }],
318+
});
319+
320+
const hasMore = children.length > limit;
321+
const items = hasMore ? children.slice(0, limit) : children;
322+
const nextCursor = hasMore ? items[items.length - 1].id : undefined;
323+
142324
return { items, nextCursor, hasMore };
143325
}
144326

0 commit comments

Comments
 (0)