feat: 실시간 알림 시스템 (Web Push + 알림 벨 + 서비스간 알림 연동)
- Phase 1: 모든 서비스 헤더에 알림 벨 UI 추가 (notification-bell.js) - Phase 2: VAPID Web Push 구독/전송 (push-sw.js, pushSubscription API) - Phase 3: 내부 알림 API + notifyHelper로 서비스간 알림 연동 - tksafety: 출입 승인/반려, 안전교육 완료, 방문자 체크인 - tkpurchase: 일용공 신청, 작업보고서 제출 - system2-report: 신고 접수/확인/처리완료 - Phase 4: 30일 이상 알림 자동 정리 cron, Redis 캐싱 - CORS에 tkuser/tkpurchase/tksafety 서브도메인 추가 - HTML cache busting 버전 갱신 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,60 @@
|
||||
// models/notificationModel.js
|
||||
const { getDb } = require('../dbPool');
|
||||
|
||||
// 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 전송 헬퍼 — 알림 생성 후 호출
|
||||
async function sendPushToUsers(userIds, payload) {
|
||||
const wp = getWebPush();
|
||||
if (!wp) return;
|
||||
|
||||
try {
|
||||
const pushModel = require('./pushSubscriptionModel');
|
||||
const subscriptions = userIds && userIds.length > 0
|
||||
? await pushModel.getByUserIds(userIds)
|
||||
: await pushModel.getAll(); // broadcast
|
||||
|
||||
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) {
|
||||
// 만료 구독 (410 Gone, 404 Not Found) 자동 정리
|
||||
if (err.statusCode === 410 || err.statusCode === 404) {
|
||||
await pushModel.deleteByEndpoint(sub.endpoint).catch(() => {});
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[notifications] Push 전송 오류:', e.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 순환 참조를 피하기 위해 함수 내에서 require
|
||||
async function getRecipientIds(notificationType) {
|
||||
const db = await getDb();
|
||||
@@ -24,6 +78,9 @@ const notificationModel = {
|
||||
[user_id || null, type || 'system', title, message || null, link_url || null, reference_type || null, reference_id || null, created_by || null]
|
||||
);
|
||||
|
||||
// Redis 캐시 무효화
|
||||
this._invalidateCache(user_id);
|
||||
|
||||
return result.insertId;
|
||||
},
|
||||
|
||||
@@ -66,10 +123,13 @@ const notificationModel = {
|
||||
// 알림 읽음 처리
|
||||
async markAsRead(notificationId) {
|
||||
const db = await getDb();
|
||||
// 읽음 처리 전 user_id 조회 (캐시 무효화용)
|
||||
const [[row]] = await db.query('SELECT user_id FROM notifications WHERE notification_id = ?', [notificationId]);
|
||||
const [result] = await db.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;
|
||||
},
|
||||
|
||||
@@ -81,6 +141,7 @@ const notificationModel = {
|
||||
WHERE is_read = 0 AND (user_id IS NULL OR user_id = ?)`,
|
||||
[userId || 0]
|
||||
);
|
||||
this._invalidateCache(userId);
|
||||
return result.affectedRows;
|
||||
},
|
||||
|
||||
@@ -104,17 +165,39 @@ const notificationModel = {
|
||||
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 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]
|
||||
);
|
||||
|
||||
try {
|
||||
const cache = require('../utils/cache');
|
||||
await cache.set(cacheKey, count, 30); // TTL 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;
|
||||
@@ -124,7 +207,7 @@ const notificationModel = {
|
||||
|
||||
if (recipientIds.length === 0) {
|
||||
// 수신자가 지정되지 않은 경우 전체 알림 (user_id = null)
|
||||
return await this.create({
|
||||
const id = await this.create({
|
||||
type: 'repair',
|
||||
title: `수리 신청: ${equipment_name || '설비'}`,
|
||||
message: `${repair_type} 수리가 신청되었습니다.`,
|
||||
@@ -133,6 +216,13 @@ const notificationModel = {
|
||||
reference_id: request_id,
|
||||
created_by
|
||||
});
|
||||
// Push (broadcast)
|
||||
sendPushToUsers([], {
|
||||
title: `수리 신청: ${equipment_name || '설비'}`,
|
||||
body: `${repair_type} 수리가 신청되었습니다.`,
|
||||
url: `/pages/admin/repair-management.html`
|
||||
});
|
||||
return id;
|
||||
}
|
||||
|
||||
// 지정된 수신자 각각에게 알림 생성
|
||||
@@ -151,6 +241,13 @@ const notificationModel = {
|
||||
results.push(notificationId);
|
||||
}
|
||||
|
||||
// Push 전송
|
||||
sendPushToUsers(recipientIds, {
|
||||
title: `수리 신청: ${equipment_name || '설비'}`,
|
||||
body: `${repair_type} 수리가 신청되었습니다.`,
|
||||
url: `/pages/admin/repair-management.html`
|
||||
});
|
||||
|
||||
return results;
|
||||
},
|
||||
|
||||
@@ -163,7 +260,7 @@ const notificationModel = {
|
||||
|
||||
if (recipientIds.length === 0) {
|
||||
// 수신자가 지정되지 않은 경우 전체 알림
|
||||
return await this.create({
|
||||
const id = await this.create({
|
||||
type,
|
||||
title,
|
||||
message,
|
||||
@@ -172,6 +269,9 @@ const notificationModel = {
|
||||
reference_id,
|
||||
created_by
|
||||
});
|
||||
// Push (broadcast)
|
||||
sendPushToUsers([], { title, body: message || '', url: link_url || '/' });
|
||||
return id;
|
||||
}
|
||||
|
||||
// 지정된 수신자 각각에게 알림 생성
|
||||
@@ -190,6 +290,9 @@ const notificationModel = {
|
||||
results.push(notificationId);
|
||||
}
|
||||
|
||||
// Push 전송
|
||||
sendPushToUsers(recipientIds, { title, body: message || '', url: link_url || '/' });
|
||||
|
||||
return results;
|
||||
}
|
||||
};
|
||||
|
||||
52
system1-factory/api/models/pushSubscriptionModel.js
Normal file
52
system1-factory/api/models/pushSubscriptionModel.js
Normal file
@@ -0,0 +1,52 @@
|
||||
// models/pushSubscriptionModel.js
|
||||
const { getDb } = require('../dbPool');
|
||||
|
||||
const pushSubscriptionModel = {
|
||||
async subscribe(userId, subscription) {
|
||||
const db = await getDb();
|
||||
const { endpoint, keys } = subscription;
|
||||
await db.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 db = await getDb();
|
||||
await db.query('DELETE FROM push_subscriptions WHERE endpoint = ?', [endpoint]);
|
||||
},
|
||||
|
||||
async getByUserId(userId) {
|
||||
const db = await getDb();
|
||||
const [rows] = await db.query(
|
||||
'SELECT * FROM push_subscriptions WHERE user_id = ?',
|
||||
[userId]
|
||||
);
|
||||
return rows;
|
||||
},
|
||||
|
||||
async getByUserIds(userIds) {
|
||||
if (!userIds || userIds.length === 0) return [];
|
||||
const db = await getDb();
|
||||
const [rows] = await db.query(
|
||||
'SELECT * FROM push_subscriptions WHERE user_id IN (?)',
|
||||
[userIds]
|
||||
);
|
||||
return rows;
|
||||
},
|
||||
|
||||
async getAll() {
|
||||
const db = await getDb();
|
||||
const [rows] = await db.query('SELECT * FROM push_subscriptions');
|
||||
return rows;
|
||||
},
|
||||
|
||||
async deleteByEndpoint(endpoint) {
|
||||
const db = await getDb();
|
||||
await db.query('DELETE FROM push_subscriptions WHERE endpoint = ?', [endpoint]);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = pushSubscriptionModel;
|
||||
Reference in New Issue
Block a user