Files
tk-factory-services/tkpurchase/api/controllers/dayLaborController.js
Hyungi Ahn 7fd646e9ba feat: 실시간 알림 시스템 (Web Push + 알림 벨 + 서비스간 알림 연동)
- Phase 1: 모든 서비스 헤더에 알림 벨 UI 추가 (notification-bell.js)
- Phase 2: VAPID Web Push 구독/전송 (push-sw.js, pushSubscription API)
- Phase 3: 내부 알림 API + notifyHelper로 서비스간 알림 연동
  - tksafety: 출입 승인/반려, 안전교육 완료, 방문자 체크인
  - tkpurchase: 일용공 신청, 작업보고서 제출
  - system2-report: 신고 접수/확인/처리완료
- Phase 4: 30일 이상 알림 자동 정리 cron, Redis 캐싱
- CORS에 tkuser/tkpurchase/tksafety 서브도메인 추가
- HTML cache busting 버전 갱신

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 15:01:44 +09:00

140 lines
4.6 KiB
JavaScript

const dayLaborModel = require('../models/dayLaborModel');
const { getPool } = require('../models/partnerModel');
const notify = require('../utils/notifyHelper');
// 일용직 요청 목록
async function list(req, res) {
try {
const { status, date_from, date_to, department_id, page, limit } = req.query;
const rows = await dayLaborModel.findAll({
status,
date_from,
date_to,
department_id: department_id ? parseInt(department_id) : undefined,
page: page ? parseInt(page) : 1,
limit: limit ? parseInt(limit) : 50
});
res.json({ success: true, data: rows });
} catch (err) {
console.error('DayLabor list error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
// 일용직 요청 상세
async function getById(req, res) {
try {
const row = await dayLaborModel.findById(req.params.id);
if (!row) return res.status(404).json({ success: false, error: '요청을 찾을 수 없습니다' });
res.json({ success: true, data: row });
} catch (err) {
console.error('DayLabor get error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
// 일용직 요청 등록
async function create(req, res) {
try {
const { work_date, worker_count } = req.body;
if (!work_date) {
return res.status(400).json({ success: false, error: '작업일은 필수입니다' });
}
if (!worker_count || worker_count < 1) {
return res.status(400).json({ success: false, error: '작업인원은 1명 이상이어야 합니다' });
}
const data = {
...req.body,
requester_id: req.user.user_id || req.user.id
};
const row = await dayLaborModel.create(data);
// 알림: 일용공 신청
notify.send({
type: 'maintenance',
title: '일용공 신청',
message: `${work_date} ${worker_count}명 일용공 신청`,
link_url: '/daylabor.html',
reference_type: 'day_labor_requests',
reference_id: row.id || row.request_id,
created_by: data.requester_id
});
res.status(201).json({ success: true, data: row });
} catch (err) {
console.error('DayLabor create error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
// 일용직 요청 승인
async function approve(req, res) {
try {
const id = req.params.id;
const approvedBy = req.user.user_id || req.user.id;
const row = await dayLaborModel.approve(id, approvedBy);
if (!row) return res.status(404).json({ success: false, error: '요청을 찾을 수 없습니다' });
// 승인 시 안전교육 보고서 자동 생성
if (row.status === 'approved') {
try {
const db = getPool();
await db.query(
`INSERT INTO safety_education_reports (target_type, target_id, education_date, status, registered_by)
VALUES ('day_labor', ?, ?, 'planned', ?)`,
[id, row.work_date, approvedBy]
);
} catch (safetyErr) {
console.error('Safety report auto-create error:', safetyErr);
// 안전교육 보고서 생성 실패해도 승인은 유지
}
}
res.json({ success: true, data: row });
} catch (err) {
console.error('DayLabor approve error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
// 일용직 요청 거절
async function reject(req, res) {
try {
const id = req.params.id;
const approvedBy = req.user.user_id || req.user.id;
const { notes } = req.body;
const row = await dayLaborModel.reject(id, approvedBy, notes);
if (!row) return res.status(404).json({ success: false, error: '요청을 찾을 수 없습니다' });
res.json({ success: true, data: row });
} catch (err) {
console.error('DayLabor reject error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
// 일용직 요청 완료
async function complete(req, res) {
try {
const row = await dayLaborModel.complete(req.params.id);
if (!row) return res.status(404).json({ success: false, error: '요청을 찾을 수 없습니다' });
res.json({ success: true, data: row });
} catch (err) {
console.error('DayLabor complete error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
// 통계
async function stats(req, res) {
try {
const { date_from, date_to } = req.query;
const rows = await dayLaborModel.getStats({ date_from, date_to });
res.json({ success: true, data: rows });
} catch (err) {
console.error('DayLabor stats error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
module.exports = { list, getById, create, approve, reject, complete, stats };