const webpush = require('web-push'); const User = require('../models/User'); // Configure Keys (Ensure these are in your .env) webpush.setVapidDetails( process.env.VAPID_EMAIL, process.env.VAPID_PUBLIC_KEY, process.env.VAPID_PRIVATE_KEY ); // SEND TO ONE USER const sendToUser = async (userId, title, message, url = '/') => { try { const user = await User.findById(userId); if (user && user.pushSubscription) { await webpush.sendNotification( user.pushSubscription, JSON.stringify({ title, body: message, url }) ); } } catch (err) { // 410 = Gone (User unsubscribed/cleared cache) if (err.statusCode === 410) { await User.findByIdAndUpdate(userId, { $unset: { pushSubscription: "" } }); } console.error("Push Error (Single):", err.statusCode); } }; // SEND TO EVERYONE (Batching for performance) const sendGlobal = async (title, message, url = '/') => { try { // Fetch users who have a subscription const users = await User.find({ pushSubscription: { $exists: true } }); const payload = JSON.stringify({ title, body: message, url }); console.log(`📢 Sending Global Push to ${users.length} devices...`); const promises = users.map(user => webpush.sendNotification(user.pushSubscription, payload).catch(err => { if (err.statusCode === 410) { User.findByIdAndUpdate(user._id, { $unset: { pushSubscription: "" } }); } }) ); await Promise.all(promises); } catch (err) { console.error("Push Error (Global):", err); } }; module.exports = { sendToUser, sendGlobal };