-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathlbuserPrefController.js
More file actions
230 lines (194 loc) · 8.04 KB
/
lbuserPrefController.js
File metadata and controls
230 lines (194 loc) · 8.04 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
const mongoose = require('mongoose');
const lbUserPrefController = function (UserPreferences, Notification) {
const normalizeObjectId = (value) => {
if (typeof value !== 'string') return null;
const trimmed = value.trim();
if (!mongoose.Types.ObjectId.isValid(trimmed)) return null;
return trimmed;
};
const normalizeObjectIdList = (values) => {
if (!Array.isArray(values)) return null;
const normalizedIds = values.map(normalizeObjectId);
return normalizedIds.every(Boolean) ? normalizedIds : null;
};
const normalizePhone = (phone) => {
if (!phone) return { normalized: '', last4: '' };
const trimmed = String(phone).trim();
const hasPlus = trimmed.startsWith('+');
const digits = trimmed.replace(/\D/g, '');
const normalized = hasPlus ? `+${digits}` : digits;
return { normalized, last4: digits.slice(-4) };
};
const maskPhone = (phone) => {
if (!phone) return '';
const digits = String(phone).replace(/\D/g, '');
if (digits.length <= 4) return digits;
return `***-***-${digits.slice(-4)}`;
};
const getPreferences = async (req, res) => {
try {
const { userId, selectedUserId } = req.body;
const normalizedUserId = normalizeObjectId(userId);
const normalizedSelectedUserId = selectedUserId
? normalizeObjectId(selectedUserId)
: null;
if (!normalizedUserId) {
return res.status(400).json({ message: 'A valid user ID is required.' });
}
if (selectedUserId && !normalizedSelectedUserId) {
return res.status(400).json({ message: 'Selected user ID must be a valid ID.' });
}
const preferences = await UserPreferences.findOne({ user: normalizedUserId }).populate(
'users.userNotifyingFor',
);
if (!preferences) {
return res.status(404).json({ message: 'Preferences not found for the user.' });
}
if (normalizedSelectedUserId) {
const selectedUserPref = preferences.users.find(
(pref) => pref.userNotifyingFor._id.toString() === normalizedSelectedUserId,
);
return res.status(200).json(selectedUserPref || { notifyInApp: false, notifyEmail: false });
}
const response = preferences.toObject();
response.smsPhoneMasked = maskPhone(preferences.smsPhone);
res.status(200).json(response);
} catch (error) {
console.error('Error fetching preferences:', error);
res.status(500).json({ message: 'Error fetching preferences', error: error.message });
}
};
const updatePreferences = async (req, res) => {
try {
const { userId, selectedUserId, notifyInApp, notifyEmail, notifySms, smsPhone } = req.body;
const normalizedUserId = normalizeObjectId(userId);
const normalizedSelectedUserId = selectedUserId
? normalizeObjectId(selectedUserId)
: null;
if (!normalizedUserId) {
return res.status(400).json({ message: 'A valid user ID is required.' });
}
if (selectedUserId && !normalizedSelectedUserId) {
return res.status(400).json({ message: 'Selected user ID must be a valid ID.' });
}
let preferences = await UserPreferences.findOne({ user: normalizedUserId });
if (!preferences) {
preferences = new UserPreferences({ user: normalizedUserId, users: [] });
}
if (normalizedSelectedUserId) {
const userIndex = preferences.users.findIndex(
(user) => user.userNotifyingFor.toString() === normalizedSelectedUserId,
);
if (userIndex === -1) {
preferences.users.push({
userNotifyingFor: normalizedSelectedUserId,
notifyInApp: notifyInApp !== undefined ? notifyInApp : false,
notifyEmail: notifyEmail !== undefined ? notifyEmail : false,
});
} else {
preferences.users[userIndex].notifyInApp =
notifyInApp !== undefined ? notifyInApp : false;
preferences.users[userIndex].notifyEmail =
notifyEmail !== undefined ? notifyEmail : false;
}
} else if (notifyInApp !== undefined || notifyEmail !== undefined) {
if (notifyInApp !== undefined) {
preferences.notifyInApp = notifyInApp;
}
if (notifyEmail !== undefined) {
preferences.notifyEmail = notifyEmail;
}
}
if (notifySms !== undefined || smsPhone !== undefined) {
const { normalized, last4 } = normalizePhone(smsPhone);
const digits = normalized.replace(/\D/g, '');
const existingDigits = String(preferences.smsPhone || '').replace(/\D/g, '');
if (notifySms && digits.length === 0 && existingDigits.length === 0) {
return res.status(400).json({ message: 'SMS phone number is required.' });
}
if (digits.length > 0 && (digits.length < 8 || digits.length > 15)) {
return res.status(400).json({ message: 'Invalid phone number format.' });
}
if (notifySms !== undefined) {
preferences.notifySms = notifySms;
}
if (smsPhone !== undefined && digits.length > 0) {
preferences.smsPhone = normalized;
preferences.smsPhoneLast4 = last4;
}
}
const updatedPreferences = await preferences.save();
res.status(200).json(updatedPreferences);
} catch (error) {
console.error('Error updating preferences:', error);
res.status(500).json({ message: 'Error updating preferences', error: error.message });
}
};
const storeNotification = async (req, res) => {
try {
const { userId, senderId, message } = req.body;
const normalizedUserId = normalizeObjectId(userId);
const normalizedSenderId = normalizeObjectId(senderId);
if (!normalizedUserId || !normalizedSenderId || !message) {
return res.status(400).json({ message: 'User ID, Sender ID, and Message are required.' });
}
const notification = new Notification({
message,
sender: normalizedSenderId,
recipient: normalizedUserId,
isSystemGenerated: false,
});
await notification.save();
res.status(201).json({ message: 'Notification stored successfully.', notification });
} catch (error) {
console.error('❌ Error storing notification:', error);
res.status(500).json({ message: 'Error storing notification.', error: error.message });
}
};
const getUnreadNotifications = async (req, res) => {
try {
const { userId } = req.params;
const normalizedUserId = normalizeObjectId(userId);
if (!normalizedUserId) {
console.error('❌ User ID is missing in the request.');
return res.status(400).json({ message: 'A valid user ID is required.' });
}
const notifications = await Notification.find({ recipient: normalizedUserId, isRead: false })
.sort({ createdTimeStamps: -1 })
.populate('sender', 'firstName lastName'); // Include sender's name
res.status(200).json(notifications);
} catch (error) {
console.error('❌ Error fetching unread notifications:', error);
res
.status(500)
.json({ message: 'Error fetching unread notifications.', error: error.message });
}
};
const markNotificationsAsRead = async (req, res) => {
try {
const { notificationIds } = req.body;
const normalizedNotificationIds = normalizeObjectIdList(notificationIds);
if (!normalizedNotificationIds) {
return res.status(400).json({ message: 'Invalid notification IDs.' });
}
const result = await Notification.updateMany(
{ _id: { $in: normalizedNotificationIds } },
{ isRead: true },
);
res.status(200).json({ message: 'Notifications marked as read.', result });
} catch (error) {
console.error('❌ Error marking notifications as read:', error);
res
.status(500)
.json({ message: 'Error marking notifications as read.', error: error.message });
}
};
return {
getPreferences,
updatePreferences,
storeNotification,
getUnreadNotifications,
markNotificationsAsRead,
};
};
module.exports = lbUserPrefController;