refactor: System 1 모델/컨트롤러 콜백→async/await 전환
11개 모델 파일의 171개 콜백 메서드를 직접 return/throw 패턴으로 변환. 8개 컨트롤러에서 new Promise 래퍼와 중첩 콜백 제거, console.error→logger.error 교체. 미사용 pageAccessModel.js 삭제. 전체 -3,600줄 감소. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -24,16 +24,10 @@ const createWorkReportService = async (reportData) => {
|
||||
|
||||
logger.info('작업 보고서 생성 요청', { count: reports.length });
|
||||
|
||||
const workReport_ids = [];
|
||||
|
||||
try {
|
||||
const workReport_ids = [];
|
||||
for (const report of reports) {
|
||||
const id = await new Promise((resolve, reject) => {
|
||||
workReportModel.create(report, (err, insertId) => {
|
||||
if (err) reject(err);
|
||||
else resolve(insertId);
|
||||
});
|
||||
});
|
||||
const id = await workReportModel.create(report);
|
||||
workReport_ids.push(id);
|
||||
}
|
||||
|
||||
@@ -66,15 +60,8 @@ const getWorkReportsByDateService = async (date) => {
|
||||
logger.info('작업 보고서 날짜별 조회 요청', { date });
|
||||
|
||||
try {
|
||||
const rows = await new Promise((resolve, reject) => {
|
||||
workReportModel.getAllByDate(date, (err, data) => {
|
||||
if (err) reject(err);
|
||||
else resolve(data);
|
||||
});
|
||||
});
|
||||
|
||||
const rows = await workReportModel.getAllByDate(date);
|
||||
logger.info('작업 보고서 조회 성공', { date, count: rows.length });
|
||||
|
||||
return rows;
|
||||
} catch (error) {
|
||||
logger.error('작업 보고서 조회 실패', { date, error: error.message });
|
||||
@@ -96,15 +83,8 @@ const getWorkReportsInRangeService = async (start, end) => {
|
||||
logger.info('작업 보고서 기간별 조회 요청', { start, end });
|
||||
|
||||
try {
|
||||
const rows = await new Promise((resolve, reject) => {
|
||||
workReportModel.getByRange(start, end, (err, data) => {
|
||||
if (err) reject(err);
|
||||
else resolve(data);
|
||||
});
|
||||
});
|
||||
|
||||
const rows = await workReportModel.getByRange(start, end);
|
||||
logger.info('작업 보고서 조회 성공', { start, end, count: rows.length });
|
||||
|
||||
return rows;
|
||||
} catch (error) {
|
||||
logger.error('작업 보고서 조회 실패', { start, end, error: error.message });
|
||||
@@ -123,12 +103,7 @@ const getWorkReportByIdService = async (id) => {
|
||||
logger.info('작업 보고서 조회 요청', { report_id: id });
|
||||
|
||||
try {
|
||||
const row = await new Promise((resolve, reject) => {
|
||||
workReportModel.getById(id, (err, data) => {
|
||||
if (err) reject(err);
|
||||
else resolve(data);
|
||||
});
|
||||
});
|
||||
const row = await workReportModel.getById(id);
|
||||
|
||||
if (!row) {
|
||||
logger.warn('작업 보고서를 찾을 수 없음', { report_id: id });
|
||||
@@ -136,13 +111,9 @@ const getWorkReportByIdService = async (id) => {
|
||||
}
|
||||
|
||||
logger.info('작업 보고서 조회 성공', { report_id: id });
|
||||
|
||||
return row;
|
||||
} catch (error) {
|
||||
if (error instanceof NotFoundError) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
if (error instanceof NotFoundError) throw error;
|
||||
logger.error('작업 보고서 조회 실패', { report_id: id, error: error.message });
|
||||
throw new DatabaseError('작업 보고서 조회 중 오류가 발생했습니다');
|
||||
}
|
||||
@@ -159,12 +130,7 @@ const updateWorkReportService = async (id, updateData) => {
|
||||
logger.info('작업 보고서 수정 요청', { report_id: id });
|
||||
|
||||
try {
|
||||
const changes = await new Promise((resolve, reject) => {
|
||||
workReportModel.update(id, updateData, (err, affectedRows) => {
|
||||
if (err) reject(err);
|
||||
else resolve(affectedRows);
|
||||
});
|
||||
});
|
||||
const changes = await workReportModel.update(id, updateData);
|
||||
|
||||
if (changes === 0) {
|
||||
logger.warn('작업 보고서를 찾을 수 없거나 변경사항 없음', { report_id: id });
|
||||
@@ -172,13 +138,9 @@ const updateWorkReportService = async (id, updateData) => {
|
||||
}
|
||||
|
||||
logger.info('작업 보고서 수정 성공', { report_id: id, changes });
|
||||
|
||||
return { changes };
|
||||
} catch (error) {
|
||||
if (error instanceof NotFoundError) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
if (error instanceof NotFoundError) throw error;
|
||||
logger.error('작업 보고서 수정 실패', { report_id: id, error: error.message });
|
||||
throw new DatabaseError('작업 보고서 수정 중 오류가 발생했습니다');
|
||||
}
|
||||
@@ -195,12 +157,7 @@ const removeWorkReportService = async (id) => {
|
||||
logger.info('작업 보고서 삭제 요청', { report_id: id });
|
||||
|
||||
try {
|
||||
const changes = await new Promise((resolve, reject) => {
|
||||
workReportModel.remove(id, (err, affectedRows) => {
|
||||
if (err) reject(err);
|
||||
else resolve(affectedRows);
|
||||
});
|
||||
});
|
||||
const changes = await workReportModel.remove(id);
|
||||
|
||||
if (changes === 0) {
|
||||
logger.warn('작업 보고서를 찾을 수 없음', { report_id: id });
|
||||
@@ -208,13 +165,9 @@ const removeWorkReportService = async (id) => {
|
||||
}
|
||||
|
||||
logger.info('작업 보고서 삭제 성공', { report_id: id, changes });
|
||||
|
||||
return { changes };
|
||||
} catch (error) {
|
||||
if (error instanceof NotFoundError) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
if (error instanceof NotFoundError) throw error;
|
||||
logger.error('작업 보고서 삭제 실패', { report_id: id, error: error.message });
|
||||
throw new DatabaseError('작업 보고서 삭제 중 오류가 발생했습니다');
|
||||
}
|
||||
@@ -237,35 +190,18 @@ const getSummaryService = async (year, month) => {
|
||||
logger.info('작업 보고서 월간 요약 조회 요청', { year, month, start, end });
|
||||
|
||||
try {
|
||||
const rows = await new Promise((resolve, reject) => {
|
||||
workReportModel.getByRange(start, end, (err, data) => {
|
||||
if (err) reject(err);
|
||||
else resolve(data);
|
||||
});
|
||||
});
|
||||
const rows = await workReportModel.getByRange(start, end);
|
||||
|
||||
if (!rows || rows.length === 0) {
|
||||
logger.warn('월간 요약 데이터 없음', { year, month });
|
||||
throw new NotFoundError('해당 기간의 작업 보고서가 없습니다');
|
||||
}
|
||||
|
||||
logger.info('작업 보고서 월간 요약 조회 성공', {
|
||||
year,
|
||||
month,
|
||||
count: rows.length
|
||||
});
|
||||
|
||||
logger.info('작업 보고서 월간 요약 조회 성공', { year, month, count: rows.length });
|
||||
return rows;
|
||||
} catch (error) {
|
||||
if (error instanceof NotFoundError) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
logger.error('작업 보고서 월간 요약 조회 실패', {
|
||||
year,
|
||||
month,
|
||||
error: error.message
|
||||
});
|
||||
if (error instanceof NotFoundError) throw error;
|
||||
logger.error('작업 보고서 월간 요약 조회 실패', { year, month, error: error.message });
|
||||
throw new DatabaseError('월간 요약 조회 중 오류가 발생했습니다');
|
||||
}
|
||||
};
|
||||
@@ -274,7 +210,6 @@ const getSummaryService = async (year, month) => {
|
||||
|
||||
/**
|
||||
* 작업 보고서의 부적합 원인 목록 조회
|
||||
* - error_type_id 또는 issue_report_id 중 하나로 연결
|
||||
*/
|
||||
const getReportDefectsService = async (reportId) => {
|
||||
const db = await getDb();
|
||||
@@ -313,22 +248,16 @@ const getReportDefectsService = async (reportId) => {
|
||||
|
||||
/**
|
||||
* 부적합 원인 저장 (전체 교체)
|
||||
* - error_type_id 또는 issue_report_id 중 하나 사용 가능
|
||||
* - issue_report_id: 신고된 이슈와 연결
|
||||
* - error_type_id: 기존 오류 유형 (레거시)
|
||||
*/
|
||||
const saveReportDefectsService = async (reportId, defects) => {
|
||||
const db = await getDb();
|
||||
try {
|
||||
await db.query('START TRANSACTION');
|
||||
|
||||
// 기존 부적합 원인 삭제
|
||||
await db.execute('DELETE FROM work_report_defects WHERE report_id = ?', [reportId]);
|
||||
|
||||
// 새 부적합 원인 추가
|
||||
if (defects && defects.length > 0) {
|
||||
for (const defect of defects) {
|
||||
// issue_report_id > category_id/item_id > error_type_id 순으로 우선
|
||||
await db.execute(`
|
||||
INSERT INTO work_report_defects (report_id, error_type_id, issue_report_id, category_id, item_id, defect_hours, note)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
@@ -344,12 +273,10 @@ const saveReportDefectsService = async (reportId, defects) => {
|
||||
}
|
||||
}
|
||||
|
||||
// 총 부적합 시간 계산 및 daily_work_reports 업데이트
|
||||
const totalErrorHours = defects
|
||||
? defects.reduce((sum, d) => sum + (parseFloat(d.defect_hours) || 0), 0)
|
||||
: 0;
|
||||
|
||||
// 첫 번째 defect의 error_type_id를 대표값으로 (레거시 호환)
|
||||
const firstErrorTypeId = defects && defects.length > 0
|
||||
? (defects.find(d => d.error_type_id)?.error_type_id || null)
|
||||
: null;
|
||||
@@ -380,7 +307,6 @@ const saveReportDefectsService = async (reportId, defects) => {
|
||||
|
||||
/**
|
||||
* 부적합 원인 추가 (단일)
|
||||
* - issue_report_id 또는 error_type_id 중 하나 사용
|
||||
*/
|
||||
const addReportDefectService = async (reportId, defectData) => {
|
||||
const db = await getDb();
|
||||
@@ -396,7 +322,6 @@ const addReportDefectService = async (reportId, defectData) => {
|
||||
defectData.note || null
|
||||
]);
|
||||
|
||||
// 총 부적합 시간 업데이트
|
||||
await updateTotalErrorHours(reportId);
|
||||
|
||||
logger.info('부적합 원인 추가 성공', { reportId, defectId: result.insertId });
|
||||
@@ -413,7 +338,6 @@ const addReportDefectService = async (reportId, defectData) => {
|
||||
const removeReportDefectService = async (defectId) => {
|
||||
const db = await getDb();
|
||||
try {
|
||||
// report_id 먼저 조회
|
||||
const [defect] = await db.execute('SELECT report_id FROM work_report_defects WHERE defect_id = ?', [defectId]);
|
||||
if (defect.length === 0) {
|
||||
throw new NotFoundError('부적합 원인을 찾을 수 없습니다');
|
||||
@@ -421,10 +345,7 @@ const removeReportDefectService = async (defectId) => {
|
||||
|
||||
const reportId = defect[0].report_id;
|
||||
|
||||
// 삭제
|
||||
await db.execute('DELETE FROM work_report_defects WHERE defect_id = ?', [defectId]);
|
||||
|
||||
// 총 부적합 시간 업데이트
|
||||
await updateTotalErrorHours(reportId);
|
||||
|
||||
logger.info('부적합 원인 삭제 성공', { defectId, reportId });
|
||||
@@ -438,7 +359,6 @@ const removeReportDefectService = async (defectId) => {
|
||||
|
||||
/**
|
||||
* 총 부적합 시간 업데이트 헬퍼
|
||||
* - issue_report_id가 있는 경우도 고려
|
||||
*/
|
||||
const updateTotalErrorHours = async (reportId) => {
|
||||
const db = await getDb();
|
||||
@@ -450,8 +370,6 @@ const updateTotalErrorHours = async (reportId) => {
|
||||
|
||||
const totalErrorHours = result[0].total || 0;
|
||||
|
||||
// 첫 번째 부적합 원인의 error_type_id를 대표값으로 사용 (레거시 호환)
|
||||
// issue_report_id만 있는 경우 error_type_id는 null
|
||||
const [firstDefect] = await db.execute(`
|
||||
SELECT error_type_id FROM work_report_defects
|
||||
WHERE report_id = ? AND error_type_id IS NOT NULL
|
||||
@@ -480,7 +398,6 @@ module.exports = {
|
||||
updateWorkReportService,
|
||||
removeWorkReportService,
|
||||
getSummaryService,
|
||||
// 부적합 원인 관리
|
||||
getReportDefectsService,
|
||||
saveReportDefectsService,
|
||||
addReportDefectService,
|
||||
|
||||
Reference in New Issue
Block a user