• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

atlp-rwanda / e-commerce-bitcrafters-bn / 0568eb11-ca70-42be-b431-0b473cd86bfc

18 Jun 2024 08:11AM UTC coverage: 93.493% (-0.3%) from 93.747%
0568eb11-ca70-42be-b431-0b473cd86bfc

push

circleci

web-flow
Fix: Adding pagination and correcting all user retrieval (#107)

458 of 509 branches covered (89.98%)

Branch coverage included in aggregate %.

45 of 49 new or added lines in 6 files covered. (91.84%)

1 existing line in 1 file now uncovered.

1496 of 1581 relevant lines covered (94.62%)

5.16 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.67
/src/controllers/NotificationController.ts
1
import { Request, Response } from 'express'
2
import Notification,{NotificationAttributes} from '../database/models/notificationModel'
2✔
3
import Paginator from '../utils/paginator'
2✔
4
import { getNotificationCount } from '../services/notificationServices';
2✔
5

6
/**
7
 * Controller class for managing notifications-related operations ..
8
 */
9
class NotificationController {
10
  /**
11
   * Get notifications for the authenticated user
12
   * @param {Request} req - Express request object
13
   * @param {Response} res - Express response object
14
   * @returns {Promise<Response>} Promise that resolves to an Express response
15
   */
16
  static async getNotifications(
17
    req: Request,
18
    res: Response,
19
  ): Promise<Response> {
20
    try {
4✔
21
      const userId = req.user.id
4✔
22
      const paginationResults = Paginator(req, res)
4✔
23

24
      if(!paginationResults){
2!
NEW
25
        return res
×
26
    .status(400)
27
    .json({ message: 'Invalid pagination parameters' })
28
  }
29
  
30
  const {offset, limit, page} = paginationResults
2✔
31
  
32
  const totalCount: number = await getNotificationCount(userId)
2✔
33
  const totalPages = Math.ceil(totalCount / limit)
2✔
34

35
      const notifications = await Notification.findAll({
2✔
36
        where: { userId },
37
        order: [['createdAt', 'DESC']],
38
        limit,
39
        offset
40
      })
41

42
      return res.status(200).json({ 
2✔
43
        message: 'Notifications retrieved successfully',
44
        notifications,
45
        pagination: { limit, page, totalPages },
46
      })
47
    } catch (error) {
48
      return res.status(500).json({ message: 'Internal server error' })
2✔
49
    }
50
  }
51
  
52
  /**
53
   * Get a single notification by ID and mark it as read.
54
   * @param {AuthenticatedRequest} req - Express request object
55
   * @param {Response} res - Express response object
56
   * @returns {Promise<Response>} Promise that resolves to an Express response
57
   */
58
  static async getSingleNotification(
59
    req: Request,
60
    res: Response,
61
  ): Promise<Response> {
62
    try {
8✔
63
      const { notificationId } = req.params;
8✔
64
      const userId = req.user.id;
8✔
65

66
      const notification = await Notification.findOne({
8✔
67
        where: { id: notificationId, userId },
68
      });
69

70
      if (!notification) {
6✔
71
        return res.status(404).json({ message: 'Notification not found' });
2✔
72
      }
73

74
      if (!notification.isRead) {
4✔
75
        await notification.update({ isRead: true });
2✔
76
      }
77

78
      return res.status(200).json({ data: notification });
4✔
79
    } catch (error) {
80

81
      return res.status(500).json({ message: 'Internal server error', error: error.message });
2✔
82
    }
83
  }
84

85
/**
86
 * Mark a notification as read or unread
87
 * @param {Request} req - Express request object
88
 * @param {Response} res - Express response object
89
 * @returns {Promise<Response>} Promise that resolves to an Express response
90
 */
91
static async changeNotificationStatus(
92
  req: Request,
93
  res: Response,
94
): Promise<Response> {
95
  try {
8✔
96
    const { notificationId } = req.params;
8✔
97

98
    const notification = await Notification.findOne({
8✔
99
      where: { id: notificationId },
100
    });
101

102
    if (!notification) {
6✔
103
      return res.status(404).json({ message: 'Notification not found' });
2✔
104
    }
105

106
    const notificationStatus = !notification.isRead;
4✔
107
    await notification.update({ isRead: notificationStatus });
4✔
108

109
    const statusMessage = notificationStatus ? 'Notification is now read' : 'Notification is now unread';
4✔
110
    return res.status(200).json({ message: statusMessage });
4✔
111
  } catch (error) {
112
    return res
2✔
113
      .status(500)
114
      .json({ message: 'Internal Server Error', error: error.message });
115
  }
116
}
117

118

119
  /**
120
 * Mark all notifications as read or unread
121
 * @param {Request} req - Express request object
122
 * @param {Response} res - Express response object
123
 * @returns {Promise<Response>} Promise that resolves to an Express response
124
 */
125
static async changeAllNotificationStatus(
126
  req: Request,
127
  res: Response
128
): Promise<Response> {
129
  try {
8✔
130
    const userId = req.user.id;
8✔
131
    const notifications = await Notification.findAll({
8✔
132
      where: { userId: userId },
133
    });
134

135
    if (notifications.length === 0) {
6✔
136
      return res.status(404).json({ message: 'No notifications found' });
2✔
137
    }
138

139

140
    const allRead = notifications.every(notification => notification.isRead);
6✔
141
    const newIsReadStatus = !allRead;
4✔
142

143
    await Promise.all(notifications.map(notification => notification.update({ isRead: newIsReadStatus })));
8✔
144

145
    const statusMessage = newIsReadStatus ? 'All notifications are now read' : 'All notifications are now unread';
4✔
146
    return res.status(200).json({ message: statusMessage });
4✔
147
  } catch (error) {
148
    return res
2✔
149
      .status(500)
150
      .json({ message: 'Internal Server Error', error: error.message });
151
  }
152
}
153
}
154

155
export default NotificationController
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc