Files
tk-factory-services/tksafety/api/models/educationModel.js
Hyungi Ahn b800792152 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>
2026-03-12 17:42:59 +09:00

81 lines
3.4 KiB
JavaScript

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