Files
tk-factory-services/user-management/api/models/pushSubscriptionModel.js
Hyungi Ahn 84cf222b81 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>
2026-03-17 15:56:41 +09:00

93 lines
2.5 KiB
JavaScript

// models/pushSubscriptionModel.js
const { getPool } = require('./userModel');
const pushSubscriptionModel = {
async subscribe(userId, subscription) {
const pool = getPool();
const { endpoint, keys } = subscription;
await pool.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 pool = getPool();
await pool.query('DELETE FROM push_subscriptions WHERE endpoint = ?', [endpoint]);
},
async getByUserId(userId) {
const pool = getPool();
const [rows] = await pool.query(
'SELECT * FROM push_subscriptions WHERE user_id = ?',
[userId]
);
return rows;
},
async getByUserIds(userIds) {
if (!userIds || userIds.length === 0) return [];
const pool = getPool();
const [rows] = await pool.query(
'SELECT * FROM push_subscriptions WHERE user_id IN (?)',
[userIds]
);
return rows;
},
async getAll() {
const pool = getPool();
const [rows] = await pool.query('SELECT * FROM push_subscriptions');
return rows;
},
async deleteByEndpoint(endpoint) {
const pool = getPool();
await pool.query('DELETE FROM push_subscriptions WHERE endpoint = ?', [endpoint]);
},
// === ntfy 구독 관련 ===
async getNtfyUserIds(userIds) {
if (!userIds || userIds.length === 0) return [];
const pool = getPool();
const [rows] = await pool.query(
'SELECT user_id FROM ntfy_subscriptions WHERE user_id IN (?)',
[userIds]
);
return rows.map(r => r.user_id);
},
async getAllNtfyUserIds() {
const pool = getPool();
const [rows] = await pool.query('SELECT user_id FROM ntfy_subscriptions');
return rows.map(r => r.user_id);
},
async ntfySubscribe(userId) {
const pool = getPool();
await pool.query(
'INSERT IGNORE INTO ntfy_subscriptions (user_id) VALUES (?)',
[userId]
);
},
async ntfyUnsubscribe(userId) {
const pool = getPool();
await pool.query('DELETE FROM ntfy_subscriptions WHERE user_id = ?', [userId]);
},
async isNtfySubscribed(userId) {
const pool = getPool();
const [rows] = await pool.query(
'SELECT 1 FROM ntfy_subscriptions WHERE user_id = ? LIMIT 1',
[userId]
);
return rows.length > 0;
}
};
module.exports = pushSubscriptionModel;