Files
cal.diy/packages/features/attributes/repositories/PrismaAttributeRepository.ts
T
8d79209e74 perf: Optmize DB queries around attribute routing (#27600)
* Replace n x m array functions with maps

* feat: add attributeIds filter to findManyByOrgMembershipIds

Allow filtering attribute-to-user assignments by specific attribute IDs.
This enables querying only the attributes needed for a routing rule
instead of fetching all attributes for all team members.

Co-Authored-By: Claude Opus 4.5 <[email protected]>

* feat: extract attribute IDs from routing rules and filter queries

Add extractAttributeIdsFromQueryValue helper to parse routing rules
and identify which attributes are referenced. Pass these IDs through
getAttributesAssignmentData to _queryAllData to only fetch the
attribute assignments that are actually needed for evaluation.

For a team with 785 members and 14 attributes, if a routing rule
only references 2-3 attributes, this reduces the query from ~3500
rows to ~500 rows.

Co-Authored-By: Claude Opus 4.5 <[email protected]>

* feat: use attribute filtering in findTeamMembersMatchingAttributeLogic

Extract attribute IDs from the routing rules (main + fallback) and
pass them to getAttributesForLogic to only fetch necessary data.

This completes the optimization to reduce database load when
evaluating routing rules that only reference a subset of attributes.

Co-Authored-By: Claude Opus 4.5 <[email protected]>

* feat: filter attributes query by attribute IDs

Also filter the attributes query (not just attributeToUser assignments)
by the attribute IDs referenced in routing rules.

For a routing rule that references 2 out of 14 attributes with 6524
total options, this reduces:
- Attributes fetched: 14 → 2
- Options fetched: 6524 → ~930 (proportional reduction)
- Fewer regex replacements in replaceAttributeOptionIdsWithOptionLabel

Co-Authored-By: Claude Opus 4.5 <[email protected]>

* test: add comprehensive test coverage for attribute routing query optimization

Co-Authored-By: [email protected] <[email protected]>

* Revert "Replace n x m array functions with maps"

This reverts commit 312eeb03bd1aba19760f093c798861a23b5fc66f.

* fix: re-establish extractAttributeIdsFromQueryValue mock after resetAllMocks

Co-Authored-By: [email protected] <[email protected]>

---------

Co-authored-by: Claude Opus 4.5 <[email protected]>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-02-04 11:28:46 -05:00

132 lines
2.7 KiB
TypeScript

import type { PrismaClient } from "@calcom/prisma";
export class PrismaAttributeRepository {
constructor(private prismaClient: PrismaClient) {}
async findManyByNamesAndOrgIdIncludeOptions({
attributeNames,
orgId,
}: {
attributeNames: string[];
orgId: number;
}) {
return this.prismaClient.attribute.findMany({
where: {
name: { in: attributeNames, mode: "insensitive" },
teamId: orgId,
},
include: {
options: {
select: {
id: true,
value: true,
slug: true,
},
},
},
});
}
async findManyByOrgId({ orgId, attributeIds }: { orgId: number; attributeIds?: string[] }) {
// It should be a faster query because of lesser number of attributes record and index on teamId
const result = await this.prismaClient.attribute.findMany({
where: {
teamId: orgId,
// Only filter by attribute IDs if provided
...(attributeIds?.length && {
id: {
in: attributeIds,
},
}),
},
select: {
id: true,
name: true,
type: true,
slug: true,
options: true,
},
});
return result;
}
async findAllByOrgIdWithOptions({ orgId }: { orgId: number }) {
return await this.prismaClient.attribute.findMany({
where: {
teamId: orgId,
},
include: {
options: true,
},
});
}
async findUniqueWithWeights({
teamId,
attributeId,
isWeightsEnabled = true,
}: {
teamId: number;
attributeId: string;
isWeightsEnabled?: boolean;
}) {
return await this.prismaClient.attribute.findUnique({
where: {
id: attributeId,
teamId,
isWeightsEnabled,
},
select: {
id: true,
name: true,
slug: true,
type: true,
options: {
select: {
id: true,
value: true,
slug: true,
assignedUsers: {
select: {
member: {
select: {
userId: true,
},
},
weight: true,
},
},
},
},
},
});
}
findManyByIdsAndOrgIdWithOptions({
attributeIds,
orgId,
}: {
attributeIds: string[];
orgId: number;
}) {
return this.prismaClient.attribute.findMany({
where: {
teamId: orgId,
id: {
in: attributeIds,
},
},
include: {
options: {
select: {
id: true,
value: true,
slug: true,
},
},
},
});
}
}