/** * 작업 이력 관리 컨트롤러 */ import { Request, Response } from 'express'; import * as workHistoryService from '../services/workHistoryService'; import { CreateWorkHistoryDto, UpdateWorkHistoryDto, WorkHistoryFilters } from '../types/workHistory'; /** * 작업 이력 목록 조회 */ export async function getWorkHistories(req: Request, res: Response): Promise { try { const filters: WorkHistoryFilters = { work_type: req.query.work_type as any, status: req.query.status as any, vehicle_number: req.query.vehicle_number as string, driver_name: req.query.driver_name as string, start_date: req.query.start_date ? new Date(req.query.start_date as string) : undefined, end_date: req.query.end_date ? new Date(req.query.end_date as string) : undefined, search: req.query.search as string, }; const histories = await workHistoryService.getWorkHistories(filters); res.json({ success: true, data: histories, }); } catch (error) { console.error('작업 이력 목록 조회 실패:', error); res.status(500).json({ success: false, message: '작업 이력 목록 조회에 실패했습니다', error: error instanceof Error ? error.message : String(error), }); } } /** * 작업 이력 단건 조회 */ export async function getWorkHistoryById(req: Request, res: Response): Promise { try { const id = parseInt(req.params.id); const history = await workHistoryService.getWorkHistoryById(id); if (!history) { res.status(404).json({ success: false, message: '작업 이력을 찾을 수 없습니다', }); return; } res.json({ success: true, data: history, }); } catch (error) { console.error('작업 이력 조회 실패:', error); res.status(500).json({ success: false, message: '작업 이력 조회에 실패했습니다', error: error instanceof Error ? error.message : String(error), }); } } /** * 작업 이력 생성 */ export async function createWorkHistory(req: Request, res: Response): Promise { try { const data: CreateWorkHistoryDto = req.body; const history = await workHistoryService.createWorkHistory(data); res.status(201).json({ success: true, data: history, message: '작업 이력이 생성되었습니다', }); } catch (error) { console.error('작업 이력 생성 실패:', error); res.status(500).json({ success: false, message: '작업 이력 생성에 실패했습니다', error: error instanceof Error ? error.message : String(error), }); } } /** * 작업 이력 수정 */ export async function updateWorkHistory(req: Request, res: Response): Promise { try { const id = parseInt(req.params.id); const data: UpdateWorkHistoryDto = req.body; const history = await workHistoryService.updateWorkHistory(id, data); res.json({ success: true, data: history, message: '작업 이력이 수정되었습니다', }); } catch (error) { console.error('작업 이력 수정 실패:', error); res.status(500).json({ success: false, message: '작업 이력 수정에 실패했습니다', error: error instanceof Error ? error.message : String(error), }); } } /** * 작업 이력 삭제 */ export async function deleteWorkHistory(req: Request, res: Response): Promise { try { const id = parseInt(req.params.id); await workHistoryService.deleteWorkHistory(id); res.json({ success: true, message: '작업 이력이 삭제되었습니다', }); } catch (error) { console.error('작업 이력 삭제 실패:', error); res.status(500).json({ success: false, message: '작업 이력 삭제에 실패했습니다', error: error instanceof Error ? error.message : String(error), }); } } /** * 작업 이력 통계 조회 */ export async function getWorkHistoryStats(req: Request, res: Response): Promise { try { const stats = await workHistoryService.getWorkHistoryStats(); res.json({ success: true, data: stats, }); } catch (error) { console.error('작업 이력 통계 조회 실패:', error); res.status(500).json({ success: false, message: '작업 이력 통계 조회에 실패했습니다', error: error instanceof Error ? error.message : String(error), }); } } /** * 월별 추이 조회 */ export async function getMonthlyTrend(req: Request, res: Response): Promise { try { const months = parseInt(req.query.months as string) || 6; const trend = await workHistoryService.getMonthlyTrend(months); res.json({ success: true, data: trend, }); } catch (error) { console.error('월별 추이 조회 실패:', error); res.status(500).json({ success: false, message: '월별 추이 조회에 실패했습니다', error: error instanceof Error ? error.message : String(error), }); } } /** * 주요 운송 경로 조회 */ export async function getTopRoutes(req: Request, res: Response): Promise { try { const limit = parseInt(req.query.limit as string) || 5; const routes = await workHistoryService.getTopRoutes(limit); res.json({ success: true, data: routes, }); } catch (error) { console.error('주요 운송 경로 조회 실패:', error); res.status(500).json({ success: false, message: '주요 운송 경로 조회에 실패했습니다', error: error instanceof Error ? error.message : String(error), }); } }