Skip to content

Commit 4e935b8

Browse files
committed
feat: Implement pre-registration company check and enhance multi-step form submission logic to prevent early submission.
1 parent bbb0e24 commit 4e935b8

2 files changed

Lines changed: 81 additions & 2 deletions

File tree

‎app/companies/register/page.tsx‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,69 @@ function CompanyRegisterContent() {
6060
console.error("Registration error:", error);
6161
};
6262

63+
// Check if user already has a company
64+
useEffect(() => {
65+
const checkExistingCompany = async () => {
66+
console.log('🔍 Checking for existing company...', { user: !!user, resubmitId });
67+
68+
if (!user || resubmitId) {
69+
console.log('⏭️ Skipping check - user:', !!user, 'resubmitId:', resubmitId);
70+
return; // Skip if already in resubmit mode
71+
}
72+
73+
try {
74+
console.log('📡 Fetching /api/companies/me...');
75+
const response = await fetch('/api/companies/me');
76+
console.log('📡 Response status:', response.status);
77+
78+
if (response.ok) {
79+
const result = await response.json();
80+
console.log('📦 API Response:', result);
81+
82+
if (result.companies && result.companies.length > 0) {
83+
console.log('🏢 Found companies:', result.companies.length);
84+
85+
// Get the company where user is owner
86+
const companyMember = result.companies.find((c: { role: string; company: CompanyData }) => c.role === 'owner');
87+
console.log('👤 Owner company member:', companyMember);
88+
89+
// Check if company member exists AND the company object is not null
90+
if (companyMember && companyMember.company && companyMember.company.id) {
91+
const company = companyMember.company; // Extract the actual company data
92+
console.log('✅ Company found - status:', company.verification_status, 'id:', company.id);
93+
94+
// If company is rejected, redirect to resubmit flow
95+
if (company.verification_status === 'rejected') {
96+
console.log('🔄 User has rejected company, redirecting to resubmit flow');
97+
router.push(`/companies/register?resubmit=${company.id}`);
98+
return;
99+
}
100+
101+
// If company is pending or verified, show message
102+
if (company.verification_status === 'pending' || company.verification_status === 'verified') {
103+
console.log('ℹ️ User already has a company');
104+
toast.info('You already have a registered company');
105+
router.push('/protected');
106+
return;
107+
}
108+
} else {
109+
console.log('❌ No valid owner company found (company may have been deleted)');
110+
}
111+
} else {
112+
console.log('📭 No companies found for user');
113+
}
114+
} else {
115+
console.log('❌ API request failed:', response.status);
116+
}
117+
} catch (error) {
118+
console.error('❌ Error checking existing company:', error);
119+
// Continue to show registration form if check fails
120+
}
121+
};
122+
123+
checkExistingCompany();
124+
}, [user, resubmitId, router]);
125+
63126
// Fetch company data for resubmission
64127
useEffect(() => {
65128
const fetchResubmitData = async () => {

‎components/companies/CompanyRegistrationForm.tsx‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ export function CompanyRegistrationForm({ onSuccess, onError, initialData, compa
210210
const handleSubmit = async (e: React.FormEvent) => {
211211
e.preventDefault();
212212

213+
// Only allow submission on the final step
214+
if (currentStep !== totalSteps) {
215+
console.log('Not on final step, preventing submission');
216+
return;
217+
}
218+
213219
if (!validateStep(currentStep)) {
214220
return;
215221
}
@@ -248,7 +254,7 @@ export function CompanyRegistrationForm({ onSuccess, onError, initialData, compa
248254

249255
// Add verification documents
250256
formData.verification_documents.forEach((file, index) => {
251-
submitData.append(`verification_document_${index}`, file);
257+
submitData.append(`verification_document_${index} `, file);
252258
});
253259

254260
const response = await fetch("/api/companies/register", {
@@ -295,7 +301,17 @@ export function CompanyRegistrationForm({ onSuccess, onError, initialData, compa
295301
<Progress value={progress} className="h-2" />
296302
</div>
297303

298-
<form onSubmit={handleSubmit} className="space-y-6">
304+
<form
305+
onSubmit={handleSubmit}
306+
className="space-y-6"
307+
onKeyDown={(e) => {
308+
// Prevent Enter key from submitting the form unless on final step
309+
if (e.key === 'Enter' && currentStep !== totalSteps) {
310+
e.preventDefault();
311+
console.log('Enter key pressed, but not on final step - preventing submission');
312+
}
313+
}}
314+
>
299315
{/* Step 1: Company Information */}
300316
{currentStep === 1 && (
301317
<motion.div

0 commit comments

Comments
 (0)