feat: add signup watchlist review mode (#27912)
* feat: add signup watchlist review feature flag and handler logic - Add 'signup-watchlist-review' global feature flag - Add SIGNUP to WatchlistSource enum in Prisma schema - When flag enabled, lock new signups and add email to watchlist - Show 'account under review' message on signup page - Add i18n strings for review UI - Create seed migration for the feature flag Co-Authored-By: [email protected] <[email protected]> * test: add isAccountUnderReview tests to fetchSignup test suite Co-Authored-By: [email protected] <[email protected]> * fix: address Cubic AI review feedback (confidence >= 9/10) - Remove 'import process from node:process' in signup-view.tsx (P0 bug in 'use client' component) - Move watchlist review check before checkoutSessionId early return in calcomSignupHandler (P1 premium bypass) - Revert selfHostedHandler to original state (out of scope per user request) - Add test mocks for FeaturesRepository and GlobalWatchlistRepository Co-Authored-By: [email protected] <[email protected]> * fix: remove node:process import from useFlags.ts (client-side file) Co-Authored-By: [email protected] <[email protected]> * fix: remove !token condition from watchlist review check Token is present in normal email-verified signups, so the !token condition was incorrectly skipping watchlist review for verified users. Co-Authored-By: [email protected] <[email protected]> * Apply suggestion from @cubic-dev-ai[bot] Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * refactor: move user lock to UserRepository.lockByEmail Co-Authored-By: [email protected] <[email protected]> * refactor: use cached getFeatureRepository() instead of deprecated FeaturesRepository Co-Authored-By: [email protected] <[email protected]> * refactor: remove user locking, keep only watchlist addition on signup review Co-Authored-By: [email protected] <[email protected]> * feat: lock user on signup review, remove watchlist entry on unlock Co-Authored-By: [email protected] <[email protected]> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
This commit is contained in:
12 files changed
+569
-415
No files matched your search
@@ -1,17 +1,15 @@
|
||||
import type { Mock } from "vitest";
|
||||
import { vi } from "vitest";
|
||||
|
||||
import type { MockResponse } from "@calcom/features/auth/signup/handlers/__tests__/mocks/next.mocks";
|
||||
|
||||
import {
|
||||
prismaMock,
|
||||
resetPrismaMock,
|
||||
} from "@calcom/features/auth/signup/handlers/__tests__/mocks/prisma.mocks";
|
||||
import {
|
||||
createMockTeam,
|
||||
createMockFoundToken,
|
||||
} from "@calcom/features/auth/signup/handlers/__tests__/mocks/signup.factories";
|
||||
import type { SignupBody } from "@calcom/features/auth/signup/handlers/__tests__/mocks/signup.factories";
|
||||
import {
|
||||
createMockFoundToken,
|
||||
createMockTeam,
|
||||
} from "@calcom/features/auth/signup/handlers/__tests__/mocks/signup.factories";
|
||||
import type { Mock } from "vitest";
|
||||
import { vi } from "vitest";
|
||||
|
||||
const mockFindTokenByToken: Mock = vi.fn();
|
||||
const mockValidateAndGetCorrectedUsernameForTeam: Mock = vi.fn();
|
||||
@@ -77,6 +75,22 @@ vi.mock("@calcom/features/watchlist/lib/telemetry", () => ({ sentrySpan: {} }));
|
||||
vi.mock("@calcom/features/watchlist/operations/check-if-email-in-watchlist.controller", () => ({
|
||||
checkIfEmailIsBlockedInWatchlistController: vi.fn().mockResolvedValue(false),
|
||||
}));
|
||||
vi.mock("@calcom/features/di/containers/FeatureRepository", () => ({
|
||||
getFeatureRepository: vi.fn().mockReturnValue({
|
||||
checkIfFeatureIsEnabledGlobally: vi.fn().mockResolvedValue(false),
|
||||
}),
|
||||
}));
|
||||
vi.mock("@calcom/features/watchlist/lib/repository/GlobalWatchlistRepository", () => {
|
||||
return {
|
||||
GlobalWatchlistRepository: class {
|
||||
findBlockedEmail = vi.fn().mockResolvedValue(null);
|
||||
createEntry = vi.fn().mockResolvedValue({});
|
||||
},
|
||||
};
|
||||
});
|
||||
vi.mock("@calcom/features/watchlist/lib/utils/normalization", () => ({
|
||||
normalizeEmail: vi.fn((e: string) => e.toLowerCase()),
|
||||
}));
|
||||
vi.mock("@calcom/web/lib/buildLegacyCtx", () => ({ buildLegacyRequest: vi.fn() }));
|
||||
vi.mock("@calcom/features/auth/signup/utils/organization", () => ({ joinAnyChildTeamOnOrgInvite: vi.fn() }));
|
||||
vi.mock("@calcom/features/auth/signup/utils/token", () => ({
|
||||
|
||||
@@ -1,14 +1,23 @@
|
||||
import { cookies, headers } from "next/headers";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
import process from "node:process";
|
||||
import { getPremiumMonthlyPlanPriceId } from "@calcom/app-store/stripepayment/lib/utils";
|
||||
import { getLocaleFromRequest } from "@calcom/features/auth/lib/getLocaleFromRequest";
|
||||
import { sendEmailVerification } from "@calcom/features/auth/lib/verifyEmail";
|
||||
import { SIGNUP_ERROR_CODES } from "@calcom/features/auth/signup/constants";
|
||||
import { createOrUpdateMemberships } from "@calcom/features/auth/signup/utils/createOrUpdateMemberships";
|
||||
import { joinAnyChildTeamOnOrgInvite } from "@calcom/features/auth/signup/utils/organization";
|
||||
import { prefillAvatar } from "@calcom/features/auth/signup/utils/prefillAvatar";
|
||||
import {
|
||||
findTokenByToken,
|
||||
throwIfTokenExpired,
|
||||
validateAndGetCorrectedUsernameForTeam,
|
||||
} from "@calcom/features/auth/signup/utils/token";
|
||||
import { validateAndGetCorrectedUsernameAndEmail } from "@calcom/features/auth/signup/utils/validateUsername";
|
||||
import { getBillingProviderService } from "@calcom/features/ee/billing/di/containers/Billing";
|
||||
import { getFeatureRepository } from "@calcom/features/di/containers/FeatureRepository";
|
||||
import { UserRepository } from "@calcom/features/users/repositories/UserRepository";
|
||||
import { GlobalWatchlistRepository } from "@calcom/features/watchlist/lib/repository/GlobalWatchlistRepository";
|
||||
import { sentrySpan } from "@calcom/features/watchlist/lib/telemetry";
|
||||
import { normalizeEmail } from "@calcom/features/watchlist/lib/utils/normalization";
|
||||
import { checkIfEmailIsBlockedInWatchlistController } from "@calcom/features/watchlist/operations/check-if-email-in-watchlist.controller";
|
||||
import { hashPassword } from "@calcom/lib/auth/hashPassword";
|
||||
import { WEBAPP_URL } from "@calcom/lib/constants";
|
||||
@@ -19,19 +28,17 @@ import type { CustomNextApiHandler } from "@calcom/lib/server/username";
|
||||
import { usernameHandler } from "@calcom/lib/server/username";
|
||||
import { getTrackingFromCookies } from "@calcom/lib/tracking";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { CreationSource } from "@calcom/prisma/enums";
|
||||
import { IdentityProvider } from "@calcom/prisma/enums";
|
||||
import {
|
||||
CreationSource,
|
||||
IdentityProvider,
|
||||
WatchlistAction,
|
||||
WatchlistSource,
|
||||
WatchlistType,
|
||||
} from "@calcom/prisma/enums";
|
||||
import { signupSchema } from "@calcom/prisma/zod-utils";
|
||||
import { buildLegacyRequest } from "@calcom/web/lib/buildLegacyCtx";
|
||||
|
||||
import { joinAnyChildTeamOnOrgInvite } from "@calcom/features/auth/signup/utils/organization";
|
||||
import { SIGNUP_ERROR_CODES } from "@calcom/features/auth/signup/constants";
|
||||
|
||||
import {
|
||||
findTokenByToken,
|
||||
throwIfTokenExpired,
|
||||
validateAndGetCorrectedUsernameForTeam,
|
||||
} from "@calcom/features/auth/signup/utils/token";
|
||||
import { cookies, headers } from "next/headers";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
const log = logger.getSubLogger({ prefix: ["signupCalcomHandler"] });
|
||||
|
||||
@@ -291,6 +298,34 @@ const handler: CustomNextApiHandler = async (body, usernameStatus, query) => {
|
||||
}
|
||||
}
|
||||
|
||||
const featureRepository = getFeatureRepository();
|
||||
const signupWatchlistReviewEnabled =
|
||||
await featureRepository.checkIfFeatureIsEnabledGlobally("signup-watchlist-review");
|
||||
|
||||
if (signupWatchlistReviewEnabled && !token) {
|
||||
const globalWatchlistRepo = new GlobalWatchlistRepository(prisma);
|
||||
const normalizedEmail = normalizeEmail(email);
|
||||
const existing = await globalWatchlistRepo.findBlockedEmail(normalizedEmail);
|
||||
|
||||
if (!existing) {
|
||||
await globalWatchlistRepo.createEntry({
|
||||
type: WatchlistType.EMAIL,
|
||||
value: normalizedEmail,
|
||||
action: WatchlistAction.BLOCK,
|
||||
source: WatchlistSource.SIGNUP,
|
||||
description: "Auto-added during signup review",
|
||||
});
|
||||
}
|
||||
|
||||
const userRepository = new UserRepository(prisma);
|
||||
await userRepository.lockByEmail({ email });
|
||||
|
||||
return NextResponse.json(
|
||||
{ message: "Created user", stripeCustomerId: customer.stripeCustomerId, accountUnderReview: true },
|
||||
{ status: 201 }
|
||||
);
|
||||
}
|
||||
|
||||
if (checkoutSessionId) {
|
||||
console.log("Created user but missing payment", checkoutSessionId);
|
||||
return NextResponse.json(
|
||||
|
||||
Reference in new issue
Block a user