- 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>
108 lines
3.6 KiB
JavaScript
108 lines
3.6 KiB
JavaScript
// 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;
|