Skip to content

Commit 186c3ae

Browse files
committed
fix: validate ids in lb user preferences
1 parent e6df7e6 commit 186c3ae

1 file changed

Lines changed: 58 additions & 20 deletions

File tree

src/controllers/lbdashboard/lbuserPrefController.js

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
1+
const mongoose = require('mongoose');
2+
13
const lbUserPrefController = function (UserPreferences, Notification) {
4+
const normalizeObjectId = (value) => {
5+
if (typeof value !== 'string') return null;
6+
7+
const trimmed = value.trim();
8+
if (!mongoose.Types.ObjectId.isValid(trimmed)) return null;
9+
10+
return trimmed;
11+
};
12+
13+
const normalizeObjectIdList = (values) => {
14+
if (!Array.isArray(values)) return null;
15+
16+
const normalizedIds = values.map(normalizeObjectId);
17+
return normalizedIds.every(Boolean) ? normalizedIds : null;
18+
};
19+
220
const normalizePhone = (phone) => {
321
if (!phone) return { normalized: '', last4: '' };
422
const trimmed = String(phone).trim();
@@ -17,22 +35,30 @@ const lbUserPrefController = function (UserPreferences, Notification) {
1735
const getPreferences = async (req, res) => {
1836
try {
1937
const { userId, selectedUserId } = req.body;
38+
const normalizedUserId = normalizeObjectId(userId);
39+
const normalizedSelectedUserId = selectedUserId
40+
? normalizeObjectId(selectedUserId)
41+
: null;
42+
43+
if (!normalizedUserId) {
44+
return res.status(400).json({ message: 'A valid user ID is required.' });
45+
}
2046

21-
if (!userId) {
22-
return res.status(400).json({ message: 'User ID is required.' });
47+
if (selectedUserId && !normalizedSelectedUserId) {
48+
return res.status(400).json({ message: 'Selected user ID must be a valid ID.' });
2349
}
2450

25-
const preferences = await UserPreferences.findOne({ user: userId }).populate(
51+
const preferences = await UserPreferences.findOne({ user: normalizedUserId }).populate(
2652
'users.userNotifyingFor',
2753
);
2854

2955
if (!preferences) {
3056
return res.status(404).json({ message: 'Preferences not found for the user.' });
3157
}
3258

33-
if (selectedUserId) {
59+
if (normalizedSelectedUserId) {
3460
const selectedUserPref = preferences.users.find(
35-
(pref) => pref.userNotifyingFor._id.toString() === selectedUserId,
61+
(pref) => pref.userNotifyingFor._id.toString() === normalizedSelectedUserId,
3662
);
3763

3864
return res.status(200).json(selectedUserPref || { notifyInApp: false, notifyEmail: false });
@@ -50,25 +76,33 @@ const lbUserPrefController = function (UserPreferences, Notification) {
5076
const updatePreferences = async (req, res) => {
5177
try {
5278
const { userId, selectedUserId, notifyInApp, notifyEmail, notifySms, smsPhone } = req.body;
79+
const normalizedUserId = normalizeObjectId(userId);
80+
const normalizedSelectedUserId = selectedUserId
81+
? normalizeObjectId(selectedUserId)
82+
: null;
83+
84+
if (!normalizedUserId) {
85+
return res.status(400).json({ message: 'A valid user ID is required.' });
86+
}
5387

54-
if (!userId) {
55-
return res.status(400).json({ message: 'User ID is required.' });
88+
if (selectedUserId && !normalizedSelectedUserId) {
89+
return res.status(400).json({ message: 'Selected user ID must be a valid ID.' });
5690
}
5791

58-
let preferences = await UserPreferences.findOne({ user: userId });
92+
let preferences = await UserPreferences.findOne({ user: normalizedUserId });
5993

6094
if (!preferences) {
61-
preferences = new UserPreferences({ user: userId, users: [] });
95+
preferences = new UserPreferences({ user: normalizedUserId, users: [] });
6296
}
6397

64-
if (selectedUserId) {
98+
if (normalizedSelectedUserId) {
6599
const userIndex = preferences.users.findIndex(
66-
(user) => user.userNotifyingFor.toString() === selectedUserId,
100+
(user) => user.userNotifyingFor.toString() === normalizedSelectedUserId,
67101
);
68102

69103
if (userIndex === -1) {
70104
preferences.users.push({
71-
userNotifyingFor: selectedUserId,
105+
userNotifyingFor: normalizedSelectedUserId,
72106
notifyInApp: notifyInApp !== undefined ? notifyInApp : false,
73107
notifyEmail: notifyEmail !== undefined ? notifyEmail : false,
74108
});
@@ -117,15 +151,17 @@ const lbUserPrefController = function (UserPreferences, Notification) {
117151
const storeNotification = async (req, res) => {
118152
try {
119153
const { userId, senderId, message } = req.body;
154+
const normalizedUserId = normalizeObjectId(userId);
155+
const normalizedSenderId = normalizeObjectId(senderId);
120156

121-
if (!userId || !senderId || !message) {
157+
if (!normalizedUserId || !normalizedSenderId || !message) {
122158
return res.status(400).json({ message: 'User ID, Sender ID, and Message are required.' });
123159
}
124160

125161
const notification = new Notification({
126162
message,
127-
sender: senderId,
128-
recipient: userId,
163+
sender: normalizedSenderId,
164+
recipient: normalizedUserId,
129165
isSystemGenerated: false,
130166
});
131167

@@ -140,13 +176,14 @@ const lbUserPrefController = function (UserPreferences, Notification) {
140176
const getUnreadNotifications = async (req, res) => {
141177
try {
142178
const { userId } = req.params;
179+
const normalizedUserId = normalizeObjectId(userId);
143180

144-
if (!userId) {
181+
if (!normalizedUserId) {
145182
console.error('❌ User ID is missing in the request.');
146-
return res.status(400).json({ message: 'User ID is required.' });
183+
return res.status(400).json({ message: 'A valid user ID is required.' });
147184
}
148185

149-
const notifications = await Notification.find({ recipient: userId, isRead: false })
186+
const notifications = await Notification.find({ recipient: normalizedUserId, isRead: false })
150187
.sort({ createdTimeStamps: -1 })
151188
.populate('sender', 'firstName lastName'); // Include sender's name
152189

@@ -162,12 +199,13 @@ const lbUserPrefController = function (UserPreferences, Notification) {
162199
const markNotificationsAsRead = async (req, res) => {
163200
try {
164201
const { notificationIds } = req.body;
202+
const normalizedNotificationIds = normalizeObjectIdList(notificationIds);
165203

166-
if (!notificationIds || !Array.isArray(notificationIds)) {
204+
if (!normalizedNotificationIds) {
167205
return res.status(400).json({ message: 'Invalid notification IDs.' });
168206
}
169207
const result = await Notification.updateMany(
170-
{ _id: { $in: notificationIds } },
208+
{ _id: { $in: normalizedNotificationIds } },
171209
{ isRead: true },
172210
);
173211

0 commit comments

Comments
 (0)