diff options
Diffstat (limited to 'lib/bidding/detail/service.ts')
| -rw-r--r-- | lib/bidding/detail/service.ts | 58 |
1 files changed, 53 insertions, 5 deletions
diff --git a/lib/bidding/detail/service.ts b/lib/bidding/detail/service.ts index d0f8070f..297c6f98 100644 --- a/lib/bidding/detail/service.ts +++ b/lib/bidding/detail/service.ts @@ -1251,9 +1251,55 @@ export async function getAwardedCompanies(biddingId: number) { } } +// 입찰의 PR 아이템 금액 합산하여 bidding 업데이트 +async function updateBiddingAmounts(biddingId: number) { + try { + // 해당 bidding의 모든 PR 아이템들의 금액 합계 계산 + const amounts = await db + .select({ + totalTargetAmount: sql<number>`COALESCE(SUM(${prItemsForBidding.targetAmount}), 0)`, + totalBudgetAmount: sql<number>`COALESCE(SUM(${prItemsForBidding.budgetAmount}), 0)`, + totalActualAmount: sql<number>`COALESCE(SUM(${prItemsForBidding.actualAmount}), 0)` + }) + .from(prItemsForBidding) + .where(eq(prItemsForBidding.biddingId, biddingId)) + + const { totalTargetAmount, totalBudgetAmount, totalActualAmount } = amounts[0] + + // bidding 테이블 업데이트 + await db + .update(biddings) + .set({ + targetPrice: totalTargetAmount, + budget: totalBudgetAmount, + finalBidPrice: totalActualAmount, + updatedAt: new Date() + }) + .where(eq(biddings.id, biddingId)) + + console.log(`Bidding ${biddingId} amounts updated: target=${totalTargetAmount}, budget=${totalBudgetAmount}, actual=${totalActualAmount}`) + } catch (error) { + console.error('Failed to update bidding amounts:', error) + throw error + } +} + // PR 품목 정보 업데이트 export async function updatePrItem(prItemId: number, input: Partial<typeof prItemsForBidding.$inferSelect>, userId: string) { try { + // 업데이트 전 biddingId 확인 + const prItem = await db + .select({ biddingId: prItemsForBidding.biddingId }) + .from(prItemsForBidding) + .where(eq(prItemsForBidding.id, prItemId)) + .limit(1) + + if (!prItem[0]?.biddingId) { + throw new Error('PR item not found or biddingId is missing') + } + + const biddingId = prItem[0].biddingId + await db .update(prItemsForBidding) .set({ @@ -1262,12 +1308,14 @@ export async function updatePrItem(prItemId: number, input: Partial<typeof prIte }) .where(eq(prItemsForBidding.id, prItemId)) + // PR 아이템 금액 합산하여 bidding 업데이트 + await updateBiddingAmounts(biddingId) + // 캐시 무효화 - if (input.biddingId) { - revalidateTag(`bidding-${input.biddingId}`) - revalidateTag('pr-items') - revalidatePath(`/evcp/bid/${input.biddingId}`) - } + revalidateTag(`bidding-${biddingId}`) + revalidateTag('pr-items') + revalidatePath(`/evcp/bid/${biddingId}`) + return { success: true, message: '품목 정보가 성공적으로 업데이트되었습니다.' } } catch (error) { console.error('Failed to update PR item:', error) |
