주요 변경사항: - services/attendanceService.js 신규 생성 (269 lines) * 9개 서비스 함수로 비즈니스 로직 분리 * 커스텀 에러 클래스 적용 (ValidationError, DatabaseError) * 구조화된 로깅 시스템 통합 - controllers/attendanceController.js 완전 재작성 (306 → 168 lines, 45% 감소) * 클래스 기반에서 함수 기반 export로 변경 * 모든 비즈니스 로직을 서비스 레이어로 이동 * asyncHandler 미들웨어로 에러 처리 자동화 - controllers/workerController.js 개선 * 커스텀 에러 클래스 적용 * console.log → logger 교체 * 캐시 무효화 로직 유지 - controllers/projectController.js 완전 재작성 (117 → 163 lines) * 모든 함수에 새로운 에러 클래스 적용 * 구조화된 로깅 추가 * 표준화된 JSON 응답 형식 기술 스택: - Custom Error Classes: ValidationError, NotFoundError, DatabaseError - Structured Logging: logger.info/error/warn/debug with context - asyncHandler: Automatic async error handling - Service Layer Pattern: Business logic separation 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
163 lines
4.2 KiB
JavaScript
163 lines
4.2 KiB
JavaScript
/**
|
|
* 프로젝트 관리 컨트롤러
|
|
*
|
|
* 프로젝트 CRUD API 엔드포인트 핸들러
|
|
*
|
|
* @author TK-FB-Project
|
|
* @since 2025-12-11
|
|
*/
|
|
|
|
const projectModel = require('../models/projectModel');
|
|
const { ValidationError, NotFoundError, DatabaseError } = require('../utils/errors');
|
|
const { asyncHandler } = require('../middlewares/errorHandler');
|
|
const logger = require('../utils/logger');
|
|
|
|
/**
|
|
* 프로젝트 생성
|
|
*/
|
|
exports.createProject = asyncHandler(async (req, res) => {
|
|
const projectData = req.body;
|
|
|
|
logger.info('프로젝트 생성 요청', { name: projectData.name });
|
|
|
|
const id = await new Promise((resolve, reject) => {
|
|
projectModel.create(projectData, (err, lastID) => {
|
|
if (err) reject(new DatabaseError('프로젝트 생성 중 오류가 발생했습니다'));
|
|
else resolve(lastID);
|
|
});
|
|
});
|
|
|
|
logger.info('프로젝트 생성 성공', { project_id: id });
|
|
|
|
res.status(201).json({
|
|
success: true,
|
|
data: { project_id: id },
|
|
message: '프로젝트가 성공적으로 생성되었습니다'
|
|
});
|
|
});
|
|
|
|
/**
|
|
* 전체 프로젝트 조회
|
|
*/
|
|
exports.getAllProjects = asyncHandler(async (req, res) => {
|
|
const rows = await new Promise((resolve, reject) => {
|
|
projectModel.getAll((err, data) => {
|
|
if (err) reject(new DatabaseError('프로젝트 목록 조회 중 오류가 발생했습니다'));
|
|
else resolve(data);
|
|
});
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
data: rows,
|
|
message: '프로젝트 목록 조회 성공'
|
|
});
|
|
});
|
|
|
|
/**
|
|
* 활성 프로젝트만 조회 (작업보고서용)
|
|
*/
|
|
exports.getActiveProjects = asyncHandler(async (req, res) => {
|
|
const rows = await new Promise((resolve, reject) => {
|
|
projectModel.getActiveProjects((err, data) => {
|
|
if (err) reject(new DatabaseError('활성 프로젝트 목록 조회 중 오류가 발생했습니다'));
|
|
else resolve(data);
|
|
});
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
data: rows,
|
|
message: '활성 프로젝트 목록 조회 성공'
|
|
});
|
|
});
|
|
|
|
/**
|
|
* 단일 프로젝트 조회
|
|
*/
|
|
exports.getProjectById = asyncHandler(async (req, res) => {
|
|
const id = parseInt(req.params.project_id, 10);
|
|
|
|
if (isNaN(id)) {
|
|
throw new ValidationError('유효하지 않은 프로젝트 ID입니다');
|
|
}
|
|
|
|
const row = await new Promise((resolve, reject) => {
|
|
projectModel.getById(id, (err, data) => {
|
|
if (err) reject(new DatabaseError('프로젝트 조회 중 오류가 발생했습니다'));
|
|
else resolve(data);
|
|
});
|
|
});
|
|
|
|
if (!row) {
|
|
throw new NotFoundError('프로젝트를 찾을 수 없습니다');
|
|
}
|
|
|
|
res.json({
|
|
success: true,
|
|
data: row,
|
|
message: '프로젝트 조회 성공'
|
|
});
|
|
});
|
|
|
|
/**
|
|
* 프로젝트 수정
|
|
*/
|
|
exports.updateProject = asyncHandler(async (req, res) => {
|
|
const id = parseInt(req.params.project_id, 10);
|
|
|
|
if (isNaN(id)) {
|
|
throw new ValidationError('유효하지 않은 프로젝트 ID입니다');
|
|
}
|
|
|
|
const data = { ...req.body, project_id: id };
|
|
|
|
const changes = await new Promise((resolve, reject) => {
|
|
projectModel.update(data, (err, ch) => {
|
|
if (err) reject(new DatabaseError('프로젝트 수정 중 오류가 발생했습니다'));
|
|
else resolve(ch);
|
|
});
|
|
});
|
|
|
|
if (changes === 0) {
|
|
throw new NotFoundError('프로젝트를 찾을 수 없습니다');
|
|
}
|
|
|
|
logger.info('프로젝트 수정 성공', { project_id: id });
|
|
|
|
res.json({
|
|
success: true,
|
|
data: { changes },
|
|
message: '프로젝트 정보가 성공적으로 수정되었습니다'
|
|
});
|
|
});
|
|
|
|
/**
|
|
* 프로젝트 삭제
|
|
*/
|
|
exports.removeProject = asyncHandler(async (req, res) => {
|
|
const id = parseInt(req.params.project_id, 10);
|
|
|
|
if (isNaN(id)) {
|
|
throw new ValidationError('유효하지 않은 프로젝트 ID입니다');
|
|
}
|
|
|
|
const changes = await new Promise((resolve, reject) => {
|
|
projectModel.remove(id, (err, ch) => {
|
|
if (err) reject(new DatabaseError('프로젝트 삭제 중 오류가 발생했습니다'));
|
|
else resolve(ch);
|
|
});
|
|
});
|
|
|
|
if (changes === 0) {
|
|
throw new NotFoundError('프로젝트를 찾을 수 없습니다');
|
|
}
|
|
|
|
logger.info('프로젝트 삭제 성공', { project_id: id });
|
|
|
|
res.json({
|
|
success: true,
|
|
message: '프로젝트가 성공적으로 삭제되었습니다'
|
|
});
|
|
});
|