diff options
Diffstat (limited to 'lib/bidding/service.ts')
| -rw-r--r-- | lib/bidding/service.ts | 55 |
1 files changed, 54 insertions, 1 deletions
diff --git a/lib/bidding/service.ts b/lib/bidding/service.ts index 90a379e1..55146c4b 100644 --- a/lib/bidding/service.ts +++ b/lib/bidding/service.ts @@ -14,7 +14,10 @@ import { users, basicContractTemplates, paymentTerms, - incoterms + incoterms, + vendors, + vendorsWithTypesView, + biddingCompanies } from '@/db/schema' import { eq, @@ -556,6 +559,7 @@ export async function createBidding(input: CreateBiddingInput, userId: string) { finalBidPrice: input.finalBidPrice ? parseFloat(input.finalBidPrice) : null, status: input.status || 'bidding_generated', + // biddingSourceType: input.biddingSourceType || 'manual', isPublic: input.isPublic || false, isUrgent: input.isUrgent || false, managerName: input.managerName, @@ -1421,4 +1425,53 @@ export async function getActiveContractTemplates() { console.error('활성 템플릿 조회 실패:', error); throw new Error('템플릿 조회에 실패했습니다.'); } +} + +// 입찰에 참여하지 않은 벤더만 검색 (중복 방지) +export async function searchVendorsForBidding(searchTerm: string = "", biddingId: number, limit: number = 100) { + try { + let whereCondition; + + if (searchTerm.trim()) { + const s = `%${searchTerm.trim()}%`; + whereCondition = or( + ilike(vendorsWithTypesView.vendorName, s), + ilike(vendorsWithTypesView.vendorCode, s) + ); + } + + // 이미 해당 입찰에 참여중인 벤더 ID들을 가져옴 + const participatingVendorIds = await db + .select({ companyId: biddingCompanies.companyId }) + .from(biddingCompanies) + .where(eq(biddingCompanies.biddingId, biddingId)); + + const excludedIds = participatingVendorIds.map(p => p.companyId); + + const result = await db + .select({ + id: vendorsWithTypesView.id, + vendorName: vendorsWithTypesView.vendorName, + vendorCode: vendorsWithTypesView.vendorCode, + status: vendorsWithTypesView.status, + country: vendorsWithTypesView.country, + }) + .from(vendorsWithTypesView) + .where( + and( + whereCondition, + // 이미 참여중인 벤더 제외 + excludedIds.length > 0 ? sql`${vendorsWithTypesView.id} NOT IN (${excludedIds})` : undefined, + // ACTIVE 상태인 벤더만 검색 + // eq(vendorsWithTypesView.status, "ACTIVE"), + ) + ) + .orderBy(asc(vendorsWithTypesView.vendorName)) + .limit(limit); + + return result; + } catch (error) { + console.error('Error searching vendors for bidding:', error) + return [] + } }
\ No newline at end of file |
