diff options
Diffstat (limited to 'lib/sedp')
| -rw-r--r-- | lib/sedp/sync-form.ts | 17 | ||||
| -rw-r--r-- | lib/sedp/sync-package.ts | 11 |
2 files changed, 19 insertions, 9 deletions
diff --git a/lib/sedp/sync-form.ts b/lib/sedp/sync-form.ts index f9e63caf..559c09a2 100644 --- a/lib/sedp/sync-form.ts +++ b/lib/sedp/sync-form.ts @@ -835,6 +835,7 @@ async function getUomById(projectCode: string, uomId: string): Promise<UOM | nul } // contractItemId 조회 함수 +// contractItemId 조회 함수 (수정됨) async function getContractItemsByItemCodes(itemCodes: string[], projectId: number): Promise<Map<string, number[]>> { try { if (!itemCodes.length) return new Map(); @@ -870,7 +871,7 @@ async function getContractItemsByItemCodes(itemCodes: string[], projectId: numbe ) ); - // itemCode와 contractItemId 배열의 매핑 생성 + // itemCode와 contractItemId 배열의 매핑 생성 (수정된 부분) const itemCodeToContractItemIds = new Map<string, number[]>(); for (const item of itemRecords) { @@ -878,9 +879,17 @@ async function getContractItemsByItemCodes(itemCodes: string[], projectId: numbe if (item.packageCode) { const matchedContractItems = contractItemRecords.filter(ci => ci.itemId === item.id); if (matchedContractItems.length > 0) { - // 일치하는 모든 contractItem을 배열로 저장 const contractItemIds = matchedContractItems.map(ci => ci.id); - itemCodeToContractItemIds.set(item.packageCode, contractItemIds); + + // 기존에 해당 packageCode가 있다면 배열에 추가, 없다면 새로 생성 + if (itemCodeToContractItemIds.has(item.packageCode)) { + const existing = itemCodeToContractItemIds.get(item.packageCode)!; + // 중복 제거하면서 합치기 + const combined = [...new Set([...existing, ...contractItemIds])]; + itemCodeToContractItemIds.set(item.packageCode, combined); + } else { + itemCodeToContractItemIds.set(item.packageCode, contractItemIds); + } } } } @@ -927,7 +936,7 @@ export async function saveFormMappingsAndMetas( /* 2. Contract‑item look‑up (TOOL_TYPE) - 수정된 부분 */ /* ------------------------------------------------------------------ */ const uniqueItemCodes = [...new Set(newRegisters.filter(nr => nr.TOOL_TYPE).map(nr => nr.TOOL_TYPE as string))]; - const itemCodeToContractItemIds = await getContractItemsByItemCodes(uniqueItemCodes, projectId); + const itemCodeToContractItemIds = await getContractItemsByItemCodes(uniqueItemCodes, projectId, projectCode); /* ------------------------------------------------------------------ */ /* 3. Buffers for bulk insert */ diff --git a/lib/sedp/sync-package.ts b/lib/sedp/sync-package.ts index cdbb5987..0261e448 100644 --- a/lib/sedp/sync-package.ts +++ b/lib/sedp/sync-package.ts @@ -2,7 +2,7 @@ // src/lib/cron/syncItemsFromCodeLists.ts import db from "@/db/db"; import { projects, items } from '@/db/schema'; -import { eq } from 'drizzle-orm'; +import { eq, and } 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/api'; @@ -154,14 +154,15 @@ export async function syncItemsFromCodeLists(): Promise<void> { // 기존 아이템 확인 (itemCode로 조회) const existingItem = await db.select() .from(items) - .where(eq(items.itemCode, codeValue.VALUE)) + .where(and(eq(items.itemCode, codeValue.VALUE),eq(items.ProjectNo, project.code))) .limit(1); if (existingItem.length > 0) { // 기존 아이템 업데이트 await db.update(items) .set(itemData) - .where(eq(items.itemCode, codeValue.VALUE)); + .where(and(eq(items.itemCode, codeValue.VALUE),eq(items.ProjectNo, project.code))) + totalItemsUpdated++; } else { // 새 아이템 삽입 @@ -253,13 +254,13 @@ export async function syncItemsForProject(projectCode: string): Promise<void> { // 기존 아이템 확인 const existingItem = await db.select() .from(items) - .where(eq(items.itemCode, codeValue.VALUE)) + .where(and(eq(items.itemCode, codeValue.VALUE),eq(items.ProjectNo, projectCode))) .limit(1); if (existingItem.length > 0) { await db.update(items) .set(itemData) - .where(eq(items.itemCode, codeValue.VALUE)); + .where(and(eq(items.itemCode, codeValue.VALUE),eq(items.ProjectNo, projectCode))); itemsUpdated++; } else { await db.insert(items).values(itemData); |
