feat: 구매/안전 시스템 전면 개편 — tkpurchase 개편 + tksafety 신규 + 권한 보강

Phase 1: tkuser 협력업체 CRUD 이관 (읽기전용 → 전체 CRUD)
Phase 2: tkpurchase 개편 — 일용공 신청/확정, 작업일정, 업무현황, 계정관리, 협력업체 포털
Phase 3: tksafety 신규 시스템 — 방문관리 + 안전교육 신고
Phase 4: SSO 인증 보강 (partner_company_id JWT, 만료일 체크), 권한 테이블 기반 접근 제어

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Hyungi Ahn
2026-03-12 17:42:59 +09:00
parent a195dd1d50
commit b800792152
63 changed files with 5548 additions and 262 deletions

11
tksafety/api/Dockerfile Normal file
View File

@@ -0,0 +1,11 @@
FROM node:18-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --omit=dev
COPY . .
RUN chown -R node:node /usr/src/app
USER node
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=3s --start-period=20s --retries=3 \
CMD node -e "require('http').get('http://localhost:3000/health', (res) => { process.exit(res.statusCode === 200 ? 0 : 1); })"
CMD ["node", "index.js"]

View File

@@ -0,0 +1,136 @@
const dailyVisitModel = require('../models/dailyVisitModel');
const PURPOSE_LABELS = {
day_labor: '일용공', equipment_repair: '설비수리', inspection: '검사',
delivery: '납품/배송', safety_audit: '안전점검', client_audit: '고객심사',
construction: '공사', other: '기타'
};
async function today(req, res) {
try {
const [visits, stats] = await Promise.all([
dailyVisitModel.findToday(),
dailyVisitModel.getTodayStats()
]);
res.json({ success: true, data: { visits, stats } });
} catch (err) {
console.error('Today visits error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
async function list(req, res) {
try {
const rows = await dailyVisitModel.findAll(req.query);
res.json({ success: true, data: rows });
} catch (err) {
console.error('Visit list error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
async function create(req, res) {
try {
const { visitor_name, purpose, company_id, company_name } = req.body;
if (!visitor_name || !visitor_name.trim()) {
return res.status(400).json({ success: false, error: '방문자명은 필수입니다' });
}
if (!purpose) {
return res.status(400).json({ success: false, error: '방문 목적은 필수입니다' });
}
if (!company_id && (!company_name || !company_name.trim())) {
return res.status(400).json({ success: false, error: '업체를 선택하거나 업체명을 입력해주세요' });
}
const userId = req.user.user_id || req.user.id;
const visit = await dailyVisitModel.create({ ...req.body, registered_by: userId });
res.status(201).json({ success: true, data: visit });
} catch (err) {
console.error('Visit create error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
async function update(req, res) {
try {
const visit = await dailyVisitModel.update(req.params.id, req.body);
if (!visit) return res.status(404).json({ success: false, error: '방문 기록을 찾을 수 없습니다' });
res.json({ success: true, data: visit });
} catch (err) {
console.error('Visit update error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
async function checkout(req, res) {
try {
const visit = await dailyVisitModel.checkout(req.params.id, req.body.checkout_note);
if (!visit) return res.status(404).json({ success: false, error: '방문 기록을 찾을 수 없습니다' });
res.json({ success: true, data: visit });
} catch (err) {
console.error('Checkout error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
async function bulkCheckout(req, res) {
try {
const result = await dailyVisitModel.bulkCheckout();
res.json({ success: true, data: { affected: result.affectedRows } });
} catch (err) {
console.error('Bulk checkout error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
async function deleteVisit(req, res) {
try {
await dailyVisitModel.deleteVisit(req.params.id);
res.json({ success: true, message: '삭제 완료' });
} catch (err) {
console.error('Visit delete error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
async function stats(req, res) {
try {
const data = await dailyVisitModel.getStats(req.query);
res.json({ success: true, data });
} catch (err) {
console.error('Stats error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
async function exportCsv(req, res) {
try {
const rows = await dailyVisitModel.exportCsv(req.query);
const BOM = '\uFEFF';
const header = '방문일,업체,방문자,인원,목적,상세,작업장,안전교육,차량번호,체크인,체크아웃,상태,담당부서,비고';
const lines = rows.map(r => [
r.visit_date ? String(r.visit_date).substring(0, 10) : '',
`"${(r.company || '').replace(/"/g, '""')}"`,
`"${(r.visitor_name || '').replace(/"/g, '""')}"`,
r.visitor_count || 1,
PURPOSE_LABELS[r.purpose] || r.purpose,
`"${(r.purpose_detail || '').replace(/"/g, '""')}"`,
`"${(r.workplace_name || '').replace(/"/g, '""')}"`,
r.safety_education_yn ? 'Y' : 'N',
r.vehicle_number || '',
r.check_in_time || '',
r.check_out_time || '',
r.status,
r.managing_department || '',
`"${(r.notes || '').replace(/"/g, '""')}"`
].join(','));
res.setHeader('Content-Type', 'text/csv; charset=utf-8');
res.setHeader('Content-Disposition', 'attachment; filename=visits.csv');
res.send(BOM + header + '\n' + lines.join('\n'));
} catch (err) {
console.error('Export error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
module.exports = { today, list, create, update, checkout, bulkCheckout, deleteVisit, stats, exportCsv };

View File

@@ -0,0 +1,70 @@
const educationModel = require('../models/educationModel');
async function list(req, res) {
try {
const rows = await educationModel.findAll(req.query);
res.json({ success: true, data: rows });
} catch (err) {
console.error('Education list error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
async function getById(req, res) {
try {
const row = await educationModel.findById(req.params.id);
if (!row) return res.status(404).json({ success: false, error: '교육 기록을 찾을 수 없습니다' });
res.json({ success: true, data: row });
} catch (err) {
console.error('Education getById error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
async function create(req, res) {
try {
const { education_date } = req.body;
if (!education_date) {
return res.status(400).json({ success: false, error: '교육일은 필수입니다' });
}
const userId = req.user.user_id || req.user.id;
const report = await educationModel.create({ ...req.body, registered_by: userId });
res.status(201).json({ success: true, data: report });
} catch (err) {
console.error('Education create error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
async function update(req, res) {
try {
const report = await educationModel.update(req.params.id, req.body);
if (!report) return res.status(404).json({ success: false, error: '교육 기록을 찾을 수 없습니다' });
res.json({ success: true, data: report });
} catch (err) {
console.error('Education update error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
async function deleteReport(req, res) {
try {
await educationModel.deleteReport(req.params.id);
res.json({ success: true, message: '삭제 완료' });
} catch (err) {
console.error('Education delete error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
async function stats(req, res) {
try {
const data = await educationModel.getStats(req.query);
res.json({ success: true, data });
} catch (err) {
console.error('Education stats error:', err);
res.status(500).json({ success: false, error: err.message });
}
}
module.exports = { list, getById, create, update, deleteReport, stats };

86
tksafety/api/index.js Normal file
View File

@@ -0,0 +1,86 @@
const express = require('express');
const cors = require('cors');
const cron = require('node-cron');
const dailyVisitRoutes = require('./routes/dailyVisitRoutes');
const educationRoutes = require('./routes/educationRoutes');
const dailyVisitModel = require('./models/dailyVisitModel');
const { requireAuth } = require('./middleware/auth');
const app = express();
const PORT = process.env.PORT || 3000;
const allowedOrigins = [
'https://tkfb.technicalkorea.net',
'https://tkreport.technicalkorea.net',
'https://tkqc.technicalkorea.net',
'https://tkuser.technicalkorea.net',
'https://tkpurchase.technicalkorea.net',
'https://tksafety.technicalkorea.net',
];
if (process.env.NODE_ENV === 'development') {
allowedOrigins.push('http://localhost:30080', 'http://localhost:30580');
}
app.use(cors({
origin: function(origin, cb) {
if (!origin || allowedOrigins.includes(origin) || /^http:\/\/192\.168\.\d+\.\d+(:\d+)?$/.test(origin)) return cb(null, true);
cb(new Error('CORS blocked: ' + origin));
},
credentials: true
}));
app.use(express.json());
// Health check
app.get('/health', (req, res) => {
res.json({ status: 'ok', service: 'tksafety-api', timestamp: new Date().toISOString() });
});
// Routes
app.use('/api/daily-visits', dailyVisitRoutes);
app.use('/api/education', educationRoutes);
// Partner search (autocomplete)
app.get('/api/partners/search', requireAuth, async (req, res) => {
try {
const q = req.query.q || '';
if (!q.trim()) return res.json({ success: true, data: [] });
const db = dailyVisitModel.getPool();
const [rows] = await db.query(
'SELECT id, company_name, business_number FROM partner_companies WHERE is_active = TRUE AND company_name LIKE ? ORDER BY company_name LIMIT 20',
[`%${q}%`]
);
res.json({ success: true, data: rows });
} catch (err) {
console.error('Partner search error:', err);
res.status(500).json({ success: false, error: err.message });
}
});
// 404
app.use((req, res) => {
res.status(404).json({ success: false, error: 'Not Found' });
});
// Error handler
app.use((err, req, res, next) => {
console.error('tksafety-api Error:', err.message);
res.status(err.status || 500).json({
success: false,
error: err.message || 'Internal Server Error'
});
});
// 자정 자동 체크아웃 (매일 23:59 KST)
cron.schedule('59 23 * * *', async () => {
try {
const result = await dailyVisitModel.autoCheckoutAll();
console.log(`Auto checkout: ${result.affectedRows} visits`);
} catch (e) {
console.error('Auto checkout failed:', e);
}
}, { timezone: 'Asia/Seoul' });
app.listen(PORT, () => {
console.log(`tksafety-api running on port ${PORT}`);
});
module.exports = app;

View File

@@ -0,0 +1,101 @@
const jwt = require('jsonwebtoken');
const mysql = require('mysql2/promise');
const JWT_SECRET = process.env.SSO_JWT_SECRET;
let pool;
function getPool() {
if (!pool) {
pool = mysql.createPool({
host: process.env.DB_HOST || 'mariadb',
port: parseInt(process.env.DB_PORT) || 3306,
user: process.env.DB_USER || 'hyungi_user',
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME || 'hyungi',
waitForConnections: true,
connectionLimit: 5,
queueLimit: 0
});
}
return pool;
}
function extractToken(req) {
const authHeader = req.headers['authorization'];
if (authHeader && authHeader.startsWith('Bearer ')) {
return authHeader.split(' ')[1];
}
if (req.cookies && req.cookies.sso_token) {
return req.cookies.sso_token;
}
return null;
}
function requireAuth(req, res, next) {
const token = extractToken(req);
if (!token) {
return res.status(401).json({ success: false, error: '인증이 필요합니다' });
}
try {
const decoded = jwt.verify(token, JWT_SECRET);
req.user = decoded;
next();
} catch {
return res.status(401).json({ success: false, error: '유효하지 않은 토큰입니다' });
}
}
function requireAdmin(req, res, next) {
const token = extractToken(req);
if (!token) {
return res.status(401).json({ success: false, error: '인증이 필요합니다' });
}
try {
const decoded = jwt.verify(token, JWT_SECRET);
if (!['admin', 'system'].includes((decoded.role || '').toLowerCase())) {
return res.status(403).json({ success: false, error: '관리자 권한이 필요합니다' });
}
req.user = decoded;
next();
} catch {
return res.status(401).json({ success: false, error: '유효하지 않은 토큰입니다' });
}
}
function requirePage(pageName) {
return async (req, res, next) => {
const userId = req.user.user_id || req.user.id;
const role = (req.user.role || '').toLowerCase();
if (role === 'admin' || role === 'system') return next();
try {
const db = getPool();
// 1. 개인 권한
const [rows] = await db.query(
'SELECT can_access FROM user_page_permissions WHERE user_id = ? AND page_name = ?',
[userId, pageName]
);
if (rows.length > 0) {
return rows[0].can_access ? next() : res.status(403).json({ success: false, error: '접근 권한이 없습니다' });
}
// 2. 부서 권한
const [userRows] = await db.query('SELECT department_id FROM sso_users WHERE user_id = ?', [userId]);
if (userRows.length > 0 && userRows[0].department_id) {
const [deptRows] = await db.query(
'SELECT can_access FROM department_page_permissions WHERE department_id = ? AND page_name = ?',
[userRows[0].department_id, pageName]
);
if (deptRows.length > 0) {
return deptRows[0].can_access ? next() : res.status(403).json({ success: false, error: '접근 권한이 없습니다' });
}
}
// 3. 기본 거부
return res.status(403).json({ success: false, error: '접근 권한이 없습니다' });
} catch (err) {
console.error('Permission check error:', err);
return res.status(500).json({ success: false, error: '권한 확인 실패' });
}
};
}
module.exports = { getPool, extractToken, requireAuth, requireAdmin, requirePage };

View File

@@ -0,0 +1,198 @@
const mysql = require('mysql2/promise');
let pool;
function getPool() {
if (!pool) {
pool = mysql.createPool({
host: process.env.DB_HOST || 'mariadb',
port: parseInt(process.env.DB_PORT) || 3306,
user: process.env.DB_USER || 'hyungi_user',
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME || 'hyungi',
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
});
}
return pool;
}
async function findToday() {
const db = getPool();
const [rows] = await db.query(
`SELECT dv.*, pc.company_name AS partner_company_name
FROM daily_visits dv
LEFT JOIN partner_companies pc ON dv.company_id = pc.id
WHERE dv.visit_date = CURDATE()
ORDER BY dv.check_in_time DESC`
);
return rows;
}
async function getTodayStats() {
const db = getPool();
const [rows] = await db.query(
`SELECT
COUNT(*) AS total,
SUM(CASE WHEN status = 'checked_in' THEN 1 ELSE 0 END) AS checked_in,
SUM(CASE WHEN status IN ('checked_out','auto_checkout') THEN 1 ELSE 0 END) AS checked_out,
SUM(CASE WHEN status = 'cancelled' THEN 1 ELSE 0 END) AS cancelled,
SUM(visitor_count) AS total_visitors
FROM daily_visits WHERE visit_date = CURDATE()`
);
return rows[0];
}
async function findAll({ visit_date, date_from, date_to, company_id, purpose, status, page = 1, limit = 50 } = {}) {
const db = getPool();
let sql = `SELECT dv.*, pc.company_name AS partner_company_name
FROM daily_visits dv
LEFT JOIN partner_companies pc ON dv.company_id = pc.id WHERE 1=1`;
const params = [];
if (visit_date) { sql += ' AND dv.visit_date = ?'; params.push(visit_date); }
if (date_from) { sql += ' AND dv.visit_date >= ?'; params.push(date_from); }
if (date_to) { sql += ' AND dv.visit_date <= ?'; params.push(date_to); }
if (company_id) { sql += ' AND dv.company_id = ?'; params.push(company_id); }
if (purpose) { sql += ' AND dv.purpose = ?'; params.push(purpose); }
if (status) { sql += ' AND dv.status = ?'; params.push(status); }
sql += ' ORDER BY dv.visit_date DESC, dv.check_in_time DESC';
const offset = (page - 1) * limit;
sql += ' LIMIT ? OFFSET ?';
params.push(limit, offset);
const [rows] = await db.query(sql, params);
return rows;
}
async function findById(id) {
const db = getPool();
const [rows] = await db.query(
`SELECT dv.*, pc.company_name AS partner_company_name
FROM daily_visits dv
LEFT JOIN partner_companies pc ON dv.company_id = pc.id
WHERE dv.id = ?`,
[id]
);
return rows[0] || null;
}
async function create(data) {
const db = getPool();
const [result] = await db.query(
`INSERT INTO daily_visits (visit_date, company_id, company_name, visitor_name, visitor_count,
purpose, purpose_detail, workplace_name, safety_education_yn, vehicle_number,
check_in_time, notes, managing_department, registered_by)
VALUES (CURDATE(), ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), ?, ?, ?)`,
[data.company_id || null, data.company_name || null, data.visitor_name, data.visitor_count || 1,
data.purpose, data.purpose_detail || null, data.workplace_name || null,
data.safety_education_yn || false, data.vehicle_number || null,
data.notes || null, data.managing_department || null, data.registered_by]
);
// 개별 인원 명단 (선택)
if (data.workers && data.workers.length > 0) {
for (const w of data.workers) {
await db.query(
'INSERT INTO daily_visit_workers (daily_visit_id, partner_worker_id, worker_name) VALUES (?, ?, ?)',
[result.insertId, w.partner_worker_id || null, w.worker_name]
);
}
}
return findById(result.insertId);
}
async function update(id, data) {
const db = getPool();
const fields = [];
const values = [];
if (data.company_id !== undefined) { fields.push('company_id = ?'); values.push(data.company_id || null); }
if (data.company_name !== undefined) { fields.push('company_name = ?'); values.push(data.company_name || null); }
if (data.visitor_name !== undefined) { fields.push('visitor_name = ?'); values.push(data.visitor_name); }
if (data.visitor_count !== undefined) { fields.push('visitor_count = ?'); values.push(data.visitor_count); }
if (data.purpose !== undefined) { fields.push('purpose = ?'); values.push(data.purpose); }
if (data.purpose_detail !== undefined) { fields.push('purpose_detail = ?'); values.push(data.purpose_detail || null); }
if (data.workplace_name !== undefined) { fields.push('workplace_name = ?'); values.push(data.workplace_name || null); }
if (data.safety_education_yn !== undefined) { fields.push('safety_education_yn = ?'); values.push(data.safety_education_yn); }
if (data.vehicle_number !== undefined) { fields.push('vehicle_number = ?'); values.push(data.vehicle_number || null); }
if (data.notes !== undefined) { fields.push('notes = ?'); values.push(data.notes || null); }
if (data.managing_department !== undefined) { fields.push('managing_department = ?'); values.push(data.managing_department || null); }
if (data.status !== undefined) { fields.push('status = ?'); values.push(data.status); }
if (fields.length === 0) return findById(id);
values.push(id);
await db.query(`UPDATE daily_visits SET ${fields.join(', ')} WHERE id = ?`, values);
return findById(id);
}
async function checkout(id, note) {
const db = getPool();
await db.query(
`UPDATE daily_visits SET status = 'checked_out', check_out_time = NOW(), checkout_note = ? WHERE id = ? AND status = 'checked_in'`,
[note || null, id]
);
return findById(id);
}
async function bulkCheckout() {
const db = getPool();
const [result] = await db.query(
`UPDATE daily_visits SET status = 'checked_out', check_out_time = NOW() WHERE visit_date = CURDATE() AND status = 'checked_in'`
);
return result;
}
async function autoCheckoutAll() {
const db = getPool();
const [result] = await db.query(
`UPDATE daily_visits SET status = 'auto_checkout', check_out_time = NOW() WHERE visit_date = CURDATE() AND status = 'checked_in'`
);
return result;
}
async function deleteVisit(id) {
const db = getPool();
await db.query('DELETE FROM daily_visit_workers WHERE daily_visit_id = ?', [id]);
await db.query('DELETE FROM daily_visits WHERE id = ?', [id]);
}
async function getStats({ date_from, date_to } = {}) {
const db = getPool();
const params = [];
let dateFilter = '';
if (date_from) { dateFilter += ' AND visit_date >= ?'; params.push(date_from); }
if (date_to) { dateFilter += ' AND visit_date <= ?'; params.push(date_to); }
const [byPurpose] = await db.query(
`SELECT purpose, COUNT(*) AS cnt, SUM(visitor_count) AS total_visitors FROM daily_visits WHERE 1=1 ${dateFilter} GROUP BY purpose ORDER BY cnt DESC`,
params
);
const [byCompany] = await db.query(
`SELECT COALESCE(pc.company_name, dv.company_name, '미등록') AS company, COUNT(*) AS cnt, SUM(dv.visitor_count) AS total_visitors
FROM daily_visits dv LEFT JOIN partner_companies pc ON dv.company_id = pc.id WHERE 1=1 ${dateFilter} GROUP BY company ORDER BY cnt DESC LIMIT 20`,
params
);
const [byDate] = await db.query(
`SELECT visit_date, COUNT(*) AS cnt, SUM(visitor_count) AS total_visitors FROM daily_visits WHERE 1=1 ${dateFilter} GROUP BY visit_date ORDER BY visit_date DESC LIMIT 30`,
params
);
return { byPurpose, byCompany, byDate };
}
async function exportCsv({ date_from, date_to, company_id, purpose } = {}) {
const db = getPool();
let sql = `SELECT dv.visit_date, COALESCE(pc.company_name, dv.company_name, '') AS company,
dv.visitor_name, dv.visitor_count, dv.purpose, dv.purpose_detail, dv.workplace_name,
dv.safety_education_yn, dv.vehicle_number, dv.check_in_time, dv.check_out_time,
dv.status, dv.managing_department, dv.notes
FROM daily_visits dv LEFT JOIN partner_companies pc ON dv.company_id = pc.id WHERE 1=1`;
const params = [];
if (date_from) { sql += ' AND dv.visit_date >= ?'; params.push(date_from); }
if (date_to) { sql += ' AND dv.visit_date <= ?'; params.push(date_to); }
if (company_id) { sql += ' AND dv.company_id = ?'; params.push(company_id); }
if (purpose) { sql += ' AND dv.purpose = ?'; params.push(purpose); }
sql += ' ORDER BY dv.visit_date DESC, dv.check_in_time DESC';
const [rows] = await db.query(sql, params);
return rows;
}
module.exports = {
getPool, findToday, getTodayStats, findAll, findById, create, update,
checkout, bulkCheckout, autoCheckoutAll, deleteVisit, getStats, exportCsv
};

View File

@@ -0,0 +1,80 @@
const { getPool } = require('./dailyVisitModel');
async function findAll({ date_from, date_to, target_type, status, page = 1, limit = 50 } = {}) {
const db = getPool();
let sql = 'SELECT * FROM safety_education_reports WHERE 1=1';
const params = [];
if (date_from) { sql += ' AND education_date >= ?'; params.push(date_from); }
if (date_to) { sql += ' AND education_date <= ?'; params.push(date_to); }
if (target_type) { sql += ' AND target_type = ?'; params.push(target_type); }
if (status) { sql += ' AND status = ?'; params.push(status); }
sql += ' ORDER BY education_date DESC';
const offset = (page - 1) * limit;
sql += ' LIMIT ? OFFSET ?';
params.push(limit, offset);
const [rows] = await db.query(sql, params);
return rows;
}
async function findById(id) {
const db = getPool();
const [rows] = await db.query('SELECT * FROM safety_education_reports WHERE id = ?', [id]);
return rows[0] || null;
}
async function create(data) {
const db = getPool();
const attendeesStr = data.attendees ? (typeof data.attendees === 'string' ? data.attendees : JSON.stringify(data.attendees)) : null;
const [result] = await db.query(
`INSERT INTO safety_education_reports (target_type, target_id, education_date, educator, attendees, status, notes, registered_by)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
[data.target_type, data.target_id || null, data.education_date, data.educator || null,
attendeesStr, data.status || 'planned', data.notes || null, data.registered_by]
);
return findById(result.insertId);
}
async function update(id, data) {
const db = getPool();
const fields = [];
const values = [];
if (data.target_type !== undefined) { fields.push('target_type = ?'); values.push(data.target_type); }
if (data.education_date !== undefined) { fields.push('education_date = ?'); values.push(data.education_date); }
if (data.educator !== undefined) { fields.push('educator = ?'); values.push(data.educator || null); }
if (data.attendees !== undefined) {
const attendeesStr = data.attendees ? (typeof data.attendees === 'string' ? data.attendees : JSON.stringify(data.attendees)) : null;
fields.push('attendees = ?');
values.push(attendeesStr);
}
if (data.status !== undefined) { fields.push('status = ?'); values.push(data.status); }
if (data.notes !== undefined) { fields.push('notes = ?'); values.push(data.notes || null); }
if (fields.length === 0) return findById(id);
values.push(id);
await db.query(`UPDATE safety_education_reports SET ${fields.join(', ')} WHERE id = ?`, values);
return findById(id);
}
async function deleteReport(id) {
const db = getPool();
await db.query('DELETE FROM safety_education_reports WHERE id = ?', [id]);
}
async function getStats({ date_from, date_to } = {}) {
const db = getPool();
const params = [];
let dateFilter = '';
if (date_from) { dateFilter += ' AND education_date >= ?'; params.push(date_from); }
if (date_to) { dateFilter += ' AND education_date <= ?'; params.push(date_to); }
const [rows] = await db.query(
`SELECT target_type, COUNT(*) AS cnt,
SUM(CASE WHEN status = 'completed' THEN 1 ELSE 0 END) AS completed,
SUM(CASE WHEN status = 'planned' THEN 1 ELSE 0 END) AS planned,
SUM(CASE WHEN status = 'cancelled' THEN 1 ELSE 0 END) AS cancelled
FROM safety_education_reports WHERE 1=1 ${dateFilter} GROUP BY target_type`,
params
);
return rows;
}
module.exports = { findAll, findById, create, update, deleteReport, getStats };

17
tksafety/api/package.json Normal file
View File

@@ -0,0 +1,17 @@
{
"name": "tksafety-api",
"version": "1.0.0",
"description": "TK Factory Services - 안전 관리 서비스",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "node --watch index.js"
},
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.0",
"mysql2": "^3.14.1",
"node-cron": "^3.0.3"
}
}

View File

@@ -0,0 +1,18 @@
const express = require('express');
const router = express.Router();
const { requireAuth, requirePage } = require('../middleware/auth');
const ctrl = require('../controllers/dailyVisitController');
router.use(requireAuth);
router.get('/today', ctrl.today);
router.get('/export', ctrl.exportCsv);
router.get('/stats', ctrl.stats);
router.get('/', ctrl.list);
router.post('/', requirePage('safety_visit'), ctrl.create);
router.post('/bulk-checkout', requirePage('safety_visit'), ctrl.bulkCheckout);
router.put('/:id', requirePage('safety_visit'), ctrl.update);
router.put('/:id/checkout', requirePage('safety_visit'), ctrl.checkout);
router.delete('/:id', requirePage('safety_visit'), ctrl.deleteVisit);
module.exports = router;

View File

@@ -0,0 +1,15 @@
const express = require('express');
const router = express.Router();
const { requireAuth, requirePage } = require('../middleware/auth');
const ctrl = require('../controllers/educationController');
router.use(requireAuth);
router.get('/', ctrl.list);
router.get('/stats', ctrl.stats);
router.get('/:id', ctrl.getById);
router.post('/', requirePage('safety_education'), ctrl.create);
router.put('/:id', requirePage('safety_education'), ctrl.update);
router.delete('/:id', requirePage('safety_education'), ctrl.deleteReport);
module.exports = router;

7
tksafety/web/Dockerfile Normal file
View File

@@ -0,0 +1,7 @@
FROM nginx:alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY index.html /usr/share/nginx/html/index.html
COPY education.html /usr/share/nginx/html/education.html
COPY static/ /usr/share/nginx/html/static/
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

194
tksafety/web/education.html Normal file
View File

@@ -0,0 +1,194 @@
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>안전교육 - TK 안전관리</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<link rel="stylesheet" href="/static/css/tksafety.css?v=20260312">
</head>
<body>
<!-- Header -->
<header class="bg-orange-700 text-white sticky top-0 z-50">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex justify-between items-center h-14">
<div class="flex items-center gap-3">
<i class="fas fa-shield-alt text-xl text-orange-200"></i>
<h1 class="text-lg font-semibold">TK 안전관리</h1>
</div>
<div class="flex items-center gap-4">
<div id="headerUserName" class="text-sm font-medium hidden sm:block">-</div>
<div id="headerUserAvatar" class="w-8 h-8 bg-orange-600 rounded-full flex items-center justify-center text-sm font-semibold">-</div>
<button onclick="doLogout()" class="text-orange-200 hover:text-white" title="로그아웃"><i class="fas fa-sign-out-alt"></i></button>
</div>
</div>
</div>
</header>
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-4 fade-in">
<div class="flex gap-6">
<!-- Sidebar Nav -->
<nav id="sideNav" class="hidden lg:flex flex-col gap-1 w-48 flex-shrink-0 pt-2"></nav>
<!-- Main -->
<div class="flex-1 min-w-0">
<!-- 필터 & 추가 -->
<div class="bg-white rounded-xl shadow-sm p-5 mb-5">
<div class="flex items-center justify-between mb-4">
<h2 class="text-base font-semibold text-gray-800"><i class="fas fa-graduation-cap text-orange-500 mr-2"></i>안전교육 관리</h2>
<button onclick="openAddEducation()" class="px-4 py-2 bg-orange-600 text-white rounded-lg hover:bg-orange-700 text-sm font-medium">
<i class="fas fa-plus mr-1"></i>교육 등록
</button>
</div>
<div class="flex flex-wrap items-end gap-3">
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">시작일</label>
<input type="date" id="eduDateFrom" class="input-field px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">종료일</label>
<input type="date" id="eduDateTo" class="input-field px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">구분</label>
<select id="eduTargetType" class="input-field px-3 py-2 rounded-lg text-sm">
<option value="">전체</option>
<option value="day_labor">일용공</option>
<option value="partner_schedule">협력업체</option>
<option value="manual">수동등록</option>
</select>
</div>
</div>
</div>
<!-- 교육 목록 -->
<div class="bg-white rounded-xl shadow-sm p-5">
<div class="overflow-x-auto">
<table class="visit-table">
<thead>
<tr>
<th>교육일</th>
<th>구분</th>
<th>교육자</th>
<th class="text-center">참석인원</th>
<th>상태</th>
<th class="hide-mobile">비고</th>
<th class="text-right">관리</th>
</tr>
</thead>
<tbody id="educationTableBody">
<tr><td colspan="7" class="text-center text-gray-400 py-8">로딩 중...</td></tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<!-- 교육 등록 모달 -->
<div id="addEducationModal" class="hidden modal-overlay" onclick="if(event.target===this)closeAddEducation()">
<div class="modal-content p-6">
<div class="flex justify-between items-center mb-4">
<h3 class="text-lg font-semibold">안전교육 등록</h3>
<button onclick="closeAddEducation()" class="text-gray-400 hover:text-gray-600"><i class="fas fa-times"></i></button>
</div>
<form id="addEducationForm">
<div class="grid grid-cols-2 gap-3">
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">구분 <span class="text-red-400">*</span></label>
<select id="newTargetType" class="input-field w-full px-3 py-2 rounded-lg text-sm" required>
<option value="day_labor">일용공</option>
<option value="partner_schedule">협력업체</option>
<option value="manual">수동등록</option>
</select>
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">교육일 <span class="text-red-400">*</span></label>
<input type="date" id="newEducationDate" class="input-field w-full px-3 py-2 rounded-lg text-sm" required>
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">교육자</label>
<input type="text" id="newEducator" class="input-field w-full px-3 py-2 rounded-lg text-sm" placeholder="교육 담당자">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">상태</label>
<select id="newEduStatus" class="input-field w-full px-3 py-2 rounded-lg text-sm">
<option value="planned">예정</option>
<option value="completed">완료</option>
<option value="cancelled">취소</option>
</select>
</div>
<div class="col-span-2">
<label class="block text-xs font-medium text-gray-600 mb-1">참석자 (한 줄에 한 명, 이름, 업체 형식)</label>
<textarea id="newAttendees" class="input-field w-full px-3 py-2 rounded-lg text-sm" rows="4" placeholder="홍길동, ABC건설&#10;김철수, XYZ설비"></textarea>
</div>
<div class="col-span-2">
<label class="block text-xs font-medium text-gray-600 mb-1">비고</label>
<input type="text" id="newEduNotes" class="input-field w-full px-3 py-2 rounded-lg text-sm" placeholder="메모">
</div>
</div>
<div class="flex justify-end mt-4 gap-2">
<button type="button" onclick="closeAddEducation()" class="px-4 py-2 border rounded-lg text-sm hover:bg-gray-50">취소</button>
<button type="submit" class="px-4 py-2 bg-orange-600 text-white rounded-lg text-sm hover:bg-orange-700">등록</button>
</div>
</form>
</div>
</div>
<!-- 교육 수정 모달 -->
<div id="editEducationModal" class="hidden modal-overlay" onclick="if(event.target===this)closeEditEducation()">
<div class="modal-content p-6">
<div class="flex justify-between items-center mb-4">
<h3 class="text-lg font-semibold">안전교육 수정</h3>
<button onclick="closeEditEducation()" class="text-gray-400 hover:text-gray-600"><i class="fas fa-times"></i></button>
</div>
<form id="editEducationForm">
<div class="grid grid-cols-2 gap-3">
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">구분</label>
<select id="editTargetType" class="input-field w-full px-3 py-2 rounded-lg text-sm">
<option value="day_labor">일용공</option>
<option value="partner_schedule">협력업체</option>
<option value="manual">수동등록</option>
</select>
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">교육일</label>
<input type="date" id="editEducationDate" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">교육자</label>
<input type="text" id="editEducator" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">상태</label>
<select id="editEduStatus" class="input-field w-full px-3 py-2 rounded-lg text-sm">
<option value="planned">예정</option>
<option value="completed">완료</option>
<option value="cancelled">취소</option>
</select>
</div>
<div class="col-span-2">
<label class="block text-xs font-medium text-gray-600 mb-1">참석자 (한 줄에 한 명, 이름, 업체 형식)</label>
<textarea id="editAttendees" class="input-field w-full px-3 py-2 rounded-lg text-sm" rows="4"></textarea>
</div>
<div class="col-span-2">
<label class="block text-xs font-medium text-gray-600 mb-1">비고</label>
<input type="text" id="editEduNotes" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
</div>
<div class="flex justify-end mt-4 gap-2">
<button type="button" onclick="closeEditEducation()" class="px-4 py-2 border rounded-lg text-sm hover:bg-gray-50">취소</button>
<button type="submit" class="px-4 py-2 bg-orange-600 text-white rounded-lg text-sm hover:bg-orange-700">저장</button>
</div>
</form>
</div>
</div>
<script src="/static/js/tksafety-core.js?v=20260312"></script>
<script src="/static/js/tksafety-education.js?v=20260312"></script>
<script>initEducationPage();</script>
</body>
</html>

281
tksafety/web/index.html Normal file
View File

@@ -0,0 +1,281 @@
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>방문 관리 - TK 안전관리</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<link rel="stylesheet" href="/static/css/tksafety.css?v=20260312">
</head>
<body>
<!-- Header -->
<header class="bg-orange-700 text-white sticky top-0 z-50">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex justify-between items-center h-14">
<div class="flex items-center gap-3">
<i class="fas fa-shield-alt text-xl text-orange-200"></i>
<h1 class="text-lg font-semibold">TK 안전관리</h1>
</div>
<div class="flex items-center gap-4">
<div id="headerUserName" class="text-sm font-medium hidden sm:block">-</div>
<div id="headerUserAvatar" class="w-8 h-8 bg-orange-600 rounded-full flex items-center justify-center text-sm font-semibold">-</div>
<button onclick="doLogout()" class="text-orange-200 hover:text-white" title="로그아웃"><i class="fas fa-sign-out-alt"></i></button>
</div>
</div>
</div>
</header>
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-4 fade-in">
<div class="flex gap-6">
<!-- Sidebar Nav -->
<nav id="sideNav" class="hidden lg:flex flex-col gap-1 w-48 flex-shrink-0 pt-2"></nav>
<!-- Main -->
<div class="flex-1 min-w-0">
<!-- 통계 카드 -->
<div class="grid grid-cols-2 sm:grid-cols-4 gap-3 mb-5">
<div class="stat-card">
<div class="stat-value text-orange-600" id="statTotal">0</div>
<div class="stat-label">오늘 방문</div>
</div>
<div class="stat-card">
<div class="stat-value text-blue-600" id="statCheckedIn">0</div>
<div class="stat-label">체크인 중</div>
</div>
<div class="stat-card">
<div class="stat-value text-gray-600" id="statCheckedOut">0</div>
<div class="stat-label">체크아웃</div>
</div>
<div class="stat-card">
<div class="stat-value text-purple-600" id="statVisitors">0</div>
<div class="stat-label">총 인원</div>
</div>
</div>
<!-- 빠른 등록 폼 -->
<div class="bg-white rounded-xl shadow-sm p-5 mb-5">
<h2 class="text-base font-semibold text-gray-800 mb-4"><i class="fas fa-plus-circle text-orange-500 mr-2"></i>방문 등록</h2>
<form id="visitForm">
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-3">
<!-- 업체 -->
<div class="sm:col-span-2 relative">
<label class="block text-xs font-medium text-gray-600 mb-1">업체</label>
<div class="flex items-center gap-2">
<div class="flex-1 relative">
<input type="text" id="companySearch" class="input-field w-full px-3 py-2 rounded-lg text-sm" placeholder="업체명 검색...">
<div id="companyDropdown" class="hidden absolute z-10 w-full mt-1 bg-white border rounded-lg shadow-lg max-h-48 overflow-y-auto"></div>
</div>
<div class="hidden flex-1">
<input type="text" id="manualCompanyName" class="input-field w-full px-3 py-2 rounded-lg text-sm" placeholder="업체명 직접입력">
</div>
<label class="flex items-center gap-1 text-xs text-gray-500 whitespace-nowrap cursor-pointer">
<input type="checkbox" id="manualCompanyToggle" class="rounded">
<span>직접입력</span>
</label>
</div>
</div>
<!-- 방문자명 -->
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">방문자명 <span class="text-red-400">*</span></label>
<input type="text" id="visitorName" class="input-field w-full px-3 py-2 rounded-lg text-sm" placeholder="대표 방문자" required>
</div>
<!-- 인원 -->
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">인원</label>
<div class="flex items-center gap-1">
<button type="button" id="countMinus" class="w-9 h-9 flex items-center justify-center border rounded-lg hover:bg-gray-50 text-gray-600"><i class="fas fa-minus text-xs"></i></button>
<input type="number" id="visitorCount" value="1" min="1" class="input-field w-14 text-center px-1 py-2 rounded-lg text-sm">
<button type="button" id="countPlus" class="w-9 h-9 flex items-center justify-center border rounded-lg hover:bg-gray-50 text-gray-600"><i class="fas fa-plus text-xs"></i></button>
</div>
</div>
<!-- 목적 -->
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">방문 목적 <span class="text-red-400">*</span></label>
<select id="visitPurpose" class="input-field w-full px-3 py-2 rounded-lg text-sm" required>
<option value="">선택</option>
<option value="day_labor">일용공</option>
<option value="equipment_repair">설비수리</option>
<option value="inspection">검사</option>
<option value="delivery">납품/배송</option>
<option value="safety_audit">안전점검</option>
<option value="client_audit">고객심사</option>
<option value="construction">공사</option>
<option value="other">기타</option>
</select>
</div>
<!-- 작업장 -->
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">작업장</label>
<input type="text" id="workplaceName" class="input-field w-full px-3 py-2 rounded-lg text-sm" placeholder="작업장소">
</div>
<!-- 안전교육 -->
<div class="flex items-end pb-1">
<label class="flex items-center gap-2 cursor-pointer">
<input type="checkbox" id="safetyCheck" class="h-5 w-5 text-orange-500 rounded border-gray-300">
<span class="text-sm text-gray-700">안전교육 이수</span>
</label>
</div>
<!-- 목적 상세 -->
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">목적 상세</label>
<input type="text" id="purposeDetail" class="input-field w-full px-3 py-2 rounded-lg text-sm" placeholder="상세 내용">
</div>
</div>
<!-- 추가 정보 (접이식) -->
<div class="mt-3">
<button type="button" onclick="toggleExtra()" class="text-sm text-gray-500 hover:text-gray-700 flex items-center gap-1">
<i id="extraToggleIcon" class="fas fa-chevron-down text-xs"></i>추가 정보
</button>
<div id="extraFields" class="collapsible-content">
<div class="grid grid-cols-1 sm:grid-cols-3 gap-3 mt-2">
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">차량번호</label>
<input type="text" id="vehicleNumber" class="input-field w-full px-3 py-2 rounded-lg text-sm" placeholder="12가 3456">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">담당부서</label>
<select id="managingDept" class="input-field w-full px-3 py-2 rounded-lg text-sm">
<option value="">선택</option>
<option value="생산">생산</option>
<option value="품질">품질</option>
<option value="구매">구매</option>
<option value="설계">설계</option>
<option value="영업">영업</option>
</select>
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">비고</label>
<input type="text" id="visitNotes" class="input-field w-full px-3 py-2 rounded-lg text-sm" placeholder="메모">
</div>
</div>
</div>
</div>
<div class="flex justify-end mt-4">
<button type="submit" class="px-6 py-2.5 bg-orange-600 text-white rounded-lg hover:bg-orange-700 text-sm font-medium">
<i class="fas fa-check mr-2"></i>등록
</button>
</div>
</form>
</div>
<!-- 오늘 방문 현황 -->
<div class="bg-white rounded-xl shadow-sm p-5 mb-5">
<div class="flex items-center justify-between mb-4">
<h2 class="text-base font-semibold text-gray-800"><i class="fas fa-list text-orange-500 mr-2"></i>오늘 방문 현황</h2>
<div class="flex gap-2">
<button onclick="doBulkCheckout()" class="text-xs text-blue-600 hover:text-blue-800 border border-blue-200 px-3 py-1.5 rounded-lg hover:bg-blue-50">
<i class="fas fa-check-double mr-1"></i>전체 마감
</button>
</div>
</div>
<div class="overflow-x-auto">
<table class="visit-table">
<thead>
<tr>
<th>업체</th>
<th>방문자</th>
<th class="text-center">인원</th>
<th>목적</th>
<th class="hide-mobile">안전교육</th>
<th>체크인</th>
<th>상태</th>
<th class="text-right">관리</th>
</tr>
</thead>
<tbody id="visitTableBody">
<tr><td colspan="8" class="text-center text-gray-400 py-8">로딩 중...</td></tr>
</tbody>
</table>
</div>
</div>
<!-- CSV 내보내기 -->
<div class="bg-white rounded-xl shadow-sm p-5">
<h2 class="text-base font-semibold text-gray-800 mb-4"><i class="fas fa-download text-orange-500 mr-2"></i>CSV 내보내기</h2>
<div class="flex flex-wrap items-end gap-3">
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">시작일</label>
<input type="date" id="exportDateFrom" class="input-field px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">종료일</label>
<input type="date" id="exportDateTo" class="input-field px-3 py-2 rounded-lg text-sm">
</div>
<button onclick="exportVisits()" class="px-4 py-2 border border-gray-300 rounded-lg text-sm hover:bg-gray-50">
<i class="fas fa-file-csv mr-1"></i>내보내기
</button>
</div>
</div>
</div>
</div>
</div>
<!-- 수정 모달 -->
<div id="editVisitModal" class="hidden modal-overlay" onclick="if(event.target===this)closeEditVisit()">
<div class="modal-content p-6">
<div class="flex justify-between items-center mb-4">
<h3 class="text-lg font-semibold">방문 수정</h3>
<button onclick="closeEditVisit()" class="text-gray-400 hover:text-gray-600"><i class="fas fa-times"></i></button>
</div>
<form id="editVisitForm">
<div class="grid grid-cols-2 gap-3">
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">방문자명</label>
<input type="text" id="editVisitorName" class="input-field w-full px-3 py-2 rounded-lg text-sm" required>
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">인원</label>
<input type="number" id="editVisitorCount" min="1" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">목적</label>
<select id="editPurpose" class="input-field w-full px-3 py-2 rounded-lg text-sm">
<option value="day_labor">일용공</option>
<option value="equipment_repair">설비수리</option>
<option value="inspection">검사</option>
<option value="delivery">납품/배송</option>
<option value="safety_audit">안전점검</option>
<option value="client_audit">고객심사</option>
<option value="construction">공사</option>
<option value="other">기타</option>
</select>
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">목적 상세</label>
<input type="text" id="editPurposeDetail" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">작업장</label>
<input type="text" id="editWorkplace" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">차량번호</label>
<input type="text" id="editVehicle" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
<div class="flex items-end pb-1">
<label class="flex items-center gap-2 cursor-pointer">
<input type="checkbox" id="editSafetyCheck" class="h-5 w-5 text-orange-500 rounded">
<span class="text-sm">안전교육 이수</span>
</label>
</div>
<div>
<label class="block text-xs font-medium text-gray-600 mb-1">비고</label>
<input type="text" id="editNotes" class="input-field w-full px-3 py-2 rounded-lg text-sm">
</div>
</div>
<div class="flex justify-end mt-4 gap-2">
<button type="button" onclick="closeEditVisit()" class="px-4 py-2 border rounded-lg text-sm hover:bg-gray-50">취소</button>
<button type="submit" class="px-4 py-2 bg-orange-600 text-white rounded-lg text-sm hover:bg-orange-700">저장</button>
</div>
</form>
</div>
</div>
<script src="/static/js/tksafety-core.js?v=20260312"></script>
<script src="/static/js/tksafety-visit.js?v=20260312"></script>
<script>initVisitPage();</script>
</body>
</html>

45
tksafety/web/nginx.conf Normal file
View File

@@ -0,0 +1,45 @@
server {
listen 80;
server_name _;
charset utf-8;
root /usr/share/nginx/html;
index index.html;
gzip on;
gzip_types text/plain text/css application/javascript application/json;
gzip_min_length 1024;
location ~* \.html$ {
expires -1;
add_header Cache-Control "no-store, no-cache, must-revalidate";
}
location ~* \.(js|css)$ {
expires 1h;
add_header Cache-Control "public, no-transform";
}
location /api/ {
proxy_pass http://tksafety-api:3000/api/;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /static/ {
expires 1h;
add_header Cache-Control "public, no-transform";
}
location / {
try_files $uri $uri/ /index.html;
}
location /health {
access_log off;
return 200 'ok';
add_header Content-Type text/plain;
}
}

View File

@@ -0,0 +1,63 @@
/* tksafety global styles */
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: #f8fafc; margin: 0; }
.fade-in { opacity: 0; transition: opacity 0.3s; }
.fade-in.visible { opacity: 1; }
/* Input */
.input-field { border: 1px solid #e2e8f0; transition: border-color 0.15s; outline: none; }
.input-field:focus { border-color: #f97316; box-shadow: 0 0 0 3px rgba(249,115,22,0.1); }
/* Toast */
.toast-message { transition: opacity 0.3s; }
/* Nav active */
.nav-link.active { background: rgba(249,115,22,0.15); color: #ea580c; font-weight: 600; }
/* Stat card */
.stat-card { background: white; border-radius: 0.75rem; padding: 1.25rem; box-shadow: 0 1px 3px rgba(0,0,0,0.08); }
.stat-card .stat-value { font-size: 1.75rem; font-weight: 700; line-height: 1.2; }
.stat-card .stat-label { font-size: 0.8rem; color: #6b7280; margin-top: 0.25rem; }
/* Table */
.visit-table { width: 100%; border-collapse: collapse; font-size: 0.875rem; }
.visit-table th { background: #f1f5f9; padding: 0.625rem 0.75rem; text-align: left; font-weight: 600; color: #475569; white-space: nowrap; border-bottom: 2px solid #e2e8f0; }
.visit-table td { padding: 0.625rem 0.75rem; border-bottom: 1px solid #f1f5f9; vertical-align: middle; }
.visit-table tr:hover { background: #f8fafc; }
/* Badge */
.badge { display: inline-flex; align-items: center; padding: 0.125rem 0.5rem; border-radius: 9999px; font-size: 0.75rem; font-weight: 500; }
.badge-green { background: #ecfdf5; color: #059669; }
.badge-blue { background: #eff6ff; color: #2563eb; }
.badge-amber { background: #fffbeb; color: #d97706; }
.badge-red { background: #fef2f2; color: #dc2626; }
.badge-gray { background: #f3f4f6; color: #6b7280; }
/* Purpose badges */
.purpose-day_labor { background: #dbeafe; color: #1d4ed8; }
.purpose-equipment_repair { background: #fef3c7; color: #92400e; }
.purpose-inspection { background: #ede9fe; color: #6d28d9; }
.purpose-delivery { background: #d1fae5; color: #065f46; }
.purpose-safety_audit { background: #fee2e2; color: #991b1b; }
.purpose-client_audit { background: #fce7f3; color: #9d174d; }
.purpose-construction { background: #e0e7ff; color: #3730a3; }
.purpose-other { background: #f3f4f6; color: #374151; }
/* Collapsible */
.collapsible-content { max-height: 0; overflow: hidden; transition: max-height 0.3s ease; }
.collapsible-content.open { max-height: 500px; }
/* Modal */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.4); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 1rem; }
.modal-content { background: white; border-radius: 0.75rem; max-width: 40rem; width: 100%; max-height: 90vh; overflow-y: auto; box-shadow: 0 20px 60px rgba(0,0,0,0.2); }
/* Safety warning */
.safety-warning { animation: pulse 2s infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.6; } }
/* Responsive */
@media (max-width: 768px) {
.stat-card .stat-value { font-size: 1.25rem; }
.visit-table { font-size: 0.8rem; }
.visit-table th, .visit-table td { padding: 0.5rem; }
.hide-mobile { display: none; }
}

View File

@@ -0,0 +1,123 @@
/* ===== 서비스 워커 해제 ===== */
if ('serviceWorker' in navigator) {
navigator.serviceWorker.getRegistrations().then(function(regs) { regs.forEach(function(r) { r.unregister(); }); });
if (typeof caches !== 'undefined') { caches.keys().then(function(ns) { ns.forEach(function(n) { caches.delete(n); }); }); }
}
/* ===== Config ===== */
const API_BASE = '/api';
const PURPOSE_LABELS = {
day_labor: '일용공', equipment_repair: '설비수리', inspection: '검사',
delivery: '납품/배송', safety_audit: '안전점검', client_audit: '고객심사',
construction: '공사', other: '기타'
};
/* ===== Token ===== */
function _cookieGet(n) { const m = document.cookie.match(new RegExp('(?:^|; )' + n + '=([^;]*)')); return m ? decodeURIComponent(m[1]) : null; }
function _cookieRemove(n) { let c = n + '=; path=/; max-age=0'; if (location.hostname.includes('technicalkorea.net')) c += '; domain=.technicalkorea.net; secure; samesite=lax'; document.cookie = c; }
function getToken() { return _cookieGet('sso_token') || localStorage.getItem('sso_token'); }
function getLoginUrl() {
const h = location.hostname;
const t = Date.now();
if (h.includes('technicalkorea.net')) return location.protocol + '//tkfb.technicalkorea.net/login?redirect=' + encodeURIComponent(location.href) + '&_t=' + t;
return location.protocol + '//' + h + ':30000/login?redirect=' + encodeURIComponent(location.href) + '&_t=' + t;
}
function decodeToken(t) { try { const b = atob(t.split('.')[1].replace(/-/g,'+').replace(/_/g,'/')); return JSON.parse(new TextDecoder().decode(Uint8Array.from(b, c => c.charCodeAt(0)))); } catch { return null; } }
/* ===== 리다이렉트 루프 방지 ===== */
const _REDIRECT_KEY = '_sso_redirect_ts';
function _safeRedirect() {
const last = parseInt(sessionStorage.getItem(_REDIRECT_KEY) || '0', 10);
if (Date.now() - last < 5000) { console.warn('[tksafety] 리다이렉트 루프 감지'); return; }
sessionStorage.setItem(_REDIRECT_KEY, String(Date.now()));
location.href = getLoginUrl();
}
/* ===== API ===== */
async function api(path, opts = {}) {
const token = getToken();
const headers = { 'Authorization': token ? `Bearer ${token}` : '', ...(opts.headers||{}) };
if (!(opts.body instanceof FormData)) headers['Content-Type'] = 'application/json';
const res = await fetch(API_BASE + path, { ...opts, headers });
if (res.status === 401) { _safeRedirect(); throw new Error('인증 만료'); }
if (res.headers.get('content-type')?.includes('text/csv')) return res;
const data = await res.json();
if (!res.ok) throw new Error(data.error || '요청 실패');
return data;
}
/* ===== Toast ===== */
function showToast(msg, type = 'success') {
document.querySelector('.toast-message')?.remove();
const el = document.createElement('div');
el.className = `toast-message fixed bottom-4 right-4 px-4 py-3 rounded-lg text-white z-[10000] shadow-lg ${type==='success'?'bg-orange-500':'bg-red-500'}`;
el.innerHTML = `<i class="fas ${type==='success'?'fa-check-circle':'fa-exclamation-circle'} mr-2"></i>${escapeHtml(msg)}`;
document.body.appendChild(el);
setTimeout(() => { el.classList.add('opacity-0'); setTimeout(() => el.remove(), 300); }, 3000);
}
/* ===== Escape ===== */
function escapeHtml(str) { if (!str) return ''; const d = document.createElement('div'); d.textContent = str; return d.innerHTML; }
/* ===== Helpers ===== */
function formatDate(d) { if (!d) return ''; return String(d).substring(0, 10); }
function formatTime(d) { if (!d) return ''; return String(d).substring(11, 16); }
function formatDateTime(d) { if (!d) return ''; return String(d).substring(0, 16).replace('T', ' '); }
function purposeLabel(p) { return PURPOSE_LABELS[p] || p || ''; }
function purposeBadge(p) { return `<span class="badge purpose-${p}">${purposeLabel(p)}</span>`; }
function statusBadge(s) {
const m = { checked_in: ['badge-green', '체크인'], checked_out: ['badge-blue', '체크아웃'], auto_checkout: ['badge-amber', '자동마감'], cancelled: ['badge-gray', '취소'] };
const [cls, label] = m[s] || ['badge-gray', s];
return `<span class="badge ${cls}">${label}</span>`;
}
/* ===== Logout ===== */
function doLogout() {
if (!confirm('로그아웃?')) return;
_cookieRemove('sso_token'); _cookieRemove('sso_user'); _cookieRemove('sso_refresh_token');
['sso_token','sso_user','sso_refresh_token','token','user','access_token','currentUser','current_user','userInfo','userPageAccess'].forEach(k => localStorage.removeItem(k));
location.href = getLoginUrl();
}
/* ===== Navbar ===== */
function renderNavbar() {
const currentPage = location.pathname.replace(/\//g, '') || 'index.html';
const links = [
{ href: '/', icon: 'fa-door-open', label: '방문 관리', match: ['', 'index.html'] },
{ href: '/education.html', icon: 'fa-graduation-cap', label: '안전교육', match: ['education.html'] },
];
const nav = document.getElementById('sideNav');
if (!nav) return;
nav.innerHTML = links.map(l => {
const active = l.match.some(m => currentPage === m || currentPage.endsWith(m));
return `<a href="${l.href}" class="nav-link flex items-center gap-3 px-4 py-2.5 rounded-lg text-sm transition-colors ${active ? 'active' : 'text-gray-600 hover:bg-gray-100'}">
<i class="fas ${l.icon} w-5 text-center"></i><span>${l.label}</span></a>`;
}).join('');
}
/* ===== State ===== */
let currentUser = null;
/* ===== Init ===== */
function initAuth() {
const token = getToken();
if (!token) { _safeRedirect(); return false; }
const decoded = decodeToken(token);
if (!decoded) { _safeRedirect(); return false; }
sessionStorage.removeItem(_REDIRECT_KEY);
if (!localStorage.getItem('sso_token')) localStorage.setItem('sso_token', token);
currentUser = {
id: decoded.user_id || decoded.id,
username: decoded.username || decoded.sub,
name: decoded.name || decoded.full_name,
role: (decoded.role || decoded.access_level || '').toLowerCase()
};
const dn = currentUser.name || currentUser.username;
const nameEl = document.getElementById('headerUserName');
const avatarEl = document.getElementById('headerUserAvatar');
if (nameEl) nameEl.textContent = dn;
if (avatarEl) avatarEl.textContent = dn.charAt(0).toUpperCase();
renderNavbar();
setTimeout(() => document.querySelector('.fade-in')?.classList.add('visible'), 50);
return true;
}

View File

@@ -0,0 +1,143 @@
/* ===== Education Management ===== */
let educationList = [];
let editingEducationId = null;
async function loadEducation() {
try {
const dateFrom = document.getElementById('eduDateFrom')?.value || '';
const dateTo = document.getElementById('eduDateTo')?.value || '';
const targetType = document.getElementById('eduTargetType')?.value || '';
const params = new URLSearchParams();
if (dateFrom) params.set('date_from', dateFrom);
if (dateTo) params.set('date_to', dateTo);
if (targetType) params.set('target_type', targetType);
const r = await api('/education?' + params.toString());
educationList = r.data || [];
renderEducationList();
} catch (e) {
showToast('교육 목록 로드 실패: ' + e.message, 'error');
}
}
function renderEducationList() {
const tbody = document.getElementById('educationTableBody');
if (!educationList.length) {
tbody.innerHTML = '<tr><td colspan="7" class="text-center text-gray-400 py-8">등록된 안전교육이 없습니다</td></tr>';
return;
}
const typeLabels = { day_labor: '일용공', partner_schedule: '협력업체', manual: '수동등록' };
const statusLabels = { planned: '예정', completed: '완료', cancelled: '취소' };
const statusColors = { planned: 'badge-amber', completed: 'badge-green', cancelled: 'badge-gray' };
tbody.innerHTML = educationList.map(e => {
const attendeeCount = e.attendees ? (typeof e.attendees === 'string' ? JSON.parse(e.attendees) : e.attendees).length : 0;
return `<tr>
<td>${formatDate(e.education_date)}</td>
<td><span class="badge ${e.target_type === 'day_labor' ? 'badge-blue' : e.target_type === 'partner_schedule' ? 'badge-green' : 'badge-gray'}">${typeLabels[e.target_type] || e.target_type}</span></td>
<td>${escapeHtml(e.educator) || '-'}</td>
<td class="text-center">${attendeeCount}명</td>
<td><span class="badge ${statusColors[e.status] || 'badge-gray'}">${statusLabels[e.status] || e.status}</span></td>
<td class="hide-mobile">${escapeHtml(e.notes) || '-'}</td>
<td class="text-right">
<button onclick="openEditEducation(${e.id})" class="text-gray-400 hover:text-gray-600 text-xs" title="수정"><i class="fas fa-pen"></i></button>
<button onclick="doDeleteEducation(${e.id})" class="text-gray-400 hover:text-red-500 text-xs ml-1" title="삭제"><i class="fas fa-trash"></i></button>
</td>
</tr>`;
}).join('');
}
/* ===== Add education modal ===== */
function openAddEducation() {
document.getElementById('addEducationModal').classList.remove('hidden');
}
function closeAddEducation() {
document.getElementById('addEducationModal').classList.add('hidden');
document.getElementById('addEducationForm').reset();
}
async function submitAddEducation(e) {
e.preventDefault();
const attendeesRaw = document.getElementById('newAttendees').value.trim();
const attendees = attendeesRaw ? attendeesRaw.split('\n').map(line => {
const parts = line.split(',').map(s => s.trim());
return { name: parts[0] || '', company: parts[1] || '' };
}).filter(a => a.name) : [];
const data = {
target_type: document.getElementById('newTargetType').value,
education_date: document.getElementById('newEducationDate').value,
educator: document.getElementById('newEducator').value.trim() || null,
attendees: attendees,
status: document.getElementById('newEduStatus').value || 'planned',
notes: document.getElementById('newEduNotes').value.trim() || null,
};
if (!data.education_date) { showToast('교육일은 필수입니다', 'error'); return; }
try {
await api('/education', { method: 'POST', body: JSON.stringify(data) });
showToast('안전교육이 등록되었습니다');
closeAddEducation();
await loadEducation();
} catch (e) { showToast(e.message, 'error'); }
}
/* ===== Edit education ===== */
function openEditEducation(id) {
const edu = educationList.find(x => x.id === id);
if (!edu) return;
editingEducationId = id;
document.getElementById('editTargetType').value = edu.target_type;
document.getElementById('editEducationDate').value = formatDate(edu.education_date);
document.getElementById('editEducator').value = edu.educator || '';
document.getElementById('editEduStatus').value = edu.status;
document.getElementById('editEduNotes').value = edu.notes || '';
const attendees = edu.attendees ? (typeof edu.attendees === 'string' ? JSON.parse(edu.attendees) : edu.attendees) : [];
document.getElementById('editAttendees').value = attendees.map(a => `${a.name}${a.company ? ', ' + a.company : ''}`).join('\n');
document.getElementById('editEducationModal').classList.remove('hidden');
}
function closeEditEducation() {
document.getElementById('editEducationModal').classList.add('hidden');
editingEducationId = null;
}
async function submitEditEducation(e) {
e.preventDefault();
if (!editingEducationId) return;
const attendeesRaw = document.getElementById('editAttendees').value.trim();
const attendees = attendeesRaw ? attendeesRaw.split('\n').map(line => {
const parts = line.split(',').map(s => s.trim());
return { name: parts[0] || '', company: parts[1] || '' };
}).filter(a => a.name) : [];
const data = {
target_type: document.getElementById('editTargetType').value,
education_date: document.getElementById('editEducationDate').value,
educator: document.getElementById('editEducator').value.trim() || null,
attendees: attendees,
status: document.getElementById('editEduStatus').value,
notes: document.getElementById('editEduNotes').value.trim() || null,
};
try {
await api(`/education/${editingEducationId}`, { method: 'PUT', body: JSON.stringify(data) });
showToast('수정되었습니다');
closeEditEducation();
await loadEducation();
} catch (e) { showToast(e.message, 'error'); }
}
async function doDeleteEducation(id) {
if (!confirm('이 교육 기록을 삭제하시겠습니까?')) return;
try {
await api(`/education/${id}`, { method: 'DELETE' });
showToast('삭제되었습니다');
await loadEducation();
} catch (e) { showToast(e.message, 'error'); }
}
function initEducationPage() {
if (!initAuth()) return;
document.getElementById('addEducationForm').addEventListener('submit', submitAddEducation);
document.getElementById('editEducationForm').addEventListener('submit', submitEditEducation);
document.getElementById('eduDateFrom')?.addEventListener('change', loadEducation);
document.getElementById('eduDateTo')?.addEventListener('change', loadEducation);
document.getElementById('eduTargetType')?.addEventListener('change', loadEducation);
loadEducation();
}

View File

@@ -0,0 +1,272 @@
/* ===== Visit Management ===== */
let todayVisits = [];
let editingVisitId = null;
async function loadTodayVisits() {
try {
const r = await api('/daily-visits/today');
const { visits, stats } = r.data;
todayVisits = visits;
renderStats(stats);
renderVisitTable(visits);
} catch (e) {
showToast('데이터 로드 실패: ' + e.message, 'error');
}
}
function renderStats(s) {
document.getElementById('statTotal').textContent = s.total || 0;
document.getElementById('statCheckedIn').textContent = s.checked_in || 0;
document.getElementById('statCheckedOut').textContent = s.checked_out || 0;
document.getElementById('statVisitors').textContent = s.total_visitors || 0;
}
function renderVisitTable(visits) {
const tbody = document.getElementById('visitTableBody');
if (!visits.length) {
tbody.innerHTML = '<tr><td colspan="8" class="text-center text-gray-400 py-8">오늘 방문 기록이 없습니다</td></tr>';
return;
}
tbody.innerHTML = visits.map(v => {
const companyName = v.partner_company_name || v.company_name || '-';
const safetyIcon = v.safety_education_yn
? '<i class="fas fa-check-circle text-orange-500"></i>'
: '<i class="fas fa-exclamation-triangle text-amber-500 safety-warning" title="안전교육 미이수"></i>';
const actions = v.status === 'checked_in'
? `<button onclick="doCheckout(${v.id})" class="text-blue-600 hover:text-blue-800 text-xs px-2 py-1 border border-blue-200 rounded hover:bg-blue-50">체크아웃</button>`
: '';
return `<tr>
<td>${escapeHtml(companyName)}</td>
<td>${escapeHtml(v.visitor_name)}</td>
<td class="text-center">${v.visitor_count}</td>
<td>${purposeBadge(v.purpose)}</td>
<td class="hide-mobile">${safetyIcon}</td>
<td>${formatTime(v.check_in_time)}</td>
<td>${statusBadge(v.status)}</td>
<td class="text-right">
${actions}
<button onclick="openEditVisit(${v.id})" class="text-gray-400 hover:text-gray-600 text-xs ml-1" title="수정"><i class="fas fa-pen"></i></button>
<button onclick="doDeleteVisit(${v.id})" class="text-gray-400 hover:text-red-500 text-xs ml-1" title="삭제"><i class="fas fa-trash"></i></button>
</td>
</tr>`;
}).join('');
}
/* ===== 업체 자동완성 ===== */
let companySearchTimeout = null;
let selectedCompanyId = null;
function initCompanySearch() {
const input = document.getElementById('companySearch');
const dropdown = document.getElementById('companyDropdown');
const manualToggle = document.getElementById('manualCompanyToggle');
const manualInput = document.getElementById('manualCompanyName');
input.addEventListener('input', () => {
clearTimeout(companySearchTimeout);
selectedCompanyId = null;
const q = input.value.trim();
if (q.length < 1) { dropdown.classList.add('hidden'); return; }
companySearchTimeout = setTimeout(async () => {
try {
const r = await api('/partners/search?q=' + encodeURIComponent(q));
const items = r.data || [];
if (items.length === 0) {
dropdown.innerHTML = '<div class="px-3 py-2 text-sm text-gray-400">검색 결과 없음</div>';
} else {
dropdown.innerHTML = items.map(c =>
`<div class="px-3 py-2 text-sm hover:bg-orange-50 cursor-pointer" onclick="selectCompany(${c.id}, '${escapeHtml(c.company_name).replace(/'/g, "\\'")}')">
<span class="font-medium">${escapeHtml(c.company_name)}</span>
${c.business_number ? `<span class="text-gray-400 text-xs ml-2">${c.business_number}</span>` : ''}
</div>`
).join('');
}
dropdown.classList.remove('hidden');
} catch (e) { dropdown.classList.add('hidden'); }
}, 300);
});
input.addEventListener('blur', () => setTimeout(() => dropdown.classList.add('hidden'), 200));
manualToggle.addEventListener('change', () => {
if (manualToggle.checked) {
input.parentElement.classList.add('hidden');
manualInput.parentElement.classList.remove('hidden');
selectedCompanyId = null;
input.value = '';
} else {
input.parentElement.classList.remove('hidden');
manualInput.parentElement.classList.add('hidden');
manualInput.value = '';
}
});
}
function selectCompany(id, name) {
selectedCompanyId = id;
document.getElementById('companySearch').value = name;
document.getElementById('companyDropdown').classList.add('hidden');
}
/* ===== 인원수 +- ===== */
function initCounterButtons() {
document.getElementById('countMinus').addEventListener('click', () => {
const el = document.getElementById('visitorCount');
const v = parseInt(el.value) || 1;
if (v > 1) el.value = v - 1;
});
document.getElementById('countPlus').addEventListener('click', () => {
const el = document.getElementById('visitorCount');
el.value = (parseInt(el.value) || 0) + 1;
});
}
/* ===== 추가정보 접이식 ===== */
function toggleExtra() {
document.getElementById('extraFields').classList.toggle('open');
const icon = document.getElementById('extraToggleIcon');
icon.classList.toggle('fa-chevron-down');
icon.classList.toggle('fa-chevron-up');
}
/* ===== 방문 등록 ===== */
async function submitVisit(e) {
e.preventDefault();
const manualMode = document.getElementById('manualCompanyToggle').checked;
const company_id = manualMode ? null : selectedCompanyId;
const company_name = manualMode ? document.getElementById('manualCompanyName').value.trim() : null;
if (!company_id && !company_name) {
showToast('업체를 선택하거나 입력해주세요', 'error'); return;
}
const data = {
company_id,
company_name: company_name || document.getElementById('companySearch').value.trim(),
visitor_name: document.getElementById('visitorName').value.trim(),
visitor_count: parseInt(document.getElementById('visitorCount').value) || 1,
purpose: document.getElementById('visitPurpose').value,
purpose_detail: document.getElementById('purposeDetail').value.trim() || null,
workplace_name: document.getElementById('workplaceName').value.trim() || null,
safety_education_yn: document.getElementById('safetyCheck').checked,
vehicle_number: document.getElementById('vehicleNumber').value.trim() || null,
notes: document.getElementById('visitNotes').value.trim() || null,
managing_department: document.getElementById('managingDept').value || null,
};
if (!data.visitor_name) { showToast('방문자명을 입력해주세요', 'error'); return; }
if (!data.purpose) { showToast('방문 목적을 선택해주세요', 'error'); return; }
try {
await api('/daily-visits/', { method: 'POST', body: JSON.stringify(data) });
showToast('방문이 등록되었습니다');
document.getElementById('visitForm').reset();
selectedCompanyId = null;
document.getElementById('manualCompanyToggle').checked = false;
document.getElementById('companySearch').parentElement.classList.remove('hidden');
document.getElementById('manualCompanyName').parentElement.classList.add('hidden');
document.getElementById('visitorCount').value = '1';
await loadTodayVisits();
} catch (e) {
showToast(e.message, 'error');
}
}
/* ===== 체크아웃 ===== */
async function doCheckout(id) {
try {
await api(`/daily-visits/${id}/checkout`, { method: 'PUT', body: JSON.stringify({}) });
showToast('체크아웃 완료');
await loadTodayVisits();
} catch (e) { showToast(e.message, 'error'); }
}
async function doBulkCheckout() {
const checkedIn = todayVisits.filter(v => v.status === 'checked_in');
if (checkedIn.length === 0) { showToast('체크인 중인 방문이 없습니다', 'error'); return; }
if (!confirm(`체크인 중인 ${checkedIn.length}건을 모두 체크아웃 하시겠습니까?`)) return;
try {
const r = await api('/daily-visits/bulk-checkout', { method: 'POST', body: JSON.stringify({}) });
showToast(`${r.data.affected}건 체크아웃 완료`);
await loadTodayVisits();
} catch (e) { showToast(e.message, 'error'); }
}
/* ===== 수정 ===== */
function openEditVisit(id) {
const v = todayVisits.find(x => x.id === id);
if (!v) return;
editingVisitId = id;
document.getElementById('editVisitorName').value = v.visitor_name;
document.getElementById('editVisitorCount').value = v.visitor_count;
document.getElementById('editPurpose').value = v.purpose;
document.getElementById('editPurposeDetail').value = v.purpose_detail || '';
document.getElementById('editWorkplace').value = v.workplace_name || '';
document.getElementById('editSafetyCheck').checked = v.safety_education_yn;
document.getElementById('editVehicle').value = v.vehicle_number || '';
document.getElementById('editNotes').value = v.notes || '';
document.getElementById('editVisitModal').classList.remove('hidden');
}
function closeEditVisit() {
document.getElementById('editVisitModal').classList.add('hidden');
editingVisitId = null;
}
async function submitEditVisit(e) {
e.preventDefault();
if (!editingVisitId) return;
const data = {
visitor_name: document.getElementById('editVisitorName').value.trim(),
visitor_count: parseInt(document.getElementById('editVisitorCount').value) || 1,
purpose: document.getElementById('editPurpose').value,
purpose_detail: document.getElementById('editPurposeDetail').value.trim() || null,
workplace_name: document.getElementById('editWorkplace').value.trim() || null,
safety_education_yn: document.getElementById('editSafetyCheck').checked,
vehicle_number: document.getElementById('editVehicle').value.trim() || null,
notes: document.getElementById('editNotes').value.trim() || null,
};
try {
await api(`/daily-visits/${editingVisitId}`, { method: 'PUT', body: JSON.stringify(data) });
showToast('수정되었습니다');
closeEditVisit();
await loadTodayVisits();
} catch (e) { showToast(e.message, 'error'); }
}
/* ===== 삭제 ===== */
async function doDeleteVisit(id) {
if (!confirm('이 방문 기록을 삭제하시겠습니까?')) return;
try {
await api(`/daily-visits/${id}`, { method: 'DELETE' });
showToast('삭제되었습니다');
await loadTodayVisits();
} catch (e) { showToast(e.message, 'error'); }
}
/* ===== CSV 내보내기 ===== */
async function exportVisits() {
const token = getToken();
const dateFrom = document.getElementById('exportDateFrom')?.value || '';
const dateTo = document.getElementById('exportDateTo')?.value || '';
let url = API_BASE + '/daily-visits/export?';
if (dateFrom) url += 'date_from=' + dateFrom + '&';
if (dateTo) url += 'date_to=' + dateTo + '&';
const res = await fetch(url, { headers: { 'Authorization': `Bearer ${token}` } });
if (!res.ok) { showToast('내보내기 실패', 'error'); return; }
const blob = await res.blob();
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = `visits_${dateFrom || 'all'}_${dateTo || 'all'}.csv`;
a.click();
}
/* ===== Init ===== */
function initVisitPage() {
if (!initAuth()) return;
initCompanySearch();
initCounterButtons();
document.getElementById('visitForm').addEventListener('submit', submitVisit);
document.getElementById('editVisitForm').addEventListener('submit', submitEditVisit);
loadTodayVisits();
}