diff --git a/tksafety/api/controllers/checklistController.js b/tksafety/api/controllers/checklistController.js index ddbb55c..7f1d3d6 100644 --- a/tksafety/api/controllers/checklistController.js +++ b/tksafety/api/controllers/checklistController.js @@ -23,7 +23,7 @@ exports.getCheckById = async (req, res) => { exports.createCheck = async (req, res) => { try { - if (!req.body.check_item) return res.status(400).json({ success: false, error: 'check_item은 필수입니다' }); + if (!req.body.item_content && !req.body.check_item) return res.status(400).json({ success: false, error: '점검 항목 내용은 필수입니다' }); const checkId = await checklistModel.createCheck(req.body); res.status(201).json({ success: true, message: '항목이 추가되었습니다', data: { check_id: checkId } }); } catch (err) { diff --git a/tksafety/api/models/checklistModel.js b/tksafety/api/models/checklistModel.js index c03446f..a89cb7a 100644 --- a/tksafety/api/models/checklistModel.js +++ b/tksafety/api/models/checklistModel.js @@ -3,7 +3,19 @@ const { getPool } = require('../middleware/auth'); // Get all safety checks async function getAllChecks() { const db = getPool(); - const [rows] = await db.query('SELECT * FROM tbm_safety_checks ORDER BY check_type, check_category, display_order, check_id'); + const [rows] = await db.query(` + SELECT c.check_id AS item_id, c.check_type AS item_type, + c.check_item AS item_content, c.check_category AS category, + c.display_order, c.is_active, c.is_required, c.description, + c.weather_condition, wc.condition_name AS weather_condition_name, + wc.condition_code AS weather_condition_id, + c.task_id, t.task_name, t.work_type_id, wt.name AS work_type_name + FROM tbm_safety_checks c + LEFT JOIN tasks t ON c.task_id = t.task_id + LEFT JOIN work_types wt ON t.work_type_id = wt.id + LEFT JOIN weather_conditions wc ON c.weather_condition = wc.condition_code + ORDER BY c.check_type, c.check_category, c.display_order, c.check_id + `); return rows; } @@ -14,24 +26,32 @@ async function getCheckById(checkId) { return rows[0]; } -// Create check +// Create check (frontend field names → DB column mapping) async function createCheck(data) { const db = getPool(); - const { check_type, check_category, check_item, description, is_required, display_order, weather_condition, task_id } = data; + const check_type = data.item_type || data.check_type; + const check_category = data.category || data.check_category || null; + const check_item = data.item_content || data.check_item; + const weather_condition = data.weather_condition_id || data.weather_condition || null; + const { description, is_required, display_order, task_id } = data; const [result] = await db.query( 'INSERT INTO tbm_safety_checks (check_type, check_category, check_item, description, is_required, display_order, weather_condition, task_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)', - [check_type, check_category || null, check_item, description || null, is_required ? 1 : 0, display_order || 0, weather_condition || null, task_id || null] + [check_type, check_category, check_item, description || null, is_required ? 1 : 0, display_order || 0, weather_condition, task_id || null] ); return result.insertId; } -// Update check +// Update check (frontend field names → DB column mapping) async function updateCheck(checkId, data) { const db = getPool(); - const { check_type, check_category, check_item, description, is_required, display_order, weather_condition, task_id } = data; + const check_type = data.item_type || data.check_type; + const check_category = data.category || data.check_category || null; + const check_item = data.item_content || data.check_item; + const weather_condition = data.weather_condition_id || data.weather_condition || null; + const { description, is_required, display_order, task_id } = data; const [result] = await db.query( 'UPDATE tbm_safety_checks SET check_type = ?, check_category = ?, check_item = ?, description = ?, is_required = ?, display_order = ?, weather_condition = ?, task_id = ? WHERE check_id = ?', - [check_type, check_category || null, check_item, description || null, is_required ? 1 : 0, display_order || 0, weather_condition || null, task_id || null, checkId] + [check_type, check_category, check_item, description || null, is_required ? 1 : 0, display_order || 0, weather_condition, task_id || null, checkId] ); return result; } @@ -53,14 +73,14 @@ async function getWeatherConditions() { // Get work types async function getWorkTypes() { const db = getPool(); - const [rows] = await db.query('SELECT * FROM work_types ORDER BY name'); + const [rows] = await db.query('SELECT id AS work_type_id, name AS work_type_name, category, description FROM work_types ORDER BY name'); return rows; } // Get tasks by work type async function getTasksByWorkType(workTypeId) { const db = getPool(); - const [rows] = await db.query('SELECT * FROM tasks WHERE work_type_id = ? ORDER BY task_name', [workTypeId]); + const [rows] = await db.query('SELECT task_id, task_name, work_type_id, description, is_active FROM tasks WHERE work_type_id = ? AND is_active = TRUE ORDER BY task_name', [workTypeId]); return rows; } diff --git a/tksafety/web/checklist.html b/tksafety/web/checklist.html index 2c29812..7ab3160 100644 --- a/tksafety/web/checklist.html +++ b/tksafety/web/checklist.html @@ -122,11 +122,18 @@