summaryrefslogtreecommitdiff
path: root/lib/sedp/sync-form.ts
diff options
context:
space:
mode:
authordujinkim <dujin.kim@dtsolution.co.kr>2025-07-10 09:55:45 +0000
committerdujinkim <dujin.kim@dtsolution.co.kr>2025-07-10 09:55:45 +0000
commitc657ef972feeafff16ab0e07cb4771f7dd141ba0 (patch)
treebefabd884b00d3cc632c628b3e3810f61cc9f38d /lib/sedp/sync-form.ts
parentb8a03c9d130435a71c5d6217d06ccb0beb9697e5 (diff)
(대표님) 20250710 작업사항 - 평가 첨부, 로그인, SEDP 변경 요구사항 반영
Diffstat (limited to 'lib/sedp/sync-form.ts')
-rw-r--r--lib/sedp/sync-form.ts44
1 files changed, 30 insertions, 14 deletions
diff --git a/lib/sedp/sync-form.ts b/lib/sedp/sync-form.ts
index c293c98e..0606f4a9 100644
--- a/lib/sedp/sync-form.ts
+++ b/lib/sedp/sync-form.ts
@@ -851,17 +851,18 @@ async function getUomById(projectCode: string, uomId: string): Promise<UOM | nul
}
// contractItemId 조회 함수
-async function getContractItemsByItemCodes(itemCodes: string[], projectId: number): Promise<Map<string, number>> {
+async function getContractItemsByItemCodes(itemCodes: string[], projectId: number): Promise<Map<string, number[]>> {
try {
if (!itemCodes.length) return new Map();
// 먼저 itemCodes에 해당하는 item 레코드를 조회
const itemRecords = await db.select({
id: items.id,
- itemCode: items.itemCode
+ itemCode: items.itemCode,
+ packageCode: items.packageCode
})
.from(items)
- .where(inArray(items.itemCode, itemCodes));
+ .where(inArray(items.packageCode, itemCodes));
if (!itemRecords.length) {
console.log(`No items found for itemCodes: ${itemCodes.join(', ')}`);
@@ -885,21 +886,22 @@ async function getContractItemsByItemCodes(itemCodes: string[], projectId: numbe
)
);
- // itemCode와 contractItemId의 매핑 생성
- const itemCodeToContractItemId = new Map<string, number>();
+ // itemCode와 contractItemId 배열의 매핑 생성
+ const itemCodeToContractItemIds = new Map<string, number[]>();
for (const item of itemRecords) {
// itemCode가 null이 아닌 경우에만 처리
if (item.itemCode) {
const matchedContractItems = contractItemRecords.filter(ci => ci.itemId === item.id);
if (matchedContractItems.length > 0) {
- // 일치하는 첫 번째 contractItem 사용
- itemCodeToContractItemId.set(item.itemCode, matchedContractItems[0].id);
+ // 일치하는 모든 contractItem을 배열로 저장
+ const contractItemIds = matchedContractItems.map(ci => ci.id);
+ itemCodeToContractItemIds.set(item.itemCode, contractItemIds);
}
}
}
- return itemCodeToContractItemId;
+ return itemCodeToContractItemIds;
} catch (error) {
console.error('ContractItems 조회 중 오류 발생:', error);
return new Map();
@@ -938,10 +940,10 @@ export async function saveFormMappingsAndMetas(
const defaultAttributes = await getDefaulTAttributes();
/* ------------------------------------------------------------------ */
- /* 2. Contract‑item look‑up (TOOL_TYPE) */
+ /* 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 itemCodeToContractItemId = await getContractItemsByItemCodes(uniqueItemCodes, projectId);
+ const itemCodeToContractItemIds = await getContractItemsByItemCodes(uniqueItemCodes, projectId);
/* ------------------------------------------------------------------ */
/* 3. Buffers for bulk insert */
@@ -1027,11 +1029,25 @@ export async function saveFormMappingsAndMetas(
mappingsToSave.push({ projectId, tagTypeLabel: tp.description, classLabel: cls.label, formCode, formName: legacy?.DESC || formCode, remark: newReg.TOOL_TYPE || null, ep: newReg.EP_ID || legacy?.EP_ID || "", createdAt: new Date(), updatedAt: new Date() });
});
- /* ---------- 4‑d. contractItem ↔ form --------------------------- */
+ /* ---------- 4‑d. contractItem ↔ form - 수정된 부분 -------------- */
if (newReg.TOOL_TYPE) {
- const cId = itemCodeToContractItemId.get(newReg.TOOL_TYPE);
- if (cId) { contractItemIdsWithForms.add(cId); formsToSave.push({ contractItemId: cId, formCode, formName: legacy?.DESC || formCode, eng: true, createdAt: new Date(), updatedAt: new Date() }); }
- else console.warn(`itemCode ${newReg.TOOL_TYPE} 의 contractItemId 없음`);
+ const contractItemIds = itemCodeToContractItemIds.get(newReg.TOOL_TYPE);
+ if (contractItemIds && contractItemIds.length > 0) {
+ // 모든 contractItemId에 대해 form 생성
+ contractItemIds.forEach(cId => {
+ contractItemIdsWithForms.add(cId);
+ formsToSave.push({
+ contractItemId: cId,
+ formCode,
+ formName: legacy?.DESC || formCode,
+ eng: true,
+ createdAt: new Date(),
+ updatedAt: new Date()
+ });
+ });
+ } else {
+ console.warn(`itemCode ${newReg.TOOL_TYPE} 의 contractItemId 없음`);
+ }
}
}