From c6ee4824783290694161f63d4f76669ba21b8537 Mon Sep 17 00:00:00 2001 From: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com> Date: Mon, 4 Mar 2024 21:18:13 +0530 Subject: [PATCH] fix: email embed for org (#13917) * fix: email embed for org * chore: use orgslig * chore: remove unused * fix: org details * test: add tests * chore: type err * fix: import missing * fix: check timeslots --- apps/web/test/lib/getSchedule.test.ts | 72 +++++++++++++++++++ .../utils/bookingScenario/bookingScenario.ts | 12 +++- .../features/bookings/Booker/utils/event.ts | 4 ++ packages/features/embed/Embed.tsx | 19 ++++- .../schedules/lib/use-schedule/useSchedule.ts | 3 + .../trpc/server/routers/viewer/slots/types.ts | 1 + .../trpc/server/routers/viewer/slots/util.ts | 9 ++- 7 files changed, 115 insertions(+), 5 deletions(-) diff --git a/apps/web/test/lib/getSchedule.test.ts b/apps/web/test/lib/getSchedule.test.ts index 1bab2e6438..fa3df7711c 100644 --- a/apps/web/test/lib/getSchedule.test.ts +++ b/apps/web/test/lib/getSchedule.test.ts @@ -13,11 +13,15 @@ import { getDate, getGoogleCalendarCredential, createBookingScenario, + createOrganization, + getOrganizer, + getScenarioData, } from "../utils/bookingScenario/bookingScenario"; vi.mock("@calcom/lib/constants", () => ({ IS_PRODUCTION: true, WEBAPP_URL: "http://localhost:3000", + RESERVED_SUBDOMAINS: ["auth", "docs"], })); declare global { @@ -1255,5 +1259,73 @@ describe("getSchedule", () => { { dateString: plus3DateString } ); }); + + test("getSchedule can get slots of org's member event type when orgSlug, eventTypeSlug passed as input", async () => { + const org = await createOrganization({ name: "acme", slug: "acme" }); + + const organizer = getOrganizer({ + name: "Organizer", + email: "organizer@example.com", + id: 101, + // So, that it picks the first schedule from the list + defaultScheduleId: null, + organizationId: org.id, + // Has morning shift with some overlap with morning shift + schedules: [TestData.schedules.IstWorkHours], + }); + + const scenario = await createBookingScenario( + getScenarioData( + { + eventTypes: [ + { + id: 1, + slotInterval: 45, + length: 45, + users: [ + { + id: 101, + }, + ], + }, + ], + organizer, + }, + { id: org.id } + ) + ); + + const { dateString: plus1DateString } = getDate({ dateIncrement: 1 }); + const { dateString: plus2DateString } = getDate({ dateIncrement: 2 }); + + const getScheduleRes = await getSchedule({ + input: { + eventTypeSlug: scenario.eventTypes[0]?.slug, + startTime: `${plus1DateString}T18:30:00.000Z`, + endTime: `${plus2DateString}T18:29:59.999Z`, + timeZone: Timezones["+5:30"], + isTeamEvent: false, + orgSlug: "acme", + usernameList: [organizer.username], + }, + }); + + expect(getScheduleRes).toHaveTimeSlots( + [ + `04:00:00.000Z`, + `04:45:00.000Z`, + `05:30:00.000Z`, + `06:15:00.000Z`, + `07:00:00.000Z`, + `07:45:00.000Z`, + `08:30:00.000Z`, + `09:15:00.000Z`, + `10:00:00.000Z`, + `10:45:00.000Z`, + `11:30:00.000Z`, + ], + { dateString: plus2DateString } + ); + }); }); }); diff --git a/apps/web/test/utils/bookingScenario/bookingScenario.ts b/apps/web/test/utils/bookingScenario/bookingScenario.ts index 5b6a59db07..de929ff154 100644 --- a/apps/web/test/utils/bookingScenario/bookingScenario.ts +++ b/apps/web/test/utils/bookingScenario/bookingScenario.ts @@ -9,6 +9,7 @@ import type Stripe from "stripe"; import type { getMockRequestDataForBooking } from "test/utils/bookingScenario/getMockRequestDataForBooking"; import { v4 as uuidv4 } from "uuid"; import "vitest-fetch-mock"; +import type { z } from "zod"; import { appStoreMetadata } from "@calcom/app-store/appStoreMetaData"; import { handleStripePaymentSuccess } from "@calcom/features/ee/payments/api/webhook"; @@ -20,6 +21,7 @@ import { ProfileRepository } from "@calcom/lib/server/repository/profile"; import type { WorkflowActions, WorkflowTemplates, WorkflowTriggerEvents } from "@calcom/prisma/client"; import type { SchedulingType } from "@calcom/prisma/enums"; import type { BookingStatus } from "@calcom/prisma/enums"; +import type { teamMetadataSchema } from "@calcom/prisma/zod-utils"; import type { userMetadataType } from "@calcom/prisma/zod-utils"; import type { AppMeta } from "@calcom/types/App"; import type { NewCalendarEventType } from "@calcom/types/Calendar"; @@ -620,11 +622,19 @@ export async function createBookingScenario(data: ScenarioData) { }; } -export async function createOrganization(orgData: { name: string; slug: string }) { +export async function createOrganization(orgData: { + name: string; + slug: string; + metadata?: z.infer; +}) { const org = await prismock.team.create({ data: { name: orgData.name, slug: orgData.slug, + metadata: { + ...(orgData.metadata || {}), + isOrganization: true, + }, }, }); return org; diff --git a/packages/features/bookings/Booker/utils/event.ts b/packages/features/bookings/Booker/utils/event.ts index e963b2eebd..763b85941f 100644 --- a/packages/features/bookings/Booker/utils/event.ts +++ b/packages/features/bookings/Booker/utils/event.ts @@ -52,6 +52,7 @@ export const useScheduleForEvent = ({ monthCount, dayCount, selectedDate, + orgSlug, }: { prefetchNextMonth?: boolean; username?: string | null; @@ -62,6 +63,7 @@ export const useScheduleForEvent = ({ monthCount?: number; dayCount?: number | null; selectedDate?: string | null; + orgSlug?: string; } = {}) => { const { timezone } = useTimePreferences(); const event = useEvent(); @@ -69,6 +71,7 @@ export const useScheduleForEvent = ({ (state) => [state.username, state.eventSlug, state.month, state.selectedDuration], shallow ); + const searchParams = useCompatSearchParams(); const rescheduleUid = searchParams?.get("rescheduleUid"); @@ -89,5 +92,6 @@ export const useScheduleForEvent = ({ month: monthFromStore ?? month, duration: durationFromStore ?? duration, isTeamEvent: pathname?.indexOf("/team/") !== -1 || isTeam, + orgSlug, }); }; diff --git a/packages/features/embed/Embed.tsx b/packages/features/embed/Embed.tsx index 14fc4f93d6..9a9afcc0fd 100644 --- a/packages/features/embed/Embed.tsx +++ b/packages/features/embed/Embed.tsx @@ -127,7 +127,15 @@ const ChooseEmbedTypesDialogContent = ({ types }: { types: EmbedTypes }) => { ); }; -const EmailEmbed = ({ eventType, username }: { eventType?: EventType; username: string }) => { +const EmailEmbed = ({ + eventType, + username, + orgSlug, +}: { + eventType?: EventType; + username: string; + orgSlug?: string; +}) => { const { t, i18n } = useLocale(); const [timezone] = useTimePreferences((state) => [state.timezone]); @@ -153,7 +161,7 @@ const EmailEmbed = ({ eventType, username }: { eventType?: EventType; username: shallow ); const event = useEvent(); - const schedule = useScheduleForEvent(); + const schedule = useScheduleForEvent({ orgSlug }); const nonEmptyScheduleDays = useNonEmptyScheduleDays(schedule?.data?.slots); const onTimeSelect = (time: string) => { @@ -516,6 +524,7 @@ const EmbedTypeCodeAndPreviewDialogContent = ({ const dialogContentRef = useRef(null); const emailContentRef = useRef(null); const { data } = useSession(); + const [month, selectedDatesAndTimes] = useBookerStore( (state) => [state.month, state.selectedDatesAndTimes], shallow @@ -710,7 +719,11 @@ const EmbedTypeCodeAndPreviewDialogContent = ({

{embed.subtitle}

{eventTypeData?.eventType && embedType === "email" ? ( - + ) : (
diff --git a/packages/features/schedules/lib/use-schedule/useSchedule.ts b/packages/features/schedules/lib/use-schedule/useSchedule.ts index 6f8f502f44..d84cb227aa 100644 --- a/packages/features/schedules/lib/use-schedule/useSchedule.ts +++ b/packages/features/schedules/lib/use-schedule/useSchedule.ts @@ -15,6 +15,7 @@ type UseScheduleWithCacheArgs = { dayCount?: number | null; rescheduleUid?: string | null; isTeamEvent?: boolean; + orgSlug?: string; }; export const useSchedule = ({ @@ -30,6 +31,7 @@ export const useSchedule = ({ dayCount, rescheduleUid, isTeamEvent, + orgSlug, }: UseScheduleWithCacheArgs) => { const now = dayjs(); const monthDayjs = month ? dayjs(month) : now; @@ -72,6 +74,7 @@ export const useSchedule = ({ timeZone: timezone!, duration: duration ? `${duration}` : undefined, rescheduleUid, + orgSlug, }, { trpc: { diff --git a/packages/trpc/server/routers/viewer/slots/types.ts b/packages/trpc/server/routers/viewer/slots/types.ts index aca1834f62..76b2e8c66d 100644 --- a/packages/trpc/server/routers/viewer/slots/types.ts +++ b/packages/trpc/server/routers/viewer/slots/types.ts @@ -23,6 +23,7 @@ export const getScheduleSchema = z rescheduleUid: z.string().optional().nullable(), // whether to do team event or user event isTeamEvent: z.boolean().optional().default(false), + orgSlug: z.string().optional(), }) .transform((val) => { // Need this so we can pass a single username in the query string form public API diff --git a/packages/trpc/server/routers/viewer/slots/util.ts b/packages/trpc/server/routers/viewer/slots/util.ts index 99de4a23bc..b8bf34d862 100644 --- a/packages/trpc/server/routers/viewer/slots/util.ts +++ b/packages/trpc/server/routers/viewer/slots/util.ts @@ -11,6 +11,7 @@ import dayjs from "@calcom/dayjs"; import { getSlugOrRequestedSlug, orgDomainConfig } from "@calcom/ee/organizations/lib/orgDomains"; import { isEventTypeLoggingEnabled } from "@calcom/features/bookings/lib/isEventTypeLoggingEnabled"; import { parseBookingLimit, parseDurationLimit } from "@calcom/lib"; +import { RESERVED_SUBDOMAINS } from "@calcom/lib/constants"; import { getDefaultEvent } from "@calcom/lib/defaultEvents"; import isTimeOutOfBounds from "@calcom/lib/isOutOfBounds"; import logger from "@calcom/lib/logger"; @@ -296,7 +297,13 @@ function applyOccupiedSeatsToCurrentSeats(currentSeats: CurrentSeats, occupiedSe } export async function getAvailableSlots({ input, ctx }: GetScheduleOptions) { - const orgDetails = orgDomainConfig(ctx?.req); + const orgDetails = input?.orgSlug + ? { + currentOrgDomain: input.orgSlug, + isValidOrgDomain: !!input.orgSlug && !RESERVED_SUBDOMAINS.includes(input.orgSlug), + } + : orgDomainConfig(ctx?.req); + if (process.env.INTEGRATION_TEST_MODE === "true") { logger.settings.minLevel = 2; }