90 lines
2.4 KiB
TypeScript
90 lines
2.4 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { getAuthService } from '@/lib/auth';
|
|
import { getDriverRepository } from '@/lib/di-container';
|
|
import { Driver } from '@gridpilot/racing';
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const authService = getAuthService();
|
|
const session = await authService.getCurrentSession();
|
|
|
|
if (!session) {
|
|
return NextResponse.json(
|
|
{ error: 'Not authenticated' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
const body = await request.json();
|
|
const { firstName, lastName, displayName, country, timezone, bio } = body;
|
|
|
|
// Validation
|
|
if (!firstName || !firstName.trim()) {
|
|
return NextResponse.json(
|
|
{ error: 'First name is required' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
if (!lastName || !lastName.trim()) {
|
|
return NextResponse.json(
|
|
{ error: 'Last name is required' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
if (!displayName || displayName.trim().length < 3) {
|
|
return NextResponse.json(
|
|
{ error: 'Display name must be at least 3 characters' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
if (!country) {
|
|
return NextResponse.json(
|
|
{ error: 'Country is required' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const driverRepo = getDriverRepository();
|
|
|
|
// Check if user already has a driver profile
|
|
if (session.user.primaryDriverId) {
|
|
const existingDriver = await driverRepo.findById(session.user.primaryDriverId);
|
|
if (existingDriver) {
|
|
return NextResponse.json(
|
|
{ error: 'Driver profile already exists' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
}
|
|
|
|
// Create the driver profile
|
|
const driverId = crypto.randomUUID();
|
|
const driver = Driver.create({
|
|
id: driverId,
|
|
iracingId: '', // Will be set later via OAuth
|
|
name: displayName.trim(),
|
|
country: country,
|
|
bio: bio || undefined,
|
|
});
|
|
|
|
await driverRepo.create(driver);
|
|
|
|
// Update user's primary driver ID in session
|
|
// Note: This would typically update the user record and refresh the session
|
|
// For now we'll just return success and let the client handle navigation
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
driverId: driverId,
|
|
});
|
|
} catch (error) {
|
|
console.error('Onboarding error:', error);
|
|
return NextResponse.json(
|
|
{ error: error instanceof Error ? error.message : 'Failed to complete onboarding' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
} |