feat: 알림 시스템 및 시설설비 관리 기능 구현
- 알림 시스템 구축 (navbar 알림 아이콘, 드롭다운) - 알림 수신자 설정 기능 (계정관리 페이지) - 시설설비 관리 페이지 추가 (수리 워크플로우) - 수리 신청 → 접수 → 처리중 → 완료 상태 관리 - 사이드바 메뉴 구조 개선 (공장 관리 카테고리) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -51,6 +51,8 @@ function setupRoutes(app) {
|
||||
const workIssueRoutes = require('../routes/workIssueRoutes');
|
||||
const departmentRoutes = require('../routes/departmentRoutes');
|
||||
const patrolRoutes = require('../routes/patrolRoutes');
|
||||
const notificationRoutes = require('../routes/notificationRoutes');
|
||||
const notificationRecipientRoutes = require('../routes/notificationRecipientRoutes');
|
||||
|
||||
// Rate Limiters 설정
|
||||
const rateLimit = require('express-rate-limit');
|
||||
@@ -157,6 +159,8 @@ function setupRoutes(app) {
|
||||
app.use('/api/work-issues', workIssueRoutes); // 문제 신고 시스템
|
||||
app.use('/api/departments', departmentRoutes); // 부서 관리
|
||||
app.use('/api/patrol', patrolRoutes); // 일일순회점검 시스템
|
||||
app.use('/api/notifications', notificationRoutes); // 알림 시스템
|
||||
app.use('/api/notification-recipients', notificationRecipientRoutes); // 알림 수신자 설정
|
||||
app.use('/api', uploadBgRoutes);
|
||||
|
||||
// Swagger API 문서
|
||||
|
||||
@@ -903,6 +903,42 @@ const EquipmentController = {
|
||||
message: '서버 오류가 발생했습니다.'
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// ADD REPAIR CATEGORY - 새 수리 항목 추가
|
||||
addRepairCategory: (req, res) => {
|
||||
try {
|
||||
const { item_name } = req.body;
|
||||
|
||||
if (!item_name || !item_name.trim()) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: '수리 유형 이름을 입력하세요.'
|
||||
});
|
||||
}
|
||||
|
||||
EquipmentModel.addRepairCategory(item_name.trim(), (error, result) => {
|
||||
if (error) {
|
||||
console.error('수리 항목 추가 오류:', error);
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: '수리 항목 추가 중 오류가 발생했습니다.'
|
||||
});
|
||||
}
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
message: result.isNew ? '새 수리 유형이 추가되었습니다.' : '기존 수리 유형을 사용합니다.',
|
||||
data: result
|
||||
});
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('수리 항목 추가 오류:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '서버 오류가 발생했습니다.'
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
165
api.hyungi.net/controllers/notificationController.js
Normal file
165
api.hyungi.net/controllers/notificationController.js
Normal file
@@ -0,0 +1,165 @@
|
||||
// controllers/notificationController.js
|
||||
const notificationModel = require('../models/notificationModel');
|
||||
|
||||
const notificationController = {
|
||||
// 읽지 않은 알림 조회
|
||||
async getUnread(req, res) {
|
||||
try {
|
||||
const userId = req.user?.id || null;
|
||||
const notifications = await notificationModel.getUnread(userId);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: notifications
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('읽지 않은 알림 조회 오류:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '알림 조회 중 오류가 발생했습니다.'
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// 전체 알림 조회
|
||||
async getAll(req, res) {
|
||||
try {
|
||||
const userId = req.user?.id || null;
|
||||
const page = parseInt(req.query.page) || 1;
|
||||
const limit = parseInt(req.query.limit) || 20;
|
||||
|
||||
const result = await notificationModel.getAll(userId, page, limit);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: result.notifications,
|
||||
pagination: {
|
||||
total: result.total,
|
||||
page: result.page,
|
||||
limit: result.limit,
|
||||
totalPages: Math.ceil(result.total / result.limit)
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('알림 목록 조회 오류:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '알림 조회 중 오류가 발생했습니다.'
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// 읽지 않은 알림 개수
|
||||
async getUnreadCount(req, res) {
|
||||
try {
|
||||
const userId = req.user?.id || null;
|
||||
const count = await notificationModel.getUnreadCount(userId);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: { count }
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('알림 개수 조회 오류:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '알림 개수 조회 중 오류가 발생했습니다.'
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// 알림 읽음 처리
|
||||
async markAsRead(req, res) {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const success = await notificationModel.markAsRead(id);
|
||||
|
||||
res.json({
|
||||
success,
|
||||
message: success ? '알림을 읽음 처리했습니다.' : '알림을 찾을 수 없습니다.'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('알림 읽음 처리 오류:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '알림 처리 중 오류가 발생했습니다.'
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// 모든 알림 읽음 처리
|
||||
async markAllAsRead(req, res) {
|
||||
try {
|
||||
const userId = req.user?.id || null;
|
||||
const count = await notificationModel.markAllAsRead(userId);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: `${count}개의 알림을 읽음 처리했습니다.`,
|
||||
data: { count }
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('전체 읽음 처리 오류:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '알림 처리 중 오류가 발생했습니다.'
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// 알림 삭제
|
||||
async delete(req, res) {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const success = await notificationModel.delete(id);
|
||||
|
||||
res.json({
|
||||
success,
|
||||
message: success ? '알림을 삭제했습니다.' : '알림을 찾을 수 없습니다.'
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('알림 삭제 오류:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '알림 삭제 중 오류가 발생했습니다.'
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// 알림 생성 (시스템용)
|
||||
async create(req, res) {
|
||||
try {
|
||||
const { type, title, message, link_url, user_id } = req.body;
|
||||
|
||||
if (!title) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: '알림 제목은 필수입니다.'
|
||||
});
|
||||
}
|
||||
|
||||
const notificationId = await notificationModel.create({
|
||||
user_id,
|
||||
type,
|
||||
title,
|
||||
message,
|
||||
link_url,
|
||||
created_by: req.user?.id
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: '알림이 생성되었습니다.',
|
||||
data: { notification_id: notificationId }
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('알림 생성 오류:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '알림 생성 중 오류가 발생했습니다.'
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = notificationController;
|
||||
@@ -0,0 +1,91 @@
|
||||
// controllers/notificationRecipientController.js
|
||||
const notificationRecipientModel = require('../models/notificationRecipientModel');
|
||||
|
||||
const notificationRecipientController = {
|
||||
// 알림 유형 목록
|
||||
getTypes: async (req, res) => {
|
||||
try {
|
||||
const types = notificationRecipientModel.getTypes();
|
||||
res.json({ success: true, data: types });
|
||||
} catch (error) {
|
||||
console.error('알림 유형 조회 오류:', error);
|
||||
res.status(500).json({ success: false, error: '알림 유형 조회 실패' });
|
||||
}
|
||||
},
|
||||
|
||||
// 전체 수신자 목록 (유형별 그룹화)
|
||||
getAll: async (req, res) => {
|
||||
try {
|
||||
console.log('🔔 알림 수신자 목록 조회 시작');
|
||||
const recipients = await notificationRecipientModel.getAll();
|
||||
console.log('✅ 알림 수신자 목록 조회 완료:', recipients);
|
||||
res.json({ success: true, data: recipients });
|
||||
} catch (error) {
|
||||
console.error('❌ 수신자 목록 조회 오류:', error.message);
|
||||
console.error('❌ 스택:', error.stack);
|
||||
res.status(500).json({ success: false, error: '수신자 목록 조회 실패', detail: error.message });
|
||||
}
|
||||
},
|
||||
|
||||
// 유형별 수신자 조회
|
||||
getByType: async (req, res) => {
|
||||
try {
|
||||
const { type } = req.params;
|
||||
const recipients = await notificationRecipientModel.getByType(type);
|
||||
res.json({ success: true, data: recipients });
|
||||
} catch (error) {
|
||||
console.error('수신자 조회 오류:', error);
|
||||
res.status(500).json({ success: false, error: '수신자 조회 실패' });
|
||||
}
|
||||
},
|
||||
|
||||
// 수신자 추가
|
||||
add: async (req, res) => {
|
||||
try {
|
||||
const { notification_type, user_id } = req.body;
|
||||
|
||||
if (!notification_type || !user_id) {
|
||||
return res.status(400).json({ success: false, error: '알림 유형과 사용자 ID가 필요합니다.' });
|
||||
}
|
||||
|
||||
await notificationRecipientModel.add(notification_type, user_id, req.user?.user_id);
|
||||
res.json({ success: true, message: '수신자가 추가되었습니다.' });
|
||||
} catch (error) {
|
||||
console.error('수신자 추가 오류:', error);
|
||||
res.status(500).json({ success: false, error: '수신자 추가 실패' });
|
||||
}
|
||||
},
|
||||
|
||||
// 수신자 제거
|
||||
remove: async (req, res) => {
|
||||
try {
|
||||
const { type, userId } = req.params;
|
||||
|
||||
await notificationRecipientModel.remove(type, userId);
|
||||
res.json({ success: true, message: '수신자가 제거되었습니다.' });
|
||||
} catch (error) {
|
||||
console.error('수신자 제거 오류:', error);
|
||||
res.status(500).json({ success: false, error: '수신자 제거 실패' });
|
||||
}
|
||||
},
|
||||
|
||||
// 유형별 수신자 일괄 설정
|
||||
setRecipients: async (req, res) => {
|
||||
try {
|
||||
const { type } = req.params;
|
||||
const { user_ids } = req.body;
|
||||
|
||||
if (!Array.isArray(user_ids)) {
|
||||
return res.status(400).json({ success: false, error: 'user_ids 배열이 필요합니다.' });
|
||||
}
|
||||
|
||||
await notificationRecipientModel.setRecipients(type, user_ids, req.user?.user_id);
|
||||
res.json({ success: true, message: '수신자가 설정되었습니다.' });
|
||||
} catch (error) {
|
||||
console.error('수신자 설정 오류:', error);
|
||||
res.status(500).json({ success: false, error: '수신자 설정 실패' });
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = notificationRecipientController;
|
||||
@@ -0,0 +1,23 @@
|
||||
-- 알림 수신자 설정 테이블
|
||||
-- 알림 유형별로 지정된 사용자에게만 알림이 전송됨
|
||||
|
||||
CREATE TABLE IF NOT EXISTS notification_recipients (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
notification_type ENUM('repair', 'safety', 'nonconformity', 'equipment', 'maintenance', 'system') NOT NULL COMMENT '알림 유형',
|
||||
user_id INT NOT NULL COMMENT '수신자 ID',
|
||||
is_active BOOLEAN DEFAULT TRUE COMMENT '활성 여부',
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
created_by INT NULL COMMENT '등록자',
|
||||
UNIQUE KEY unique_type_user (notification_type, user_id),
|
||||
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE,
|
||||
INDEX idx_nr_type (notification_type),
|
||||
INDEX idx_nr_active (is_active)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='알림 수신자 설정';
|
||||
|
||||
-- 알림 유형 설명:
|
||||
-- repair: 설비 수리 신청
|
||||
-- safety: 안전 신고
|
||||
-- nonconformity: 부적합 신고
|
||||
-- equipment: 설비 관련
|
||||
-- maintenance: 정기점검
|
||||
-- system: 시스템 알림
|
||||
@@ -0,0 +1,46 @@
|
||||
-- 알림 시스템 테이블 생성
|
||||
-- 실행: docker exec -i tkfb_db mysql -u hyungi -p'your_password' hyungi < db/migrations/20260205004000_create_notifications.sql
|
||||
|
||||
-- ============================================
|
||||
-- STEP 1: notifications 테이블 생성
|
||||
-- ============================================
|
||||
|
||||
CREATE TABLE IF NOT EXISTS notifications (
|
||||
notification_id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
user_id INT NULL COMMENT '특정 사용자에게만 표시 (NULL이면 전체)',
|
||||
type ENUM('repair', 'safety', 'system', 'equipment', 'maintenance') NOT NULL DEFAULT 'system',
|
||||
title VARCHAR(200) NOT NULL,
|
||||
message TEXT,
|
||||
link_url VARCHAR(500) COMMENT '클릭시 이동할 URL',
|
||||
reference_type VARCHAR(50) COMMENT '연관 테이블 (equipment_repair_requests 등)',
|
||||
reference_id INT COMMENT '연관 레코드 ID',
|
||||
is_read BOOLEAN DEFAULT FALSE,
|
||||
read_at DATETIME NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
created_by INT NULL,
|
||||
INDEX idx_notifications_user (user_id),
|
||||
INDEX idx_notifications_type (type),
|
||||
INDEX idx_notifications_is_read (is_read),
|
||||
INDEX idx_notifications_created (created_at DESC)
|
||||
);
|
||||
|
||||
-- 수리 요청 테이블 수정 (알림 연동을 위해)
|
||||
-- equipment_repair_requests 테이블이 없으면 생성
|
||||
CREATE TABLE IF NOT EXISTS equipment_repair_requests (
|
||||
request_id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
equipment_id INT UNSIGNED NOT NULL,
|
||||
repair_type VARCHAR(100) NOT NULL,
|
||||
description TEXT,
|
||||
urgency ENUM('low', 'normal', 'high', 'urgent') DEFAULT 'normal',
|
||||
status ENUM('pending', 'in_progress', 'completed', 'cancelled') DEFAULT 'pending',
|
||||
requested_by INT,
|
||||
assigned_to INT NULL,
|
||||
completed_at DATETIME NULL,
|
||||
notes TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (equipment_id) REFERENCES equipments(equipment_id) ON DELETE CASCADE,
|
||||
INDEX idx_err_equipment (equipment_id),
|
||||
INDEX idx_err_status (status),
|
||||
INDEX idx_err_created (created_at DESC)
|
||||
);
|
||||
@@ -1,5 +1,6 @@
|
||||
// models/equipmentModel.js
|
||||
const { getDb } = require('../dbPool');
|
||||
const notificationModel = require('./notificationModel');
|
||||
|
||||
const EquipmentModel = {
|
||||
// CREATE - 설비 생성
|
||||
@@ -823,6 +824,20 @@ const EquipmentModel = {
|
||||
['repair_needed', requestData.equipment_id]
|
||||
);
|
||||
|
||||
// 알림 생성
|
||||
try {
|
||||
await notificationModel.createRepairNotification({
|
||||
equipment_id: requestData.equipment_id,
|
||||
equipment_name: requestData.equipment_name || '설비',
|
||||
repair_type: requestData.repair_type || '일반 수리',
|
||||
request_id: result.insertId,
|
||||
created_by: requestData.reported_by
|
||||
});
|
||||
} catch (notifError) {
|
||||
console.error('알림 생성 실패:', notifError);
|
||||
// 알림 생성 실패해도 수리 신청은 성공으로 처리
|
||||
}
|
||||
|
||||
callback(null, {
|
||||
report_id: result.insertId,
|
||||
equipment_id: requestData.equipment_id,
|
||||
@@ -881,6 +896,53 @@ const EquipmentModel = {
|
||||
} catch (error) {
|
||||
callback(error);
|
||||
}
|
||||
},
|
||||
|
||||
// ADD REPAIR CATEGORY - 새 수리 항목 추가
|
||||
addRepairCategory: async (itemName, callback) => {
|
||||
try {
|
||||
const db = await getDb();
|
||||
|
||||
// 설비 수리 카테고리 ID 조회
|
||||
const [categories] = await db.query(
|
||||
"SELECT category_id FROM issue_report_categories WHERE category_name = '설비 수리'"
|
||||
);
|
||||
|
||||
if (categories.length === 0) {
|
||||
return callback(new Error('설비 수리 카테고리가 없습니다.'));
|
||||
}
|
||||
|
||||
const categoryId = categories[0].category_id;
|
||||
|
||||
// 중복 확인
|
||||
const [existing] = await db.query(
|
||||
'SELECT item_id FROM issue_report_items WHERE category_id = ? AND item_name = ?',
|
||||
[categoryId, itemName]
|
||||
);
|
||||
|
||||
if (existing.length > 0) {
|
||||
// 이미 존재하면 해당 ID 반환
|
||||
return callback(null, { item_id: existing[0].item_id, item_name: itemName, isNew: false });
|
||||
}
|
||||
|
||||
// 다음 display_order 구하기
|
||||
const [maxOrder] = await db.query(
|
||||
'SELECT MAX(display_order) as max_order FROM issue_report_items WHERE category_id = ?',
|
||||
[categoryId]
|
||||
);
|
||||
const nextOrder = (maxOrder[0].max_order || 0) + 1;
|
||||
|
||||
// 새 항목 추가
|
||||
const [result] = await db.query(
|
||||
`INSERT INTO issue_report_items (category_id, item_name, display_order, is_active)
|
||||
VALUES (?, ?, ?, 1)`,
|
||||
[categoryId, itemName, nextOrder]
|
||||
);
|
||||
|
||||
callback(null, { item_id: result.insertId, item_name: itemName, isNew: true });
|
||||
} catch (error) {
|
||||
callback(error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
197
api.hyungi.net/models/notificationModel.js
Normal file
197
api.hyungi.net/models/notificationModel.js
Normal file
@@ -0,0 +1,197 @@
|
||||
// models/notificationModel.js
|
||||
const { getDb } = require('../dbPool');
|
||||
|
||||
// 순환 참조를 피하기 위해 함수 내에서 require
|
||||
async function getRecipientIds(notificationType) {
|
||||
const db = await getDb();
|
||||
const [rows] = await db.query(
|
||||
`SELECT user_id FROM notification_recipients
|
||||
WHERE notification_type = ? AND is_active = 1`,
|
||||
[notificationType]
|
||||
);
|
||||
return rows.map(r => r.user_id);
|
||||
}
|
||||
|
||||
const notificationModel = {
|
||||
// 알림 생성
|
||||
async create(notificationData) {
|
||||
const db = await getDb();
|
||||
const { user_id, type, title, message, link_url, reference_type, reference_id, created_by } = notificationData;
|
||||
|
||||
const [result] = await db.query(
|
||||
`INSERT INTO notifications (user_id, type, title, message, link_url, reference_type, reference_id, created_by)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
[user_id || null, type || 'system', title, message || null, link_url || null, reference_type || null, reference_id || null, created_by || null]
|
||||
);
|
||||
|
||||
return result.insertId;
|
||||
},
|
||||
|
||||
// 읽지 않은 알림 조회 (특정 사용자 또는 전체)
|
||||
async getUnread(userId = null) {
|
||||
const db = await getDb();
|
||||
const [rows] = await db.query(
|
||||
`SELECT * FROM notifications
|
||||
WHERE is_read = 0
|
||||
AND (user_id IS NULL OR user_id = ?)
|
||||
ORDER BY created_at DESC
|
||||
LIMIT 50`,
|
||||
[userId || 0]
|
||||
);
|
||||
return rows;
|
||||
},
|
||||
|
||||
// 전체 알림 조회 (페이징)
|
||||
async getAll(userId = null, page = 1, limit = 20) {
|
||||
const db = await getDb();
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
const [rows] = await db.query(
|
||||
`SELECT * FROM notifications
|
||||
WHERE (user_id IS NULL OR user_id = ?)
|
||||
ORDER BY created_at DESC
|
||||
LIMIT ? OFFSET ?`,
|
||||
[userId || 0, limit, offset]
|
||||
);
|
||||
|
||||
const [[{ total }]] = await db.query(
|
||||
`SELECT COUNT(*) as total FROM notifications
|
||||
WHERE (user_id IS NULL OR user_id = ?)`,
|
||||
[userId || 0]
|
||||
);
|
||||
|
||||
return { notifications: rows, total, page, limit };
|
||||
},
|
||||
|
||||
// 알림 읽음 처리
|
||||
async markAsRead(notificationId) {
|
||||
const db = await getDb();
|
||||
const [result] = await db.query(
|
||||
`UPDATE notifications SET is_read = 1, read_at = NOW() WHERE notification_id = ?`,
|
||||
[notificationId]
|
||||
);
|
||||
return result.affectedRows > 0;
|
||||
},
|
||||
|
||||
// 모든 알림 읽음 처리
|
||||
async markAllAsRead(userId = null) {
|
||||
const db = await getDb();
|
||||
const [result] = await db.query(
|
||||
`UPDATE notifications SET is_read = 1, read_at = NOW()
|
||||
WHERE is_read = 0 AND (user_id IS NULL OR user_id = ?)`,
|
||||
[userId || 0]
|
||||
);
|
||||
return result.affectedRows;
|
||||
},
|
||||
|
||||
// 알림 삭제
|
||||
async delete(notificationId) {
|
||||
const db = await getDb();
|
||||
const [result] = await db.query(
|
||||
`DELETE FROM notifications WHERE notification_id = ?`,
|
||||
[notificationId]
|
||||
);
|
||||
return result.affectedRows > 0;
|
||||
},
|
||||
|
||||
// 오래된 알림 삭제 (30일 이상)
|
||||
async deleteOld(days = 30) {
|
||||
const db = await getDb();
|
||||
const [result] = await db.query(
|
||||
`DELETE FROM notifications WHERE created_at < DATE_SUB(NOW(), INTERVAL ? DAY)`,
|
||||
[days]
|
||||
);
|
||||
return result.affectedRows;
|
||||
},
|
||||
|
||||
// 읽지 않은 알림 개수
|
||||
async getUnreadCount(userId = null) {
|
||||
const db = await getDb();
|
||||
const [[{ count }]] = await db.query(
|
||||
`SELECT COUNT(*) as count FROM notifications
|
||||
WHERE is_read = 0 AND (user_id IS NULL OR user_id = ?)`,
|
||||
[userId || 0]
|
||||
);
|
||||
return count;
|
||||
},
|
||||
|
||||
// 수리 신청 알림 생성 헬퍼 (지정된 수신자에게 전송)
|
||||
async createRepairNotification(repairData) {
|
||||
const { equipment_id, equipment_name, repair_type, request_id, created_by } = repairData;
|
||||
|
||||
// 수리 알림 수신자 목록 가져오기
|
||||
const recipientIds = await getRecipientIds('repair');
|
||||
|
||||
if (recipientIds.length === 0) {
|
||||
// 수신자가 지정되지 않은 경우 전체 알림 (user_id = null)
|
||||
return await this.create({
|
||||
type: 'repair',
|
||||
title: `수리 신청: ${equipment_name || '설비'}`,
|
||||
message: `${repair_type} 수리가 신청되었습니다.`,
|
||||
link_url: `/pages/admin/repair-management.html`,
|
||||
reference_type: 'work_issue_reports',
|
||||
reference_id: request_id,
|
||||
created_by
|
||||
});
|
||||
}
|
||||
|
||||
// 지정된 수신자 각각에게 알림 생성
|
||||
const results = [];
|
||||
for (const userId of recipientIds) {
|
||||
const notificationId = await this.create({
|
||||
user_id: userId,
|
||||
type: 'repair',
|
||||
title: `수리 신청: ${equipment_name || '설비'}`,
|
||||
message: `${repair_type} 수리가 신청되었습니다.`,
|
||||
link_url: `/pages/admin/repair-management.html`,
|
||||
reference_type: 'work_issue_reports',
|
||||
reference_id: request_id,
|
||||
created_by
|
||||
});
|
||||
results.push(notificationId);
|
||||
}
|
||||
|
||||
return results;
|
||||
},
|
||||
|
||||
// 일반 알림 생성 (유형별 지정된 수신자에게 전송)
|
||||
async createTypedNotification(notificationData) {
|
||||
const { type, title, message, link_url, reference_type, reference_id, created_by } = notificationData;
|
||||
|
||||
// 해당 유형의 수신자 목록 가져오기
|
||||
const recipientIds = await getRecipientIds(type);
|
||||
|
||||
if (recipientIds.length === 0) {
|
||||
// 수신자가 지정되지 않은 경우 전체 알림
|
||||
return await this.create({
|
||||
type,
|
||||
title,
|
||||
message,
|
||||
link_url,
|
||||
reference_type,
|
||||
reference_id,
|
||||
created_by
|
||||
});
|
||||
}
|
||||
|
||||
// 지정된 수신자 각각에게 알림 생성
|
||||
const results = [];
|
||||
for (const userId of recipientIds) {
|
||||
const notificationId = await this.create({
|
||||
user_id: userId,
|
||||
type,
|
||||
title,
|
||||
message,
|
||||
link_url,
|
||||
reference_type,
|
||||
reference_id,
|
||||
created_by
|
||||
});
|
||||
results.push(notificationId);
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = notificationModel;
|
||||
146
api.hyungi.net/models/notificationRecipientModel.js
Normal file
146
api.hyungi.net/models/notificationRecipientModel.js
Normal file
@@ -0,0 +1,146 @@
|
||||
// models/notificationRecipientModel.js
|
||||
const { getDb } = require('../dbPool');
|
||||
|
||||
const NOTIFICATION_TYPES = {
|
||||
repair: '설비 수리',
|
||||
safety: '안전 신고',
|
||||
nonconformity: '부적합 신고',
|
||||
equipment: '설비 관련',
|
||||
maintenance: '정기점검',
|
||||
system: '시스템'
|
||||
};
|
||||
|
||||
const notificationRecipientModel = {
|
||||
// 알림 유형 목록 가져오기
|
||||
getTypes() {
|
||||
return NOTIFICATION_TYPES;
|
||||
},
|
||||
|
||||
// 유형별 수신자 목록 조회
|
||||
async getByType(notificationType) {
|
||||
const db = await getDb();
|
||||
const [rows] = await db.query(
|
||||
`SELECT nr.*, u.username, u.name as user_name, r.name as role
|
||||
FROM notification_recipients nr
|
||||
JOIN users u ON nr.user_id = u.user_id
|
||||
LEFT JOIN roles r ON u.role_id = r.id
|
||||
WHERE nr.notification_type = ? AND nr.is_active = 1
|
||||
ORDER BY u.name`,
|
||||
[notificationType]
|
||||
);
|
||||
return rows;
|
||||
},
|
||||
|
||||
// 전체 수신자 목록 조회 (유형별 그룹화)
|
||||
async getAll() {
|
||||
const db = await getDb();
|
||||
const [rows] = await db.query(
|
||||
`SELECT nr.*, u.username, u.name as user_name, r.name as role
|
||||
FROM notification_recipients nr
|
||||
JOIN users u ON nr.user_id = u.user_id
|
||||
LEFT JOIN roles r ON u.role_id = r.id
|
||||
WHERE nr.is_active = 1
|
||||
ORDER BY nr.notification_type, u.name`
|
||||
);
|
||||
|
||||
// 유형별로 그룹화
|
||||
const grouped = {};
|
||||
for (const type in NOTIFICATION_TYPES) {
|
||||
grouped[type] = {
|
||||
label: NOTIFICATION_TYPES[type],
|
||||
recipients: []
|
||||
};
|
||||
}
|
||||
|
||||
rows.forEach(row => {
|
||||
if (grouped[row.notification_type]) {
|
||||
grouped[row.notification_type].recipients.push(row);
|
||||
}
|
||||
});
|
||||
|
||||
return grouped;
|
||||
},
|
||||
|
||||
// 수신자 추가
|
||||
async add(notificationType, userId, createdBy = null) {
|
||||
const db = await getDb();
|
||||
const [result] = await db.query(
|
||||
`INSERT INTO notification_recipients (notification_type, user_id, created_by)
|
||||
VALUES (?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE is_active = 1`,
|
||||
[notificationType, userId, createdBy]
|
||||
);
|
||||
return result.insertId || result.affectedRows > 0;
|
||||
},
|
||||
|
||||
// 수신자 제거 (soft delete)
|
||||
async remove(notificationType, userId) {
|
||||
const db = await getDb();
|
||||
const [result] = await db.query(
|
||||
`UPDATE notification_recipients SET is_active = 0
|
||||
WHERE notification_type = ? AND user_id = ?`,
|
||||
[notificationType, userId]
|
||||
);
|
||||
return result.affectedRows > 0;
|
||||
},
|
||||
|
||||
// 수신자 완전 삭제
|
||||
async delete(notificationType, userId) {
|
||||
const db = await getDb();
|
||||
const [result] = await db.query(
|
||||
`DELETE FROM notification_recipients
|
||||
WHERE notification_type = ? AND user_id = ?`,
|
||||
[notificationType, userId]
|
||||
);
|
||||
return result.affectedRows > 0;
|
||||
},
|
||||
|
||||
// 유형별 수신자 user_id 목록만 가져오기 (알림 생성용)
|
||||
async getRecipientIds(notificationType) {
|
||||
const db = await getDb();
|
||||
const [rows] = await db.query(
|
||||
`SELECT user_id FROM notification_recipients
|
||||
WHERE notification_type = ? AND is_active = 1`,
|
||||
[notificationType]
|
||||
);
|
||||
return rows.map(r => r.user_id);
|
||||
},
|
||||
|
||||
// 사용자가 특정 유형의 수신자인지 확인
|
||||
async isRecipient(notificationType, userId) {
|
||||
const db = await getDb();
|
||||
const [[row]] = await db.query(
|
||||
`SELECT 1 FROM notification_recipients
|
||||
WHERE notification_type = ? AND user_id = ? AND is_active = 1`,
|
||||
[notificationType, userId]
|
||||
);
|
||||
return !!row;
|
||||
},
|
||||
|
||||
// 유형별 수신자 일괄 설정
|
||||
async setRecipients(notificationType, userIds, createdBy = null) {
|
||||
const db = await getDb();
|
||||
|
||||
// 기존 수신자 비활성화
|
||||
await db.query(
|
||||
`UPDATE notification_recipients SET is_active = 0
|
||||
WHERE notification_type = ?`,
|
||||
[notificationType]
|
||||
);
|
||||
|
||||
// 새 수신자 추가
|
||||
if (userIds && userIds.length > 0) {
|
||||
const values = userIds.map(userId => [notificationType, userId, createdBy]);
|
||||
await db.query(
|
||||
`INSERT INTO notification_recipients (notification_type, user_id, created_by)
|
||||
VALUES ?
|
||||
ON DUPLICATE KEY UPDATE is_active = 1`,
|
||||
[values]
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = notificationRecipientModel;
|
||||
@@ -43,6 +43,9 @@ router.post('/external-logs/:logId/return', equipmentController.returnEquipment)
|
||||
// 수리 항목 목록 조회
|
||||
router.get('/repair-categories', equipmentController.getRepairCategories);
|
||||
|
||||
// 새 수리 항목 추가
|
||||
router.post('/repair-categories', equipmentController.addRepairCategory);
|
||||
|
||||
// ==================== 사진 관리 ====================
|
||||
|
||||
// 사진 삭제 (설비 ID 없이 photo_id만으로)
|
||||
|
||||
28
api.hyungi.net/routes/notificationRecipientRoutes.js
Normal file
28
api.hyungi.net/routes/notificationRecipientRoutes.js
Normal file
@@ -0,0 +1,28 @@
|
||||
// routes/notificationRecipientRoutes.js
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const notificationRecipientController = require('../controllers/notificationRecipientController');
|
||||
const { verifyToken, requireMinLevel } = require('../middlewares/authMiddleware');
|
||||
|
||||
// 모든 라우트에 인증 필요
|
||||
router.use(verifyToken);
|
||||
|
||||
// 알림 유형 목록
|
||||
router.get('/types', notificationRecipientController.getTypes);
|
||||
|
||||
// 전체 수신자 목록 (유형별 그룹화)
|
||||
router.get('/', notificationRecipientController.getAll);
|
||||
|
||||
// 유형별 수신자 조회
|
||||
router.get('/:type', notificationRecipientController.getByType);
|
||||
|
||||
// 수신자 추가 (관리자만)
|
||||
router.post('/', requireMinLevel('admin'), notificationRecipientController.add);
|
||||
|
||||
// 유형별 수신자 일괄 설정 (관리자만)
|
||||
router.put('/:type', requireMinLevel('admin'), notificationRecipientController.setRecipients);
|
||||
|
||||
// 수신자 제거 (관리자만)
|
||||
router.delete('/:type/:userId', requireMinLevel('admin'), notificationRecipientController.remove);
|
||||
|
||||
module.exports = router;
|
||||
27
api.hyungi.net/routes/notificationRoutes.js
Normal file
27
api.hyungi.net/routes/notificationRoutes.js
Normal file
@@ -0,0 +1,27 @@
|
||||
// routes/notificationRoutes.js
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const notificationController = require('../controllers/notificationController');
|
||||
|
||||
// 읽지 않은 알림 조회
|
||||
router.get('/unread', notificationController.getUnread);
|
||||
|
||||
// 읽지 않은 알림 개수
|
||||
router.get('/unread/count', notificationController.getUnreadCount);
|
||||
|
||||
// 전체 알림 조회 (페이징)
|
||||
router.get('/', notificationController.getAll);
|
||||
|
||||
// 알림 생성 (시스템/관리자용)
|
||||
router.post('/', notificationController.create);
|
||||
|
||||
// 모든 알림 읽음 처리
|
||||
router.post('/read-all', notificationController.markAllAsRead);
|
||||
|
||||
// 특정 알림 읽음 처리
|
||||
router.post('/:id/read', notificationController.markAsRead);
|
||||
|
||||
// 알림 삭제
|
||||
router.delete('/:id', notificationController.delete);
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user