feat: Implement smart factory log management features
- Added new API endpoints for retrieving company-specific user lists and sending immediate logs for selected users. - Enhanced the smartFactoryLogController with functions to handle user retrieval and immediate log sending, improving operational efficiency. - Updated adminRoutes to include routes for the new functionalities, ensuring proper access control for super admins. - Refactored the sendSmartFactoryLog function to improve logging and error handling, providing better insights into the log transmission process. These changes aim to enhance the smart factory log management capabilities, facilitating better user interaction and operational tracking.
This commit is contained in:
@@ -7,6 +7,7 @@ import { query, queryOne } from "../database/db";
|
||||
import { logger } from "../utils/logger";
|
||||
import { encryptionService } from "../services/encryptionService";
|
||||
import {
|
||||
sendSmartFactoryLog,
|
||||
runScheduleNow,
|
||||
getTodayPlanStatus,
|
||||
planDailySends,
|
||||
@@ -502,3 +503,92 @@ export const deleteApiKey = async (
|
||||
res.status(500).json({ success: false, message: "API 키 삭제 실패" });
|
||||
}
|
||||
};
|
||||
|
||||
// ─── 즉시 전송 ───
|
||||
|
||||
/**
|
||||
* GET /api/admin/smart-factory-log/users/:companyCode
|
||||
* 회사별 사용자 목록 조회 (즉시 전송 대상 선택용)
|
||||
*/
|
||||
export const getCompanyUsers = async (
|
||||
req: AuthenticatedRequest,
|
||||
res: Response
|
||||
): Promise<void> => {
|
||||
try {
|
||||
const { companyCode } = req.params;
|
||||
|
||||
const users = await query<any>(
|
||||
`SELECT user_id, user_name, dept_name
|
||||
FROM user_info
|
||||
WHERE company_code = $1 AND (status = 'active' OR status IS NULL)
|
||||
ORDER BY user_name`,
|
||||
[companyCode]
|
||||
);
|
||||
|
||||
res.json({ success: true, data: users });
|
||||
} catch (error) {
|
||||
logger.error("사용자 목록 조회 실패:", error);
|
||||
res.status(500).json({ success: false, message: "사용자 목록 조회 실패" });
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* POST /api/admin/smart-factory-log/send-now
|
||||
* 선택한 사용자 즉시 전송
|
||||
* body: { companyCode, userIds: string[], timeStart?, timeEnd? }
|
||||
*/
|
||||
export const sendNow = async (
|
||||
req: AuthenticatedRequest,
|
||||
res: Response
|
||||
): Promise<void> => {
|
||||
try {
|
||||
const { companyCode, userIds } = req.body;
|
||||
|
||||
logger.info(`=== 즉시 전송 API 호출 === companyCode=${companyCode}, userIds=${JSON.stringify(userIds)}`);
|
||||
|
||||
if (!companyCode || !userIds || userIds.length === 0) {
|
||||
res.status(400).json({ success: false, message: "회사코드와 사용자를 선택해주세요." });
|
||||
return;
|
||||
}
|
||||
|
||||
// 사용자 정보 조회
|
||||
const users = await query<{ user_id: string; user_name: string }>(
|
||||
`SELECT user_id, user_name FROM user_info WHERE company_code = $1 AND user_id = ANY($2)`,
|
||||
[companyCode, userIds]
|
||||
);
|
||||
|
||||
logger.info(`즉시 전송 대상: ${users.length}명 (조회된 사용자: ${users.map(u => u.user_id).join(", ")})`);
|
||||
|
||||
// 현재 시간으로 즉시 전송
|
||||
let success = 0;
|
||||
let fail = 0;
|
||||
const remoteAddr = req.ip || "127.0.0.1";
|
||||
|
||||
for (const user of users) {
|
||||
try {
|
||||
logger.info(`즉시 전송 시작: ${user.user_id}`);
|
||||
await sendSmartFactoryLog({
|
||||
userId: user.user_id,
|
||||
userName: user.user_name,
|
||||
remoteAddr,
|
||||
useType: "접속",
|
||||
companyCode,
|
||||
});
|
||||
success++;
|
||||
logger.info(`즉시 전송 성공: ${user.user_id}`);
|
||||
} catch (e) {
|
||||
fail++;
|
||||
logger.error(`즉시 전송 실패: ${user.user_id}`, e);
|
||||
}
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: { total: users.length, success, fail },
|
||||
message: `${success}명 전송 완료${fail > 0 ? `, ${fail}명 실패` : ""}`,
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error("즉시 전송 실패:", error);
|
||||
res.status(500).json({ success: false, message: "즉시 전송 실패" });
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user