bulltrackers-module 1.0.602 → 1.0.604

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.
@@ -1935,39 +1935,51 @@ const checkDataStatus = async (db, userId) => {
1935
1935
  let computationDate = null;
1936
1936
  let fallbackWindowExhausted = false;
1937
1937
 
1938
- // Determine which computation to check based on user type
1939
- let computationName = 'SignedInUserProfileMetrics';
1938
+ // Determine which computations to check based on user type
1939
+ // Signed-in users always have SignedInUserProfileMetrics
1940
+ // If they're also a PI, they may have SignedInUserPIPersonalizedMetrics
1941
+ const computationNames = ['SignedInUserProfileMetrics'];
1940
1942
  try {
1941
1943
  await fetchPopularInvestorMasterList(db, userId);
1942
- computationName = 'SignedInUserPIPersonalizedMetrics';
1944
+ // User is a PI, also check for PI-specific computation
1945
+ computationNames.push('SignedInUserPIPersonalizedMetrics');
1943
1946
  } catch (e) {
1944
- // User is not a PI, use default
1947
+ // User is not a PI, only check SignedInUserProfileMetrics
1945
1948
  }
1946
1949
 
1947
1950
  // Check for computation results in the last 7 days
1951
+ // Check all relevant computation names for this user
1948
1952
  for (let i = 0; i < lookbackDays; i++) {
1949
1953
  const checkDate = new Date(today);
1950
1954
  checkDate.setDate(checkDate.getDate() - i);
1951
1955
  const dateStr = checkDate.toISOString().split('T')[0];
1952
1956
 
1953
- try {
1954
- const pageRef = db.collection('unified_insights')
1955
- .doc(dateStr)
1956
- .collection('results')
1957
- .doc('popular-investor')
1958
- .collection('computations')
1959
- .doc(computationName)
1960
- .collection('pages')
1961
- .doc(String(userId));
1962
-
1963
- const pageSnap = await pageRef.get();
1964
- if (pageSnap.exists) {
1965
- computationDate = dateStr;
1966
- break;
1957
+ // Check each computation name
1958
+ for (const compName of computationNames) {
1959
+ try {
1960
+ const pageRef = db.collection('unified_insights')
1961
+ .doc(dateStr)
1962
+ .collection('results')
1963
+ .doc('popular-investor')
1964
+ .collection('computations')
1965
+ .doc(compName)
1966
+ .collection('pages')
1967
+ .doc(String(userId));
1968
+
1969
+ const pageSnap = await pageRef.get();
1970
+ if (pageSnap.exists) {
1971
+ computationDate = dateStr;
1972
+ break;
1973
+ }
1974
+ } catch (error) {
1975
+ // Continue checking other dates/computations
1976
+ console.error(`Error checking computation ${compName} for ${dateStr}:`, error);
1967
1977
  }
1968
- } catch (error) {
1969
- // Continue checking other dates
1970
- console.error(`Error checking computation for ${dateStr}:`, error);
1978
+ }
1979
+
1980
+ // If we found a computation date, stop checking
1981
+ if (computationDate) {
1982
+ break;
1971
1983
  }
1972
1984
  }
1973
1985
 
@@ -31,5 +31,17 @@ module.exports = (dependencies) => {
31
31
  router.use('/settings', settingsRoutes);
32
32
  router.use('/reviews', reviewsRoutes);
33
33
 
34
+ // Legacy route: /user/:userId/sync -> forwards to sync/request logic
35
+ // This maintains backward compatibility with frontend calls
36
+ router.post('/user/:userId/sync', async (req, res) => {
37
+ // Import sync handler (need to require it here to get the exported function)
38
+ const { handleSyncRequest } = require('./sync.js');
39
+ // Set targetId from URL param for the handler
40
+ req.body = req.body || {};
41
+ req.body.targetId = req.params.userId;
42
+ // Call the sync request handler
43
+ await handleSyncRequest(req, res);
44
+ });
45
+
34
46
  return router;
35
47
  };
@@ -11,12 +11,14 @@ const {
11
11
 
12
12
  const router = express.Router();
13
13
 
14
- // POST /sync/request (Unified - Auto-detects user type)
15
- router.post('/request', async (req, res) => {
14
+ // Helper function for sync request logic (shared between /sync/request and /user/:userId/sync)
15
+ const handleSyncRequest = async (req, res) => {
16
16
  try {
17
17
  const { pubsub, db } = req.dependencies;
18
18
  // targetId can be passed (for PIs) or default to self
19
- const targetId = req.body.targetId || req.targetUserId;
19
+ // For /user/:userId/sync, targetId comes from URL param
20
+ // For /sync/request, targetId comes from body or req.targetUserId
21
+ const targetId = req.body.targetId || req.params.userId || req.targetUserId;
20
22
 
21
23
  // 1. Rate Limits
22
24
  const isDev = await isDeveloper(db, req.targetUserId);
@@ -107,6 +109,8 @@ router.post('/request', async (req, res) => {
107
109
  res.json({
108
110
  success: true,
109
111
  requestIds,
112
+ requestId: requestIds[0], // Return first request ID for backward compatibility
113
+ status: 'queued',
110
114
  userTypes: {
111
115
  isSignedInUser: isSignedIn,
112
116
  isPopularInvestor: isPI
@@ -116,7 +120,10 @@ router.post('/request', async (req, res) => {
116
120
  } catch (error) {
117
121
  res.status(500).json({ error: error.message });
118
122
  }
119
- });
123
+ };
124
+
125
+ // POST /sync/request (Unified - Auto-detects user type)
126
+ router.post('/request', handleSyncRequest);
120
127
 
121
128
  // GET /sync/status/:targetId
122
129
  router.get('/status/:targetId', async (req, res) => {
@@ -129,4 +136,6 @@ router.get('/status/:targetId', async (req, res) => {
129
136
  }
130
137
  });
131
138
 
132
- module.exports = router;
139
+ // Export handler for use in main router
140
+ module.exports = router;
141
+ module.exports.handleSyncRequest = handleSyncRequest;
@@ -244,7 +244,12 @@ async function handleComputationTask(message, config, dependencies) {
244
244
  computation,
245
245
  getComputationDisplayName(computation),
246
246
  true,
247
- null
247
+ null,
248
+ {
249
+ collectionRegistry: dependencies.collectionRegistry,
250
+ config: config,
251
+ notificationType: 'userActionCompletions'
252
+ }
248
253
  );
249
254
  } catch (notifError) {
250
255
  // Non-critical, log and continue
@@ -291,7 +296,12 @@ async function handleComputationTask(message, config, dependencies) {
291
296
  computation,
292
297
  getComputationDisplayName(computation),
293
298
  false,
294
- err.message
299
+ err.message,
300
+ {
301
+ collectionRegistry: dependencies.collectionRegistry,
302
+ config: config,
303
+ notificationType: 'userActionCompletions'
304
+ }
295
305
  );
296
306
  } catch (notifError) {
297
307
  // Non-critical, log and continue
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bulltrackers-module",
3
- "version": "1.0.602",
3
+ "version": "1.0.604",
4
4
  "description": "Helper Functions for Bulltrackers.",
5
5
  "main": "index.js",
6
6
  "files": [