summaryrefslogtreecommitdiff
path: root/lib/bidding/service.ts
diff options
context:
space:
mode:
authordujinkim <dujin.kim@dtsolution.co.kr>2025-09-16 09:20:58 +0000
committerdujinkim <dujin.kim@dtsolution.co.kr>2025-09-16 09:20:58 +0000
commit6c11fccc84f4c84fa72ee01f9caad9f76f35cea2 (patch)
treefa88d10ea7d21fe6b59ed0c1569856a73d56547a /lib/bidding/service.ts
parent14e3990aba7e1ad1cdd0965cbd167c50230cbfbf (diff)
(대표님, 최겸) 계약, 업로드 관련, 메뉴처리, 입찰, 프리쿼트, rfqLast관련, tbeLast관련
Diffstat (limited to 'lib/bidding/service.ts')
-rw-r--r--lib/bidding/service.ts55
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