// /services/dailyIssueReportService.js const dailyIssueReportModel = require('../models/dailyIssueReportModel'); /** * 일일 이슈 보고서를 생성하는 비즈니스 로직을 처리합니다. * 한 번에 여러 작업자에 대해 동일한 이슈를 등록할 수 있습니다. * @param {object} issueData - 컨트롤러에서 전달된 이슈 데이터 * @returns {Promise} 생성 결과 */ const createDailyIssueReportService = async (issueData) => { const { date, project_id, start_time, end_time, issue_type_id, worker_ids } = issueData; // 1. 유효성 검사 if (!date || !project_id || !start_time || !end_time || !issue_type_id || !worker_ids) { throw new Error('필수 필드가 누락되었습니다.'); } if (!Array.isArray(worker_ids) || worker_ids.length === 0) { throw new Error('worker_ids는 최소 한 명 이상의 작업자를 포함하는 배열이어야 합니다.'); } // 2. 모델에 전달할 데이터 준비 const reportsToCreate = worker_ids.map(worker_id => ({ date, project_id, start_time, end_time, issue_type_id, worker_id })); // 3. 모델 함수 호출 (모델에 createMany와 같은 함수가 필요) try { const insertedIds = await dailyIssueReportModel.createMany(reportsToCreate); return { message: `${insertedIds.length}개의 이슈 보고서가 성공적으로 생성되었습니다.`, issue_report_ids: insertedIds }; } catch (error) { console.error('[Service] 이슈 보고서 생성 중 오류 발생:', error); throw error; } }; /** * 특정 날짜의 모든 이슈 보고서를 조회합니다. * @param {string} date - 조회할 날짜 (YYYY-MM-DD) * @returns {Promise} 조회된 이슈 보고서 배열 */ const getDailyIssuesByDateService = async (date) => { if (!date) { throw new Error('조회를 위해 날짜(date)는 필수입니다.'); } try { const issues = await dailyIssueReportModel.getAllByDate(date); return issues; } catch (error) { console.error(`[Service] ${date}의 이슈 보고서 조회 중 오류 발생:`, error); throw error; } }; /** * 특정 ID의 이슈 보고서를 삭제합니다. * @param {string} issueId - 삭제할 이슈 보고서의 ID * @returns {Promise} 삭제 결과 */ const removeDailyIssueService = async (issueId) => { if (!issueId) { throw new Error('삭제를 위해 이슈 보고서 ID가 필요합니다.'); } try { const affectedRows = await dailyIssueReportModel.remove(issueId); if (affectedRows === 0) { const notFoundError = new Error('삭제할 이슈 보고서를 찾을 수 없습니다.'); notFoundError.statusCode = 404; throw notFoundError; } return { message: '이슈 보고서가 성공적으로 삭제되었습니다.', deleted_id: issueId, affected_rows: affectedRows }; } catch (error) { console.error(`[Service] 이슈 보고서(id: ${issueId}) 삭제 중 오류 발생:`, error); throw error; } }; module.exports = { createDailyIssueReportService, getDailyIssuesByDateService, removeDailyIssueService, };