From becae27b457e92e37d97112b6a6af0ac91b3a02c Mon Sep 17 00:00:00 2001 From: Benny Joo Date: Mon, 8 Dec 2025 17:54:46 +0900 Subject: [PATCH] refactor: migrate `verifyCodeUnAuthenticated` from TRPC layer to features layer (#25639) * rm * update test * verifyCodeUnAuthenticated * wip --- .../auth/lib/verifyCodeUnAuthenticated.ts} | 0 .../checkIfBookerEmailIsBlocked.ts | 2 +- .../test/booking-validations.test.ts | 10 ++++--- .../test/email-verification-booking.test.ts | 26 ++++++++++++++----- .../lib/service/RegularBookingService.ts | 2 +- packages/platform/libraries/index.ts | 2 +- .../auth/verifyCodeUnAuthenticated.handler.ts | 3 +-- 7 files changed, 30 insertions(+), 15 deletions(-) rename packages/{trpc/server/routers/viewer/auth/util.ts => features/auth/lib/verifyCodeUnAuthenticated.ts} (100%) diff --git a/packages/trpc/server/routers/viewer/auth/util.ts b/packages/features/auth/lib/verifyCodeUnAuthenticated.ts similarity index 100% rename from packages/trpc/server/routers/viewer/auth/util.ts rename to packages/features/auth/lib/verifyCodeUnAuthenticated.ts diff --git a/packages/features/bookings/lib/handleNewBooking/checkIfBookerEmailIsBlocked.ts b/packages/features/bookings/lib/handleNewBooking/checkIfBookerEmailIsBlocked.ts index 1e019f6615..689be8d2d1 100644 --- a/packages/features/bookings/lib/handleNewBooking/checkIfBookerEmailIsBlocked.ts +++ b/packages/features/bookings/lib/handleNewBooking/checkIfBookerEmailIsBlocked.ts @@ -1,8 +1,8 @@ +import { verifyCodeUnAuthenticated } from "@calcom/features/auth/lib/verifyCodeUnAuthenticated"; import { ErrorCode } from "@calcom/lib/errorCodes"; import { ErrorWithCode } from "@calcom/lib/errors"; import { extractBaseEmail } from "@calcom/lib/extract-base-email"; import prisma from "@calcom/prisma"; -import { verifyCodeUnAuthenticated } from "@calcom/trpc/server/routers/viewer/auth/util"; export const checkIfBookerEmailIsBlocked = async ({ bookerEmail, diff --git a/packages/features/bookings/lib/handleNewBooking/test/booking-validations.test.ts b/packages/features/bookings/lib/handleNewBooking/test/booking-validations.test.ts index 5a1962dac9..79e9ea06cf 100644 --- a/packages/features/bookings/lib/handleNewBooking/test/booking-validations.test.ts +++ b/packages/features/bookings/lib/handleNewBooking/test/booking-validations.test.ts @@ -24,7 +24,7 @@ import { test } from "@calcom/web/test/fixtures/fixtures"; import { getNewBookingHandler } from "./getNewBookingHandler"; -vi.mock("@calcom/trpc/server/routers/viewer/auth/util", () => ({ +vi.mock("@calcom/features/auth/lib/verifyCodeUnAuthenticated", () => ({ verifyCodeUnAuthenticated: vi.fn(), })); @@ -554,7 +554,9 @@ describe("Booking Validation Specifications", () => { test("should create booking when main booker provides valid verification code", async () => { const handleNewBooking = getNewBookingHandler(); - const { verifyCodeUnAuthenticated } = await import("@calcom/trpc/server/routers/viewer/auth/util"); + const { verifyCodeUnAuthenticated } = await import( + "@calcom/features/auth/lib/verifyCodeUnAuthenticated" + ); vi.mocked(verifyCodeUnAuthenticated).mockResolvedValue(true); @@ -1067,7 +1069,9 @@ describe("Booking Validation Specifications", () => { test("should throw error when invalid verification code is provided", async () => { const handleNewBooking = getNewBookingHandler(); - const { verifyCodeUnAuthenticated } = await import("@calcom/trpc/server/routers/viewer/auth/util"); + const { verifyCodeUnAuthenticated } = await import( + "@calcom/features/auth/lib/verifyCodeUnAuthenticated" + ); vi.mocked(verifyCodeUnAuthenticated).mockResolvedValue(false); diff --git a/packages/features/bookings/lib/handleNewBooking/test/email-verification-booking.test.ts b/packages/features/bookings/lib/handleNewBooking/test/email-verification-booking.test.ts index 1925ec45a1..46b6f0e60b 100644 --- a/packages/features/bookings/lib/handleNewBooking/test/email-verification-booking.test.ts +++ b/packages/features/bookings/lib/handleNewBooking/test/email-verification-booking.test.ts @@ -21,7 +21,7 @@ import { test } from "@calcom/web/test/fixtures/fixtures"; import { getNewBookingHandler } from "./getNewBookingHandler"; -vi.mock("@calcom/trpc/server/routers/viewer/auth/util", () => ({ +vi.mock("@calcom/features/auth/lib/verifyCodeUnAuthenticated", () => ({ verifyCodeUnAuthenticated: vi.fn(), })); @@ -39,7 +39,9 @@ describe("handleNewBooking - Email Verification", () => { "should throw error when email verification is required but no verification code is provided", async () => { const handleNewBooking = getNewBookingHandler(); - const { verifyCodeUnAuthenticated } = await import("@calcom/trpc/server/routers/viewer/auth/util"); + const { verifyCodeUnAuthenticated } = await import( + "@calcom/features/auth/lib/verifyCodeUnAuthenticated" + ); const booker = getBooker({ email: "booker@example.com", @@ -99,7 +101,9 @@ describe("handleNewBooking - Email Verification", () => { "should throw error when email verification is required and verification code is invalid", async () => { const handleNewBooking = getNewBookingHandler(); - const { verifyCodeUnAuthenticated } = await import("@calcom/trpc/server/routers/viewer/auth/util"); + const { verifyCodeUnAuthenticated } = await import( + "@calcom/features/auth/lib/verifyCodeUnAuthenticated" + ); vi.mocked(verifyCodeUnAuthenticated).mockRejectedValue(new Error("Invalid verification code")); @@ -162,7 +166,9 @@ describe("handleNewBooking - Email Verification", () => { "should successfully create booking when email verification is required and valid verification code is provided", async () => { const handleNewBooking = getNewBookingHandler(); - const { verifyCodeUnAuthenticated } = await import("@calcom/trpc/server/routers/viewer/auth/util"); + const { verifyCodeUnAuthenticated } = await import( + "@calcom/features/auth/lib/verifyCodeUnAuthenticated" + ); vi.mocked(verifyCodeUnAuthenticated).mockResolvedValue(undefined); @@ -223,7 +229,9 @@ describe("handleNewBooking - Email Verification", () => { "should handle rate limiting error from verification service", async () => { const handleNewBooking = getNewBookingHandler(); - const { verifyCodeUnAuthenticated } = await import("@calcom/trpc/server/routers/viewer/auth/util"); + const { verifyCodeUnAuthenticated } = await import( + "@calcom/features/auth/lib/verifyCodeUnAuthenticated" + ); const rateLimitError = new Error("Rate limit exceeded"); rateLimitError.name = "RateLimitError"; @@ -288,7 +296,9 @@ describe("handleNewBooking - Email Verification", () => { "should proceed normally when email verification is not required", async () => { const handleNewBooking = getNewBookingHandler(); - const { verifyCodeUnAuthenticated } = await import("@calcom/trpc/server/routers/viewer/auth/util"); + const { verifyCodeUnAuthenticated } = await import( + "@calcom/features/auth/lib/verifyCodeUnAuthenticated" + ); const booker = getBooker({ email: "booker@example.com", @@ -346,7 +356,9 @@ describe("handleNewBooking - Email Verification", () => { "should allow rescheduling without email verification even when requiresBookerEmailVerification is true", async () => { const handleNewBooking = getNewBookingHandler(); - const { verifyCodeUnAuthenticated } = await import("@calcom/trpc/server/routers/viewer/auth/util"); + const { verifyCodeUnAuthenticated } = await import( + "@calcom/features/auth/lib/verifyCodeUnAuthenticated" + ); const booker = getBooker({ email: "booker@example.com", diff --git a/packages/features/bookings/lib/service/RegularBookingService.ts b/packages/features/bookings/lib/service/RegularBookingService.ts index 9b1de598fb..01c3cce1ac 100644 --- a/packages/features/bookings/lib/service/RegularBookingService.ts +++ b/packages/features/bookings/lib/service/RegularBookingService.ts @@ -22,6 +22,7 @@ import dayjs from "@calcom/dayjs"; import { scheduleMandatoryReminder } from "@calcom/ee/workflows/lib/reminders/scheduleMandatoryReminder"; import getICalUID from "@calcom/emails/lib/getICalUID"; import { CalendarEventBuilder } from "@calcom/features/CalendarEventBuilder"; +import { verifyCodeUnAuthenticated } from "@calcom/features/auth/lib/verifyCodeUnAuthenticated"; import EventManager, { placeholderCreatedEvent } from "@calcom/features/bookings/lib/EventManager"; import type { BookingDataSchemaGetter } from "@calcom/features/bookings/lib/dto/types"; import type { @@ -85,7 +86,6 @@ import { CreationSource, } from "@calcom/prisma/enums"; import { userMetadata as userMetadataSchema } from "@calcom/prisma/zod-utils"; -import { verifyCodeUnAuthenticated } from "@calcom/trpc/server/routers/viewer/auth/util"; import { getAllWorkflowsFromEventType } from "@calcom/trpc/server/routers/viewer/workflows/util"; import type { AdditionalInformation, diff --git a/packages/platform/libraries/index.ts b/packages/platform/libraries/index.ts index 1f7fa378d2..37ac05ec70 100644 --- a/packages/platform/libraries/index.ts +++ b/packages/platform/libraries/index.ts @@ -119,7 +119,7 @@ export { checkAdminOrOwner } from "@calcom/features/auth/lib/checkAdminOrOwner"; export { verifyPhoneNumber, sendVerificationCode }; -export { verifyCodeUnAuthenticated } from "@calcom/trpc/server/routers/viewer/auth/util"; +export { verifyCodeUnAuthenticated } from "@calcom/features/auth/lib/verifyCodeUnAuthenticated"; export { verifyCode as verifyCodeAuthenticated } from "@calcom/trpc/server/routers/viewer/organizations/verifyCode.handler"; diff --git a/packages/trpc/server/routers/viewer/auth/verifyCodeUnAuthenticated.handler.ts b/packages/trpc/server/routers/viewer/auth/verifyCodeUnAuthenticated.handler.ts index 72bc0e82fa..d41c679e7b 100644 --- a/packages/trpc/server/routers/viewer/auth/verifyCodeUnAuthenticated.handler.ts +++ b/packages/trpc/server/routers/viewer/auth/verifyCodeUnAuthenticated.handler.ts @@ -1,9 +1,8 @@ +import { verifyCodeUnAuthenticated } from "@calcom/features/auth/lib/verifyCodeUnAuthenticated"; import type { ZVerifyCodeInputSchema } from "@calcom/prisma/zod-utils"; import { TRPCError } from "@trpc/server"; -import { verifyCodeUnAuthenticated } from "./util"; - type VerifyTokenOptions = { input: ZVerifyCodeInputSchema; };