diff options
| author | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-05-12 11:35:26 +0000 |
|---|---|---|
| committer | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-05-12 11:35:26 +0000 |
| commit | df91418cd28e98ce05845e476e51aa810202bf33 (patch) | |
| tree | ec47d9698f43224c3b87aed2988110af5d2ebd45 /lib/sedp/sync-form.ts | |
| parent | 033d96b6bfe812e26e9728c4a3f1e69abf858470 (diff) | |
(대표님) rfq 로직 관련 (libs) 업데이트트
Diffstat (limited to 'lib/sedp/sync-form.ts')
| -rw-r--r-- | lib/sedp/sync-form.ts | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/lib/sedp/sync-form.ts b/lib/sedp/sync-form.ts index a3caa809..2cb677b7 100644 --- a/lib/sedp/sync-form.ts +++ b/lib/sedp/sync-form.ts @@ -1,11 +1,11 @@ // src/lib/cron/syncTagFormMappings.ts import db from "@/db/db"; -import { projects, tagTypes, tagClasses, tagTypeClassFormMappings, formMetas, forms, contractItems, items } from '@/db/schema'; +import { projects, tagTypes, tagClasses, tagTypeClassFormMappings, formMetas, forms, contractItems, items, contracts } from '@/db/schema'; import { eq, and, inArray, ilike } from 'drizzle-orm'; import { getSEDPToken } from "./sedp-token"; // 환경 변수 -const SEDP_API_BASE_URL = process.env.SEDP_API_BASE_URL || 'http://sedpwebapi.ship.samsung.co.kr/dev/api'; +const SEDP_API_BASE_URL = process.env.SEDP_API_BASE_URL || 'http://sedpwebapi.ship.samsung.co.kr/api'; // 인터페이스 정의 interface TagTypeClassFormMapping { @@ -661,7 +661,7 @@ async function getUomById(projectCode: string, uomId: string): Promise<UOM | nul } // contractItemId 조회 함수 -async function getContractItemsByItemCodes(itemCodes: string[]): Promise<Map<string, number>> { +async function getContractItemsByItemCodes(itemCodes: string[], projectId: number): Promise<Map<string, number>> { try { if (!itemCodes.length) return new Map(); @@ -681,13 +681,19 @@ async function getContractItemsByItemCodes(itemCodes: string[]): Promise<Map<str // item ID 목록 추출 const itemIds = itemRecords.map(item => item.id); - // contractItems 조회 + // contracts와 join하여 projectId로 필터링하면서 contractItems 조회 const contractItemRecords = await db.select({ id: contractItems.id, itemId: contractItems.itemId }) .from(contractItems) - .where(inArray(contractItems.itemId, itemIds)); + .innerJoin(contracts, eq(contractItems.contractId, contracts.id)) + .where( + and( + inArray(contractItems.itemId, itemIds), + eq(contracts.projectId, projectId) + ) + ); // itemCode와 contractItemId의 매핑 생성 const itemCodeToContractItemId = new Map<string, number>(); @@ -751,7 +757,7 @@ async function saveFormMappingsAndMetas( const uniqueItemCodes = [...new Set(allItemCodes)]; // 모든 itemCode에 대한 contractItemId 조회 - const itemCodeToContractItemId = await getContractItemsByItemCodes(uniqueItemCodes); + const itemCodeToContractItemId = await getContractItemsByItemCodes(uniqueItemCodes , projectId); console.log(`${uniqueItemCodes.length}개의 고유 itemCode 중 ${itemCodeToContractItemId.size}개의 contractItem을 찾았습니다`); @@ -769,11 +775,7 @@ async function saveFormMappingsAndMetas( if (register.DELETED) continue; // REMARK에서 itemCodes 추출 - const itemCodes = extractItemCodes(register.REMARK || ''); - if (!itemCodes.length) { - console.log(`Register ${register.TYPE_ID} (${register.DESC})의 REMARK에 유효한 itemCode가 없습니다`); - continue; - } + // 폼 메타용 columns 구성 const columns: FormColumn[] = []; @@ -887,6 +889,11 @@ async function saveFormMappingsAndMetas( }); } + const itemCodes = extractItemCodes(register.REMARK || ''); + if (!itemCodes.length) { + console.log(`Register ${register.TYPE_ID} (${register.DESC})의 REMARK에 유효한 itemCode가 없습니다`); + continue; + } // 폼 레코드 준비 for (const itemCode of itemCodes) { const contractItemId = itemCodeToContractItemId.get(itemCode); |
