diff options
Diffstat (limited to 'lib/rfq-last/service.ts')
| -rw-r--r-- | lib/rfq-last/service.ts | 87 |
1 files changed, 77 insertions, 10 deletions
diff --git a/lib/rfq-last/service.ts b/lib/rfq-last/service.ts index 8eed9bee..09d707d7 100644 --- a/lib/rfq-last/service.ts +++ b/lib/rfq-last/service.ts @@ -3643,7 +3643,7 @@ async function handleTbeSession({ sessionCode: sessionCode, sessionTitle: `${rfqData.rfqCode} - ${vendor.vendorName} 기술검토`, sessionType: "initial", - status: "준비중", + status: "생성중", evaluationResult: null, plannedStartDate: rfqData.dueDate ? addDays(new Date(rfqData.dueDate), 1) @@ -4738,13 +4738,12 @@ export async function updateShortList( // 트랜잭션으로 처리 const result = await db.transaction(async (tx) => { - // 해당 RFQ의 모든 벤더들의 shortList를 먼저 false로 설정 (선택적) - // 만약 선택된 것만 true로 하고 나머지는 그대로 두려면 이 부분 제거 + // 1. 해당 RFQ의 모든 벤더들의 shortList를 먼저 false로 설정 await tx .update(rfqLastDetails) .set({ shortList: false, - updatedBy: session.user.id, + updatedBy: Number(session.user.id), updatedAt: new Date() }) .where( @@ -4754,15 +4753,16 @@ export async function updateShortList( ) ); - // 선택된 벤더들의 shortList를 true로 설정 + // 2. 선택된 벤더들 처리 if (vendorIds.length > 0) { - const updates = await Promise.all( + // 2-1. 선택된 벤더들의 shortList를 true로 설정 + const updatedDetails = await Promise.all( vendorIds.map(vendorId => tx .update(rfqLastDetails) .set({ shortList: shortListStatus, - updatedBy: session.user.id, + updatedBy: Number(session.user.id), updatedAt: new Date() }) .where( @@ -4776,17 +4776,84 @@ export async function updateShortList( ) ); + // 2-2. TBE 세션 처리 (shortList가 true인 경우에만) + if (shortListStatus) { + // 각 벤더에 대한 rfqLastDetailsId 추출 + const detailsMap = new Map( + updatedDetails.flat().map(detail => [detail.vendorsId, detail.id]) + ); + + // TBE 세션 생성 또는 업데이트 + await Promise.all( + vendorIds.map(async (vendorId) => { + const rfqLastDetailsId = detailsMap.get(vendorId); + + if (!rfqLastDetailsId) { + console.warn(`rfqLastDetailsId not found for vendorId: ${vendorId}`); + return; + } + + // 기존 활성 TBE 세션이 있는지 확인 + const existingSession = await tx + .select() + .from(rfqLastTbeSessions) + .where( + and( + eq(rfqLastTbeSessions.rfqsLastId, rfqId), + eq(rfqLastTbeSessions.vendorId, vendorId), + inArray(rfqLastTbeSessions.status, ["생성중", "준비중", "진행중", "검토중", "보류"]) + ) + ) + .limit(1); + + if (existingSession.length > 0) { + // 기존 세션이 있으면 상태 업데이트 + await tx + .update(rfqLastTbeSessions) + .set({ + status: "준비중", + updatedBy: session.user.id, + updatedAt: new Date() + }) + .where(eq(rfqLastTbeSessions.id, existingSession[0].id)); + } + }) + ); + } else { + // shortList가 false인 경우, 해당 벤더들의 활성 TBE 세션을 취소 상태로 변경 + await Promise.all( + vendorIds.map(vendorId => + tx + .update(rfqLastTbeSessions) + .set({ + status: "취소", + updatedBy: Number(session.user.id), + updatedAt: new Date() + }) + .where( + and( + eq(rfqLastTbeSessions.rfqsLastId, rfqId), + eq(rfqLastTbeSessions.vendorId, vendorId), + inArray(rfqLastTbeSessions.status, ["생성중", "준비중", "진행중", "검토중", "보류"]) + ) + ) + ) + ); + } + return { success: true, - updatedCount: updates.length, - vendorIds + updatedCount: updatedDetails.length, + vendorIds, + tbeSessionsUpdated: shortListStatus }; } return { success: true, updatedCount: 0, - vendorIds: [] + vendorIds: [], + tbeSessionsUpdated: false }; }); |
