diff options
| author | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-09-26 09:57:24 +0000 |
|---|---|---|
| committer | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-09-26 09:57:24 +0000 |
| commit | 8b23b471638a155fd1bfa3a8c853b26d9315b272 (patch) | |
| tree | 47353e9dd342011cb2f1dcd24b09661707a8421b /app/api/rfq-attachments/upload/route.ts | |
| parent | d62368d2b68d73da895977e60a18f9b1286b0545 (diff) | |
(대표님) 권한관리, 문서업로드, rfq첨부, SWP문서룰 등
(최겸) 입찰
Diffstat (limited to 'app/api/rfq-attachments/upload/route.ts')
| -rw-r--r-- | app/api/rfq-attachments/upload/route.ts | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/app/api/rfq-attachments/upload/route.ts b/app/api/rfq-attachments/upload/route.ts index 3343c905..0f9a1902 100644 --- a/app/api/rfq-attachments/upload/route.ts +++ b/app/api/rfq-attachments/upload/route.ts @@ -2,7 +2,7 @@ import { NextRequest, NextResponse } from "next/server"; import { getServerSession } from 'next-auth'; import { authOptions } from '@/app/api/auth/[...nextauth]/route'; import db from "@/db/db"; -import { eq, and } from "drizzle-orm"; +import { eq, and, sql } from "drizzle-orm"; import { rfqLastAttachments, rfqLastAttachmentRevisions } from "@/db/schema"; import { saveFile } from "@/lib/file-stroage"; @@ -10,19 +10,27 @@ import { saveFile } from "@/lib/file-stroage"; async function generateSerialNo(rfqId: number, attachmentType: string, index: number = 0): Promise<string> { const prefix = attachmentType === "설계" ? "DES" : "PUR"; - const existingAttachments = await db - .select({ id: rfqLastAttachments.id }) + // 데이터베이스에서 최대 시리얼 번호 찾기 + const maxSerialResult = await db + .select({ serialNo: rfqLastAttachments.serialNo }) .from(rfqLastAttachments) .where( and( eq(rfqLastAttachments.rfqId, rfqId), eq(rfqLastAttachments.attachmentType, attachmentType as "설계" | "구매") ) - ); + ) + .orderBy(sql`CAST(SUBSTRING(${rfqLastAttachments.serialNo} FROM '[^-]+$') AS INTEGER) DESC`) + .limit(1); - const nextNumber = existingAttachments.length + 1 + index; - const paddedNumber = String(nextNumber).padStart(4, "0"); + let nextNumber = 1; + if (maxSerialResult.length > 0) { + const lastSerialNo = maxSerialResult[0].serialNo; + const lastNumber = parseInt(lastSerialNo.split('-').pop() || '0'); + nextNumber = lastNumber + 1; + } + const paddedNumber = String(nextNumber).padStart(4, "0"); return `${prefix}-${rfqId}-${paddedNumber}`; } |
