diff options
Diffstat (limited to 'app/api/sync/import/status/route.ts')
| -rw-r--r-- | app/api/sync/import/status/route.ts | 208 |
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 |
