feat: 일일 작업 보고서 생성 API 구조 개선 (C-S-M 패턴 적용)
- dailyWorkReportController의 생성 로직을 Service와 Model로 분리 - Service: 데이터 유효성 검사 등 비즈니스 로직 담당 - Model: 트랜잭션을 사용한 DB 쿼리 담당 - Controller: HTTP 요청/응답 처리만 담당하도록 단순화
This commit is contained in:
84
api.hyungi.net/services/dailyWorkReportService.js
Normal file
84
api.hyungi.net/services/dailyWorkReportService.js
Normal file
@@ -0,0 +1,84 @@
|
||||
const dailyWorkReportModel = require('../models/dailyWorkReportModel');
|
||||
|
||||
/**
|
||||
* 일일 작업 보고서를 생성하는 비즈니스 로직을 처리합니다.
|
||||
* @param {object} reportData - 컨트롤러에서 전달된 보고서 데이터
|
||||
* @returns {Promise<object>} 생성 결과 또는 에러
|
||||
*/
|
||||
const createDailyWorkReportService = async (reportData) => {
|
||||
const { report_date, worker_id, work_entries, created_by, created_by_name } = reportData;
|
||||
|
||||
// 1. 기본 유효성 검사
|
||||
if (!report_date || !worker_id || !work_entries) {
|
||||
throw new Error('필수 필드(report_date, worker_id, work_entries)가 누락되었습니다.');
|
||||
}
|
||||
|
||||
if (!Array.isArray(work_entries) || work_entries.length === 0) {
|
||||
throw new Error('최소 하나의 작업 항목(work_entries)이 필요합니다.');
|
||||
}
|
||||
|
||||
if (!created_by) {
|
||||
throw new Error('사용자 인증 정보(created_by)가 없습니다.');
|
||||
}
|
||||
|
||||
// 2. 작업 항목 유효성 검사
|
||||
for (let i = 0; i < work_entries.length; i++) {
|
||||
const entry = work_entries[i];
|
||||
const requiredFields = ['project_id', 'task_id', 'work_hours'];
|
||||
|
||||
for (const field of requiredFields) {
|
||||
if (entry[field] === undefined || entry[field] === null || entry[field] === '') {
|
||||
throw new Error(`작업 항목 ${i + 1}의 '${field}' 필드가 누락되었습니다.`);
|
||||
}
|
||||
}
|
||||
|
||||
// is_error가 true일 때 error_type_code_id 필수 검사
|
||||
if (entry.is_error === true && !entry.error_type_code_id) {
|
||||
throw new Error(`작업 항목 ${i + 1}이 에러 상태인 경우 'error_type_code_id'가 필요합니다.`);
|
||||
}
|
||||
|
||||
const hours = parseFloat(entry.work_hours);
|
||||
if (isNaN(hours) || hours <= 0 || hours > 24) {
|
||||
throw new Error(`작업 항목 ${i + 1}의 작업 시간이 유효하지 않습니다 (0 초과 24 이하).`);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 모델에 전달할 데이터 준비
|
||||
const modelData = {
|
||||
report_date,
|
||||
worker_id: parseInt(worker_id),
|
||||
entries: work_entries.map(entry => ({
|
||||
project_id: entry.project_id,
|
||||
task_id: entry.task_id,
|
||||
work_hours: parseFloat(entry.work_hours),
|
||||
is_error: entry.is_error || false,
|
||||
error_type_code_id: entry.error_type_code_id || null,
|
||||
created_by_user_id: created_by
|
||||
}))
|
||||
};
|
||||
|
||||
console.log('📝 [Service] 작업보고서 생성 요청:', {
|
||||
date: report_date,
|
||||
worker: worker_id,
|
||||
creator: created_by_name,
|
||||
entries_count: modelData.entries.length
|
||||
});
|
||||
|
||||
// 4. 모델 함수 호출
|
||||
try {
|
||||
const result = await dailyWorkReportModel.createReportEntries(modelData);
|
||||
console.log('✅ [Service] 작업보고서 생성 성공:', result);
|
||||
return {
|
||||
message: '작업보고서가 성공적으로 생성되었습니다.',
|
||||
...result
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('[Service] 작업보고서 생성 중 오류 발생:', error);
|
||||
// 모델에서 발생한 오류를 그대로 상위로 전달
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
createDailyWorkReportService,
|
||||
};
|
||||
Reference in New Issue
Block a user