bulltrackers-module 1.0.614 → 1.0.615

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1146,21 +1146,73 @@ const fetchNotifications = async (firestore, userId, options = {}) => {
1146
1146
 
1147
1147
  // 12. Mark Notification Read
1148
1148
  const markNotificationRead = async (firestore, userId, notificationIds, markAll = false) => {
1149
- const batch = firestore.batch();
1150
1149
  const collectionRef = firestore.collection('SignedInUsers').doc(userId).collection('notifications');
1150
+ const MAX_BATCH_SIZE = 500; // Firestore batch limit
1151
+ const batches = [];
1152
+ let currentBatch = firestore.batch();
1153
+ let batchCount = 0;
1151
1154
 
1152
1155
  if (markAll) {
1153
- const snapshot = await collectionRef.where('read', '==', false).get();
1156
+ // Query all unread notifications with a reasonable limit to prevent timeouts
1157
+ // Process in chunks to avoid overwhelming the system
1158
+ const MAX_NOTIFICATIONS_TO_PROCESS = 1000; // Limit to prevent timeouts
1159
+ const snapshot = await collectionRef.where('read', '==', false).limit(MAX_NOTIFICATIONS_TO_PROCESS).get();
1160
+ console.log(`[markNotificationRead] Marking ${snapshot.size} notifications as read for user ${userId} (limited to ${MAX_NOTIFICATIONS_TO_PROCESS})`);
1161
+
1162
+ if (snapshot.size === 0) {
1163
+ console.log(`[markNotificationRead] No unread notifications found for user ${userId}`);
1164
+ return;
1165
+ }
1166
+
1154
1167
  snapshot.docs.forEach(doc => {
1155
- batch.update(doc.ref, { read: true, readAt: new Date() });
1168
+ currentBatch.update(doc.ref, { read: true, readAt: new Date() });
1169
+ batchCount++;
1170
+
1171
+ // If batch is full, commit it and start a new one
1172
+ if (batchCount >= MAX_BATCH_SIZE) {
1173
+ batches.push(currentBatch);
1174
+ currentBatch = firestore.batch();
1175
+ batchCount = 0;
1176
+ }
1156
1177
  });
1157
- } else if (Array.isArray(notificationIds)) {
1178
+
1179
+ // Add the last batch if it has operations
1180
+ if (batchCount > 0) {
1181
+ batches.push(currentBatch);
1182
+ }
1183
+ } else if (Array.isArray(notificationIds) && notificationIds.length > 0) {
1184
+ // Mark specific notifications
1158
1185
  notificationIds.forEach(id => {
1159
1186
  const ref = collectionRef.doc(id);
1160
- batch.update(ref, { read: true, readAt: new Date() });
1187
+ currentBatch.update(ref, { read: true, readAt: new Date() });
1188
+ batchCount++;
1189
+
1190
+ // If batch is full, commit it and start a new one
1191
+ if (batchCount >= MAX_BATCH_SIZE) {
1192
+ batches.push(currentBatch);
1193
+ currentBatch = firestore.batch();
1194
+ batchCount = 0;
1195
+ }
1161
1196
  });
1197
+
1198
+ // Add the last batch if it has operations
1199
+ if (batchCount > 0) {
1200
+ batches.push(currentBatch);
1201
+ }
1202
+ } else {
1203
+ // No valid operation
1204
+ return;
1205
+ }
1206
+
1207
+ // Commit all batches sequentially to avoid overwhelming Firestore and prevent timeouts
1208
+ if (batches.length > 0) {
1209
+ console.log(`[markNotificationRead] Committing ${batches.length} batch(es) for user ${userId}`);
1210
+ for (let i = 0; i < batches.length; i++) {
1211
+ await batches[i].commit();
1212
+ console.log(`[markNotificationRead] Committed batch ${i + 1}/${batches.length} for user ${userId}`);
1213
+ }
1214
+ console.log(`[markNotificationRead] Successfully marked notifications as read for user ${userId}`);
1162
1215
  }
1163
- await batch.commit();
1164
1216
  };
1165
1217
 
1166
1218
 
@@ -27,10 +27,14 @@ router.post('/mark-read', async (req, res) => {
27
27
  const { db } = req.dependencies;
28
28
  const { notificationIds, markAll } = req.body; // Array of IDs or boolean flag
29
29
 
30
+ console.log(`[notifications/mark-read] Request for user ${req.targetUserId}, markAll: ${markAll}, notificationIds: ${notificationIds?.length || 0}`);
31
+
32
+ // Execute the operation with a reasonable timeout
30
33
  await markNotificationRead(db, req.targetUserId, notificationIds, markAll);
31
34
 
32
35
  res.json({ success: true });
33
36
  } catch (error) {
37
+ console.error(`[notifications/mark-read] Error for user ${req.targetUserId}:`, error.message, error.stack);
34
38
  res.status(500).json({ error: error.message });
35
39
  }
36
40
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bulltrackers-module",
3
- "version": "1.0.614",
3
+ "version": "1.0.615",
4
4
  "description": "Helper Functions for Bulltrackers.",
5
5
  "main": "index.js",
6
6
  "files": [