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>
This commit is contained in:
Hyungi Ahn
2026-03-13 08:18:53 +09:00
parent fa4c899d95
commit b5b0fa1728
12 changed files with 228 additions and 36 deletions

View File

@@ -1,5 +1,6 @@
const checkinModel = require('../models/checkinModel');
const workReportModel = require('../models/workReportModel');
const scheduleModel = require('../models/scheduleModel');
// 일정별 체크인 목록
async function list(req, res) {
@@ -38,6 +39,20 @@ async function checkIn(req, res) {
if (!resolvedCompanyId) {
return res.status(400).json({ success: false, error: '업체 정보가 필요합니다' });
}
// 일정 기간 내 체크인 검증
const schedule = await scheduleModel.findById(schedule_id);
if (!schedule) {
return res.status(404).json({ success: false, error: '일정을 찾을 수 없습니다' });
}
const today = new Date();
today.setHours(0, 0, 0, 0);
const startDate = new Date(schedule.start_date);
startDate.setHours(0, 0, 0, 0);
const endDate = new Date(schedule.end_date);
endDate.setHours(0, 0, 0, 0);
if (today < startDate || today > endDate) {
return res.status(400).json({ success: false, error: '오늘은 해당 일정의 작업 기간이 아닙니다' });
}
const data = {
schedule_id,
company_id: resolvedCompanyId,

View File

@@ -0,0 +1,13 @@
const projectModel = require('../models/projectModel');
async function getActive(req, res) {
try {
const rows = await projectModel.findActive();
res.json({ success: true, data: rows });
} catch (err) {
console.error('Project getActive error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
module.exports = { getActive };

View File

@@ -3,12 +3,13 @@ const scheduleModel = require('../models/scheduleModel');
// 일정 목록
async function list(req, res) {
try {
const { company_id, date_from, date_to, status, page, limit } = req.query;
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
});
@@ -49,12 +50,18 @@ async function mySchedules(req, res) {
// 일정 등록
async function create(req, res) {
try {
const { company_id, work_date } = req.body;
const { company_id, start_date, end_date } = req.body;
if (!company_id) {
return res.status(400).json({ success: false, error: '업체를 선택해주세요' });
}
if (!work_date) {
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,
@@ -71,6 +78,10 @@ async function create(req, res) {
// 일정 수정
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 });

View File

@@ -7,6 +7,7 @@ const scheduleRoutes = require('./routes/scheduleRoutes');
const checkinRoutes = require('./routes/checkinRoutes');
const workReportRoutes = require('./routes/workReportRoutes');
const partnerAccountRoutes = require('./routes/partnerAccountRoutes');
const projectRoutes = require('./routes/projectRoutes');
const app = express();
const PORT = process.env.PORT || 3000;
@@ -43,6 +44,7 @@ app.use('/api/schedules', scheduleRoutes);
app.use('/api/checkins', checkinRoutes);
app.use('/api/work-reports', workReportRoutes);
app.use('/api/partner-accounts', partnerAccountRoutes);
app.use('/api/projects', projectRoutes);
// 404
app.use((req, res) => {

View File

@@ -0,0 +1,13 @@
const { getPool } = require('./partnerModel');
async function findActive() {
const db = getPool();
const [rows] = await db.query(
`SELECT project_id, job_no, project_name, site
FROM projects
WHERE is_active = 1 AND project_status = 'active'
ORDER BY project_name`);
return rows;
}
module.exports = { findActive };

View File

@@ -1,18 +1,21 @@
const { getPool } = require('./partnerModel');
async function findAll({ company_id, date_from, date_to, status, page = 1, limit = 50 } = {}) {
async function findAll({ company_id, date_from, date_to, status, project_id, page = 1, limit = 50 } = {}) {
const db = getPool();
let sql = `SELECT ps.*, pc.company_name, su.name AS registered_by_name
let sql = `SELECT ps.*, pc.company_name, su.name AS registered_by_name,
p.project_name, p.job_no
FROM partner_schedules ps
LEFT JOIN partner_companies pc ON ps.company_id = pc.id
LEFT JOIN sso_users su ON ps.registered_by = su.user_id
LEFT JOIN projects p ON ps.project_id = p.project_id
WHERE 1=1`;
const params = [];
if (company_id) { sql += ' AND ps.company_id = ?'; params.push(company_id); }
if (date_from) { sql += ' AND ps.work_date >= ?'; params.push(date_from); }
if (date_to) { sql += ' AND ps.work_date <= ?'; params.push(date_to); }
if (date_from) { sql += ' AND ps.end_date >= ?'; params.push(date_from); }
if (date_to) { sql += ' AND ps.start_date <= ?'; params.push(date_to); }
if (status) { sql += ' AND ps.status = ?'; params.push(status); }
sql += ' ORDER BY ps.work_date DESC, ps.created_at DESC';
if (project_id) { sql += ' AND ps.project_id = ?'; params.push(project_id); }
sql += ' ORDER BY ps.start_date DESC, ps.created_at DESC';
const offset = (page - 1) * limit;
sql += ' LIMIT ? OFFSET ?';
params.push(limit, offset);
@@ -23,31 +26,47 @@ async function findAll({ company_id, date_from, date_to, status, page = 1, limit
async function findById(id) {
const db = getPool();
const [rows] = await db.query(
`SELECT ps.*, pc.company_name, su.name AS registered_by_name
`SELECT ps.*, pc.company_name, su.name AS registered_by_name,
p.project_name, p.job_no
FROM partner_schedules ps
LEFT JOIN partner_companies pc ON ps.company_id = pc.id
LEFT JOIN sso_users su ON ps.registered_by = su.user_id
LEFT JOIN projects p ON ps.project_id = p.project_id
WHERE ps.id = ?`, [id]);
return rows[0] || null;
}
async function findByCompanyToday(companyId) {
const db = getPool();
const [rows] = await db.query(
`SELECT ps.*, pc.company_name, p.project_name, p.job_no
FROM partner_schedules ps
LEFT JOIN partner_companies pc ON ps.company_id = pc.id
LEFT JOIN projects p ON ps.project_id = p.project_id
WHERE ps.company_id = ? AND ps.start_date <= CURDATE() AND ps.end_date >= CURDATE()
ORDER BY ps.created_at DESC`, [companyId]);
return rows;
}
async function findByProject(projectId) {
const db = getPool();
const [rows] = await db.query(
`SELECT ps.*, pc.company_name
FROM partner_schedules ps
LEFT JOIN partner_companies pc ON ps.company_id = pc.id
WHERE ps.company_id = ? AND ps.work_date = CURDATE()
ORDER BY ps.created_at DESC`, [companyId]);
WHERE ps.project_id = ?
ORDER BY ps.start_date, pc.company_name`, [projectId]);
return rows;
}
async function create(data) {
const db = getPool();
const [result] = await db.query(
`INSERT INTO partner_schedules (company_id, work_date, work_description, workplace_name, expected_workers, registered_by, notes)
VALUES (?, ?, ?, ?, ?, ?, ?)`,
[data.company_id, data.work_date, data.work_description || null,
`INSERT INTO partner_schedules (company_id, project_id, start_date, end_date, work_description, workplace_name, expected_workers, registered_by, notes)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[data.company_id, data.project_id || null,
data.start_date, data.end_date || data.start_date,
data.work_description || null,
data.workplace_name || null, data.expected_workers || null,
data.registered_by, data.notes || null]);
return findById(result.insertId);
@@ -58,7 +77,9 @@ async function update(id, data) {
const fields = [];
const values = [];
if (data.company_id !== undefined) { fields.push('company_id = ?'); values.push(data.company_id); }
if (data.work_date !== undefined) { fields.push('work_date = ?'); values.push(data.work_date); }
if (data.project_id !== undefined) { fields.push('project_id = ?'); values.push(data.project_id || null); }
if (data.start_date !== undefined) { fields.push('start_date = ?'); values.push(data.start_date); }
if (data.end_date !== undefined) { fields.push('end_date = ?'); values.push(data.end_date); }
if (data.work_description !== undefined) { fields.push('work_description = ?'); values.push(data.work_description || null); }
if (data.workplace_name !== undefined) { fields.push('workplace_name = ?'); values.push(data.workplace_name || null); }
if (data.expected_workers !== undefined) { fields.push('expected_workers = ?'); values.push(data.expected_workers || null); }
@@ -81,4 +102,4 @@ async function deleteSchedule(id) {
await db.query('DELETE FROM partner_schedules WHERE id = ?', [id]);
}
module.exports = { findAll, findById, findByCompanyToday, create, update, updateStatus, deleteSchedule };
module.exports = { findAll, findById, findByCompanyToday, findByProject, create, update, updateStatus, deleteSchedule };

View File

@@ -0,0 +1,10 @@
const express = require('express');
const router = express.Router();
const { requireAuth } = require('../middleware/auth');
const ctrl = require('../controllers/projectController');
router.use(requireAuth);
router.get('/active', ctrl.getActive);
module.exports = router;