File size: 2,689 Bytes
486340a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const { db } = require('../config/firebase');
const { pushToUser } = require('../utils/notificationHelper');

const sendNotification = async (req, res) => {
  const { userId, title, body, data } = req.body;
  if (!userId || !title || !body) {
    return res.status(400).json({ success: false, error: 'userId, title, body required', code: 'MISSING_FIELDS' });
  }
  try {
    await pushToUser(userId, { title, body, type: data?.type || 'general', data });
    return res.json({ success: true, message: 'Notification sent' });
  } catch (err) {
    return res.status(500).json({ success: false, error: err.message, code: 'NOTIFICATION_ERROR' });
  }
};

const getMyNotifications = async (req, res) => {
  try {
    const snap = await db
      .collection('notifications')
      .where('userId', '==', req.user.uid)
      .limit(50)
      .get();
    const notifications = snap.docs
      .map((d) => ({ id: d.id, ...d.data() }))
      .sort((a, b) =>
        String(b.createdAt || '').localeCompare(String(a.createdAt || '')),
      );
    return res.json({ success: true, notifications });
  } catch (err) {
    return res.status(500).json({ success: false, error: err.message, code: 'GET_NOTIFICATIONS_ERROR' });
  }
};

const getNotificationSummary = async (req, res) => {
  try {
    const snap = await db
      .collection('notifications')
      .where('userId', '==', req.user.uid)
      .where('read', '==', false)
      .limit(100)
      .get();
    const byType = {};
    const byScreen = {};
    snap.docs.forEach((doc) => {
      const n = doc.data() || {};
      const type = (n.type || 'general').toString();
      const screen = (n.data?.screen || '').toString();
      byType[type] = (byType[type] || 0) + 1;
      if (screen) byScreen[screen] = (byScreen[screen] || 0) + 1;
    });
    return res.json({
      success: true,
      unreadCount: snap.size,
      byType,
      byScreen,
    });
  } catch (err) {
    return res.status(500).json({ success: false, error: err.message, code: 'NOTIFICATION_SUMMARY_ERROR' });
  }
};

const markNotificationsRead = async (req, res) => {
  try {
    const snap = await db
      .collection('notifications')
      .where('userId', '==', req.user.uid)
      .where('read', '==', false)
      .get();
    const batch = db.batch();
    snap.docs.forEach((doc) => batch.update(doc.ref, { read: true }));
    await batch.commit();
    return res.json({ success: true, message: 'Notifications marked read' });
  } catch (err) {
    return res.status(500).json({ success: false, error: err.message, code: 'MARK_READ_ERROR' });
  }
};

module.exports = { sendNotification, getMyNotifications, getNotificationSummary, markNotificationsRead };