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;
|
||||
@@ -21,6 +21,8 @@ const partnerRoutes = require('./routes/partnerRoutes');
|
||||
const vendorRoutes = require('./routes/vendorRoutes');
|
||||
const consumableItemRoutes = require('./routes/consumableItemRoutes');
|
||||
const notificationRecipientRoutes = require('./routes/notificationRecipientRoutes');
|
||||
const notificationRoutes = require('./routes/notificationRoutes');
|
||||
const pushSubscriptionRoutes = require('./routes/pushSubscriptionRoutes');
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3000;
|
||||
@@ -31,6 +33,8 @@ const allowedOrigins = [
|
||||
'https://tkqc.technicalkorea.net',
|
||||
'https://tkuser.technicalkorea.net',
|
||||
'https://tkpurchase.technicalkorea.net',
|
||||
'https://tksafety.technicalkorea.net',
|
||||
'https://tksupport.technicalkorea.net',
|
||||
];
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
allowedOrigins.push('http://localhost:30080', 'http://localhost:30180', 'http://localhost:30280');
|
||||
@@ -64,6 +68,8 @@ app.use('/api/partners', partnerRoutes);
|
||||
app.use('/api/vendors', vendorRoutes);
|
||||
app.use('/api/consumable-items', consumableItemRoutes);
|
||||
app.use('/api/notification-recipients', notificationRecipientRoutes);
|
||||
app.use('/api/notifications', notificationRoutes);
|
||||
app.use('/api/push', pushSubscriptionRoutes);
|
||||
|
||||
// 404
|
||||
app.use((req, res) => {
|
||||
@@ -83,4 +89,21 @@ app.listen(PORT, () => {
|
||||
console.log(`tkuser-api running on port ${PORT}`);
|
||||
});
|
||||
|
||||
// 오래된 알림 정리 cron (매일 03:00 KST)
|
||||
(function scheduleNotificationCleanup() {
|
||||
const notificationModel = require('./models/notificationModel');
|
||||
function runCleanup() {
|
||||
const now = new Date();
|
||||
const kstHour = (now.getUTCHours() + 9) % 24;
|
||||
if (kstHour === 3 && now.getMinutes() < 1) {
|
||||
notificationModel.deleteOld(30).then(count => {
|
||||
if (count > 0) console.log(`오래된 알림 ${count}건 정리 완료`);
|
||||
}).catch(err => {
|
||||
console.error('알림 정리 실패:', err.message);
|
||||
});
|
||||
}
|
||||
}
|
||||
setInterval(runCleanup, 60000);
|
||||
})();
|
||||
|
||||
module.exports = app;
|
||||
|
||||
@@ -76,4 +76,31 @@ function requireAdminOrPermission(pageName) {
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = { extractToken, requireAuth, requireAdmin, requireAdminOrPermission };
|
||||
/**
|
||||
* 최소 권한 레벨 체크 미들웨어
|
||||
* worker(1) < group_leader(2) < support_team(3) < admin(4) < system(5)
|
||||
*/
|
||||
const ACCESS_LEVELS = { worker: 1, group_leader: 2, support_team: 3, admin: 4, system: 5 };
|
||||
|
||||
function requireMinLevel(minLevel) {
|
||||
return (req, res, next) => {
|
||||
const token = extractToken(req);
|
||||
if (!token) {
|
||||
return res.status(401).json({ success: false, error: '인증이 필요합니다' });
|
||||
}
|
||||
try {
|
||||
const decoded = jwt.verify(token, JWT_SECRET);
|
||||
req.user = decoded;
|
||||
const userLevel = ACCESS_LEVELS[decoded.access_level] || ACCESS_LEVELS[decoded.role] || 0;
|
||||
const requiredLevel = ACCESS_LEVELS[minLevel] || 999;
|
||||
if (userLevel < requiredLevel) {
|
||||
return res.status(403).json({ success: false, error: `${minLevel} 이상의 권한이 필요합니다` });
|
||||
}
|
||||
next();
|
||||
} catch {
|
||||
return res.status(401).json({ success: false, error: '유효하지 않은 토큰입니다' });
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = { extractToken, requireAuth, requireAdmin, requireAdminOrPermission, requireMinLevel };
|
||||
|
||||
311
user-management/api/models/notificationModel.js
Normal file
311
user-management/api/models/notificationModel.js
Normal file
@@ -0,0 +1,311 @@
|
||||
// models/notificationModel.js
|
||||
const { getPool } = require('./userModel');
|
||||
|
||||
// Web Push (lazy init)
|
||||
let webpush = null;
|
||||
let vapidConfigured = false;
|
||||
|
||||
function getWebPush() {
|
||||
if (!webpush) {
|
||||
try {
|
||||
webpush = require('web-push');
|
||||
if (process.env.VAPID_PUBLIC_KEY && process.env.VAPID_PRIVATE_KEY) {
|
||||
webpush.setVapidDetails(
|
||||
process.env.VAPID_SUBJECT || 'mailto:admin@technicalkorea.net',
|
||||
process.env.VAPID_PUBLIC_KEY,
|
||||
process.env.VAPID_PRIVATE_KEY
|
||||
);
|
||||
vapidConfigured = true;
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[notifications] web-push 모듈 로드 실패:', e.message);
|
||||
}
|
||||
}
|
||||
return vapidConfigured ? webpush : null;
|
||||
}
|
||||
|
||||
// Push 전송 헬퍼 — 알림 생성 후 호출 (ntfy 우선, 나머지 Web Push)
|
||||
async function sendPushToUsers(userIds, payload) {
|
||||
const pushModel = require('./pushSubscriptionModel');
|
||||
const { sendNtfy } = require('../utils/ntfySender');
|
||||
|
||||
try {
|
||||
// 1) ntfy 구독자 분리
|
||||
const ntfyUserIds = userIds && userIds.length > 0
|
||||
? await pushModel.getNtfyUserIds(userIds)
|
||||
: await pushModel.getAllNtfyUserIds();
|
||||
|
||||
const ntfySet = new Set(ntfyUserIds);
|
||||
|
||||
// 2) ntfy 병렬 발송
|
||||
if (ntfyUserIds.length > 0) {
|
||||
await Promise.allSettled(
|
||||
ntfyUserIds.map(uid =>
|
||||
sendNtfy(uid, { title: payload.title, body: payload.body, url: payload.url })
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// 3) Web Push — ntfy 구독자 제외
|
||||
const wp = getWebPush();
|
||||
if (!wp) return;
|
||||
|
||||
let subscriptions;
|
||||
if (userIds && userIds.length > 0) {
|
||||
const webPushUserIds = userIds.filter(id => !ntfySet.has(id));
|
||||
subscriptions = webPushUserIds.length > 0
|
||||
? await pushModel.getByUserIds(webPushUserIds)
|
||||
: [];
|
||||
} else {
|
||||
// broadcast: 전체 구독 가져온 뒤 ntfy 사용자 제외
|
||||
subscriptions = (await pushModel.getAll()).filter(s => !ntfySet.has(s.user_id));
|
||||
}
|
||||
|
||||
const payloadStr = JSON.stringify(payload);
|
||||
for (const sub of subscriptions) {
|
||||
try {
|
||||
await wp.sendNotification({
|
||||
endpoint: sub.endpoint,
|
||||
keys: { p256dh: sub.p256dh, auth: sub.auth }
|
||||
}, payloadStr);
|
||||
} catch (err) {
|
||||
if (err.statusCode === 410 || err.statusCode === 404) {
|
||||
await pushModel.deleteByEndpoint(sub.endpoint).catch(() => {});
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[notifications] Push 전송 오류:', e.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function getRecipientIds(notificationType) {
|
||||
const pool = getPool();
|
||||
const [rows] = await pool.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 pool = getPool();
|
||||
const { user_id, type, title, message, link_url, reference_type, reference_id, created_by } = notificationData;
|
||||
|
||||
const [result] = await pool.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]
|
||||
);
|
||||
|
||||
this._invalidateCache(user_id);
|
||||
|
||||
return result.insertId;
|
||||
},
|
||||
|
||||
// 읽지 않은 알림 조회 (특정 사용자 또는 전체)
|
||||
async getUnread(userId = null) {
|
||||
const pool = getPool();
|
||||
const [rows] = await pool.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 pool = getPool();
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
const [rows] = await pool.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 pool.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 pool = getPool();
|
||||
const [[row]] = await pool.query('SELECT user_id FROM notifications WHERE notification_id = ?', [notificationId]);
|
||||
const [result] = await pool.query(
|
||||
`UPDATE notifications SET is_read = 1, read_at = NOW() WHERE notification_id = ?`,
|
||||
[notificationId]
|
||||
);
|
||||
if (row) this._invalidateCache(row.user_id);
|
||||
return result.affectedRows > 0;
|
||||
},
|
||||
|
||||
// 모든 알림 읽음 처리
|
||||
async markAllAsRead(userId = null) {
|
||||
const pool = getPool();
|
||||
const [result] = await pool.query(
|
||||
`UPDATE notifications SET is_read = 1, read_at = NOW()
|
||||
WHERE is_read = 0 AND (user_id IS NULL OR user_id = ?)`,
|
||||
[userId || 0]
|
||||
);
|
||||
this._invalidateCache(userId);
|
||||
return result.affectedRows;
|
||||
},
|
||||
|
||||
// 알림 삭제
|
||||
async delete(notificationId) {
|
||||
const pool = getPool();
|
||||
const [result] = await pool.query(
|
||||
`DELETE FROM notifications WHERE notification_id = ?`,
|
||||
[notificationId]
|
||||
);
|
||||
return result.affectedRows > 0;
|
||||
},
|
||||
|
||||
// 오래된 알림 삭제 (30일 이상)
|
||||
async deleteOld(days = 30) {
|
||||
const pool = getPool();
|
||||
const [result] = await pool.query(
|
||||
`DELETE FROM notifications WHERE created_at < DATE_SUB(NOW(), INTERVAL ? DAY)`,
|
||||
[days]
|
||||
);
|
||||
return result.affectedRows;
|
||||
},
|
||||
|
||||
// 읽지 않은 알림 개수 (캐싱)
|
||||
async getUnreadCount(userId = null) {
|
||||
const cacheKey = `notif:unread:${userId || 0}`;
|
||||
try {
|
||||
const cache = require('../utils/cache');
|
||||
const cached = await cache.get(cacheKey);
|
||||
if (cached !== null && cached !== undefined) return cached;
|
||||
} catch (e) { /* 무시 */ }
|
||||
|
||||
const pool = getPool();
|
||||
const [[{ count }]] = await pool.query(
|
||||
`SELECT COUNT(*) as count FROM notifications
|
||||
WHERE is_read = 0 AND (user_id IS NULL OR user_id = ?)`,
|
||||
[userId || 0]
|
||||
);
|
||||
|
||||
try {
|
||||
const cache = require('../utils/cache');
|
||||
await cache.set(cacheKey, count, 30);
|
||||
} catch (e) { /* 무시 */ }
|
||||
|
||||
return count;
|
||||
},
|
||||
|
||||
// 캐시 무효화
|
||||
_invalidateCache(userId) {
|
||||
try {
|
||||
const cache = require('../utils/cache');
|
||||
cache.del(`notif:unread:${userId || 0}`).catch(() => {});
|
||||
if (userId) cache.del('notif:unread:0').catch(() => {});
|
||||
} catch (e) { /* 무시 */ }
|
||||
},
|
||||
|
||||
// 수리 신청 알림 생성 헬퍼 (지정된 수신자에게 전송)
|
||||
async createRepairNotification(repairData) {
|
||||
const { equipment_id, equipment_name, repair_type, request_id, created_by } = repairData;
|
||||
|
||||
const recipientIds = await getRecipientIds('repair');
|
||||
|
||||
if (recipientIds.length === 0) {
|
||||
const id = 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
|
||||
});
|
||||
sendPushToUsers([], {
|
||||
title: `수리 신청: ${equipment_name || '설비'}`,
|
||||
body: `${repair_type} 수리가 신청되었습니다.`,
|
||||
url: `/pages/admin/repair-management.html`
|
||||
});
|
||||
return id;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
sendPushToUsers(recipientIds, {
|
||||
title: `수리 신청: ${equipment_name || '설비'}`,
|
||||
body: `${repair_type} 수리가 신청되었습니다.`,
|
||||
url: `/pages/admin/repair-management.html`
|
||||
});
|
||||
|
||||
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) {
|
||||
const id = await this.create({
|
||||
type,
|
||||
title,
|
||||
message,
|
||||
link_url,
|
||||
reference_type,
|
||||
reference_id,
|
||||
created_by
|
||||
});
|
||||
sendPushToUsers([], { title, body: message || '', url: link_url || '/' });
|
||||
return id;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
sendPushToUsers(recipientIds, { title, body: message || '', url: link_url || '/' });
|
||||
|
||||
return results;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = notificationModel;
|
||||
92
user-management/api/models/pushSubscriptionModel.js
Normal file
92
user-management/api/models/pushSubscriptionModel.js
Normal file
@@ -0,0 +1,92 @@
|
||||
// models/pushSubscriptionModel.js
|
||||
const { getPool } = require('./userModel');
|
||||
|
||||
const pushSubscriptionModel = {
|
||||
async subscribe(userId, subscription) {
|
||||
const pool = getPool();
|
||||
const { endpoint, keys } = subscription;
|
||||
await pool.query(
|
||||
`INSERT INTO push_subscriptions (user_id, endpoint, p256dh, auth)
|
||||
VALUES (?, ?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE user_id = VALUES(user_id), p256dh = VALUES(p256dh), auth = VALUES(auth)`,
|
||||
[userId, endpoint, keys.p256dh, keys.auth]
|
||||
);
|
||||
},
|
||||
|
||||
async unsubscribe(endpoint) {
|
||||
const pool = getPool();
|
||||
await pool.query('DELETE FROM push_subscriptions WHERE endpoint = ?', [endpoint]);
|
||||
},
|
||||
|
||||
async getByUserId(userId) {
|
||||
const pool = getPool();
|
||||
const [rows] = await pool.query(
|
||||
'SELECT * FROM push_subscriptions WHERE user_id = ?',
|
||||
[userId]
|
||||
);
|
||||
return rows;
|
||||
},
|
||||
|
||||
async getByUserIds(userIds) {
|
||||
if (!userIds || userIds.length === 0) return [];
|
||||
const pool = getPool();
|
||||
const [rows] = await pool.query(
|
||||
'SELECT * FROM push_subscriptions WHERE user_id IN (?)',
|
||||
[userIds]
|
||||
);
|
||||
return rows;
|
||||
},
|
||||
|
||||
async getAll() {
|
||||
const pool = getPool();
|
||||
const [rows] = await pool.query('SELECT * FROM push_subscriptions');
|
||||
return rows;
|
||||
},
|
||||
|
||||
async deleteByEndpoint(endpoint) {
|
||||
const pool = getPool();
|
||||
await pool.query('DELETE FROM push_subscriptions WHERE endpoint = ?', [endpoint]);
|
||||
},
|
||||
|
||||
// === ntfy 구독 관련 ===
|
||||
|
||||
async getNtfyUserIds(userIds) {
|
||||
if (!userIds || userIds.length === 0) return [];
|
||||
const pool = getPool();
|
||||
const [rows] = await pool.query(
|
||||
'SELECT user_id FROM ntfy_subscriptions WHERE user_id IN (?)',
|
||||
[userIds]
|
||||
);
|
||||
return rows.map(r => r.user_id);
|
||||
},
|
||||
|
||||
async getAllNtfyUserIds() {
|
||||
const pool = getPool();
|
||||
const [rows] = await pool.query('SELECT user_id FROM ntfy_subscriptions');
|
||||
return rows.map(r => r.user_id);
|
||||
},
|
||||
|
||||
async ntfySubscribe(userId) {
|
||||
const pool = getPool();
|
||||
await pool.query(
|
||||
'INSERT IGNORE INTO ntfy_subscriptions (user_id) VALUES (?)',
|
||||
[userId]
|
||||
);
|
||||
},
|
||||
|
||||
async ntfyUnsubscribe(userId) {
|
||||
const pool = getPool();
|
||||
await pool.query('DELETE FROM ntfy_subscriptions WHERE user_id = ?', [userId]);
|
||||
},
|
||||
|
||||
async isNtfySubscribed(userId) {
|
||||
const pool = getPool();
|
||||
const [rows] = await pool.query(
|
||||
'SELECT 1 FROM ntfy_subscriptions WHERE user_id = ? LIMIT 1',
|
||||
[userId]
|
||||
);
|
||||
return rows.length > 0;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = pushSubscriptionModel;
|
||||
@@ -13,6 +13,8 @@
|
||||
"express": "^4.18.2",
|
||||
"jsonwebtoken": "^9.0.0",
|
||||
"multer": "^1.4.5-lts.1",
|
||||
"mysql2": "^3.14.1"
|
||||
"mysql2": "^3.14.1",
|
||||
"node-cache": "^5.1.2",
|
||||
"web-push": "^3.6.7"
|
||||
}
|
||||
}
|
||||
|
||||
34
user-management/api/routes/notificationRoutes.js
Normal file
34
user-management/api/routes/notificationRoutes.js
Normal file
@@ -0,0 +1,34 @@
|
||||
// routes/notificationRoutes.js
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const notificationController = require('../controllers/notificationController');
|
||||
const { requireAuth, requireMinLevel } = require('../middleware/auth');
|
||||
|
||||
// 내부 서비스용 알림 생성 (X-Internal-Service-Key 인증, JWT 불필요)
|
||||
router.post('/internal', notificationController.createInternal);
|
||||
|
||||
// 이하 모든 라우트는 JWT 인증 필요
|
||||
router.use(requireAuth);
|
||||
|
||||
// 읽지 않은 알림 조회 (본인 알림만)
|
||||
router.get('/unread', notificationController.getUnread);
|
||||
|
||||
// 읽지 않은 알림 개수
|
||||
router.get('/unread/count', notificationController.getUnreadCount);
|
||||
|
||||
// 전체 알림 조회 (페이징)
|
||||
router.get('/', notificationController.getAll);
|
||||
|
||||
// 알림 생성 (시스템/관리자용)
|
||||
router.post('/', requireMinLevel('support_team'), notificationController.create);
|
||||
|
||||
// 모든 알림 읽음 처리 (본인 알림만)
|
||||
router.post('/read-all', notificationController.markAllAsRead);
|
||||
|
||||
// 특정 알림 읽음 처리 (본인 알림만)
|
||||
router.post('/:id/read', notificationController.markAsRead);
|
||||
|
||||
// 알림 삭제 (본인 알림만)
|
||||
router.delete('/:id', notificationController.delete);
|
||||
|
||||
module.exports = router;
|
||||
19
user-management/api/routes/pushSubscriptionRoutes.js
Normal file
19
user-management/api/routes/pushSubscriptionRoutes.js
Normal file
@@ -0,0 +1,19 @@
|
||||
// routes/pushSubscriptionRoutes.js
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const pushController = require('../controllers/pushSubscriptionController');
|
||||
const { requireAuth } = require('../middleware/auth');
|
||||
|
||||
// VAPID 공개키 (인증 불필요)
|
||||
router.get('/vapid-public-key', pushController.getVapidPublicKey);
|
||||
|
||||
// 구독/해제 (인증 필요)
|
||||
router.post('/subscribe', requireAuth, pushController.subscribe);
|
||||
router.delete('/unsubscribe', requireAuth, pushController.unsubscribe);
|
||||
|
||||
// ntfy 구독 관리
|
||||
router.post('/ntfy/subscribe', requireAuth, pushController.ntfySubscribe);
|
||||
router.delete('/ntfy/unsubscribe', requireAuth, pushController.ntfyUnsubscribe);
|
||||
router.get('/ntfy/status', requireAuth, pushController.ntfyStatus);
|
||||
|
||||
module.exports = router;
|
||||
39
user-management/api/utils/cache.js
Normal file
39
user-management/api/utils/cache.js
Normal file
@@ -0,0 +1,39 @@
|
||||
// utils/cache.js - NodeCache 기반 간소화 캐시
|
||||
const NodeCache = require('node-cache');
|
||||
|
||||
const memoryCache = new NodeCache({
|
||||
stdTTL: 600,
|
||||
checkperiod: 120,
|
||||
useClones: false
|
||||
});
|
||||
|
||||
const get = async (key) => {
|
||||
try {
|
||||
return memoryCache.get(key) || null;
|
||||
} catch (error) {
|
||||
console.warn(`캐시 조회 오류 (${key}):`, error.message);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const set = async (key, value, ttl = 600) => {
|
||||
try {
|
||||
memoryCache.set(key, value, ttl);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.warn(`캐시 저장 오류 (${key}):`, error.message);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const del = async (key) => {
|
||||
try {
|
||||
memoryCache.del(key);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.warn(`캐시 삭제 오류 (${key}):`, error.message);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = { get, set, del };
|
||||
31
user-management/api/utils/ntfySender.js
Normal file
31
user-management/api/utils/ntfySender.js
Normal file
@@ -0,0 +1,31 @@
|
||||
// utils/ntfySender.js — ntfy HTTP POST 래퍼
|
||||
const NTFY_BASE_URL = process.env.NTFY_BASE_URL || 'http://ntfy:80';
|
||||
const NTFY_PUBLISH_TOKEN = process.env.NTFY_PUBLISH_TOKEN;
|
||||
const TKFB_BASE_URL = process.env.TKFB_BASE_URL || 'https://tkfb.technicalkorea.net';
|
||||
|
||||
async function sendNtfy(userId, { title, body, url }) {
|
||||
if (!NTFY_PUBLISH_TOKEN) return;
|
||||
|
||||
const topic = `tkfactory-user-${userId}`;
|
||||
const headers = {
|
||||
'Authorization': `Bearer ${NTFY_PUBLISH_TOKEN}`,
|
||||
'Title': title || '',
|
||||
'Tags': 'bell',
|
||||
};
|
||||
if (url) {
|
||||
headers['Click'] = url.startsWith('http') ? url : `${TKFB_BASE_URL}${url}`;
|
||||
}
|
||||
|
||||
try {
|
||||
const resp = await fetch(`${NTFY_BASE_URL}/${topic}`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: body || '',
|
||||
});
|
||||
if (!resp.ok) console.warn(`[ntfy] ${userId} 발송 실패: ${resp.status}`);
|
||||
} catch (e) {
|
||||
console.error(`[ntfy] ${userId} 발송 오류:`, e.message);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { sendNtfy };
|
||||
@@ -211,7 +211,7 @@ async function init() {
|
||||
/* ===== 알림 벨 ===== */
|
||||
function _loadNotificationBell() {
|
||||
const s = document.createElement('script');
|
||||
s.src = (location.hostname.includes('technicalkorea.net') ? 'https://tkds.technicalkorea.net' : location.protocol + '//' + location.hostname + ':30000') + '/shared/notification-bell.js?v=3';
|
||||
s.src = (location.hostname.includes('technicalkorea.net') ? 'https://tkds.technicalkorea.net' : location.protocol + '//' + location.hostname + ':30000') + '/shared/notification-bell.js?v=4';
|
||||
document.head.appendChild(s);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user