feat: 일일 작업 보고서 생성 API 구조 개선 (C-S-M 패턴 적용)
- dailyWorkReportController의 생성 로직을 Service와 Model로 분리 - Service: 데이터 유효성 검사 등 비즈니스 로직 담당 - Model: 트랜잭션을 사용한 DB 쿼리 담당 - Controller: HTTP 요청/응답 처리만 담당하도록 단순화
This commit is contained in:
@@ -794,6 +794,57 @@ const getStatistics = async (start_date, end_date, callback) => {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* [V2] 여러 작업 보고서 항목을 트랜잭션으로 생성합니다. (Promise 기반)
|
||||
* @param {object} modelData - 서비스 레이어에서 전달된 데이터
|
||||
* @returns {Promise<object>} 삽입된 항목의 ID 배열과 개수
|
||||
*/
|
||||
const createReportEntries = async ({ report_date, worker_id, entries }) => {
|
||||
const db = await getDb();
|
||||
const conn = await db.getConnection();
|
||||
try {
|
||||
await conn.beginTransaction();
|
||||
|
||||
const insertedIds = [];
|
||||
const sql = `
|
||||
INSERT INTO daily_work_reports
|
||||
(report_date, worker_id, project_id, task_id, work_hours, is_error, error_type_code_id, created_by_user_id)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`;
|
||||
|
||||
for (const entry of entries) {
|
||||
const { project_id, task_id, work_hours, is_error, error_type_code_id, created_by_user_id } = entry;
|
||||
const [result] = await conn.query(sql, [
|
||||
report_date,
|
||||
worker_id,
|
||||
project_id,
|
||||
task_id,
|
||||
work_hours,
|
||||
is_error,
|
||||
error_type_code_id,
|
||||
created_by_user_id
|
||||
]);
|
||||
insertedIds.push(result.insertId);
|
||||
}
|
||||
|
||||
await conn.commit();
|
||||
|
||||
console.log(`[Model] ${insertedIds.length}개 작업 항목 생성 완료.`);
|
||||
return {
|
||||
inserted_ids: insertedIds,
|
||||
inserted_count: insertedIds.length
|
||||
};
|
||||
|
||||
} catch (err) {
|
||||
await conn.rollback();
|
||||
console.error('[Model] 작업 보고서 생성 중 오류 발생:', err);
|
||||
// 여기서 발생한 에러는 서비스 레이어로 전파됩니다.
|
||||
throw new Error('데이터베이스에 작업 보고서를 생성하는 중 오류가 발생했습니다.');
|
||||
} finally {
|
||||
conn.release();
|
||||
}
|
||||
};
|
||||
|
||||
// 모든 함수 내보내기 (기존 기능 + 누적 기능)
|
||||
module.exports = {
|
||||
// 📋 마스터 데이터
|
||||
@@ -826,5 +877,8 @@ module.exports = {
|
||||
updateById,
|
||||
removeById,
|
||||
removeByDateAndWorker,
|
||||
getStatistics
|
||||
getStatistics,
|
||||
|
||||
// 새로 추가된 V2 함수
|
||||
createReportEntries
|
||||
};
|
||||
Reference in New Issue
Block a user