diff options
| author | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-04-28 02:13:30 +0000 |
|---|---|---|
| committer | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-04-28 02:13:30 +0000 |
| commit | ef4c533ebacc2cdc97e518f30e9a9350004fcdfb (patch) | |
| tree | 345251a3ed0f4429716fa5edaa31024d8f4cb560 /app/api/upload/signed-contract | |
| parent | 9ceed79cf32c896f8a998399bf1b296506b2cd4a (diff) | |
~20250428 작업사항
Diffstat (limited to 'app/api/upload/signed-contract')
| -rw-r--r-- | app/api/upload/signed-contract/route.ts | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/app/api/upload/signed-contract/route.ts b/app/api/upload/signed-contract/route.ts new file mode 100644 index 00000000..f26e20ba --- /dev/null +++ b/app/api/upload/signed-contract/route.ts @@ -0,0 +1,57 @@ +// app/api/upload/signed-contract/route.ts +import { NextRequest, NextResponse } from 'next/server'; +import fs from 'fs/promises'; +import path from 'path'; +import { v4 as uuidv4 } from 'uuid'; +import db from "@/db/db"; +import { basicContract } from '@/db/schema'; +import { eq } from 'drizzle-orm'; +import { revalidateTag } from 'next/cache'; + +export async function POST(request: NextRequest) { + try { + const formData = await request.formData(); + const file = formData.get('file') as File; + const tableRowId = parseInt(formData.get('tableRowId') as string); + const templateName = formData.get('templateName') as string; + + if (!file || !tableRowId || !templateName) { + return NextResponse.json({ result: false, error: '필수 파라미터가 누락되었습니다.' }, { status: 400 }); + } + + const originalName = `${tableRowId}_${templateName}`; + const ext = path.extname(originalName); + const uniqueName = uuidv4() + ext; + + const publicDir = path.join(process.cwd(), "public", "basicContract"); + const relativePath = `/basicContract/${uniqueName}`; + const absolutePath = path.join(publicDir, uniqueName); + const buffer = Buffer.from(await file.arrayBuffer()); + + await fs.mkdir(publicDir, { recursive: true }); + await fs.writeFile(absolutePath, buffer); + + await db.transaction(async (tx) => { + await tx + .update(basicContract) + .set({ + status: "COMPLETED", + fileName: originalName, + filePath: relativePath, + updatedAt: new Date(), + completedAt: new Date() + }) + .where(eq(basicContract.id, tableRowId)); + }); + + // 캐시 무효화 + revalidateTag("basic-contract-requests"); + revalidateTag("basicContractView-vendor"); + + return NextResponse.json({ result: true }); + } catch (error) { + console.error('서명된 계약서 저장 오류:', error); + const errorMessage = error instanceof Error ? error.message : "알 수 없는 오류"; + return NextResponse.json({ result: false, error: errorMessage }, { status: 500 }); + } +}
\ No newline at end of file |
