Files
tk-factory-services/tkpurchase/api/controllers/scheduleController.js
Hyungi Ahn b5b0fa1728 feat: 작업일정 기간 기반 + 프로젝트 연결
- partner_schedules: work_date → start_date/end_date 기간 기반으로 변경
- project_id 컬럼 추가 (projects 테이블 연결, 선택사항)
- 프로젝트 조회 API 추가 (GET /projects/active)
- 일정 조회 시 기간 겹침 조건으로 필터링
- 체크인 시 기간 내 검증 추가
- 프론트엔드: 시작일/종료일 입력 + 프로젝트 선택 드롭다운
- 마이그레이션 SQL 포함 (scripts/migration-schedule-daterange.sql)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 08:18:53 +09:00

122 lines
4.2 KiB
JavaScript

const scheduleModel = require('../models/scheduleModel');
// 일정 목록
async function list(req, res) {
try {
const { company_id, date_from, date_to, status, project_id, page, limit } = req.query;
const rows = await scheduleModel.findAll({
company_id: company_id ? parseInt(company_id) : undefined,
date_from,
date_to,
status,
project_id: project_id ? parseInt(project_id) : undefined,
page: page ? parseInt(page) : 1,
limit: limit ? parseInt(limit) : 50
});
res.json({ success: true, data: rows });
} catch (err) {
console.error('Schedule list error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
// 일정 상세
async function getById(req, res) {
try {
const row = await scheduleModel.findById(req.params.id);
if (!row) return res.status(404).json({ success: false, error: '일정을 찾을 수 없습니다' });
res.json({ success: true, data: row });
} catch (err) {
console.error('Schedule get error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
// 내 일정 (협력업체 포탈)
async function mySchedules(req, res) {
try {
const companyId = req.user.partner_company_id;
if (!companyId) {
return res.status(403).json({ success: false, error: '협력업체 계정이 아닙니다' });
}
const rows = await scheduleModel.findByCompanyToday(companyId);
res.json({ success: true, data: rows });
} catch (err) {
console.error('Schedule mySchedules error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
// 일정 등록
async function create(req, res) {
try {
const { company_id, start_date, end_date } = req.body;
if (!company_id) {
return res.status(400).json({ success: false, error: '업체를 선택해주세요' });
}
if (!start_date) {
return res.status(400).json({ success: false, error: '시작일은 필수입니다' });
}
if (!end_date) {
return res.status(400).json({ success: false, error: '종료일은 필수입니다' });
}
if (end_date < start_date) {
return res.status(400).json({ success: false, error: '종료일은 시작일 이후여야 합니다' });
}
const data = {
...req.body,
registered_by: req.user.user_id || req.user.id
};
const row = await scheduleModel.create(data);
res.status(201).json({ success: true, data: row });
} catch (err) {
console.error('Schedule create error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
// 일정 수정
async function update(req, res) {
try {
const { start_date, end_date } = req.body;
if (start_date && end_date && end_date < start_date) {
return res.status(400).json({ success: false, error: '종료일은 시작일 이후여야 합니다' });
}
const row = await scheduleModel.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('Schedule update error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
// 일정 상태 변경
async function updateStatus(req, res) {
try {
const { status } = req.body;
if (!status) {
return res.status(400).json({ success: false, error: '상태값은 필수입니다' });
}
const row = await scheduleModel.updateStatus(req.params.id, status);
if (!row) return res.status(404).json({ success: false, error: '일정을 찾을 수 없습니다' });
res.json({ success: true, data: row });
} catch (err) {
console.error('Schedule updateStatus error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
// 일정 삭제
async function deleteSchedule(req, res) {
try {
await scheduleModel.deleteSchedule(req.params.id);
res.json({ success: true, message: '삭제 완료' });
} catch (err) {
console.error('Schedule delete error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
module.exports = { list, getById, mySchedules, create, update, updateStatus, deleteSchedule };