diff options
Diffstat (limited to 'app/api/rfq-attachments')
| -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}`; } |
