const workReportModel = require('../models/workReportModel'); const checkinModel = require('../models/checkinModel'); // 작업보고 목록 async function list(req, res) { try { const { company_id, date_from, date_to, schedule_id, confirmed, page, limit } = req.query; const rows = await workReportModel.findAll({ company_id: company_id ? parseInt(company_id) : undefined, date_from, date_to, schedule_id: schedule_id ? parseInt(schedule_id) : undefined, confirmed, page: page ? parseInt(page) : 1, limit: limit ? parseInt(limit) : 50 }); res.json({ success: true, data: rows }); } catch (err) { console.error('WorkReport list error:', err); res.status(500).json({ success: false, error: err.message }); } } // 작업보고 상세 async function getById(req, res) { try { const row = await workReportModel.findById(req.params.id); if (!row) return res.status(404).json({ success: false, error: '작업보고를 찾을 수 없습니다' }); res.json({ success: true, data: row }); } catch (err) { console.error('WorkReport get error:', err); res.status(500).json({ success: false, error: err.message }); } } // 내 작업보고 (협력업체 포탈) async function myReports(req, res) { try { const companyId = req.user.partner_company_id; if (!companyId) { return res.status(403).json({ success: false, error: '협력업체 계정이 아닙니다' }); } const { date_from, date_to, page, limit } = req.query; const rows = await workReportModel.findAll({ company_id: companyId, date_from, date_to, page: page ? parseInt(page) : 1, limit: limit ? parseInt(limit) : 50 }); res.json({ success: true, data: rows }); } catch (err) { console.error('WorkReport myReports error:', err); res.status(500).json({ success: false, error: err.message }); } } // 작업보고 등록 async function create(req, res) { try { const { checkin_id, schedule_id, company_id, report_date } = req.body; if (!report_date) { return res.status(400).json({ success: false, error: '보고일은 필수입니다' }); } if (!checkin_id) { return res.status(400).json({ success: false, error: '체크인 ID는 필수입니다' }); } const checkin = await checkinModel.findById(checkin_id); if (!checkin) { return res.status(400).json({ success: false, error: '유효하지 않은 체크인 ID입니다' }); } if (schedule_id && checkin.schedule_id !== schedule_id) { return res.status(400).json({ success: false, error: '체크인의 일정 정보가 일치하지 않습니다' }); } const resolvedCompanyId = company_id || req.user.partner_company_id; if (!resolvedCompanyId) { return res.status(400).json({ success: false, error: '업체 정보가 필요합니다' }); } const data = { ...req.body, company_id: resolvedCompanyId, reporter_id: req.user.user_id || req.user.id }; const row = await workReportModel.create(data); res.status(201).json({ success: true, data: row }); } catch (err) { console.error('WorkReport create error:', err); res.status(500).json({ success: false, error: err.message }); } } // 작업보고 수정 async function update(req, res) { try { const row = await workReportModel.update(req.params.id, req.body); if (!row) return res.status(404).json({ success: false, error: '작업보고를 찾을 수 없습니다' }); res.json({ success: true, data: row }); } catch (err) { console.error('WorkReport update error:', err); res.status(500).json({ success: false, error: err.message }); } } // 작업보고 확인 async function confirm(req, res) { try { const confirmedBy = req.user.user_id || req.user.id; const row = await workReportModel.confirm(req.params.id, confirmedBy); if (!row) return res.status(404).json({ success: false, error: '작업보고를 찾을 수 없습니다' }); res.json({ success: true, data: row }); } catch (err) { console.error('WorkReport confirm error:', err); res.status(500).json({ success: false, error: err.message }); } } module.exports = { list, getById, myReports, create, update, confirm };