배치 UPSERT 기능 및 고정값 매핑 버그 수정
This commit is contained in:
@@ -2,6 +2,7 @@ import cron, { ScheduledTask } from "node-cron";
|
||||
import { BatchService } from "./batchService";
|
||||
import { BatchExecutionLogService } from "./batchExecutionLogService";
|
||||
import { logger } from "../utils/logger";
|
||||
import { query } from "../database/db";
|
||||
|
||||
export class BatchSchedulerService {
|
||||
private static scheduledTasks: Map<number, ScheduledTask> = new Map();
|
||||
@@ -214,9 +215,16 @@ export class BatchSchedulerService {
|
||||
}
|
||||
|
||||
// 테이블별로 매핑을 그룹화
|
||||
// 고정값 매핑(mapping_type === 'fixed')은 별도 그룹으로 분리하지 않고 나중에 처리
|
||||
const tableGroups = new Map<string, typeof config.batch_mappings>();
|
||||
const fixedMappingsGlobal: typeof config.batch_mappings = [];
|
||||
|
||||
for (const mapping of config.batch_mappings) {
|
||||
// 고정값 매핑은 별도로 모아둠 (FROM 소스가 필요 없음)
|
||||
if (mapping.mapping_type === "fixed") {
|
||||
fixedMappingsGlobal.push(mapping);
|
||||
continue;
|
||||
}
|
||||
const key = `${mapping.from_connection_type}:${mapping.from_connection_id || "internal"}:${mapping.from_table_name}`;
|
||||
if (!tableGroups.has(key)) {
|
||||
tableGroups.set(key, []);
|
||||
@@ -224,6 +232,14 @@ export class BatchSchedulerService {
|
||||
tableGroups.get(key)!.push(mapping);
|
||||
}
|
||||
|
||||
// 고정값 매핑만 있고 일반 매핑이 없는 경우 처리
|
||||
if (tableGroups.size === 0 && fixedMappingsGlobal.length > 0) {
|
||||
logger.warn(
|
||||
`일반 매핑이 없고 고정값 매핑만 있습니다. 고정값만으로는 배치를 실행할 수 없습니다.`
|
||||
);
|
||||
return { totalRecords, successRecords, failedRecords };
|
||||
}
|
||||
|
||||
// 각 테이블 그룹별로 처리
|
||||
for (const [tableKey, mappings] of tableGroups) {
|
||||
try {
|
||||
@@ -244,10 +260,31 @@ export class BatchSchedulerService {
|
||||
"./batchExternalDbService"
|
||||
);
|
||||
|
||||
// auth_service_name이 설정된 경우 auth_tokens에서 토큰 조회
|
||||
let apiKey = firstMapping.from_api_key || "";
|
||||
if (config.auth_service_name) {
|
||||
const tokenResult = await query<{ access_token: string }>(
|
||||
`SELECT access_token FROM auth_tokens
|
||||
WHERE service_name = $1
|
||||
ORDER BY created_date DESC LIMIT 1`,
|
||||
[config.auth_service_name]
|
||||
);
|
||||
if (tokenResult.length > 0 && tokenResult[0].access_token) {
|
||||
apiKey = tokenResult[0].access_token;
|
||||
logger.info(
|
||||
`auth_tokens에서 토큰 조회 성공: ${config.auth_service_name}`
|
||||
);
|
||||
} else {
|
||||
logger.warn(
|
||||
`auth_tokens에서 토큰을 찾을 수 없음: ${config.auth_service_name}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 👇 Body 파라미터 추가 (POST 요청 시)
|
||||
const apiResult = await BatchExternalDbService.getDataFromRestApi(
|
||||
firstMapping.from_api_url!,
|
||||
firstMapping.from_api_key!,
|
||||
apiKey,
|
||||
firstMapping.from_table_name,
|
||||
(firstMapping.from_api_method as
|
||||
| "GET"
|
||||
@@ -266,7 +303,36 @@ export class BatchSchedulerService {
|
||||
);
|
||||
|
||||
if (apiResult.success && apiResult.data) {
|
||||
fromData = apiResult.data;
|
||||
// 데이터 배열 경로가 설정되어 있으면 해당 경로에서 배열 추출
|
||||
if (config.data_array_path) {
|
||||
const extractArrayByPath = (obj: any, path: string): any[] => {
|
||||
if (!path) return Array.isArray(obj) ? obj : [obj];
|
||||
const keys = path.split(".");
|
||||
let current = obj;
|
||||
for (const key of keys) {
|
||||
if (current === null || current === undefined) return [];
|
||||
current = current[key];
|
||||
}
|
||||
return Array.isArray(current)
|
||||
? current
|
||||
: current
|
||||
? [current]
|
||||
: [];
|
||||
};
|
||||
|
||||
// apiResult.data가 단일 객체인 경우 (API 응답 전체)
|
||||
const rawData =
|
||||
Array.isArray(apiResult.data) && apiResult.data.length === 1
|
||||
? apiResult.data[0]
|
||||
: apiResult.data;
|
||||
|
||||
fromData = extractArrayByPath(rawData, config.data_array_path);
|
||||
logger.info(
|
||||
`데이터 배열 경로 '${config.data_array_path}'에서 ${fromData.length}개 레코드 추출`
|
||||
);
|
||||
} else {
|
||||
fromData = apiResult.data;
|
||||
}
|
||||
} else {
|
||||
throw new Error(`REST API 데이터 조회 실패: ${apiResult.message}`);
|
||||
}
|
||||
@@ -298,6 +364,11 @@ export class BatchSchedulerService {
|
||||
const mappedData = fromData.map((row) => {
|
||||
const mappedRow: any = {};
|
||||
for (const mapping of mappings) {
|
||||
// 고정값 매핑은 이미 분리되어 있으므로 여기서는 처리하지 않음
|
||||
if (mapping.mapping_type === "fixed") {
|
||||
continue;
|
||||
}
|
||||
|
||||
// DB → REST API 배치인지 확인
|
||||
if (
|
||||
firstMapping.to_connection_type === "restapi" &&
|
||||
@@ -315,6 +386,13 @@ export class BatchSchedulerService {
|
||||
}
|
||||
}
|
||||
|
||||
// 고정값 매핑 적용 (전역으로 분리된 fixedMappingsGlobal 사용)
|
||||
for (const fixedMapping of fixedMappingsGlobal) {
|
||||
// from_column_name에 고정값이 저장되어 있음
|
||||
mappedRow[fixedMapping.to_column_name] =
|
||||
fixedMapping.from_column_name;
|
||||
}
|
||||
|
||||
// 멀티테넌시: TO가 DB일 때 company_code 자동 주입
|
||||
// - 배치 설정에 company_code가 있고
|
||||
// - 매핑에서 company_code를 명시적으로 다루지 않은 경우만
|
||||
@@ -384,12 +462,14 @@ export class BatchSchedulerService {
|
||||
insertResult = { successCount: 0, failedCount: 0 };
|
||||
}
|
||||
} else {
|
||||
// DB에 데이터 삽입
|
||||
// DB에 데이터 삽입 (save_mode, conflict_key 지원)
|
||||
insertResult = await BatchService.insertDataToTable(
|
||||
firstMapping.to_table_name,
|
||||
mappedData,
|
||||
firstMapping.to_connection_type as "internal" | "external",
|
||||
firstMapping.to_connection_id || undefined
|
||||
firstMapping.to_connection_id || undefined,
|
||||
(config.save_mode as "INSERT" | "UPSERT") || "INSERT",
|
||||
config.conflict_key || undefined
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user