diff options
Diffstat (limited to 'lib/bidding/detail/service.ts')
| -rw-r--r-- | lib/bidding/detail/service.ts | 76 |
1 files changed, 75 insertions, 1 deletions
diff --git a/lib/bidding/detail/service.ts b/lib/bidding/detail/service.ts index 0b68eaa7..e425959c 100644 --- a/lib/bidding/detail/service.ts +++ b/lib/bidding/detail/service.ts @@ -3,7 +3,7 @@ import db from '@/db/db' import { biddings, prItemsForBidding, biddingDocuments, biddingCompanies, vendors, companyPrItemBids, companyConditionResponses, vendorSelectionResults, priceAdjustmentForms, users, vendorContacts } from '@/db/schema' import { specificationMeetings, biddingCompaniesContacts } from '@/db/schema/bidding' -import { eq, and, sql, desc, ne } from 'drizzle-orm' +import { eq, and, sql, desc, ne, asc } from 'drizzle-orm' import { revalidatePath, revalidateTag } from 'next/cache' import { unstable_cache } from "@/lib/unstable-cache"; import { sendEmail } from '@/lib/mail/sendEmail' @@ -207,6 +207,80 @@ export async function getBiddingCompaniesData(biddingId: number) { } } +// 입찰 접수 화면용: 모든 초대된 협력사 조회 (필터링 없음, contact 정보 포함) +export async function getAllBiddingCompanies(biddingId: number) { + try { + // 1. 기본 협력사 정보 조회 + const companies = await db + .select({ + id: biddingCompanies.id, + biddingId: biddingCompanies.biddingId, + companyId: biddingCompanies.companyId, + vendorName: vendors.vendorName, + vendorCode: vendors.vendorCode, + invitationStatus: biddingCompanies.invitationStatus, + invitedAt: biddingCompanies.invitedAt, + respondedAt: biddingCompanies.respondedAt, + preQuoteAmount: biddingCompanies.preQuoteAmount, + preQuoteSubmittedAt: biddingCompanies.preQuoteSubmittedAt, + isPreQuoteSelected: biddingCompanies.isPreQuoteSelected, + finalQuoteAmount: biddingCompanies.finalQuoteAmount, + finalQuoteSubmittedAt: biddingCompanies.finalQuoteSubmittedAt, + isWinner: biddingCompanies.isWinner, + notes: biddingCompanies.notes, + createdAt: biddingCompanies.createdAt, + updatedAt: biddingCompanies.updatedAt + }) + .from(biddingCompanies) + .leftJoin(vendors, eq(biddingCompanies.companyId, vendors.id)) + .where(eq(biddingCompanies.biddingId, biddingId)) + .orderBy(biddingCompanies.invitedAt) + + // 2. 각 협력사의 첫 번째 contact 정보 조회 + const companiesWithContacts = await Promise.all( + companies.map(async (company) => { + if (!company.companyId) { + return { + ...company, + contactPerson: null, + contactEmail: null, + contactPhone: null + } + } + + // biddingCompaniesContacts에서 첫 번째 contact 조회 + const [firstContact] = await db + .select({ + contactName: biddingCompaniesContacts.contactName, + contactEmail: biddingCompaniesContacts.contactEmail, + contactNumber: biddingCompaniesContacts.contactNumber, + }) + .from(biddingCompaniesContacts) + .where( + and( + eq(biddingCompaniesContacts.biddingId, biddingId), + eq(biddingCompaniesContacts.vendorId, company.companyId) + ) + ) + .orderBy(asc(biddingCompaniesContacts.id)) + .limit(1) + + return { + ...company, + contactPerson: firstContact?.contactName || null, + contactEmail: firstContact?.contactEmail || null, + contactPhone: firstContact?.contactNumber || null + } + }) + ) + + return companiesWithContacts + } catch (error) { + console.error('Failed to get all bidding companies:', error) + return [] + } +} + // prItemsForBidding 테이블에서 품목 정보 조회 (캐시 미적용, always fresh) export async function getPRItemsForBidding(biddingId: number) { try { |
