diff options
| author | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-11-26 09:55:47 +0000 |
|---|---|---|
| committer | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-11-26 09:55:47 +0000 |
| commit | 93cc002186d3816d991925e57d44e0586dba2e75 (patch) | |
| tree | b749eb37f638d18f4f089aa5e132a81cfdf5c2fd /lib/bidding/service.ts | |
| parent | 530775fb70ea24471b122e1c308cca3a0cd591b1 (diff) | |
(최겸) 구매 입찰 수정 v2
Diffstat (limited to 'lib/bidding/service.ts')
| -rw-r--r-- | lib/bidding/service.ts | 81 |
1 files changed, 80 insertions, 1 deletions
diff --git a/lib/bidding/service.ts b/lib/bidding/service.ts index d1a0d25c..521f4c33 100644 --- a/lib/bidding/service.ts +++ b/lib/bidding/service.ts @@ -32,7 +32,8 @@ import { SQL, like, notInArray, - inArray + inArray, + isNull } from 'drizzle-orm' import { revalidatePath } from 'next/cache' import { filterColumns } from '@/lib/filter-columns' @@ -3603,6 +3604,83 @@ export async function increaseRoundOrRebid(biddingId: number, userId: string | u }) } + // 11. 첨부파일 복제 (SHI용 및 협력업체용 첨부파일만) + const existingDocuments = await tx + .select() + .from(biddingDocuments) + .where(and( + eq(biddingDocuments.biddingId, biddingId), + // PR 아이템에 연결된 첨부파일은 제외 (SHI용과 협력업체용만 복제) + isNull(biddingDocuments.prItemId), + // SHI용(evaluation_doc) 또는 협력업체용(company_proposal) 문서만 복제 + or( + eq(biddingDocuments.documentType, 'evaluation_doc'), + eq(biddingDocuments.documentType, 'company_proposal') + ) + )) + + if (existingDocuments.length > 0) { + for (const doc of existingDocuments) { + try { + // 기존 파일을 Buffer로 읽어서 File 객체 생성 + const { readFileSync, existsSync } = await import('fs') + const { join } = await import('path') + + const oldFilePath = doc.filePath.startsWith('/uploads/') + ? join(process.cwd(), 'public', doc.filePath) + : doc.filePath + + if (!existsSync(oldFilePath)) { + console.warn(`원본 파일이 존재하지 않음: ${oldFilePath}`) + continue + } + + // 파일 내용을 읽어서 Buffer 생성 + const fileBuffer = readFileSync(oldFilePath) + + // Buffer를 File 객체로 변환 (브라우저 File API 시뮬레이션) + const file = new File([fileBuffer], doc.fileName, { + type: doc.mimeType || 'application/octet-stream' + }) + + // saveFile을 사용하여 새 파일 저장 + const saveResult = await saveFile({ + file, + directory: `biddings/${newBidding.id}/attachments/${doc.documentType === 'evaluation_doc' ? 'shi' : 'vendor'}`, + originalName: `copied_${Date.now()}_${doc.fileName}`, + userId: userName + }) + + if (saveResult.success) { + // 새 첨부파일 레코드 삽입 + await tx.insert(biddingDocuments).values({ + biddingId: newBidding.id, + companyId: doc.companyId, + prItemId: null as any, + specificationMeetingId: null, + documentType: doc.documentType, + fileName: saveResult.fileName!, + originalFileName: doc.originalFileName, + fileSize: saveResult.fileSize!, + mimeType: doc.mimeType, + filePath: saveResult.publicPath!, + title: `${doc.documentType === 'evaluation_doc' ? 'SHI용' : '협력업체용'} 첨부파일 복제 - ${doc.originalFileName}`, + description: doc.description, + isPublic: doc.isPublic, + isRequired: doc.isRequired, + uploadedBy: userName, + uploadedAt: new Date(), + }) + } else { + console.error(`첨부파일 저장 실패 (${doc.fileName}):`, saveResult.error) + } + + } catch (error) { + console.error(`첨부파일 복제 실패 (${doc.fileName}):`, error) + } + } + } + revalidatePath('/bidding') revalidatePath(`/bidding/${biddingId}`) // 기존 입찰 페이지도 갱신 revalidatePath(`/bidding/${newBidding.id}`) @@ -3984,6 +4062,7 @@ export async function getBiddingsForFailure(input: GetBiddingsSchema) { basicConditions.push( or( eq(biddings.status, 'bidding_disposal'), + eq(biddings.status, 'approval_pending'), eq(biddings.status, 'bid_closure') )! ) |
