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'); return rows; } // Get check by ID async function getCheckById(checkId) { const db = getPool(); const [rows] = await db.query('SELECT * FROM tbm_safety_checks WHERE check_id = ?', [checkId]); return rows[0]; } // Create check 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 [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] ); return result.insertId; } // Update check 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 [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] ); return result; } // Delete check async function deleteCheck(checkId) { const db = getPool(); const [result] = await db.query('DELETE FROM tbm_safety_checks WHERE check_id = ?', [checkId]); return result; } // Get weather conditions async function getWeatherConditions() { const db = getPool(); const [rows] = await db.query('SELECT * FROM weather_conditions ORDER BY display_order, condition_code'); return rows; } // Get work types async function getWorkTypes() { const db = getPool(); const [rows] = await db.query('SELECT * 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]); return rows; } module.exports = { getAllChecks, getCheckById, createCheck, updateCheck, deleteCheck, getWeatherConditions, getWorkTypes, getTasksByWorkType };