import { NextRequest, NextResponse } from "next/server" import { syncService } from "@/lib/vendor-document-list/sync-service" import { getServerSession } from "next-auth" import { authOptions } from "@/app/api/auth/[...nextauth]/route" export async function GET(request: NextRequest) { try { const { searchParams } = new URL(request.url) const vendorId = searchParams.get('vendorId') const targetSystem = searchParams.get('targetSystem') || 'SHI' if (!vendorId) { return NextResponse.json( { error: 'Contract ID is required' }, { status: 400 } ) } const config = await syncService.getSyncConfig( parseInt(vendorId), targetSystem ) // 민감한 정보 제거 if (config) { const { authToken, ...safeConfig } = config return NextResponse.json({ ...safeConfig, hasAuthToken: !!authToken }) } return NextResponse.json(null) } catch (error) { console.error('Failed to get sync config:', error) return NextResponse.json( { error: 'Failed to get sync config' }, { status: 500 } ) } } export async function POST(request: NextRequest) { try { const session = await getServerSession(authOptions) if (!session?.user?.id) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }) } const body = await request.json() const { projectId, targetSystem, endpointUrl, authToken, syncEnabled, syncIntervalMinutes, maxBatchSize } = body if (!projectId || !targetSystem || !endpointUrl) { return NextResponse.json( { error: 'Contract ID, target system, and endpoint URL are required' }, { status: 400 } ) } await syncService.upsertSyncConfig({ vendorId, targetSystem, endpointUrl, authToken, syncEnabled, syncIntervalMinutes, maxBatchSize }) return NextResponse.json({ success: true }) } catch (error) { console.error('Failed to update sync config:', error) return NextResponse.json( { error: 'Failed to update sync config' }, { status: 500 } ) } }