1+ import { NextRequest , NextResponse } from 'next/server'
2+ import { createServerClient } from '@/lib/supabase-server'
3+ import { deploymentEngine } from '@/lib/deployment/deployment-engine'
4+ import { FragmentSchema } from '@/lib/schema'
5+
6+ export const dynamic = 'force-dynamic'
7+
8+ // POST /api/deployments - Deploy fragment
9+ export async function POST ( request : NextRequest ) {
10+ try {
11+ const supabase = createServerClient ( )
12+ const { data : { session } } = await supabase . auth . getSession ( )
13+
14+ if ( ! session ?. user ?. id ) {
15+ return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
16+ }
17+
18+ const body = await request . json ( )
19+ const { fragment, config } = body
20+
21+ if ( ! fragment || ! config ) {
22+ return NextResponse . json (
23+ { error : 'Fragment and config are required' } ,
24+ { status : 400 }
25+ )
26+ }
27+
28+ // Validate fragment schema
29+ const fragmentData = fragment as FragmentSchema
30+ if ( ! fragmentData . template || ! fragmentData . code ) {
31+ return NextResponse . json (
32+ { error : 'Fragment must have template and code' } ,
33+ { status : 400 }
34+ )
35+ }
36+
37+ // Start deployment in background
38+ const deploymentResult = await deploymentEngine . deployFragment ( fragmentData , config )
39+
40+ return NextResponse . json ( deploymentResult )
41+ } catch ( error ) {
42+ console . error ( 'Error deploying fragment:' , error )
43+ return NextResponse . json (
44+ { error : 'Internal server error' } ,
45+ { status : 500 }
46+ )
47+ }
48+ }
49+
50+ // GET /api/deployments - List deployments
51+ export async function GET ( request : NextRequest ) {
52+ try {
53+ const supabase = createServerClient ( )
54+ const { data : { session } } = await supabase . auth . getSession ( )
55+
56+ if ( ! session ?. user ?. id ) {
57+ return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
58+ }
59+
60+ const searchParams = request . nextUrl . searchParams
61+ const fragmentId = searchParams . get ( 'fragment_id' )
62+
63+ if ( fragmentId ) {
64+ const history = deploymentEngine . getDeploymentHistory ( fragmentId )
65+ return NextResponse . json ( { deployments : history } )
66+ }
67+
68+ // For now, return empty array since we don't have user-specific deployment tracking
69+ return NextResponse . json ( { deployments : [ ] } )
70+ } catch ( error ) {
71+ console . error ( 'Error listing deployments:' , error )
72+ return NextResponse . json (
73+ { error : 'Internal server error' } ,
74+ { status : 500 }
75+ )
76+ }
77+ }
0 commit comments