- notificationRecipientModel에 ntfy CRUD 메서드 추가 (같은 DB 직접 쿼리) - ntfy 라우트 3개 추가 (GET/POST/DELETE, /:type 위에 배치) - 알림 수신자 탭 상단에 ntfy 구독 관리 카드 렌더링 - ntfy 추가 모달에 앱 설정 안내 문구 포함 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
152 lines
5.6 KiB
JavaScript
152 lines
5.6 KiB
JavaScript
// controllers/notificationRecipientController.js
|
|
const notificationRecipientModel = require('../models/notificationRecipientModel');
|
|
const permissionModel = require('../models/permissionModel');
|
|
|
|
async function checkNrPermission(user) {
|
|
const role = (user.role || '').toLowerCase();
|
|
if (['admin', 'system'].includes(role)) return true;
|
|
const access = await permissionModel.checkAccess(user.user_id, 'tkuser.notification_recipients');
|
|
return access.can_access;
|
|
}
|
|
|
|
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 {
|
|
const recipients = await notificationRecipientModel.getAll();
|
|
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가 필요합니다.' });
|
|
}
|
|
|
|
if (!await checkNrPermission(req.user)) {
|
|
return res.status(403).json({ success: false, error: '알림 수신자 관리 권한이 없습니다.' });
|
|
}
|
|
|
|
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;
|
|
|
|
if (!await checkNrPermission(req.user)) {
|
|
return res.status(403).json({ success: false, error: '알림 수신자 관리 권한이 없습니다.' });
|
|
}
|
|
|
|
await notificationRecipientModel.remove(type, userId);
|
|
res.json({ success: true, message: '수신자가 제거되었습니다.' });
|
|
} catch (error) {
|
|
console.error('수신자 제거 오류:', error);
|
|
res.status(500).json({ success: false, error: '수신자 제거 실패' });
|
|
}
|
|
},
|
|
|
|
// === ntfy 구독 관리 ===
|
|
|
|
// ntfy 구독자 목록
|
|
getNtfySubscribers: async (req, res) => {
|
|
try {
|
|
const subscribers = await notificationRecipientModel.getNtfySubscribers();
|
|
res.json({ success: true, data: subscribers });
|
|
} catch (error) {
|
|
console.error('ntfy 구독자 조회 오류:', error);
|
|
res.status(500).json({ success: false, error: 'ntfy 구독자 조회 실패' });
|
|
}
|
|
},
|
|
|
|
// ntfy 구독 등록 (관리자)
|
|
addNtfySubscription: async (req, res) => {
|
|
try {
|
|
const { userId } = req.params;
|
|
if (!userId) {
|
|
return res.status(400).json({ success: false, error: '사용자 ID가 필요합니다.' });
|
|
}
|
|
if (!await checkNrPermission(req.user)) {
|
|
return res.status(403).json({ success: false, error: '알림 수신자 관리 권한이 없습니다.' });
|
|
}
|
|
await notificationRecipientModel.addNtfySubscription(Number(userId));
|
|
res.json({ success: true, message: 'ntfy 구독이 등록되었습니다.' });
|
|
} catch (error) {
|
|
console.error('ntfy 구독 등록 오류:', error);
|
|
res.status(500).json({ success: false, error: 'ntfy 구독 등록 실패' });
|
|
}
|
|
},
|
|
|
|
// ntfy 구독 해제 (관리자)
|
|
removeNtfySubscription: async (req, res) => {
|
|
try {
|
|
const { userId } = req.params;
|
|
if (!await checkNrPermission(req.user)) {
|
|
return res.status(403).json({ success: false, error: '알림 수신자 관리 권한이 없습니다.' });
|
|
}
|
|
await notificationRecipientModel.removeNtfySubscription(Number(userId));
|
|
res.json({ success: true, message: 'ntfy 구독이 해제되었습니다.' });
|
|
} catch (error) {
|
|
console.error('ntfy 구독 해제 오류:', error);
|
|
res.status(500).json({ success: false, error: 'ntfy 구독 해제 실패' });
|
|
}
|
|
},
|
|
|
|
// 유형별 수신자 일괄 설정
|
|
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;
|