제어관리 회사코드 저장 안되는 문제 수정

This commit is contained in:
kjs
2025-12-11 10:41:28 +09:00
parent 088596480f
commit f272f0c4c7
6 changed files with 962 additions and 751 deletions

View File

@@ -218,46 +218,62 @@ router.delete("/:flowId", async (req: Request, res: Response) => {
* 플로우 실행
* POST /api/dataflow/node-flows/:flowId/execute
*/
router.post("/:flowId/execute", authenticateToken, async (req: AuthenticatedRequest, res: Response) => {
try {
const { flowId } = req.params;
const contextData = req.body;
router.post(
"/:flowId/execute",
authenticateToken,
async (req: AuthenticatedRequest, res: Response) => {
try {
const { flowId } = req.params;
const contextData = req.body;
logger.info(`🚀 플로우 실행 요청: flowId=${flowId}`, {
contextDataKeys: Object.keys(contextData),
userId: req.user?.userId,
companyCode: req.user?.companyCode,
});
logger.info(`🚀 플로우 실행 요청: flowId=${flowId}`, {
contextDataKeys: Object.keys(contextData),
userId: req.user?.userId,
companyCode: req.user?.companyCode,
});
// 사용자 정보를 contextData에 추가
const enrichedContextData = {
...contextData,
userId: req.user?.userId,
userName: req.user?.userName,
companyCode: req.user?.companyCode,
};
// 🔍 디버깅: req.user 전체 확인
logger.info(`🔍 req.user 전체 정보:`, {
user: req.user,
hasUser: !!req.user,
});
// 플로우 실행
const result = await NodeFlowExecutionService.executeFlow(
parseInt(flowId, 10),
enrichedContextData
);
// 사용자 정보를 contextData에 추가
const enrichedContextData = {
...contextData,
userId: req.user?.userId,
userName: req.user?.userName,
companyCode: req.user?.companyCode,
};
return res.json({
success: result.success,
message: result.message,
data: result,
});
} catch (error) {
logger.error("플로우 실행 실패:", error);
return res.status(500).json({
success: false,
message:
error instanceof Error
? error.message
: "플로우 실행 중 오류가 발생했습니다.",
});
// 🔍 디버깅: enrichedContextData 확인
logger.info(`🔍 enrichedContextData:`, {
userId: enrichedContextData.userId,
companyCode: enrichedContextData.companyCode,
});
// 플로우 실행
const result = await NodeFlowExecutionService.executeFlow(
parseInt(flowId, 10),
enrichedContextData
);
return res.json({
success: result.success,
message: result.message,
data: result,
});
} catch (error) {
logger.error("플로우 실행 실패:", error);
return res.status(500).json({
success: false,
message:
error instanceof Error
? error.message
: "플로우 실행 중 오류가 발생했습니다.",
});
}
}
});
);
export default router;

View File

@@ -754,12 +754,19 @@ export class DynamicFormService {
// 🎯 제어관리 실행 (새로 추가)
try {
// savedData 또는 insertedRecord에서 company_code 추출
const recordCompanyCode =
(insertedRecord as Record<string, any>)?.company_code ||
dataToInsert.company_code ||
"*";
await this.executeDataflowControlIfConfigured(
screenId,
tableName,
insertedRecord as Record<string, any>,
"insert",
created_by || "system"
created_by || "system",
recordCompanyCode
);
} catch (controlError) {
console.error("⚠️ 제어관리 실행 오류:", controlError);
@@ -1107,12 +1114,19 @@ export class DynamicFormService {
// 🎯 제어관리 실행 (UPDATE 트리거)
try {
// updatedRecord에서 company_code 추출
const recordCompanyCode =
(updatedRecord as Record<string, any>)?.company_code ||
company_code ||
"*";
await this.executeDataflowControlIfConfigured(
0, // UPDATE는 screenId를 알 수 없으므로 0으로 설정 (추후 개선 필요)
tableName,
updatedRecord as Record<string, any>,
"update",
updated_by || "system"
updated_by || "system",
recordCompanyCode
);
} catch (controlError) {
console.error("⚠️ 제어관리 실행 오류:", controlError);
@@ -1251,12 +1265,17 @@ export class DynamicFormService {
try {
if (result && Array.isArray(result) && result.length > 0) {
const deletedRecord = result[0] as Record<string, any>;
// deletedRecord에서 company_code 추출
const recordCompanyCode =
deletedRecord?.company_code || companyCode || "*";
await this.executeDataflowControlIfConfigured(
0, // DELETE는 screenId를 알 수 없으므로 0으로 설정 (추후 개선 필요)
tableName,
deletedRecord,
"delete",
userId || "system"
userId || "system",
recordCompanyCode
);
}
} catch (controlError) {
@@ -1562,7 +1581,8 @@ export class DynamicFormService {
tableName: string,
savedData: Record<string, any>,
triggerType: "insert" | "update" | "delete",
userId: string = "system"
userId: string = "system",
companyCode: string = "*"
): Promise<void> {
try {
console.log(`🎯 제어관리 설정 확인 중... (screenId: ${screenId})`);
@@ -1636,6 +1656,7 @@ export class DynamicFormService {
buttonId: "save-button",
screenId: screenId,
userId: userId,
companyCode: companyCode,
formData: savedData,
}
);

View File

@@ -117,6 +117,18 @@ export class NodeFlowExecutionService {
try {
logger.info(`🚀 플로우 실행 시작: flowId=${flowId}`);
// 🔍 디버깅: contextData 상세 로그
logger.info(`🔍 contextData 상세:`, {
directCompanyCode: contextData.companyCode,
nestedCompanyCode: contextData.context?.companyCode,
directUserId: contextData.userId,
nestedUserId: contextData.context?.userId,
contextKeys: Object.keys(contextData),
nestedContextKeys: contextData.context
? Object.keys(contextData.context)
: "no nested context",
});
// 1. 플로우 데이터 조회
const flow = await queryOne<{
flow_id: number;
@@ -979,12 +991,25 @@ export class NodeFlowExecutionService {
const userId = context.buttonContext?.userId;
const companyCode = context.buttonContext?.companyCode;
// 🔍 디버깅: 자동 추가 조건 확인
console.log(` 🔍 INSERT 자동 추가 조건 확인:`, {
hasWriterMapping,
hasCompanyCodeMapping,
userId,
companyCode,
buttonContext: context.buttonContext,
});
// writer 자동 추가 (매핑에 없고, 컨텍스트에 userId가 있는 경우)
if (!hasWriterMapping && userId) {
fields.push("writer");
values.push(userId);
insertedData.writer = userId;
console.log(` 🔧 자동 추가: writer = ${userId}`);
} else {
console.log(
` ⚠️ writer 자동 추가 스킵: hasWriterMapping=${hasWriterMapping}, userId=${userId}`
);
}
// company_code 자동 추가 (매핑에 없고, 컨텍스트에 companyCode가 있는 경우)
@@ -993,6 +1018,10 @@ export class NodeFlowExecutionService {
values.push(companyCode);
insertedData.company_code = companyCode;
console.log(` 🔧 자동 추가: company_code = ${companyCode}`);
} else {
console.log(
` ⚠️ company_code 자동 추가 스킵: hasCompanyCodeMapping=${hasCompanyCodeMapping}, companyCode=${companyCode}`
);
}
const sql = `
@@ -2251,6 +2280,34 @@ export class NodeFlowExecutionService {
values.push(value);
});
// 🆕 writer와 company_code 자동 추가 (필드 매핑에 없는 경우)
const hasWriterMapping = fieldMappings.some(
(m: any) => m.targetField === "writer"
);
const hasCompanyCodeMapping = fieldMappings.some(
(m: any) => m.targetField === "company_code"
);
// 컨텍스트에서 사용자 정보 추출
const userId = context.buttonContext?.userId;
const companyCode = context.buttonContext?.companyCode;
// writer 자동 추가 (매핑에 없고, 컨텍스트에 userId가 있는 경우)
if (!hasWriterMapping && userId) {
columns.push("writer");
values.push(userId);
logger.info(` 🔧 UPSERT INSERT - 자동 추가: writer = ${userId}`);
}
// company_code 자동 추가 (매핑에 없고, 컨텍스트에 companyCode가 있는 경우)
if (!hasCompanyCodeMapping && companyCode && companyCode !== "*") {
columns.push("company_code");
values.push(companyCode);
logger.info(
` 🔧 UPSERT INSERT - 자동 추가: company_code = ${companyCode}`
);
}
const placeholders = values.map((_, i) => `$${i + 1}`).join(", ");
const insertSql = `
INSERT INTO ${targetTable} (${columns.join(", ")})