feat: Add savedIds to UPSERT response and update related components
- Enhanced the UPSERT process in DataService to include savedIds in the response, allowing tracking of newly saved record IDs. - Updated the dataApi to reflect the new savedIds field in the Promise return type. - Modified the SelectedItemsDetailInputComponent to handle and inject saved mapping IDs into detail records, improving data integrity and management during the save process. - Added logging for savedIds to facilitate debugging and tracking of saved records.
This commit is contained in:
parent
45029bf5f4
commit
9e1a54c738
|
|
@ -742,6 +742,7 @@ router.post(
|
||||||
inserted: result.data?.inserted || 0,
|
inserted: result.data?.inserted || 0,
|
||||||
updated: result.data?.updated || 0,
|
updated: result.data?.updated || 0,
|
||||||
deleted: result.data?.deleted || 0,
|
deleted: result.data?.deleted || 0,
|
||||||
|
savedIds: result.data?.savedIds || [],
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("그룹화된 데이터 UPSERT 오류:", error);
|
console.error("그룹화된 데이터 UPSERT 오류:", error);
|
||||||
|
|
|
||||||
|
|
@ -1519,11 +1519,12 @@ class DataService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`✅ UPSERT 완료:`, { inserted, updated, deleted });
|
const savedIds = Array.from(processedIds);
|
||||||
|
console.log(`✅ UPSERT 완료:`, { inserted, updated, deleted, savedIds });
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
data: { inserted, updated, deleted },
|
data: { inserted, updated, deleted, savedIds },
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`UPSERT 오류 (${tableName}):`, error);
|
console.error(`UPSERT 오류 (${tableName}):`, error);
|
||||||
|
|
|
||||||
|
|
@ -238,7 +238,7 @@ export const dataApi = {
|
||||||
parentKeys: Record<string, any>,
|
parentKeys: Record<string, any>,
|
||||||
records: Array<Record<string, any>>,
|
records: Array<Record<string, any>>,
|
||||||
options?: { deleteOrphans?: boolean }
|
options?: { deleteOrphans?: boolean }
|
||||||
): Promise<{ success: boolean; inserted?: number; updated?: number; deleted?: number; message?: string; error?: string }> => {
|
): Promise<{ success: boolean; inserted?: number; updated?: number; deleted?: number; savedIds?: string[]; message?: string; error?: string }> => {
|
||||||
try {
|
try {
|
||||||
console.log("📡 [dataApi.upsertGroupedRecords] 요청 데이터:", {
|
console.log("📡 [dataApi.upsertGroupedRecords] 요청 데이터:", {
|
||||||
tableName,
|
tableName,
|
||||||
|
|
|
||||||
|
|
@ -689,6 +689,8 @@ export const SelectedItemsDetailInputComponent: React.FC<SelectedItemsDetailInpu
|
||||||
// 레코드에 id(기존 DB PK)가 있으면 EDIT 모드 → 고아 삭제
|
// 레코드에 id(기존 DB PK)가 있으면 EDIT 모드 → 고아 삭제
|
||||||
// id 없으면 CREATE 모드 → 기존 레코드 건드리지 않음
|
// id 없으면 CREATE 모드 → 기존 레코드 건드리지 않음
|
||||||
const mappingHasDbIds = mappingRecords.some((r) => !!r.id);
|
const mappingHasDbIds = mappingRecords.some((r) => !!r.id);
|
||||||
|
// 저장된 매핑 ID를 추적 (디테일 테이블에 mapping_id 주입용)
|
||||||
|
let savedMappingIds: string[] = [];
|
||||||
try {
|
try {
|
||||||
const mappingResult = await dataApi.upsertGroupedRecords(
|
const mappingResult = await dataApi.upsertGroupedRecords(
|
||||||
mainTable,
|
mainTable,
|
||||||
|
|
@ -696,6 +698,11 @@ export const SelectedItemsDetailInputComponent: React.FC<SelectedItemsDetailInpu
|
||||||
mappingRecords,
|
mappingRecords,
|
||||||
{ deleteOrphans: mappingHasDbIds },
|
{ deleteOrphans: mappingHasDbIds },
|
||||||
);
|
);
|
||||||
|
// 백엔드에서 반환된 저장된 레코드 ID 목록
|
||||||
|
if (mappingResult.success && mappingResult.savedIds) {
|
||||||
|
savedMappingIds = mappingResult.savedIds;
|
||||||
|
console.log(`✅ ${mainTable} 저장 완료, savedIds:`, savedMappingIds);
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(`❌ ${mainTable} 저장 실패:`, err);
|
console.error(`❌ ${mainTable} 저장 실패:`, err);
|
||||||
}
|
}
|
||||||
|
|
@ -755,6 +762,18 @@ export const SelectedItemsDetailInputComponent: React.FC<SelectedItemsDetailInpu
|
||||||
priceRecords.push(emptyRecord);
|
priceRecords.push(emptyRecord);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Step1에서 저장된 매핑 ID를 디테일 레코드에 주입
|
||||||
|
// (customer_item_prices.mapping_id ← customer_item_mapping.id)
|
||||||
|
if (savedMappingIds.length > 0) {
|
||||||
|
const mappingId = savedMappingIds[0]; // 일반적으로 1:N (매핑 1개 : 단가 N개)
|
||||||
|
priceRecords.forEach((record) => {
|
||||||
|
if (!record.mapping_id) {
|
||||||
|
record.mapping_id = mappingId;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
console.log(`🔗 디테일 레코드에 mapping_id 주입: ${mappingId}`);
|
||||||
|
}
|
||||||
|
|
||||||
const priceHasDbIds = priceRecords.some((r) => !!r.id);
|
const priceHasDbIds = priceRecords.some((r) => !!r.id);
|
||||||
try {
|
try {
|
||||||
const detailResult = await dataApi.upsertGroupedRecords(
|
const detailResult = await dataApi.upsertGroupedRecords(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue