Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | 1x 1x 1x 1x 5x 5x 2x 3x 3x 3x 3x 4x 4x 4x 3x 3x 2x 2x 1x 1x 1x 4x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 3x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 1x 3x 3x 3x 3x 3x 3x 3x 2x 2x 1x 1x 2x 2x 1x 3x 1x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 3x 1x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 5x 2x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 1x 1x 2x 2x 1x | /**
* 작업 보고서 관리 서비스
*
* 작업 보고서 CRUD 및 조회 관련 비즈니스 로직 처리
*
* @author TK-FB-Project
* @since 2025-12-11
*/
const workReportModel = require('../models/workReportModel');
const { ValidationError, NotFoundError, DatabaseError } = require('../utils/errors');
const logger = require('../utils/logger');
/**
* 작업 보고서 생성 (단일 또는 다중)
*/
const createWorkReportService = async (reportData) => {
const reports = Array.isArray(reportData) ? reportData : [reportData];
if (reports.length === 0) {
throw new ValidationError('보고서 데이터가 필요합니다');
}
logger.info('작업 보고서 생성 요청', { count: reports.length });
const workReport_ids = [];
try {
for (const report of reports) {
const id = await new Promise((resolve, reject) => {
workReportModel.create(report, (err, insertId) => {
if (err) reject(err);
else resolve(insertId);
});
});
workReport_ids.push(id);
}
logger.info('작업 보고서 생성 성공', {
count: workReport_ids.length,
ids: workReport_ids
});
return { workReport_ids };
} catch (error) {
logger.error('작업 보고서 생성 실패', {
count: reports.length,
error: error.message
});
throw new DatabaseError('작업 보고서 생성 중 오류가 발생했습니다');
}
};
/**
* 날짜별 작업 보고서 조회
*/
const getWorkReportsByDateService = async (date) => {
if (!date) {
throw new ValidationError('날짜가 필요합니다', {
required: ['date'],
received: { date }
});
}
logger.info('작업 보고서 날짜별 조회 요청', { date });
try {
const rows = await new Promise((resolve, reject) => {
workReportModel.getAllByDate(date, (err, data) => {
if (err) reject(err);
else resolve(data);
});
});
logger.info('작업 보고서 조회 성공', { date, count: rows.length });
return rows;
} catch (error) {
logger.error('작업 보고서 조회 실패', { date, error: error.message });
throw new DatabaseError('작업 보고서 조회 중 오류가 발생했습니다');
}
};
/**
* 기간별 작업 보고서 조회
*/
const getWorkReportsInRangeService = async (start, end) => {
if (!start || !end) {
throw new ValidationError('시작일과 종료일이 필요합니다', {
required: ['start', 'end'],
received: { start, end }
});
}
logger.info('작업 보고서 기간별 조회 요청', { start, end });
try {
const rows = await new Promise((resolve, reject) => {
workReportModel.getByRange(start, end, (err, data) => {
Iif (err) reject(err);
else resolve(data);
});
});
logger.info('작업 보고서 조회 성공', { start, end, count: rows.length });
return rows;
} catch (error) {
logger.error('작업 보고서 조회 실패', { start, end, error: error.message });
throw new DatabaseError('작업 보고서 조회 중 오류가 발생했습니다');
}
};
/**
* 단일 작업 보고서 조회
*/
const getWorkReportByIdService = async (id) => {
if (!id) {
throw new ValidationError('보고서 ID가 필요합니다');
}
logger.info('작업 보고서 조회 요청', { report_id: id });
try {
const row = await new Promise((resolve, reject) => {
workReportModel.getById(id, (err, data) => {
Iif (err) reject(err);
else resolve(data);
});
});
if (!row) {
logger.warn('작업 보고서를 찾을 수 없음', { report_id: id });
throw new NotFoundError('작업 보고서를 찾을 수 없습니다');
}
logger.info('작업 보고서 조회 성공', { report_id: id });
return row;
} catch (error) {
Eif (error instanceof NotFoundError) {
throw error;
}
logger.error('작업 보고서 조회 실패', { report_id: id, error: error.message });
throw new DatabaseError('작업 보고서 조회 중 오류가 발생했습니다');
}
};
/**
* 작업 보고서 수정
*/
const updateWorkReportService = async (id, updateData) => {
if (!id) {
throw new ValidationError('보고서 ID가 필요합니다');
}
logger.info('작업 보고서 수정 요청', { report_id: id });
try {
const changes = await new Promise((resolve, reject) => {
workReportModel.update(id, updateData, (err, affectedRows) => {
Iif (err) reject(err);
else resolve(affectedRows);
});
});
if (changes === 0) {
logger.warn('작업 보고서를 찾을 수 없거나 변경사항 없음', { report_id: id });
throw new NotFoundError('작업 보고서를 찾을 수 없습니다');
}
logger.info('작업 보고서 수정 성공', { report_id: id, changes });
return { changes };
} catch (error) {
Eif (error instanceof NotFoundError) {
throw error;
}
logger.error('작업 보고서 수정 실패', { report_id: id, error: error.message });
throw new DatabaseError('작업 보고서 수정 중 오류가 발생했습니다');
}
};
/**
* 작업 보고서 삭제
*/
const removeWorkReportService = async (id) => {
if (!id) {
throw new ValidationError('보고서 ID가 필요합니다');
}
logger.info('작업 보고서 삭제 요청', { report_id: id });
try {
const changes = await new Promise((resolve, reject) => {
workReportModel.remove(id, (err, affectedRows) => {
Iif (err) reject(err);
else resolve(affectedRows);
});
});
if (changes === 0) {
logger.warn('작업 보고서를 찾을 수 없음', { report_id: id });
throw new NotFoundError('작업 보고서를 찾을 수 없습니다');
}
logger.info('작업 보고서 삭제 성공', { report_id: id, changes });
return { changes };
} catch (error) {
Eif (error instanceof NotFoundError) {
throw error;
}
logger.error('작업 보고서 삭제 실패', { report_id: id, error: error.message });
throw new DatabaseError('작업 보고서 삭제 중 오류가 발생했습니다');
}
};
/**
* 월간 요약 조회
*/
const getSummaryService = async (year, month) => {
if (!year || !month) {
throw new ValidationError('연도와 월이 필요합니다', {
required: ['year', 'month'],
received: { year, month }
});
}
const start = `${year.padStart(4, '0')}-${month.padStart(2, '0')}-01`;
const end = `${year.padStart(4, '0')}-${month.padStart(2, '0')}-31`;
logger.info('작업 보고서 월간 요약 조회 요청', { year, month, start, end });
try {
const rows = await new Promise((resolve, reject) => {
workReportModel.getByRange(start, end, (err, data) => {
Iif (err) reject(err);
else resolve(data);
});
});
if (!rows || rows.length === 0) {
logger.warn('월간 요약 데이터 없음', { year, month });
throw new NotFoundError('해당 기간의 작업 보고서가 없습니다');
}
logger.info('작업 보고서 월간 요약 조회 성공', {
year,
month,
count: rows.length
});
return rows;
} catch (error) {
Eif (error instanceof NotFoundError) {
throw error;
}
logger.error('작업 보고서 월간 요약 조회 실패', {
year,
month,
error: error.message
});
throw new DatabaseError('월간 요약 조회 중 오류가 발생했습니다');
}
};
module.exports = {
createWorkReportService,
getWorkReportsByDateService,
getWorkReportsInRangeService,
getWorkReportByIdService,
updateWorkReportService,
removeWorkReportService,
getSummaryService
};
|