- 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>
64 lines
1.9 KiB
JavaScript
64 lines
1.9 KiB
JavaScript
// utils/notifyHelper.js — 공용 알림 헬퍼
|
|
// system1-factory의 내부 알림 API를 통해 DB 저장 + Push 전송
|
|
const http = require('http');
|
|
|
|
const NOTIFY_URL = 'http://tkuser-api:3000/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;
|