|
| 1 | +import { CustomI18nProvider } from "app/CustomI18nProvider"; |
| 2 | +import type { PageProps, Params } from "app/_types"; |
| 3 | +import { generateMeetingMetadata } from "app/_utils"; |
| 4 | +import { headers, cookies } from "next/headers"; |
| 5 | +import { notFound, redirect } from "next/navigation"; |
| 6 | +import { z } from "zod"; |
| 7 | + |
| 8 | +import { getServerSession } from "@calcom/features/auth/lib/getServerSession"; |
| 9 | +import { getBookingForReschedule, type GetBookingType } from "@calcom/features/bookings/lib/get-booking"; |
| 10 | +import { getOrgFullOrigin, orgDomainConfig } from "@calcom/features/ee/organizations/lib/orgDomains"; |
| 11 | +import { getOrganizationSEOSettings } from "@calcom/features/ee/organizations/lib/orgSettings"; |
| 12 | +import type { TeamData } from "@calcom/features/ee/teams/lib/getTeamData"; |
| 13 | +import { shouldHideBrandingForTeamEvent } from "@calcom/lib/hideBranding"; |
| 14 | +import { loadTranslations } from "@calcom/lib/server/i18n"; |
| 15 | +import slugify from "@calcom/lib/slugify"; |
| 16 | +import { BookingStatus, RedirectType } from "@calcom/prisma/enums"; |
| 17 | + |
| 18 | +import { buildLegacyCtx, buildLegacyRequest } from "@lib/buildLegacyCtx"; |
| 19 | +import { getTemporaryOrgRedirect } from "@lib/getTemporaryOrgRedirect"; |
| 20 | + |
| 21 | +import CachedClientView, { type TeamBookingPageProps } from "~/team/type-view-cached"; |
| 22 | + |
| 23 | +import { getCachedTeamData, getEnrichedEventType, getCRMData, shouldUseApiV2ForTeamSlots } from "./queries"; |
| 24 | + |
| 25 | +const paramsSchema = z.object({ |
| 26 | + slug: z.string().transform((s) => slugify(s)), |
| 27 | + type: z.string().transform((s) => slugify(s)), |
| 28 | +}); |
| 29 | + |
| 30 | +const _getTeamMetadataForBooking = (teamData: NonNullable<TeamData>, eventTypeId: number) => { |
| 31 | + const organizationSettings = getOrganizationSEOSettings(teamData); |
| 32 | + const allowSEOIndexing = organizationSettings?.allowSEOIndexing ?? false; |
| 33 | + |
| 34 | + return { |
| 35 | + orgBannerUrl: teamData.parent?.bannerUrl ?? "", |
| 36 | + hideBranding: shouldHideBrandingForTeamEvent({ |
| 37 | + eventTypeId, |
| 38 | + team: teamData, |
| 39 | + }), |
| 40 | + isSEOIndexable: allowSEOIndexing, |
| 41 | + }; |
| 42 | +}; |
| 43 | + |
| 44 | +async function _getOrgContext(params: Params) { |
| 45 | + const result = paramsSchema.safeParse({ |
| 46 | + slug: params?.slug, |
| 47 | + type: params?.type, |
| 48 | + }); |
| 49 | + |
| 50 | + if (!result.success) return notFound(); // should never happen |
| 51 | + |
| 52 | + const { slug: teamSlug, type: meetingSlug } = result.data; |
| 53 | + const { currentOrgDomain, isValidOrgDomain } = orgDomainConfig( |
| 54 | + buildLegacyRequest(await headers(), await cookies()), |
| 55 | + params?.orgSlug ?? undefined |
| 56 | + ); |
| 57 | + |
| 58 | + return { |
| 59 | + currentOrgDomain, |
| 60 | + isValidOrgDomain, |
| 61 | + teamSlug, |
| 62 | + meetingSlug, |
| 63 | + }; |
| 64 | +} |
| 65 | + |
| 66 | +const _getMultipleDurationValue = ( |
| 67 | + multipleDurationConfig: number[] | undefined, |
| 68 | + queryDuration: string | string[] | null | undefined, |
| 69 | + defaultValue: number |
| 70 | +) => { |
| 71 | + if (!multipleDurationConfig) return null; |
| 72 | + if (multipleDurationConfig.includes(Number(queryDuration))) return Number(queryDuration); |
| 73 | + return defaultValue; |
| 74 | +}; |
| 75 | + |
| 76 | +export const generateMetadata = async ({ params, searchParams }: PageProps) => { |
| 77 | + const { currentOrgDomain, isValidOrgDomain, teamSlug, meetingSlug } = await _getOrgContext(await params); |
| 78 | + |
| 79 | + const teamData = await getCachedTeamData(teamSlug, currentOrgDomain); |
| 80 | + if (!teamData) return {}; // should never happen |
| 81 | + |
| 82 | + const enrichedEventType = await getEnrichedEventType({ |
| 83 | + teamSlug, |
| 84 | + meetingSlug, |
| 85 | + orgSlug: isValidOrgDomain ? currentOrgDomain : null, |
| 86 | + fromRedirectOfNonOrgLink: (await searchParams).orgRedirection === "true", |
| 87 | + }); |
| 88 | + if (!enrichedEventType) return {}; // should never happen |
| 89 | + |
| 90 | + const title = enrichedEventType.title; |
| 91 | + const profileName = enrichedEventType.profile.name ?? ""; |
| 92 | + const profileImage = enrichedEventType.profile.image; |
| 93 | + |
| 94 | + const meeting = { |
| 95 | + title, |
| 96 | + profile: { name: profileName, image: profileImage }, |
| 97 | + users: [ |
| 98 | + ...(enrichedEventType?.subsetOfUsers || []).map((user) => ({ |
| 99 | + name: `${user.name}`, |
| 100 | + username: `${user.username}`, |
| 101 | + })), |
| 102 | + ], |
| 103 | + }; |
| 104 | + |
| 105 | + const { hideBranding, isSEOIndexable } = _getTeamMetadataForBooking(teamData, enrichedEventType.id); |
| 106 | + |
| 107 | + const metadata = await generateMeetingMetadata( |
| 108 | + meeting, |
| 109 | + () => `${title} | ${profileName}`, |
| 110 | + () => title, |
| 111 | + hideBranding, |
| 112 | + getOrgFullOrigin(enrichedEventType.entity.orgSlug ?? null), |
| 113 | + `/team/${teamSlug}/${meetingSlug}` |
| 114 | + ); |
| 115 | + |
| 116 | + return { |
| 117 | + ...metadata, |
| 118 | + robots: { |
| 119 | + follow: !(enrichedEventType.hidden || !isSEOIndexable), |
| 120 | + index: !(enrichedEventType.hidden || !isSEOIndexable), |
| 121 | + }, |
| 122 | + }; |
| 123 | +}; |
| 124 | + |
| 125 | +const CachedTeamBooker = async ({ params, searchParams }: PageProps) => { |
| 126 | + const { currentOrgDomain, isValidOrgDomain, teamSlug, meetingSlug } = await _getOrgContext(await params); |
| 127 | + const isOrgContext = currentOrgDomain && isValidOrgDomain; |
| 128 | + const legacyCtx = buildLegacyCtx(await headers(), await cookies(), await params, await searchParams); |
| 129 | + |
| 130 | + // Handle org redirects for non-org contexts |
| 131 | + if (!isOrgContext) { |
| 132 | + const redirectResult = await getTemporaryOrgRedirect({ |
| 133 | + slugs: teamSlug, |
| 134 | + redirectType: RedirectType.Team, |
| 135 | + eventTypeSlug: meetingSlug, |
| 136 | + currentQuery: legacyCtx.query, |
| 137 | + }); |
| 138 | + if (redirectResult) return redirect(redirectResult.redirect.destination); |
| 139 | + } |
| 140 | + |
| 141 | + const teamData = await getCachedTeamData(teamSlug, currentOrgDomain); |
| 142 | + |
| 143 | + if (!teamData) return notFound(); |
| 144 | + |
| 145 | + const enrichedEventType = await getEnrichedEventType({ |
| 146 | + teamSlug, |
| 147 | + meetingSlug, |
| 148 | + orgSlug: isValidOrgDomain ? currentOrgDomain : null, |
| 149 | + fromRedirectOfNonOrgLink: legacyCtx.query.orgRedirection === "true", |
| 150 | + }); |
| 151 | + if (!enrichedEventType) return notFound(); |
| 152 | + |
| 153 | + // Handle rescheduling |
| 154 | + const { rescheduleUid } = legacyCtx.query; |
| 155 | + let bookingForReschedule: GetBookingType | null = null; |
| 156 | + if (rescheduleUid) { |
| 157 | + const session = await getServerSession({ req: legacyCtx.req }); |
| 158 | + bookingForReschedule = await getBookingForReschedule(`${rescheduleUid}`, session?.user?.id); |
| 159 | + if (enrichedEventType.disableRescheduling) return redirect(`/booking/${rescheduleUid}`); |
| 160 | + |
| 161 | + if ( |
| 162 | + bookingForReschedule?.status === BookingStatus.CANCELLED && |
| 163 | + legacyCtx.query.allowRescheduleForCancelledBooking !== "true" && |
| 164 | + !enrichedEventType.allowReschedulingCancelledBookings |
| 165 | + ) { |
| 166 | + // redirecting to the same booking page without `rescheduleUid` search param |
| 167 | + return redirect(`/team/${teamSlug}/${meetingSlug}`); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + const [crmData, useApiV2] = await Promise.all([ |
| 172 | + getCRMData(legacyCtx.query, { |
| 173 | + id: enrichedEventType.id, |
| 174 | + isInstantEvent: enrichedEventType.isInstantEvent, |
| 175 | + schedulingType: enrichedEventType.schedulingType, |
| 176 | + metadata: enrichedEventType.metadata, |
| 177 | + length: enrichedEventType.length, |
| 178 | + }), |
| 179 | + shouldUseApiV2ForTeamSlots(teamData.id), |
| 180 | + ]); |
| 181 | + |
| 182 | + const props: TeamBookingPageProps = { |
| 183 | + ..._getTeamMetadataForBooking(teamData, enrichedEventType.id), |
| 184 | + ...crmData, |
| 185 | + useApiV2, |
| 186 | + isInstantMeeting: legacyCtx.query.isInstantMeeting === "true", |
| 187 | + eventSlug: meetingSlug, |
| 188 | + username: teamSlug, |
| 189 | + eventData: { |
| 190 | + ...enrichedEventType, |
| 191 | + }, |
| 192 | + entity: { ...enrichedEventType.entity }, |
| 193 | + bookingData: bookingForReschedule, |
| 194 | + isTeamEvent: true, |
| 195 | + durationConfig: enrichedEventType.metadata?.multipleDuration, |
| 196 | + duration: _getMultipleDurationValue( |
| 197 | + enrichedEventType.metadata?.multipleDuration, |
| 198 | + legacyCtx.query.duration, |
| 199 | + enrichedEventType.length |
| 200 | + ), |
| 201 | + }; |
| 202 | + const Booker = <CachedClientView {...props} />; |
| 203 | + |
| 204 | + const eventLocale = enrichedEventType.interfaceLanguage; |
| 205 | + if (eventLocale) { |
| 206 | + const ns = "common"; |
| 207 | + const translations = await loadTranslations(eventLocale, ns); |
| 208 | + return ( |
| 209 | + <CustomI18nProvider translations={translations} locale={eventLocale} ns={ns}> |
| 210 | + {Booker} |
| 211 | + </CustomI18nProvider> |
| 212 | + ); |
| 213 | + } |
| 214 | + |
| 215 | + return Booker; |
| 216 | +}; |
| 217 | + |
| 218 | +export default CachedTeamBooker; |
0 commit comments