diff --git a/apps/web/app/(use-page-wrapper)/apps/routing-forms/forms/[[...pages]]/Forms.tsx b/apps/web/app/(use-page-wrapper)/apps/routing-forms/forms/[[...pages]]/Forms.tsx index 20c38e8448..08f949f681 100644 --- a/apps/web/app/(use-page-wrapper)/apps/routing-forms/forms/[[...pages]]/Forms.tsx +++ b/apps/web/app/(use-page-wrapper)/apps/routing-forms/forms/[[...pages]]/Forms.tsx @@ -6,6 +6,7 @@ import { useFormContext } from "react-hook-form"; import { isFallbackRoute } from "@calcom/app-store/routing-forms/lib/isFallbackRoute"; import type { RoutingFormWithResponseCount } from "@calcom/app-store/routing-forms/types/types"; +import { useHasPaidPlan } from "@calcom/features/billing/hooks/useHasPaidPlan"; import LicenseRequired from "@calcom/features/ee/common/components/LicenseRequired"; import SkeletonLoaderTeamList from "@calcom/features/ee/teams/components/SkeletonloaderTeamList"; import { CreateButtonWithTeamsList } from "@calcom/features/ee/teams/components/createButton/CreateButtonWithTeamsList"; @@ -15,7 +16,6 @@ import { getTeamsFiltersFromQuery } from "@calcom/features/filters/lib/getTeamsF import { ShellMain } from "@calcom/features/shell/Shell"; import { UpgradeTip } from "@calcom/features/tips"; import { WEBAPP_URL } from "@calcom/lib/constants"; -import { useHasPaidPlan } from "@calcom/lib/hooks/useHasPaidPlan"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { useRouterQuery } from "@calcom/lib/hooks/useRouterQuery"; import { MembershipRole } from "@calcom/prisma/enums"; diff --git a/apps/web/app/api/defaultResponderForAppDir.ts b/apps/web/app/api/defaultResponderForAppDir.ts index 9c04ab9d2a..8ff9ff3b4e 100644 --- a/apps/web/app/api/defaultResponderForAppDir.ts +++ b/apps/web/app/api/defaultResponderForAppDir.ts @@ -3,9 +3,13 @@ import { ApiError } from "next/dist/server/api-utils"; import type { NextRequest } from "next/server"; import { NextResponse } from "next/server"; +import { HttpError } from "@calcom/lib/http-error"; import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown"; import { performance } from "@calcom/lib/server/perfObserver"; +import { TRPCError } from "@trpc/server"; +import { getHTTPStatusCodeFromError } from "@trpc/server/http"; + type Handler = ( req: NextRequest, { params }: { params: Promise } @@ -34,15 +38,18 @@ export const defaultResponderForAppDir = { return { - boot: (_props: IntercomBootProps) => {}, show: noop, shutdown: noop, - update: (_props: Partial) => {}, }; }; diff --git a/packages/features/ee/teams/components/TeamInviteList.tsx b/packages/features/ee/teams/components/TeamInviteList.tsx index 7428a6ba46..aebb8e47e1 100644 --- a/packages/features/ee/teams/components/TeamInviteList.tsx +++ b/packages/features/ee/teams/components/TeamInviteList.tsx @@ -1,6 +1,6 @@ import { useState } from "react"; -import { trackFormbricksAction } from "@calcom/lib/formbricks-client"; +import { trackFormbricksAction } from "@calcom/features/formbricks/formbricks-client"; import type { MembershipRole } from "@calcom/prisma/enums"; import { trpc } from "@calcom/trpc/react"; import { showToast } from "@calcom/ui/components/toast"; diff --git a/packages/features/ee/teams/components/TeamList.tsx b/packages/features/ee/teams/components/TeamList.tsx index a6c5e37f3d..ebddcbfc07 100644 --- a/packages/features/ee/teams/components/TeamList.tsx +++ b/packages/features/ee/teams/components/TeamList.tsx @@ -1,10 +1,10 @@ import { useState } from "react"; +import { trackFormbricksAction } from "@calcom/features/formbricks/formbricks-client"; import { ORG_SELF_SERVE_ENABLED, ORG_MINIMUM_PUBLISHED_TEAMS_SELF_SERVE_HELPER_DIALOGUE, } from "@calcom/lib/constants"; -import { trackFormbricksAction } from "@calcom/lib/formbricks-client"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import type { RouterOutputs } from "@calcom/trpc/react"; import { trpc } from "@calcom/trpc/react"; diff --git a/packages/features/ee/teams/pages/team-profile-view.tsx b/packages/features/ee/teams/pages/team-profile-view.tsx index 5d8827fe60..81249a24cd 100644 --- a/packages/features/ee/teams/pages/team-profile-view.tsx +++ b/packages/features/ee/teams/pages/team-profile-view.tsx @@ -11,10 +11,10 @@ import { z } from "zod"; import { checkAdminOrOwner } from "@calcom/features/auth/lib/checkAdminOrOwner"; import { Dialog } from "@calcom/features/components/controlled-dialog"; import { getTeamUrlSync } from "@calcom/features/ee/organizations/lib/getTeamUrlSync"; +import { trackFormbricksAction } from "@calcom/features/formbricks/formbricks-client"; import SectionBottomActions from "@calcom/features/settings/SectionBottomActions"; import { IS_TEAM_BILLING_ENABLED, WEBAPP_URL } from "@calcom/lib/constants"; import { getPlaceholderAvatar } from "@calcom/lib/defaultAvatarImage"; -import { trackFormbricksAction } from "@calcom/lib/formbricks-client"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { useParamsWithFallback } from "@calcom/lib/hooks/useParamsWithFallback"; import { md } from "@calcom/lib/markdownIt"; diff --git a/packages/features/ee/video/ViewRecordingsDialog.tsx b/packages/features/ee/video/ViewRecordingsDialog.tsx index 534f257092..ad1203fe6a 100644 --- a/packages/features/ee/video/ViewRecordingsDialog.tsx +++ b/packages/features/ee/video/ViewRecordingsDialog.tsx @@ -4,9 +4,9 @@ import { useRouter } from "next/navigation"; import { Suspense, useEffect, useState } from "react"; import dayjs from "@calcom/dayjs"; +import { useHasTeamPlan } from "@calcom/features/billing/hooks/useHasPaidPlan"; import { Dialog } from "@calcom/features/components/controlled-dialog"; import LicenseRequired from "@calcom/features/ee/common/components/LicenseRequired"; -import { useHasTeamPlan } from "@calcom/lib/hooks/useHasPaidPlan"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import type { RecordingItemSchema } from "@calcom/prisma/zod-utils"; import type { RouterOutputs } from "@calcom/trpc/react"; diff --git a/packages/features/ee/workflows/components/WorkflowDetailsPage.tsx b/packages/features/ee/workflows/components/WorkflowDetailsPage.tsx index afb3902ecf..7d44ca9c21 100644 --- a/packages/features/ee/workflows/components/WorkflowDetailsPage.tsx +++ b/packages/features/ee/workflows/components/WorkflowDetailsPage.tsx @@ -3,9 +3,9 @@ import type { Dispatch, SetStateAction } from "react"; import { useState, useEffect } from "react"; import type { UseFormReturn } from "react-hook-form"; +import type { WorkflowPermissions } from "@calcom/features/workflows/repositories/WorkflowPermissionsRepository"; import { SENDER_ID, SENDER_NAME, SCANNING_WORKFLOW_STEPS } from "@calcom/lib/constants"; import { useLocale } from "@calcom/lib/hooks/useLocale"; -import type { WorkflowPermissions } from "@calcom/lib/server/repository/workflow-permissions"; import { getTimeFormatStringFromUserTimeFormat } from "@calcom/lib/timeFormat"; import { WorkflowActions } from "@calcom/prisma/enums"; import { WorkflowTemplates } from "@calcom/prisma/enums"; diff --git a/packages/features/ee/workflows/components/WorkflowListPage.tsx b/packages/features/ee/workflows/components/WorkflowListPage.tsx index 1b831db1a0..8349f11ade 100644 --- a/packages/features/ee/workflows/components/WorkflowListPage.tsx +++ b/packages/features/ee/workflows/components/WorkflowListPage.tsx @@ -3,9 +3,9 @@ import Link from "next/link"; import { useRouter } from "next/navigation"; import { useState } from "react"; +import type { WorkflowPermissions } from "@calcom/features/workflows/repositories/WorkflowPermissionsRepository"; import { getPlaceholderAvatar } from "@calcom/lib/defaultAvatarImage"; import { useLocale } from "@calcom/lib/hooks/useLocale"; -import type { WorkflowPermissions } from "@calcom/lib/server/repository/workflow-permissions"; import type { Membership, Workflow } from "@calcom/prisma/client"; import { trpc } from "@calcom/trpc/react"; import classNames from "@calcom/ui/classNames"; diff --git a/packages/features/ee/workflows/components/WorkflowStepContainer.tsx b/packages/features/ee/workflows/components/WorkflowStepContainer.tsx index 4438997af1..0eae491dcb 100644 --- a/packages/features/ee/workflows/components/WorkflowStepContainer.tsx +++ b/packages/features/ee/workflows/components/WorkflowStepContainer.tsx @@ -6,12 +6,12 @@ import type { UseFormReturn } from "react-hook-form"; import { Controller, useWatch } from "react-hook-form"; import "react-phone-number-input/style.css"; +import { useHasActiveTeamPlan } from "@calcom/features/billing/hooks/useHasPaidPlan"; import type { RetellAgentWithDetails } from "@calcom/features/calAIPhone/providers/retellAI"; import { Dialog } from "@calcom/features/components/controlled-dialog"; import PhoneInput from "@calcom/features/components/phone-input"; import { SENDER_ID, SENDER_NAME } from "@calcom/lib/constants"; import { formatPhoneNumber } from "@calcom/lib/formatPhoneNumber"; -import { useHasActiveTeamPlan } from "@calcom/lib/hooks/useHasPaidPlan"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import useMediaQuery from "@calcom/lib/hooks/useMediaQuery"; import { HttpError } from "@calcom/lib/http-error"; diff --git a/packages/features/ee/workflows/pages/workflow.tsx b/packages/features/ee/workflows/pages/workflow.tsx index a88f401b45..76983ec958 100644 --- a/packages/features/ee/workflows/pages/workflow.tsx +++ b/packages/features/ee/workflows/pages/workflow.tsx @@ -8,10 +8,10 @@ import { useForm, useWatch } from "react-hook-form"; import { Toaster } from "sonner"; import type { WorkflowRepository } from "@calcom/features/ee/workflows/repositories/WorkflowRepository"; +import type { WorkflowPermissions } from "@calcom/features/workflows/repositories/WorkflowPermissionsRepository"; import { SENDER_ID } from "@calcom/lib/constants"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { HttpError } from "@calcom/lib/http-error"; -import type { WorkflowPermissions } from "@calcom/lib/server/repository/workflow-permissions"; import type { WorkflowStep } from "@calcom/prisma/client"; import type { TimeUnit, WorkflowTriggerEvents } from "@calcom/prisma/enums"; import { WorkflowActions } from "@calcom/prisma/enums"; diff --git a/packages/lib/formbricks-client.ts b/packages/features/formbricks/formbricks-client.ts similarity index 100% rename from packages/lib/formbricks-client.ts rename to packages/features/formbricks/formbricks-client.ts diff --git a/packages/lib/server/repository/schedule.ts b/packages/features/schedules/repositories/ScheduleRepository.ts similarity index 97% rename from packages/lib/server/repository/schedule.ts rename to packages/features/schedules/repositories/ScheduleRepository.ts index 3842e9e98c..4d25018f81 100644 --- a/packages/lib/server/repository/schedule.ts +++ b/packages/features/schedules/repositories/ScheduleRepository.ts @@ -1,13 +1,12 @@ -import type { PrismaClient } from "@calcom/prisma"; -import prisma from "@calcom/prisma"; -import { getDefaultScheduleId } from "@calcom/trpc/server/routers/viewer/availability/util"; - -import { hasReadPermissionsForUserId } from "../../hasEditPermissionForUser"; +import { hasReadPermissionsForUserId } from "@calcom/lib/hasEditPermissionForUser"; import { transformAvailabilityForAtom, transformDateOverridesForAtom, transformWorkingHoursForAtom, -} from "../../schedules/transformers"; +} from "@calcom/lib/schedules/transformers"; +import type { PrismaClient } from "@calcom/prisma"; +import prisma from "@calcom/prisma"; +import { getDefaultScheduleId } from "@calcom/trpc/server/routers/viewer/availability/util"; export type FindDetailedScheduleByIdReturnType = Awaited< ReturnType diff --git a/packages/features/schedules/services/ScheduleService.ts b/packages/features/schedules/services/ScheduleService.ts new file mode 100644 index 0000000000..c91d1da1e5 --- /dev/null +++ b/packages/features/schedules/services/ScheduleService.ts @@ -0,0 +1,146 @@ +import { getAvailabilityFromSchedule } from "@calcom/lib/availability"; +import { hasEditPermissionForUserID } from "@calcom/lib/hasEditPermissionForUser"; +import { HttpError } from "@calcom/lib/http-error"; +import { transformScheduleToAvailabilityForAtom } from "@calcom/lib/schedules/transformers/for-atom"; +import type { PrismaClient } from "@calcom/prisma"; +import type { TUpdateInputSchema } from "@calcom/trpc/server/routers/viewer/availability/schedule/update.schema"; +import { setupDefaultSchedule } from "@calcom/trpc/server/routers/viewer/availability/util"; +import type { TrpcSessionUser } from "@calcom/trpc/server/types"; + +interface IUpdateScheduleOptions { + input: TUpdateInputSchema; + user: Pick, "id" | "defaultScheduleId" | "timeZone">; +} + +export type UpdateScheduleResponse = Awaited>; + +export class ScheduleService { + constructor(private prisma: PrismaClient) {} + + async update({ input, user }: IUpdateScheduleOptions) { + const availability = input.schedule + ? getAvailabilityFromSchedule(input.schedule) + : (input.dateOverrides || []).map((dateOverride) => ({ + startTime: dateOverride.start, + endTime: dateOverride.end, + date: dateOverride.start, + days: [], + })); + + // Not able to update the schedule with userId where clause, so fetch schedule separately and then validate + // Bug: https://github.com/prisma/prisma/issues/7290 + const userSchedule = await this.prisma.schedule.findUnique({ + where: { + id: input.scheduleId, + }, + select: { + userId: true, + name: true, + id: true, + }, + }); + + if (!userSchedule) { + throw new HttpError({ + statusCode: 401, + message: "Unauthorized", + }); + } + + if (userSchedule?.userId !== user.id) { + const hasEditPermission = await hasEditPermissionForUserID({ + ctx: { + user, + }, + input: { memberId: userSchedule.userId }, + }); + if (!hasEditPermission) { + throw new HttpError({ + statusCode: 401, + message: "Unauthorized", + }); + } + } + + let updatedUser; + if (input.isDefault) { + const setupDefault = await setupDefaultSchedule(user.id, input.scheduleId, this.prisma); + updatedUser = setupDefault; + } + + if (!input.name) { + // TODO: Improve + // We don't want to pass the full schedule for just a set as default update + // but in the current logic, this wipes the existing availability. + // Return early to prevent this from happening. + return { + schedule: userSchedule, + isDefault: updatedUser + ? updatedUser.defaultScheduleId === input.scheduleId + : user.defaultScheduleId === input.scheduleId, + }; + } + + const schedule = await this.prisma.schedule.update({ + where: { + id: input.scheduleId, + }, + data: { + timeZone: input.timeZone, + name: input.name, + availability: { + deleteMany: { + scheduleId: { + equals: input.scheduleId, + }, + }, + createMany: { + data: [ + ...availability, + ...(input.dateOverrides || []).map((override) => ({ + date: override.start, + startTime: override.start, + endTime: override.end, + })), + ], + }, + }, + }, + select: { + id: true, + userId: true, + name: true, + availability: true, + timeZone: true, + eventType: { + select: { + id: true, + eventName: true, + }, + }, + }, + }); + + const userAvailability = transformScheduleToAvailabilityForAtom(schedule); + + return { + schedule, + availability: userAvailability, + timeZone: schedule.timeZone || user.timeZone, + isDefault: updatedUser + ? updatedUser.defaultScheduleId === schedule.id + : user.defaultScheduleId === schedule.id, + prevDefaultId: user.defaultScheduleId, + currentDefaultId: updatedUser ? updatedUser.defaultScheduleId : user.defaultScheduleId, + }; + } +} + +export const updateSchedule = async ({ + input, + user, + prisma, +}: IUpdateScheduleOptions & { prisma: PrismaClient }) => { + const service = new ScheduleService(prisma); + return service.update({ input, user }); +}; diff --git a/packages/features/settings/outOfOffice/CreateOrEditOutOfOfficeModal.tsx b/packages/features/settings/outOfOffice/CreateOrEditOutOfOfficeModal.tsx index 54dd97d87b..6f71a862c0 100644 --- a/packages/features/settings/outOfOffice/CreateOrEditOutOfOfficeModal.tsx +++ b/packages/features/settings/outOfOffice/CreateOrEditOutOfOfficeModal.tsx @@ -2,10 +2,10 @@ import { useState } from "react"; import { Controller, useForm } from "react-hook-form"; import dayjs from "@calcom/dayjs"; +import { useHasTeamPlan } from "@calcom/features/billing/hooks/useHasPaidPlan"; import { Dialog } from "@calcom/features/components/controlled-dialog"; import { useCompatSearchParams } from "@calcom/lib/hooks/useCompatSearchParams"; import { useDebounce } from "@calcom/lib/hooks/useDebounce"; -import { useHasTeamPlan } from "@calcom/lib/hooks/useHasPaidPlan"; import { useInViewObserver } from "@calcom/lib/hooks/useInViewObserver"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { trpc } from "@calcom/trpc/react"; @@ -67,7 +67,7 @@ export const CreateOrEditOutOfOfficeEntryModal = ({ value: number; label: string; avatarUrl: string | null; - }[] = !!currentlyEditingOutOfOfficeEntry + }[] = currentlyEditingOutOfOfficeEntry ? [ { value: currentlyEditingOutOfOfficeEntry.forUserId || -1, @@ -236,7 +236,7 @@ export const CreateOrEditOutOfOfficeEntryModal = ({ />
{oooMemberListOptions.map((member) => (