From 4e935b873848a80ceb9717b05327f6b5fe5d7a36 Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 21 Nov 2025 12:44:27 +0530 Subject: [PATCH 1/2] feat: Implement pre-registration company check and enhance multi-step form submission logic to prevent early submission. --- app/companies/register/page.tsx | 63 +++++++++++++++++++ .../companies/CompanyRegistrationForm.tsx | 20 +++++- 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/app/companies/register/page.tsx b/app/companies/register/page.tsx index 5cb63545..8591a315 100644 --- a/app/companies/register/page.tsx +++ b/app/companies/register/page.tsx @@ -60,6 +60,69 @@ function CompanyRegisterContent() { console.error("Registration error:", error); }; + // Check if user already has a company + useEffect(() => { + const checkExistingCompany = async () => { + console.log('🔍 Checking for existing company...', { user: !!user, resubmitId }); + + if (!user || resubmitId) { + console.log('â­ī¸ Skipping check - user:', !!user, 'resubmitId:', resubmitId); + return; // Skip if already in resubmit mode + } + + try { + console.log('📡 Fetching /api/companies/me...'); + const response = await fetch('/api/companies/me'); + console.log('📡 Response status:', response.status); + + if (response.ok) { + const result = await response.json(); + console.log('đŸ“Ļ API Response:', result); + + if (result.companies && result.companies.length > 0) { + console.log('đŸĸ Found companies:', result.companies.length); + + // Get the company where user is owner + const companyMember = result.companies.find((c: { role: string; company: CompanyData }) => c.role === 'owner'); + console.log('👤 Owner company member:', companyMember); + + // Check if company member exists AND the company object is not null + if (companyMember && companyMember.company && companyMember.company.id) { + const company = companyMember.company; // Extract the actual company data + console.log('✅ Company found - status:', company.verification_status, 'id:', company.id); + + // If company is rejected, redirect to resubmit flow + if (company.verification_status === 'rejected') { + console.log('🔄 User has rejected company, redirecting to resubmit flow'); + router.push(`/companies/register?resubmit=${company.id}`); + return; + } + + // If company is pending or verified, show message + if (company.verification_status === 'pending' || company.verification_status === 'verified') { + console.log('â„šī¸ User already has a company'); + toast.info('You already have a registered company'); + router.push('/protected'); + return; + } + } else { + console.log('❌ No valid owner company found (company may have been deleted)'); + } + } else { + console.log('📭 No companies found for user'); + } + } else { + console.log('❌ API request failed:', response.status); + } + } catch (error) { + console.error('❌ Error checking existing company:', error); + // Continue to show registration form if check fails + } + }; + + checkExistingCompany(); + }, [user, resubmitId, router]); + // Fetch company data for resubmission useEffect(() => { const fetchResubmitData = async () => { diff --git a/components/companies/CompanyRegistrationForm.tsx b/components/companies/CompanyRegistrationForm.tsx index 75c77d89..4fb8f0ec 100644 --- a/components/companies/CompanyRegistrationForm.tsx +++ b/components/companies/CompanyRegistrationForm.tsx @@ -210,6 +210,12 @@ export function CompanyRegistrationForm({ onSuccess, onError, initialData, compa const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); + // Only allow submission on the final step + if (currentStep !== totalSteps) { + console.log('Not on final step, preventing submission'); + return; + } + if (!validateStep(currentStep)) { return; } @@ -248,7 +254,7 @@ export function CompanyRegistrationForm({ onSuccess, onError, initialData, compa // Add verification documents formData.verification_documents.forEach((file, index) => { - submitData.append(`verification_document_${index}`, file); + submitData.append(`verification_document_${index} `, file); }); const response = await fetch("/api/companies/register", { @@ -295,7 +301,17 @@ export function CompanyRegistrationForm({ onSuccess, onError, initialData, compa -
+ { + // Prevent Enter key from submitting the form unless on final step + if (e.key === 'Enter' && currentStep !== totalSteps) { + e.preventDefault(); + console.log('Enter key pressed, but not on final step - preventing submission'); + } + }} + > {/* Step 1: Company Information */} {currentStep === 1 && ( Date: Fri, 21 Nov 2025 13:01:36 +0530 Subject: [PATCH 2/2] refactor: Explicitly manage form submission via button `onClick` and prevent default `Enter` key form submission. --- components/companies/CompanyRegistrationForm.tsx | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/components/companies/CompanyRegistrationForm.tsx b/components/companies/CompanyRegistrationForm.tsx index 4fb8f0ec..f8eda28c 100644 --- a/components/companies/CompanyRegistrationForm.tsx +++ b/components/companies/CompanyRegistrationForm.tsx @@ -302,13 +302,11 @@ export function CompanyRegistrationForm({ onSuccess, onError, initialData, compa { // Prevent Enter key from submitting the form unless on final step - if (e.key === 'Enter' && currentStep !== totalSteps) { + if (e.key === 'Enter') { e.preventDefault(); - console.log('Enter key pressed, but not on final step - preventing submission'); } }} > @@ -720,7 +718,8 @@ export function CompanyRegistrationForm({ onSuccess, onError, initialData, compa ) : (