/** * 일일 이슈 보고서 관리 서비스 * * 일일 이슈 보고서 생성, 조회, 삭제 관련 비즈니스 로직 처리 * * @author TK-FB-Project * @since 2025-12-11 */ const dailyIssueReportModel = require('../models/dailyIssueReportModel'); const { ValidationError, NotFoundError, DatabaseError } = require('../utils/errors'); const logger = require('../utils/logger'); /** * 일일 이슈 보고서 생성 * * 한 번에 여러 작업자에 대해 동일한 이슈를 등록할 수 있습니다. * * @param {object} issueData - 컨트롤러에서 전달된 이슈 데이터 * @param {string} issueData.date - 이슈 발생 날짜 (YYYY-MM-DD) * @param {number} issueData.project_id - 프로젝트 ID * @param {string} issueData.start_time - 이슈 시작 시간 * @param {string} issueData.end_time - 이슈 종료 시간 * @param {number} issueData.issue_type_id - 이슈 유형 ID * @param {number[]} issueData.user_ids - 작업자 ID 배열 * @returns {Promise} 생성 결과 */ const createDailyIssueReportService = async (issueData) => { const { date, project_id, start_time, end_time, issue_type_id, user_ids } = issueData; // 필수 필드 검증 if (!date || !project_id || !start_time || !end_time || !issue_type_id || !user_ids) { throw new ValidationError('필수 필드가 누락되었습니다', { required: ['date', 'project_id', 'start_time', 'end_time', 'issue_type_id', 'user_ids'], received: { date, project_id, start_time, end_time, issue_type_id, user_ids: !!user_ids } }); } if (!Array.isArray(user_ids) || user_ids.length === 0) { throw new ValidationError('user_ids는 최소 한 명 이상의 작업자를 포함하는 배열이어야 합니다', { received: { user_ids, isArray: Array.isArray(user_ids), length: user_ids?.length } }); } logger.info('이슈 보고서 생성 요청', { date, project_id, issue_type_id, worker_count: user_ids.length }); // 모델에 전달할 데이터 준비 const reportsToCreate = user_ids.map(user_id => ({ date, project_id, start_time, end_time, issue_type_id, user_id })); try { const insertedIds = await dailyIssueReportModel.createMany(reportsToCreate); logger.info('이슈 보고서 생성 성공', { count: insertedIds.length, issue_report_ids: insertedIds }); return { message: `${insertedIds.length}개의 이슈 보고서가 성공적으로 생성되었습니다`, issue_report_ids: insertedIds }; } catch (error) { logger.error('이슈 보고서 생성 실패', { date, project_id, user_ids, error: error.message }); throw new DatabaseError('이슈 보고서 생성 중 오류가 발생했습니다'); } }; /** * 특정 날짜의 모든 이슈 보고서 조회 * * @param {string} date - 조회할 날짜 (YYYY-MM-DD) * @returns {Promise} 조회된 이슈 보고서 배열 */ const getDailyIssuesByDateService = async (date) => { if (!date) { throw new ValidationError('날짜가 필요합니다', { required: ['date'], received: { date } }); } logger.info('이슈 보고서 날짜별 조회 요청', { date }); try { const issues = await dailyIssueReportModel.getAllByDate(date); logger.info('이슈 보고서 조회 성공', { date, count: issues.length }); return issues; } catch (error) { logger.error('이슈 보고서 조회 실패', { date, error: error.message }); throw new DatabaseError('이슈 보고서 조회 중 오류가 발생했습니다'); } }; /** * 특정 ID의 이슈 보고서 삭제 * * @param {string|number} issueId - 삭제할 이슈 보고서의 ID * @returns {Promise} 삭제 결과 */ const removeDailyIssueService = async (issueId) => { if (!issueId) { throw new ValidationError('이슈 보고서 ID가 필요합니다', { required: ['issue_id'], received: { issueId } }); } logger.info('이슈 보고서 삭제 요청', { issue_id: issueId }); try { const affectedRows = await dailyIssueReportModel.remove(issueId); if (affectedRows === 0) { logger.warn('삭제할 이슈 보고서를 찾을 수 없음', { issue_id: issueId }); throw new NotFoundError('삭제할 이슈 보고서를 찾을 수 없습니다'); } logger.info('이슈 보고서 삭제 성공', { issue_id: issueId, affected_rows: affectedRows }); return { message: '이슈 보고서가 성공적으로 삭제되었습니다', deleted_id: issueId, affected_rows: affectedRows }; } catch (error) { if (error instanceof NotFoundError) { throw error; } logger.error('이슈 보고서 삭제 실패', { issue_id: issueId, error: error.message }); throw new DatabaseError('이슈 보고서 삭제 중 오류가 발생했습니다'); } }; module.exports = { createDailyIssueReportService, getDailyIssuesByDateService, removeDailyIssueService };