Files
tk-factory-services/system1-factory/api/models/pushSubscriptionModel.js
Hyungi Ahn 1cef745cc9 feat(ntfy): Phase 2 — sendPushToUsers() ntfy 연동 + 구독 관리 UI
- ntfy_subscriptions 테이블 마이그레이션 추가
- ntfySender.js 유틸 (ntfy HTTP POST 래퍼)
- sendPushToUsers() ntfy 우선 분기 (ntfy 구독자 → ntfy, 나머지 → Web Push)
- ntfy subscribe/unsubscribe/status API 엔드포인트
- notification-bell.js ntfy 토글 버튼 + 앱 설정 안내 모달
- docker-compose system1-api에 NTFY 환경변수 추가

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-17 15:01:03 +09:00

93 lines
2.5 KiB
JavaScript

// 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;