@@ -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 ( ) => {
0 commit comments