Files
tk-factory-services/tksafety/api/models/checklistModel.js
Hyungi Ahn 2fc4179052 fix(tksafety): DB 스키마 불일치로 인한 API 500 에러 수정
- departments.name → department_name (3곳)
- users → sso_users 테이블 참조 수정 (7곳)
- tbm_sessions.start_time → created_at (존재하지 않는 컬럼)
- tbm_team_assignments JOIN: ta.user_id → ta.worker_id
- workers leader JOIN: leader.worker_id → leader.user_id
- tbm_weather_conditions → weather_conditions 테이블명 수정

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 14:51:06 +09:00

68 lines
2.6 KiB
JavaScript

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 };