summaryrefslogtreecommitdiff
path: root/app/api/sync/config/route.ts
blob: 2e9a073a900f7f75e8b6fe113f60b96be2aaffa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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 }
    )
  }
}