summaryrefslogtreecommitdiff
path: root/app/api/sync/import/status/route.ts
diff options
context:
space:
mode:
Diffstat (limited to 'app/api/sync/import/status/route.ts')
-rw-r--r--app/api/sync/import/status/route.ts208
1 files changed, 16 insertions, 192 deletions
diff --git a/app/api/sync/import/status/route.ts b/app/api/sync/import/status/route.ts
index 8b6144d6..c5b4b0bd 100644
--- a/app/api/sync/import/status/route.ts
+++ b/app/api/sync/import/status/route.ts
@@ -1,19 +1,8 @@
-// app/api/sync/status/route.ts
+// app/api/sync/import/status/route.ts
import { NextRequest, NextResponse } from "next/server"
+import { importService } from "@/lib/vendor-document-list/import-service"
import { getServerSession } from "next-auth"
import { authOptions } from "@/app/api/auth/[...nextauth]/route"
-import db from "@/db/db"
-import { documents, revisions, documentAttachments, contracts, projects, vendors } from "@/db/schema"
-import { eq, and, sql, desc } from "drizzle-orm"
-
-interface SyncStatus {
- syncEnabled: boolean
- pendingChanges: number
- syncedChanges: number
- failedChanges: number
- lastSyncAt?: string
- error?: string
-}
export async function GET(request: NextRequest) {
try {
@@ -24,7 +13,7 @@ export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url)
const contractId = searchParams.get('contractId')
- const targetSystem = searchParams.get('targetSystem') || 'SHI'
+ const sourceSystem = searchParams.get('sourceSystem') || 'DOLCE'
if (!contractId) {
return NextResponse.json(
@@ -33,185 +22,20 @@ export async function GET(request: NextRequest) {
)
}
- // πŸ”₯ μ•ˆμ „ν•˜κ²Œ 동기화 μƒνƒœ 쑰회
- const syncStatus = await getSyncStatusSafely(Number(contractId), targetSystem)
-
- return NextResponse.json(syncStatus)
-
- } catch (error) {
- console.error('Unexpected error in sync status API:', error)
-
- // πŸ”₯ μ—λŸ¬ μ‹œμ—λ„ 200으둜 μ‘λ‹΅ν•˜κ³  error ν•„λ“œ 포함
- return NextResponse.json({
- syncEnabled: false,
- pendingChanges: 0,
- syncedChanges: 0,
- failedChanges: 0,
- lastSyncAt: undefined,
- error: 'μ‹œμŠ€ν…œ 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. μž μ‹œ ν›„ λ‹€μ‹œ μ‹œλ„ν•΄μ£Όμ„Έμš”.'
- }, { status: 200 })
- }
-}
-
-async function getSyncStatusSafely(contractId: number, targetSystem: string): Promise<SyncStatus> {
- try {
- // 1. 계약 정보 확인
- const contractInfo = await db
- .select({
- projectCode: projects.code,
- vendorCode: vendors.vendorCode,
- contractStatus: contracts.status
- })
- .from(contracts)
- .innerJoin(projects, eq(contracts.projectId, projects.id))
- .innerJoin(vendors, eq(contracts.vendorId, vendors.id))
- .where(eq(contracts.id, contractId))
- .limit(1)
-
- // 계약 정보가 μ—†λŠ” 경우
- if (!contractInfo || contractInfo.length === 0) {
- return {
- syncEnabled: false,
- pendingChanges: 0,
- syncedChanges: 0,
- failedChanges: 0,
- error: `계약 ${contractId}λ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€.`
- }
- }
-
- const contract = contractInfo[0]
-
- // ν”„λ‘œμ νŠΈ μ½”λ“œλ‚˜ 벀더 μ½”λ“œκ°€ μ—†λŠ” 경우
- if (!contract.projectCode || !contract.vendorCode) {
- return {
- syncEnabled: false,
- pendingChanges: 0,
- syncedChanges: 0,
- failedChanges: 0,
- error: `계약 ${contractId}에 ν”„λ‘œμ νŠΈ μ½”λ“œ λ˜λŠ” 벀더 μ½”λ“œκ°€ μ„€μ •λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.`
- }
- }
-
- // 2. λ§ˆμ§€λ§‰ 동기화 μ‹œκ°„ 쑰회
- const [lastSync] = await db
- .select({
- lastSyncAt: sql<string>`MAX(${documents.externalSyncedAt})`
- })
- .from(documents)
- .where(and(
- eq(documents.contractId, contractId),
- eq(documents.externalSystemType, targetSystem)
- ))
-
- // 3. λ¬Έμ„œλ³„ 변경사항 뢄석
- const documentStats = await db
- .select({
- id: documents.id,
- docNumber: documents.docNumber,
- updatedAt: documents.updatedAt,
- externalSyncedAt: documents.externalSyncedAt,
- syncStatus: documents.syncStatus
- })
- .from(documents)
- .where(eq(documents.contractId, contractId))
-
- let pendingChanges = 0
- let syncedChanges = 0
- let failedChanges = 0
-
- // 각 λ¬Έμ„œμ˜ 동기화 μƒνƒœ 뢄석
- for (const doc of documentStats) {
- // λ¬Έμ„œ μžμ²΄κ°€ λ³€κ²½λ˜μ—ˆλŠ”μ§€ 확인
- const docNeedsSync = !doc.externalSyncedAt ||
- (doc.updatedAt && doc.externalSyncedAt && doc.updatedAt > doc.externalSyncedAt)
-
- if (docNeedsSync) {
- if (doc.syncStatus === 'FAILED') {
- failedChanges++
- } else if (doc.syncStatus === 'SYNCED') {
- syncedChanges++
- } else {
- pendingChanges++
- }
- }
-
- // ν•΄λ‹Ή λ¬Έμ„œμ˜ 리비전 변경사항 확인
- const revisionStats = await db
- .select({
- updatedAt: revisions.updatedAt,
- externalSyncedAt: revisions.externalSyncedAt,
- syncStatus: revisions.syncStatus
- })
- .from(revisions)
- .innerJoin(documents, eq(revisions.documentId, documents.id))
- .where(eq(documents.id, doc.id))
-
- for (const revision of revisionStats) {
- const revisionNeedsSync = !revision.externalSyncedAt ||
- (revision.updatedAt && revision.externalSyncedAt && revision.updatedAt > revision.externalSyncedAt)
-
- if (revisionNeedsSync) {
- if (revision.syncStatus === 'FAILED') {
- failedChanges++
- } else if (revision.syncStatus === 'SYNCED') {
- syncedChanges++
- } else {
- pendingChanges++
- }
- }
- }
-
- // μ²¨λΆ€νŒŒμΌ 변경사항 확인
- const attachmentStats = await db
- .select({
- updatedAt: documentAttachments.updatedAt,
- externalSyncedAt: documentAttachments.externalSyncedAt,
- syncStatus: documentAttachments.syncStatus
- })
- .from(documentAttachments)
- .innerJoin(revisions, eq(documentAttachments.revisionId, revisions.id))
- .innerJoin(documents, eq(revisions.documentId, documents.id))
- .where(eq(documents.id, doc.id))
-
- for (const attachment of attachmentStats) {
- const attachmentNeedsSync = !attachment.externalSyncedAt ||
- (attachment.updatedAt && attachment.externalSyncedAt && attachment.updatedAt > attachment.externalSyncedAt)
-
- if (attachmentNeedsSync) {
- if (attachment.syncStatus === 'FAILED') {
- failedChanges++
- } else if (attachment.syncStatus === 'SYNCED') {
- syncedChanges++
- } else {
- pendingChanges++
- }
- }
- }
- }
-
- // 4. 동기화 ν™œμ„±ν™” μ—¬λΆ€ 확인
- const syncEnabled = contract.contractStatus === 'ACTIVE' &&
- Boolean(contract.projectCode) &&
- Boolean(contract.vendorCode) &&
- process.env[`SYNC_${targetSystem.toUpperCase()}_ENABLED`] === 'true'
-
- return {
- syncEnabled,
- pendingChanges,
- syncedChanges,
- failedChanges,
- lastSyncAt: lastSync?.lastSyncAt ? new Date(lastSync.lastSyncAt).toISOString() : undefined
- }
+ const status = await importService.getImportStatus(
+ Number(contractId),
+ sourceSystem
+ )
+ return NextResponse.json(status)
} catch (error) {
- console.error(`Failed to get sync status for contract ${contractId}:`, error)
-
- return {
- syncEnabled: false,
- pendingChanges: 0,
- syncedChanges: 0,
- failedChanges: 0,
- error: error instanceof Error ? error.message : '동기화 μƒνƒœλ₯Ό 확인할 수 μ—†μŠ΅λ‹ˆλ‹€.'
- }
+ console.error('Failed to get import status:', error)
+ return NextResponse.json(
+ {
+ error: 'Failed to get import status',
+ message: error instanceof Error ? error.message : 'Unknown error'
+ },
+ { status: 500 }
+ )
}
} \ No newline at end of file