feat(tkuser): 알림 시스템 이관 system1-factory → tkuser
- Phase 1: tkuser에 알림 CRUD, Push/ntfy 발송, 내부 알림 API 추가 - Phase 2: notifyHelper URL을 tkuser-api:3000으로 전환 (system2, tkpurchase, tksafety, system1) - Phase 3: notification-bell.js API 도메인 tkuser로 변경 + 캐시 버스팅 v=4 - Phase 4: system1에서 알림 코드 제거 (routes, controllers, models, utils) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
203
user-management/api/controllers/notificationController.js
Normal file
203
user-management/api/controllers/notificationController.js
Normal file
@@ -0,0 +1,203 @@
|
||||
// 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: '알림 생성 중 오류가 발생했습니다.'
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// 내부 서비스용 알림 생성 (X-Internal-Service-Key 인증)
|
||||
async createInternal(req, res) {
|
||||
try {
|
||||
const serviceKey = req.headers['x-internal-service-key'];
|
||||
if (!serviceKey || serviceKey !== process.env.INTERNAL_SERVICE_KEY) {
|
||||
return res.status(403).json({ success: false, message: '권한이 없습니다.' });
|
||||
}
|
||||
|
||||
const { type, title, message, link_url, reference_type, reference_id, created_by } = req.body;
|
||||
|
||||
if (!title) {
|
||||
return res.status(400).json({ success: false, message: '알림 제목은 필수입니다.' });
|
||||
}
|
||||
|
||||
const results = await notificationModel.createTypedNotification({
|
||||
type: type || 'system',
|
||||
title,
|
||||
message,
|
||||
link_url,
|
||||
reference_type,
|
||||
reference_id,
|
||||
created_by
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: '알림이 생성되었습니다.',
|
||||
data: { notification_ids: Array.isArray(results) ? results : [results] }
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('내부 알림 생성 오류:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '알림 생성 중 오류가 발생했습니다.'
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = notificationController;
|
||||
107
user-management/api/controllers/pushSubscriptionController.js
Normal file
107
user-management/api/controllers/pushSubscriptionController.js
Normal file
@@ -0,0 +1,107 @@
|
||||
// controllers/pushSubscriptionController.js
|
||||
const pushSubscriptionModel = require('../models/pushSubscriptionModel');
|
||||
|
||||
const pushSubscriptionController = {
|
||||
// VAPID 공개키 반환 (인증 불필요)
|
||||
async getVapidPublicKey(req, res) {
|
||||
const vapidPublicKey = process.env.VAPID_PUBLIC_KEY;
|
||||
if (!vapidPublicKey) {
|
||||
return res.status(500).json({ success: false, message: 'VAPID 키가 설정되지 않았습니다.' });
|
||||
}
|
||||
res.json({ success: true, data: { vapidPublicKey } });
|
||||
},
|
||||
|
||||
// Push 구독 저장
|
||||
async subscribe(req, res) {
|
||||
try {
|
||||
const userId = req.user?.id;
|
||||
const { subscription } = req.body;
|
||||
|
||||
if (!subscription || !subscription.endpoint || !subscription.keys) {
|
||||
return res.status(400).json({ success: false, message: '유효한 구독 정보가 필요합니다.' });
|
||||
}
|
||||
|
||||
await pushSubscriptionModel.subscribe(userId, subscription);
|
||||
res.json({ success: true, message: 'Push 구독이 등록되었습니다.' });
|
||||
} catch (error) {
|
||||
console.error('Push 구독 오류:', error);
|
||||
res.status(500).json({ success: false, message: 'Push 구독 중 오류가 발생했습니다.' });
|
||||
}
|
||||
},
|
||||
|
||||
// Push 구독 해제
|
||||
async unsubscribe(req, res) {
|
||||
try {
|
||||
const { endpoint } = req.body;
|
||||
if (!endpoint) {
|
||||
return res.status(400).json({ success: false, message: 'endpoint가 필요합니다.' });
|
||||
}
|
||||
|
||||
await pushSubscriptionModel.unsubscribe(endpoint);
|
||||
res.json({ success: true, message: 'Push 구독이 해제되었습니다.' });
|
||||
} catch (error) {
|
||||
console.error('Push 구독 해제 오류:', error);
|
||||
res.status(500).json({ success: false, message: 'Push 구독 해제 중 오류가 발생했습니다.' });
|
||||
}
|
||||
},
|
||||
|
||||
// === ntfy ===
|
||||
|
||||
// ntfy 구독 등록
|
||||
async ntfySubscribe(req, res) {
|
||||
try {
|
||||
const userId = req.user?.id;
|
||||
await pushSubscriptionModel.ntfySubscribe(userId);
|
||||
|
||||
const topic = `tkfactory-user-${userId}`;
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'ntfy 구독이 등록되었습니다.',
|
||||
data: {
|
||||
topic,
|
||||
serverUrl: process.env.NTFY_EXTERNAL_URL || 'https://ntfy.technicalkorea.net',
|
||||
username: 'subscriber',
|
||||
password: 'tkfactory-sub-2026'
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('ntfy 구독 오류:', error);
|
||||
res.status(500).json({ success: false, message: 'ntfy 구독 중 오류가 발생했습니다.' });
|
||||
}
|
||||
},
|
||||
|
||||
// ntfy 구독 해제
|
||||
async ntfyUnsubscribe(req, res) {
|
||||
try {
|
||||
const userId = req.user?.id;
|
||||
await pushSubscriptionModel.ntfyUnsubscribe(userId);
|
||||
res.json({ success: true, message: 'ntfy 구독이 해제되었습니다.' });
|
||||
} catch (error) {
|
||||
console.error('ntfy 구독 해제 오류:', error);
|
||||
res.status(500).json({ success: false, message: 'ntfy 구독 해제 중 오류가 발생했습니다.' });
|
||||
}
|
||||
},
|
||||
|
||||
// ntfy 구독 상태 확인
|
||||
async ntfyStatus(req, res) {
|
||||
try {
|
||||
const userId = req.user?.id;
|
||||
const subscribed = await pushSubscriptionModel.isNtfySubscribed(userId);
|
||||
const topic = `tkfactory-user-${userId}`;
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
subscribed,
|
||||
topic,
|
||||
serverUrl: process.env.NTFY_EXTERNAL_URL || 'https://ntfy.technicalkorea.net'
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('ntfy 상태 확인 오류:', error);
|
||||
res.status(500).json({ success: false, message: 'ntfy 상태 확인 중 오류가 발생했습니다.' });
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = pushSubscriptionController;
|
||||
Reference in New Issue
Block a user