Skip to content

Commit 0ea673e

Browse files
authored
feat: update global feature flag to team feature flag for team booking page cache (calcom#23154)
* add team id fetcher based on slugs * export getOrgContext * update RSC * refactor * further refactor
1 parent cf367c9 commit 0ea673e

3 files changed

Lines changed: 36 additions & 10 deletions

File tree

apps/web/app/(booking-page-wrapper)/team/[slug]/[type]/page.tsx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CustomI18nProvider } from "app/CustomI18nProvider";
22
import { withAppDirSsr } from "app/WithAppDirSsr";
3-
import type { PageProps, SearchParams } from "app/_types";
3+
import type { PageProps, Params, SearchParams } from "app/_types";
44
import { generateMeetingMetadata } from "app/_utils";
55
import { cookies, headers } from "next/headers";
66

@@ -15,20 +15,34 @@ import { getServerSideProps } from "@lib/team/[slug]/[type]/getServerSideProps";
1515
import LegacyPage from "~/team/type-view";
1616
import type { PageProps as LegacyPageProps } from "~/team/type-view";
1717

18-
import CachedTeamBooker, { generateMetadata as generateCachedMetadata } from "./pageWithCachedData";
18+
import CachedTeamBooker, {
19+
generateMetadata as generateCachedMetadata,
20+
getOrgContext,
21+
} from "./pageWithCachedData";
22+
import { getTeamId } from "./queries";
23+
24+
async function isCachedTeamBookingEnabled(params: Params, searchParams: SearchParams): Promise<boolean> {
25+
if (searchParams.experimentalTeamBookingPageCache !== "true") return false;
26+
27+
const { teamSlug, currentOrgDomain, isValidOrgDomain } = await getOrgContext(params);
28+
const orgSlug = isValidOrgDomain ? currentOrgDomain : null;
29+
const teamId = await getTeamId(teamSlug, orgSlug);
30+
31+
if (!teamId) return false;
1932

20-
async function isCachedTeamBookingEnabled(searchParams: SearchParams): Promise<boolean> {
2133
const featuresRepository = new FeaturesRepository(prisma);
22-
const isGloballyEnabled = await featuresRepository.checkIfFeatureIsEnabledGlobally(
34+
const isTeamFeatureEnabled = await featuresRepository.checkIfTeamHasFeature(
35+
teamId,
2336
"team-booking-page-cache"
2437
);
25-
return isGloballyEnabled && searchParams.experimentalTeamBookingPageCache === "true";
38+
return isTeamFeatureEnabled;
2639
}
2740

2841
export const generateMetadata = async ({ params, searchParams }: PageProps) => {
29-
if (await isCachedTeamBookingEnabled(await searchParams)) {
42+
if (await isCachedTeamBookingEnabled(await params, await searchParams)) {
3043
return await generateCachedMetadata({ params, searchParams });
3144
}
45+
3246
const legacyCtx = buildLegacyCtx(await headers(), await cookies(), await params, await searchParams);
3347
const props = await getData(legacyCtx);
3448
const { booking, isSEOIndexable, eventData, isBrandingHidden } = props;
@@ -68,7 +82,7 @@ export const generateMetadata = async ({ params, searchParams }: PageProps) => {
6882
const getData = withAppDirSsr<LegacyPageProps>(getServerSideProps);
6983

7084
const ServerPage = async ({ params, searchParams }: PageProps) => {
71-
if (await isCachedTeamBookingEnabled(await searchParams)) {
85+
if (await isCachedTeamBookingEnabled(await params, await searchParams)) {
7286
return await CachedTeamBooker({ params, searchParams });
7387
}
7488

apps/web/app/(booking-page-wrapper)/team/[slug]/[type]/pageWithCachedData.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const _getTeamMetadataForBooking = (teamData: NonNullable<TeamData>, eventTypeId
4141
};
4242
};
4343

44-
async function _getOrgContext(params: Params) {
44+
export async function getOrgContext(params: Params) {
4545
const result = paramsSchema.safeParse({
4646
slug: params?.slug,
4747
type: params?.type,
@@ -74,7 +74,7 @@ const _getMultipleDurationValue = (
7474
};
7575

7676
export const generateMetadata = async ({ params, searchParams }: PageProps) => {
77-
const { currentOrgDomain, isValidOrgDomain, teamSlug, meetingSlug } = await _getOrgContext(await params);
77+
const { currentOrgDomain, isValidOrgDomain, teamSlug, meetingSlug } = await getOrgContext(await params);
7878

7979
const teamData = await getCachedTeamData(teamSlug, currentOrgDomain);
8080
if (!teamData) return {}; // should never happen
@@ -123,7 +123,7 @@ export const generateMetadata = async ({ params, searchParams }: PageProps) => {
123123
};
124124

125125
const CachedTeamBooker = async ({ params, searchParams }: PageProps) => {
126-
const { currentOrgDomain, isValidOrgDomain, teamSlug, meetingSlug } = await _getOrgContext(await params);
126+
const { currentOrgDomain, isValidOrgDomain, teamSlug, meetingSlug } = await getOrgContext(await params);
127127
const legacyCtx = buildLegacyCtx(await headers(), await cookies(), await params, await searchParams);
128128

129129
// Handle org redirects

apps/web/app/(booking-page-wrapper)/team/[slug]/[type]/queries.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { getTeamEventType } from "@calcom/features/eventtypes/lib/getTeamEventTy
1313
import { FeaturesRepository } from "@calcom/features/flags/features.repository";
1414
import { NEXTJS_CACHE_TTL } from "@calcom/lib/constants";
1515
import { getPlaceholderAvatar } from "@calcom/lib/defaultAvatarImage";
16+
import { TeamRepository } from "@calcom/lib/server/repository/team";
1617
import { UserRepository } from "@calcom/lib/server/repository/user";
1718
import { prisma } from "@calcom/prisma";
1819
import type { SchedulingType } from "@calcom/prisma/enums";
@@ -158,3 +159,14 @@ export async function getCRMData(
158159
crmRecordId,
159160
};
160161
}
162+
163+
export async function getTeamId(teamSlug: string, orgSlug: string | null): Promise<number | null> {
164+
const teamRepo = new TeamRepository(prisma);
165+
const team = await teamRepo.findFirstBySlugAndParentSlug({
166+
slug: teamSlug,
167+
parentSlug: orgSlug,
168+
select: { id: true },
169+
});
170+
171+
return team?.id ?? null;
172+
}

0 commit comments

Comments
 (0)