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:
Hyungi Ahn
2026-03-17 15:56:41 +09:00
parent afa10c044f
commit 84cf222b81
30 changed files with 244 additions and 172 deletions

View File

@@ -50,8 +50,6 @@ function setupRoutes(app) {
const workIssueRoutes = require('../routes/workIssueRoutes');
const departmentRoutes = require('../routes/departmentRoutes');
const patrolRoutes = require('../routes/patrolRoutes');
const notificationRoutes = require('../routes/notificationRoutes');
const pushSubscriptionRoutes = require('../routes/pushSubscriptionRoutes');
const purchaseRequestRoutes = require('../routes/purchaseRequestRoutes');
const purchaseRoutes = require('../routes/purchaseRoutes');
const settlementRoutes = require('../routes/settlementRoutes');
@@ -112,8 +110,6 @@ function setupRoutes(app) {
'/api/setup/check-data-status',
'/api/monthly-status/calendar',
'/api/monthly-status/daily-details',
'/api/push/vapid-public-key',
'/api/notifications/internal'
];
// 인증 미들웨어 - 공개 경로를 제외한 모든 API (rate limiter보다 먼저 실행)
@@ -164,8 +160,6 @@ function setupRoutes(app) {
app.use('/api/work-issues', workIssueRoutes); // 카테고리/아이템 + 신고 조회 (같은 MariaDB 공유)
app.use('/api/departments', departmentRoutes); // 부서 관리
app.use('/api/patrol', patrolRoutes); // 일일순회점검 시스템
app.use('/api/notifications', notificationRoutes); // 알림 시스템
app.use('/api/push', pushSubscriptionRoutes); // Push 구독
app.use('/api/purchase-requests', purchaseRequestRoutes); // 구매신청
app.use('/api/purchases', purchaseRoutes); // 구매 내역
app.use('/api/settlements', settlementRoutes); // 월간 정산

View File

@@ -1,203 +0,0 @@
// 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;

View File

@@ -1,107 +0,0 @@
// 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;

View File

@@ -120,21 +120,4 @@ process.on('uncaughtException', (error) => {
}
})();
// 오래된 알림 정리 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) logger.info(`오래된 알림 ${count}건 정리 완료`);
}).catch(err => {
logger.error('알림 정리 실패:', { error: err.message });
});
}
}
setInterval(runCleanup, 60000); // 1분마다 확인
})();
module.exports = app;

View File

@@ -1,6 +1,6 @@
// models/equipmentModel.js
const { getDb } = require('../dbPool');
const notificationModel = require('./notificationModel');
const notifyHelper = require('../utils/notifyHelper');
const EquipmentModel = {
// CREATE - 설비 생성
@@ -669,17 +669,16 @@ const EquipmentModel = {
['repair_needed', requestData.equipment_id]
);
try {
await notificationModel.createRepairNotification({
equipment_id: requestData.equipment_id,
equipment_name: requestData.equipment_name || '설비',
repair_type: requestData.repair_type || '일반 수리',
request_id: result.insertId,
created_by: requestData.reported_by
});
} catch (notifError) {
// 알림 생성 실패해도 수리 신청은 성공으로 처리
}
// fire-and-forget: 알림 실패가 수리 신청을 블로킹하면 안 됨
notifyHelper.send({
type: 'repair',
title: `수리 신청: ${requestData.equipment_name || '설비'}`,
message: `${requestData.repair_type || '일반 수리'} 수리가 신청되었습니다.`,
link_url: '/pages/admin/repair-management.html',
reference_type: 'work_issue_reports',
reference_id: result.insertId,
created_by: requestData.reported_by
}).catch(() => {});
return {
report_id: result.insertId,

View File

@@ -1,324 +0,0 @@
// 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 전송 헬퍼 — 알림 생성 후 호출 (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);
}
}
// 순환 참조를 피하기 위해 함수 내에서 require
async function getRecipientIds(notificationType) {
const db = await getDb();
const [rows] = await db.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 db = await getDb();
const { user_id, type, title, message, link_url, reference_type, reference_id, created_by } = notificationData;
const [result] = await db.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]
);
// Redis 캐시 무효화
this._invalidateCache(user_id);
return result.insertId;
},
// 읽지 않은 알림 조회 (특정 사용자 또는 전체)
async getUnread(userId = null) {
const db = await getDb();
const [rows] = await db.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 db = await getDb();
const offset = (page - 1) * limit;
const [rows] = await db.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 db.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 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;
},
// 모든 알림 읽음 처리
async markAllAsRead(userId = null) {
const db = await getDb();
const [result] = await db.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 db = await getDb();
const [result] = await db.query(
`DELETE FROM notifications WHERE notification_id = ?`,
[notificationId]
);
return result.affectedRows > 0;
},
// 오래된 알림 삭제 (30일 이상)
async deleteOld(days = 30) {
const db = await getDb();
const [result] = await db.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 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;
// 수리 알림 수신자 목록 가져오기
const recipientIds = await getRecipientIds('repair');
if (recipientIds.length === 0) {
// 수신자가 지정되지 않은 경우 전체 알림 (user_id = null)
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
});
// Push (broadcast)
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);
}
// Push 전송
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
});
// Push (broadcast)
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);
}
// Push 전송
sendPushToUsers(recipientIds, { title, body: message || '', url: link_url || '/' });
return results;
}
};
module.exports = notificationModel;

