Scheduling infrastructure for absolutely everyone.
perf: migrate listHandler to AttributeRepository + cache attributes fetching in RSCs (#21552)
devin-ai-integration[bot]e115095198bc
diff --git a/apps/web/app/(use-page-wrapper)/settings/organizations/[id]/members/page.tsx b/apps/web/app/(use-page-wrapper)/settings/organizations/[id]/members/page.tsxindex 5e15a03a6a0..3d269da8161 100644--- a/apps/web/app/(use-page-wrapper)/settings/organizations/[id]/members/page.tsx+++ b/apps/web/app/(use-page-wrapper)/settings/organizations/[id]/members/page.tsx@@ -1,11 +1,5 @@-import { createRouterCaller } from "app/_trpc/context";import { _generateMetadata } from "app/_utils";-import { attributesRouter } from "@calcom/trpc/server/routers/viewer/attributes/_router";-import { viewerOrganizationsRouter } from "@calcom/trpc/server/routers/viewer/organizations/_router";--import { MembersView } from "~/members/members-view";-export const generateMetadata = async ({ params }: { params: Promise<{ id: string }> }) =>await _generateMetadata((t) => t("organization_members"),@@ -15,20 +9,4 @@ export const generateMetadata = async ({ params }: { params: Promise<{ id: strin`/settings/organizations/${(await params).id}/members`);-const ServerPageWrapper = async () => {- const [orgCaller, attributesCaller] = await Promise.all([- createRouterCaller(viewerOrganizationsRouter),- createRouterCaller(attributesRouter),- ]);- const [org, teams, facetedTeamValues, attributes] = await Promise.all([- orgCaller.listCurrent(),- orgCaller.getTeams(),- orgCaller.getFacetedValues(),- attributesCaller.list(),- ]);- return (- <MembersView org={org} teams={teams} facetedTeamValues={facetedTeamValues} attributes={attributes} />- );-};--export default ServerPageWrapper;+export { default } from "../../members/page";diff --git a/apps/web/app/(use-page-wrapper)/settings/organizations/members/actions.ts b/apps/web/app/(use-page-wrapper)/settings/organizations/members/actions.tsnew file mode 100644index 00000000000..061f4a88a84--- /dev/null+++ b/apps/web/app/(use-page-wrapper)/settings/organizations/members/actions.ts@@ -0,0 +1,7 @@+"use server";++import { revalidateTag } from "next/cache";++export async function revalidateAttributesList() {+ revalidateTag("viewer.attributes.list");+}diff --git a/apps/web/app/(use-page-wrapper)/settings/organizations/members/page.tsx b/apps/web/app/(use-page-wrapper)/settings/organizations/members/page.tsxindex 6a499d66af9..82cdb793547 100644--- a/apps/web/app/(use-page-wrapper)/settings/organizations/members/page.tsx+++ b/apps/web/app/(use-page-wrapper)/settings/organizations/members/page.tsx@@ -1,7 +1,8 @@import { createRouterCaller } from "app/_trpc/context";import { _generateMetadata } from "app/_utils";+import { unstable_cache } from "next/cache";-import { attributesRouter } from "@calcom/trpc/server/routers/viewer/attributes/_router";+import { AttributeRepository } from "@calcom/lib/server/repository/attribute";import { viewerOrganizationsRouter } from "@calcom/trpc/server/routers/viewer/organizations/_router";import { MembersView } from "~/members/members-view";@@ -15,18 +16,22 @@ export const generateMetadata = async () =>"/settings/organizations/members");+const getCachedAttributes = unstable_cache(+ async (orgId: number) => {+ return await AttributeRepository.findAllByOrgIdWithOptions({ orgId });+ },+ ["viewer.attributes.list"],+ { revalidate: 3600 } // Cache for 1 hour+);+const Page = async () => {- const [orgCaller, attributesCaller] = await Promise.all([- createRouterCaller(viewerOrganizationsRouter),- createRouterCaller(attributesRouter),- ]);- const [org, teams, facetedTeamValues, attributes] = await Promise.all([+ const orgCaller = await createRouterCaller(viewerOrganizationsRouter);+ const [org, teams, facetedTeamValues] = await Promise.all([orgCaller.listCurrent(),orgCaller.getTeams(),orgCaller.getFacetedValues(),- attributesCaller.list(),]);-+ const attributes = await getCachedAttributes(org.id);return (<MembersView org={org} teams={teams} facetedTeamValues={facetedTeamValues} attributes={attributes} />);diff --git a/packages/features/ee/organizations/pages/settings/attributes/DeleteAttributeModal.tsx b/packages/features/ee/organizations/pages/settings/attributes/DeleteAttributeModal.tsxindex e0db01c8609..5dc4a80d437 100644--- a/packages/features/ee/organizations/pages/settings/attributes/DeleteAttributeModal.tsx+++ b/packages/features/ee/organizations/pages/settings/attributes/DeleteAttributeModal.tsx@@ -6,6 +6,7 @@ import type { RouterOutputs } from "@calcom/trpc/react";import { trpc } from "@calcom/trpc/react";import { ConfirmationDialogContent } from "@calcom/ui/components/dialog";import { showToast } from "@calcom/ui/components/toast";+import { revalidateAttributesList } from "@calcom/web/app/(use-page-wrapper)/settings/organizations/members/actions";type AttributeItemProps = RouterOutputs["viewer"]["attributes"]["list"][number];@@ -23,6 +24,7 @@ export function DeleteAttributeModal({onSuccess: () => {showToast(t("attribute_deleted_successfully"), "success");utils.viewer.attributes.list.invalidate();+ revalidateAttributesList();},onError: (err) => {showToast(err.message, "error");diff --git a/packages/features/ee/organizations/pages/settings/attributes/attributes-edit-view.tsx b/packages/features/ee/organizations/pages/settings/attributes/attributes-edit-view.tsxindex 40caad1b0f5..52cce1dcc1e 100644--- a/packages/features/ee/organizations/pages/settings/attributes/attributes-edit-view.tsx+++ b/packages/features/ee/organizations/pages/settings/attributes/attributes-edit-view.tsx@@ -7,8 +7,9 @@ import { z } from "zod";import LicenseRequired from "@calcom/features/ee/common/components/LicenseRequired";import { useLocale } from "@calcom/lib/hooks/useLocale";import { trpc } from "@calcom/trpc/react";-import { showToast } from "@calcom/ui/components/toast";import { Button } from "@calcom/ui/components/button";+import { showToast } from "@calcom/ui/components/toast";+import { revalidateAttributesList } from "@calcom/web/app/(use-page-wrapper)/settings/organizations/members/actions";import { AttributeForm } from "./AttributesForm";@@ -37,6 +38,7 @@ function CreateAttributesPage() {id,});utils.viewer.attributes.list.invalidate();+ revalidateAttributesList();},onError: (err) => {showToast(err.message, "error");diff --git a/packages/lib/server/repository/attribute.ts b/packages/lib/server/repository/attribute.tsindex ac3942b9f16..5c0ab24ee6b 100644--- a/packages/lib/server/repository/attribute.ts+++ b/packages/lib/server/repository/attribute.ts@@ -42,4 +42,15 @@ export class AttributeRepository {return result;}++ static async findAllByOrgIdWithOptions({ orgId }: { orgId: number }) {+ return await prisma.attribute.findMany({+ where: {+ teamId: orgId,+ },+ include: {+ options: true,+ },+ });+ }}diff --git a/packages/trpc/server/routers/viewer/attributes/list.handler.ts b/packages/trpc/server/routers/viewer/attributes/list.handler.tsindex dbb1be54fab..4bcb590b350 100644--- a/packages/trpc/server/routers/viewer/attributes/list.handler.ts+++ b/packages/trpc/server/routers/viewer/attributes/list.handler.ts@@ -1,4 +1,4 @@-import prisma from "@calcom/prisma";+import { AttributeRepository } from "@calcom/lib/server/repository/attribute";import { TRPCError } from "@trpc/server";@@ -20,14 +20,7 @@ const listHandler = async (opts: GetOptions) => {});}- return await prisma.attribute.findMany({- where: {- teamId: org.id,- },- include: {- options: true,- },- });+ return await AttributeRepository.findAllByOrgIdWithOptions({ orgId: org.id });};export default listHandler;