perf: remove 2 DB fetches by removing getFacetedValues (#21808)

* perf: remove 2 server fetches by removing getFacetedValues

* fix

* fix

* use set

* fix
This commit is contained in:
Benny Joo
2025-06-15 11:31:11 +00:00
committed by GitHub
parent 8c5aebdca1
commit cb24e5d0aa
5 changed files with 26 additions and 75 deletions
@@ -3,6 +3,7 @@ import { _generateMetadata } from "app/_utils";
import { unstable_cache } from "next/cache";
import { AttributeRepository } from "@calcom/lib/server/repository/attribute";
import { MembershipRole } from "@calcom/prisma/enums";
import { viewerOrganizationsRouter } from "@calcom/trpc/server/routers/viewer/organizations/_router";
import { MembersView } from "~/members/members-view";
@@ -26,12 +27,20 @@ const getCachedAttributes = unstable_cache(
const Page = async () => {
const orgCaller = await createRouterCaller(viewerOrganizationsRouter);
const [org, teams, facetedTeamValues] = await Promise.all([
orgCaller.listCurrent(),
orgCaller.getTeams(),
orgCaller.getFacetedValues(),
]);
const [org, teams] = await Promise.all([orgCaller.listCurrent(), orgCaller.getTeams()]);
const attributes = await getCachedAttributes(org.id);
const facetedTeamValues = {
roles: [MembershipRole.OWNER, MembershipRole.ADMIN, MembershipRole.MEMBER],
teams,
attributes: attributes.map((attribute) => ({
id: attribute.id,
name: attribute.name,
options: Array.from(new Set(attribute.options.map((option) => option.value))).map((value) => ({
value,
})),
})),
};
return (
<MembersView org={org} teams={teams} facetedTeamValues={facetedTeamValues} attributes={attributes} />
);
@@ -30,6 +30,7 @@ import {
} from "@calcom/lib/csvUtils";
import { getUserAvatarUrl } from "@calcom/lib/getAvatarUrl";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import type { MembershipRole } from "@calcom/prisma/enums";
import { trpc } from "@calcom/trpc";
import type { RouterOutputs } from "@calcom/trpc/react";
import classNames from "@calcom/ui/classNames";
@@ -109,8 +110,18 @@ function reducer(state: UserTableState, action: UserTableAction): UserTableState
export type UserListTableProps = {
org: RouterOutputs["viewer"]["organizations"]["listCurrent"];
teams: RouterOutputs["viewer"]["organizations"]["getTeams"];
facetedTeamValues?: RouterOutputs["viewer"]["organizations"]["getFacetedValues"];
attributes?: RouterOutputs["viewer"]["attributes"]["list"];
facetedTeamValues?: {
roles: MembershipRole[];
teams: RouterOutputs["viewer"]["organizations"]["getTeams"];
attributes: {
id: string;
name: string;
options: {
value: string;
}[];
}[];
};
};
export function UserListTable(props: UserListTableProps) {
@@ -29,10 +29,6 @@ import { ZSetPasswordSchema } from "./setPassword.schema";
import { ZUpdateInputSchema } from "./update.schema";
import { ZUpdateUserInputSchema } from "./updateUser.schema";
const NAMESPACE = "organizations";
const namespaced = (s: string) => `${NAMESPACE}.${s}`;
export const viewerOrganizationsRouter = router({
getOrganizationOnboarding: authedProcedure.query(async (opts) => {
const { default: handler } = await import("./getOrganizationOnboarding.handler");
@@ -162,8 +158,4 @@ export const viewerOrganizationsRouter = router({
const { default: handler } = await import("./createPhoneCall.handler");
return handler(opts);
}),
getFacetedValues: authedProcedure.query(async (opts) => {
const { default: handler } = await import("./getFacetedValues.handler");
return handler(opts);
}),
});
@@ -1,54 +0,0 @@
import { prisma } from "@calcom/prisma";
import { MembershipRole } from "@calcom/prisma/enums";
import { TRPCError } from "@trpc/server";
import type { TrpcSessionUser } from "../../../types";
type DeleteOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
};
};
export const getFacetedValuesHandler = async ({ ctx }: DeleteOptions) => {
const { user } = ctx;
const organizationId = user.organization?.id;
if (!organizationId) {
throw new TRPCError({ code: "NOT_FOUND", message: "Organization not found" });
}
const [teams, attributes] = await Promise.all([
prisma.team.findMany({
where: {
parentId: organizationId,
},
select: {
id: true,
name: true,
},
}),
prisma.attribute.findMany({
where: { teamId: organizationId },
select: {
id: true,
name: true,
options: {
select: {
value: true,
},
distinct: "value",
},
},
}),
]);
return {
teams: teams,
attributes: attributes,
roles: [MembershipRole.OWNER, MembershipRole.ADMIN, MembershipRole.MEMBER],
};
};
export default getFacetedValuesHandler;
@@ -1,7 +0,0 @@
import { z } from "zod";
export const ZGetFacetedValuesInput = z.object({
include: z.enum(["users", "roles", "teams", "attributes"]).array().optional(),
});
export type TGetFacetedValuesInputSchema = z.infer<typeof ZGetFacetedValuesInput>;