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:
Hyungi Ahn
2026-03-13 15:01:44 +09:00
parent 1ad82fd52c
commit 7fd646e9ba
102 changed files with 1446 additions and 94 deletions

View File

@@ -5,6 +5,7 @@
const workIssueModel = require('../models/workIssueModel');
const imageUploadService = require('../services/imageUploadService');
const mProjectService = require('../services/mProjectService');
const notify = require('../utils/notifyHelper');
// ==================== 신고 카테고리 관리 ====================
@@ -191,6 +192,17 @@ exports.createReport = async (req, res) => {
const reportId = await workIssueModel.createReport(reportData);
// 알림: 신고 접수
notify.send({
type: 'safety',
title: '신고 접수',
message: `${catInfo ? catInfo.category_name : '문제'} 신고가 접수되었습니다.`,
link_url: '/pages/safety-report-list.html',
reference_type: 'work_issue_reports',
reference_id: reportId,
created_by: req.user.id || req.user.user_id
});
res.status(201).json({
success: true,
message: '문제 신고가 등록되었습니다.',
@@ -398,6 +410,18 @@ exports.receiveReport = async (req, res) => {
try {
const { id } = req.params;
await workIssueModel.receiveReport(id, req.user.user_id);
// 알림: 신고 상태 변경 → 접수됨
notify.send({
type: 'safety',
title: '신고 접수 확인',
message: '신고가 접수되어 처리가 시작됩니다.',
link_url: '/pages/safety-report-list.html',
reference_type: 'work_issue_reports',
reference_id: parseInt(id),
created_by: req.user.user_id
});
res.json({ success: true, message: '신고가 접수되었습니다.' });
} catch (err) {
console.error('신고 접수 실패:', err);
@@ -442,6 +466,18 @@ exports.completeReport = async (req, res) => {
if (resolution_photos[1]) resolution_photo_path2 = await imageUploadService.saveBase64Image(resolution_photos[1], 'resolution');
await workIssueModel.completeReport(id, { resolution_notes, resolution_photo_path1, resolution_photo_path2, resolved_by: req.user.user_id });
// 알림: 신고 처리 완료
notify.send({
type: 'safety',
title: '신고 처리 완료',
message: '신고 처리가 완료되었습니다.',
link_url: '/pages/safety-report-list.html',
reference_type: 'work_issue_reports',
reference_id: parseInt(id),
created_by: req.user.user_id
});
res.json({ success: true, message: '처리가 완료되었습니다.' });
} catch (err) {
console.error('처리 완료 실패:', err);

View File

@@ -0,0 +1,63 @@
// utils/notifyHelper.js — 공용 알림 헬퍼
// system1-factory의 내부 알림 API를 통해 DB 저장 + Push 전송
const http = require('http');
const NOTIFY_URL = 'http://system1-api:3005/api/notifications/internal';
const SERVICE_KEY = process.env.INTERNAL_SERVICE_KEY || '';
const notifyHelper = {
/**
* 알림 전송
* @param {Object} opts
* @param {string} opts.type - 알림 유형 (safety, maintenance, repair, system)
* @param {string} opts.title - 알림 제목
* @param {string} [opts.message] - 알림 내용
* @param {string} [opts.link_url] - 클릭 시 이동 URL
* @param {string} [opts.reference_type] - 연관 테이블명
* @param {number} [opts.reference_id] - 연관 레코드 ID
* @param {number} [opts.created_by] - 생성자 user_id
*/
async send(opts) {
try {
const body = JSON.stringify(opts);
const url = new URL(NOTIFY_URL);
return new Promise((resolve) => {
const req = http.request({
hostname: url.hostname,
port: url.port,
path: url.pathname,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Internal-Service-Key': SERVICE_KEY,
'Content-Length': Buffer.byteLength(body)
},
timeout: 5000
}, (res) => {
res.resume(); // drain
resolve(true);
});
req.on('error', (err) => {
console.error('[notifyHelper] 알림 전송 실패:', err.message);
resolve(false);
});
req.on('timeout', () => {
req.destroy();
console.error('[notifyHelper] 알림 전송 타임아웃');
resolve(false);
});
req.write(body);
req.end();
});
} catch (err) {
console.error('[notifyHelper] 알림 전송 오류:', err.message);
return false;
}
}
};
module.exports = notifyHelper;

View File

@@ -1,10 +1,14 @@
// /js/api-base.js
// API 기본 설정 및 보안 유틸리티 - System 2 (신고 시스템)
// 서비스 워커 해제 (캐시 간섭으로 인한 인증 루프 방지)
// 서비스 워커 해제 (push-sw.js 제외)
if ('serviceWorker' in navigator) {
navigator.serviceWorker.getRegistrations().then(function(registrations) {
registrations.forEach(function(registration) { registration.unregister(); });
registrations.forEach(function(registration) {
if (!registration.active || !registration.active.scriptURL.includes('push-sw.js')) {
registration.unregister();
}
});
});
if (typeof caches !== 'undefined') {
caches.keys().then(function(names) {
@@ -145,5 +149,13 @@ if ('serviceWorker' in navigator) {
return response.json();
};
// 알림 벨 로드
window._loadNotificationBell = function() {
var h = window.location.hostname;
var s = document.createElement('script');
s.src = (h.includes('technicalkorea.net') ? 'https://tkfb.technicalkorea.net' : window.location.protocol + '//' + h + ':30000') + '/shared/notification-bell.js?v=1';
document.head.appendChild(s);
};
console.log('[System2] API 설정 완료:', window.API_BASE_URL);
})();

View File

@@ -90,6 +90,9 @@
var token = window.getSSOToken ? window.getSSOToken() : null;
if (token && !localStorage.getItem('sso_token')) localStorage.setItem('sso_token', token);
console.log('[System2] 인증 확인:', currentUser.username);
// 알림 벨 로드
if (window._loadNotificationBell) window._loadNotificationBell();
}
// DOMContentLoaded 시 실행

View File

@@ -6,8 +6,8 @@
<title>AI 신고 도우미 | (주)테크니컬코리아</title>
<link rel="icon" type="image/png" href="/img/favicon.png">
<link rel="stylesheet" href="/css/chat-report.css?v=3">
<script src="/js/api-base.js?v=20260309"></script>
<script src="/js/app-init.js?v=20260309" defer></script>
<script src="/js/api-base.js?v=20260313"></script>
<script src="/js/app-init.js?v=20260313" defer></script>
</head>
<body>
<!-- Header -->

View File

@@ -8,8 +8,8 @@
<link rel="stylesheet" href="/css/common.css?v=2">
<link rel="stylesheet" href="/css/project-management.css?v=3">
<link rel="icon" type="image/png" href="/img/favicon.png">
<script src="/js/api-base.js?v=20260309"></script>
<script src="/js/app-init.js?v=20260309" defer></script>
<script src="/js/api-base.js?v=20260313"></script>
<script src="/js/app-init.js?v=20260313" defer></script>
<script src="https://instant.page/5.2.0" type="module"></script>
<style>
/* 상태 배지 */

View File

@@ -5,8 +5,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>신고 등록 | (주)테크니컬코리아</title>
<link rel="icon" type="image/png" href="/img/favicon.png">
<script src="/js/api-base.js?v=20260309"></script>
<script src="/js/app-init.js?v=20260309" defer></script>
<script src="/js/api-base.js?v=20260313"></script>
<script src="/js/app-init.js?v=20260313" defer></script>
<style>
* { box-sizing: border-box; }
body {

View File

@@ -8,8 +8,8 @@
<link rel="stylesheet" href="/css/common.css?v=2">
<link rel="stylesheet" href="/css/project-management.css?v=3">
<link rel="icon" type="image/png" href="/img/favicon.png">
<script src="/js/api-base.js?v=20260309"></script>
<script src="/js/app-init.js?v=20260309" defer></script>
<script src="/js/api-base.js?v=20260313"></script>
<script src="/js/app-init.js?v=20260313" defer></script>
<script src="https://instant.page/5.2.0" type="module"></script>
<style>
/* 통계 카드 */

View File

@@ -0,0 +1,41 @@
// Push Notification Service Worker
// 캐싱 없음 — Push 수신 전용
self.addEventListener('push', function(event) {
var data = { title: '알림', body: '새 알림이 있습니다.', url: '/' };
if (event.data) {
try { data = Object.assign(data, event.data.json()); } catch(e) {}
}
event.waitUntil(
self.registration.showNotification(data.title, {
body: data.body,
icon: '/static/img/icon-192.png',
badge: '/static/img/badge-72.png',
data: { url: data.url || '/' },
tag: 'tk-notification-' + Date.now(),
renotify: true
})
);
// 메인 페이지에 뱃지 갱신 신호 전송
self.clients.matchAll({ type: 'window' }).then(function(clients) {
clients.forEach(function(client) {
client.postMessage({ type: 'NOTIFICATION_RECEIVED' });
});
});
});
self.addEventListener('notificationclick', function(event) {
event.notification.close();
var url = (event.notification.data && event.notification.data.url) || '/';
event.waitUntil(
self.clients.matchAll({ type: 'window' }).then(function(clients) {
for (var i = 0; i < clients.length; i++) {
if (clients[i].url.includes(self.location.origin)) {
clients[i].navigate(url);
return clients[i].focus();
}
}
return self.clients.openWindow(url);
})
);
});