View File

@@ -92,19 +92,15 @@ const PurchaseModel = {
} catch (err) {
console.error('[purchase] 설비 자동 등록 실패:', err.message);
// admin 알림 전송
try {
const notificationModel = require('./notificationModel');
await notificationModel.createTypedNotification({
type: 'equipment',
title: `설비 자동 등록 실패: ${purchaseData.item_name}`,
message: `구매 완료 후 설비 자동 등록에 실패했습니다. 수동으로 등록해주세요. 오류: ${err.message}`,
link_url: '/pages/admin/equipments.html',
created_by: purchaseData.purchaser_id
});
} catch (notifErr) {
console.error('[purchase] 설비 등록 실패 알림 전송 오류:', notifErr.message);
}
// fire-and-forget: admin 알림 전송
const notifyHelper = require('../utils/notifyHelper');
notifyHelper.send({
type: 'equipment',
title: `설비 자동 등록 실패: ${purchaseData.item_name}`,
message: `구매 완료 후 설비 자동 등록 실패했습니다. 수동으로 등록해주세요. 오류: ${err.message}`,
link_url: '/pages/admin/equipments.html',
created_by: purchaseData.purchaser_id
}).catch(() => {});
return { success: false, error: err.message };
}

View File

@@ -1,92 +0,0 @@
// 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]);
},
// === ntfy 구독 관련 ===
async getNtfyUserIds(userIds) {
if (!userIds || userIds.length === 0) return [];
const db = await getDb();
const [rows] = await db.query(
'SELECT user_id FROM ntfy_subscriptions WHERE user_id IN (?)',
[userIds]
);
return rows.map(r => r.user_id);
},
async getAllNtfyUserIds() {
const db = await getDb();
const [rows] = await db.query('SELECT user_id FROM ntfy_subscriptions');
return rows.map(r => r.user_id);
},
async ntfySubscribe(userId) {
const db = await getDb();
await db.query(
'INSERT IGNORE INTO ntfy_subscriptions (user_id) VALUES (?)',
[userId]
);
},
async ntfyUnsubscribe(userId) {
const db = await getDb();
await db.query('DELETE FROM ntfy_subscriptions WHERE user_id = ?', [userId]);
},
async isNtfySubscribed(userId) {
const db = await getDb();
const [rows] = await db.query(
'SELECT 1 FROM ntfy_subscriptions WHERE user_id = ? LIMIT 1',
[userId]
);
return rows.length > 0;
}
};
module.exports = pushSubscriptionModel;

View File

@@ -39,8 +39,7 @@
"qrcode": "^1.5.4",
"redis": "^5.9.0",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.1",
"web-push": "^3.6.7"
"swagger-ui-express": "^5.0.1"
},
"devDependencies": {
"@types/jest": "^29.5.12",

View File

@@ -1,34 +0,0 @@
// routes/notificationRoutes.js
const express = require('express');
const router = express.Router();
const notificationController = require('../controllers/notificationController');
const { requireAuth, requireMinLevel } = require('../middlewares/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;

View File

@@ -1,19 +0,0 @@
// routes/pushSubscriptionRoutes.js
const express = require('express');
const router = express.Router();
const pushController = require('../controllers/pushSubscriptionController');
const { requireAuth } = require('../middlewares/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;

View File

@@ -0,0 +1,63 @@
// utils/notifyHelper.js — 공용 알림 헬퍼
// tkuser-api의 내부 알림 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;

View File

@@ -1,31 +0,0 @@
// 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 };

View File

@@ -1,41 +0,0 @@
// 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);
})
);
});

View File

@@ -299,7 +299,7 @@ async function initAuth() {
/* ===== 알림 벨 ===== */
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);
}