Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions app/api/companies/register/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,56 @@ export async function POST(request: NextRequest) {
socials: body.socials as Record<string, string> | undefined,
}

// Create company
const company = await companyService.createCompany(registrationData, user.id)
// Check if this is a resubmission (updating existing rejected company)
const companyId = body.companyId as string | undefined
let company

if (companyId) {
// Resubmission flow - update existing company
const { data: existingCompany, error: fetchError } = await supabase
.from('companies')
.select('*')
.eq('id', companyId)
.single()

if (fetchError || !existingCompany) {
return NextResponse.json(
{ error: 'Company not found for resubmission' },
{ status: 404 }
)
}

// Verify the user is the company owner
if (existingCompany.owner_id !== user.id) {
return NextResponse.json(
{ error: 'Forbidden: You are not the owner of this company' },
{ status: 403 }
)
}

// Verify company is in rejected status
if (existingCompany.verification_status !== 'rejected') {
return NextResponse.json(
{ error: 'Company is not in rejected status and cannot be resubmitted' },
{ status: 400 }
)
}

// Update company with new data and reset verification status
const updateData: Partial<typeof existingCompany> = {
...registrationData,
verification_status: 'pending' as const,
verification_notes: null,
verified_by: null,
updated_at: new Date().toISOString(),
}

company = await companyService.updateCompany(companyId, updateData)
} else {
// New registration flow
company = await companyService.createCompany(registrationData, user.id)
}


// Upload verification documents if provided
let uploadedDocuments: string[] = []
Expand Down
111 changes: 111 additions & 0 deletions app/api/companies/resubmit/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// API route to fetch company data for resubmission after rejection
import { NextRequest, NextResponse } from 'next/server'
import { createClient } from '@/lib/supabase/server'

/**
* GET /api/companies/resubmit/[id]
* Fetch company data for resubmission (owner only)
*/
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const supabase = await createClient()
const { id: companyId } = await params

// Check authentication
const {
data: { user },
error: authError,
} = await supabase.auth.getUser()

if (authError || !user) {
return NextResponse.json(
{ success: false, error: 'Unauthorized' },
{ status: 401 }
)
}

// Get company details
const { data: company, error: companyError } = await supabase
.from('companies')
.select('*')
.eq('id', companyId)
.single()

if (companyError || !company) {
return NextResponse.json(
{ success: false, error: 'Company not found' },
{ status: 404 }
)
}

// Verify the user is the company owner
console.log('πŸ” Ownership verification:', {
userId: user.id,
companyOwnerId: company.owner_id,
match: company.owner_id === user.id,
companyId: company.id,
companyName: company.name
});

if (company.owner_id !== user.id) {
return NextResponse.json(
{ success: false, error: 'Forbidden: You are not the owner of this company' },
{ status: 403 }
)
}
// Only allow resubmission for rejected companies
if (company.verification_status !== 'rejected') {
return NextResponse.json(
{
success: false,
error: 'Company is not in rejected status',
currentStatus: company.verification_status
},
{ status: 400 }
)
}

// Return company data for form pre-population
return NextResponse.json({
success: true,
company: {
id: company.id,
name: company.name,
legal_name: company.legal_name,
email: company.email,
phone: company.phone,
website: company.website,
industry: company.industry,
company_size: company.company_size,
description: company.description,
// Construct address object from individual columns
address: {
street: company.address_street || '',
city: company.address_city || '',
state: company.address_state || '',
country: company.address_country || '',
zip: company.address_zip || '',
},
// Construct socials object from individual columns
socials: {
linkedin: company.linkedin_url || '',
twitter: company.twitter_url || '',
facebook: company.facebook_url || '',
instagram: company.instagram_url || '',
},
verification_status: company.verification_status,
verification_notes: company.verification_notes,
logo_url: company.logo_url,
},
})
} catch (error) {
console.error('Unexpected error in GET /api/companies/resubmit/[id]:', error)
return NextResponse.json(
{ success: false, error: 'Internal server error' },
{ status: 500 }
)
}
}
Loading
Loading