feat(tkpurchase): 협력업체 작업 신청 기능 추가
협력업체 포탈에서 오늘 일정이 없을 때 직접 작업을 신청할 수 있는 기능. 구매팀이 승인하면 일정이 생성되고, 반려 시 재신청 가능. - DB: status ENUM에 requested/rejected 추가, requested_by 컬럼 추가 - API: POST /schedules/request, PUT /:id/approve, PUT /:id/reject - 포탈: 신청 폼 + 승인 대기/반려 상태 카드 - 관리자: 신청 배지 + 승인 모달 (프로젝트 배정, 작업장 보정) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -40,14 +40,104 @@ async function mySchedules(req, res) {
|
||||
if (!companyId) {
|
||||
return res.status(403).json({ success: false, error: '협력업체 계정이 아닙니다' });
|
||||
}
|
||||
const rows = await scheduleModel.findByCompanyToday(companyId);
|
||||
res.json({ success: true, data: rows });
|
||||
const [schedules, requests] = await Promise.all([
|
||||
scheduleModel.findByCompanyToday(companyId),
|
||||
scheduleModel.findRequestsByCompany(companyId)
|
||||
]);
|
||||
res.json({ success: true, data: { schedules, requests } });
|
||||
} catch (err) {
|
||||
console.error('Schedule mySchedules error:', err);
|
||||
res.status(500).json({ success: false, error: err.message });
|
||||
}
|
||||
}
|
||||
|
||||
// 협력업체 작업 신청
|
||||
async function requestSchedule(req, res) {
|
||||
try {
|
||||
const companyId = req.user.partner_company_id;
|
||||
if (!companyId) {
|
||||
return res.status(403).json({ success: false, error: '협력업체 계정이 아닙니다' });
|
||||
}
|
||||
const { start_date, work_description, workplace_name, expected_workers } = req.body;
|
||||
if (!start_date) {
|
||||
return res.status(400).json({ success: false, error: '작업일은 필수입니다' });
|
||||
}
|
||||
const data = {
|
||||
company_id: companyId,
|
||||
project_id: null,
|
||||
start_date,
|
||||
end_date: start_date,
|
||||
work_description: work_description || null,
|
||||
workplace_name: workplace_name || null,
|
||||
expected_workers: expected_workers || null,
|
||||
requested_by: req.user.user_id || req.user.id,
|
||||
status: 'requested'
|
||||
};
|
||||
const row = await scheduleModel.create(data);
|
||||
res.status(201).json({ success: true, data: row });
|
||||
} catch (err) {
|
||||
console.error('Schedule requestSchedule error:', err);
|
||||
res.status(500).json({ success: false, error: err.message });
|
||||
}
|
||||
}
|
||||
|
||||
// 구매팀 승인
|
||||
async function approveRequest(req, res) {
|
||||
try {
|
||||
const schedule = await scheduleModel.findById(req.params.id);
|
||||
if (!schedule) return res.status(404).json({ success: false, error: '일정을 찾을 수 없습니다' });
|
||||
if (schedule.status !== 'requested') {
|
||||
return res.status(400).json({ success: false, error: '신청 상태가 아닙니다' });
|
||||
}
|
||||
const updateData = {
|
||||
status: 'scheduled',
|
||||
registered_by: req.user.user_id || req.user.id
|
||||
};
|
||||
if (req.body.project_id !== undefined) updateData.project_id = req.body.project_id;
|
||||
if (req.body.workplace_name !== undefined) updateData.workplace_name = req.body.workplace_name;
|
||||
if (req.body.start_date) updateData.start_date = req.body.start_date;
|
||||
if (req.body.end_date) updateData.end_date = req.body.end_date;
|
||||
if (req.body.expected_workers !== undefined) updateData.expected_workers = req.body.expected_workers;
|
||||
if (req.body.work_description !== undefined) updateData.work_description = req.body.work_description;
|
||||
|
||||
// update() handles dynamic fields including status and registered_by via the fields map
|
||||
// But registered_by is not in the update() fields list, so use raw query for it
|
||||
const db = require('../models/partnerModel').getPool();
|
||||
const fields = ['status = ?', 'registered_by = ?'];
|
||||
const values = ['scheduled', updateData.registered_by];
|
||||
if (updateData.project_id !== undefined) { fields.push('project_id = ?'); values.push(updateData.project_id || null); }
|
||||
if (updateData.workplace_name !== undefined) { fields.push('workplace_name = ?'); values.push(updateData.workplace_name || null); }
|
||||
if (updateData.start_date) { fields.push('start_date = ?'); values.push(updateData.start_date); }
|
||||
if (updateData.end_date) { fields.push('end_date = ?'); values.push(updateData.end_date); }
|
||||
if (updateData.expected_workers !== undefined) { fields.push('expected_workers = ?'); values.push(updateData.expected_workers || null); }
|
||||
if (updateData.work_description !== undefined) { fields.push('work_description = ?'); values.push(updateData.work_description || null); }
|
||||
values.push(req.params.id);
|
||||
await db.query(`UPDATE partner_schedules SET ${fields.join(', ')} WHERE id = ?`, values);
|
||||
|
||||
const row = await scheduleModel.findById(req.params.id);
|
||||
res.json({ success: true, data: row });
|
||||
} catch (err) {
|
||||
console.error('Schedule approveRequest error:', err);
|
||||
res.status(500).json({ success: false, error: err.message });
|
||||
}
|
||||
}
|
||||
|
||||
// 구매팀 반려
|
||||
async function rejectRequest(req, res) {
|
||||
try {
|
||||
const schedule = await scheduleModel.findById(req.params.id);
|
||||
if (!schedule) return res.status(404).json({ success: false, error: '일정을 찾을 수 없습니다' });
|
||||
if (schedule.status !== 'requested') {
|
||||
return res.status(400).json({ success: false, error: '신청 상태가 아닙니다' });
|
||||
}
|
||||
const row = await scheduleModel.updateStatus(req.params.id, 'rejected');
|
||||
res.json({ success: true, data: row });
|
||||
} catch (err) {
|
||||
console.error('Schedule rejectRequest error:', err);
|
||||
res.status(500).json({ success: false, error: err.message });
|
||||
}
|
||||
}
|
||||
|
||||
// 일정 등록
|
||||
async function create(req, res) {
|
||||
try {
|
||||
@@ -119,4 +209,4 @@ async function deleteSchedule(req, res) {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { list, getById, mySchedules, create, update, updateStatus, deleteSchedule };
|
||||
module.exports = { list, getById, mySchedules, create, update, updateStatus, deleteSchedule, requestSchedule, approveRequest, rejectRequest };
|
||||
|
||||
Reference in New Issue
Block a user