bulltrackers-module 1.0.655 → 1.0.657

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.
@@ -3,6 +3,7 @@ const { FieldValue, Timestamp } = require('@google-cloud/firestore');
3
3
  const { dispatchSyncRequest } = require('../task_engine_helper.js');
4
4
  const { sanitizeCid, sanitizeDocId } = require('../security_utils.js');
5
5
  const crypto = require('crypto');
6
+ const zlib = require('zlib');
6
7
 
7
8
  // 1. Fetch latest stored snapshots of user data from a user-centric collection
8
9
 
@@ -1277,6 +1278,42 @@ const getComputationResults = async (db, computationName, dateStr, userId = null
1277
1278
  }
1278
1279
  };
1279
1280
 
1281
+ // 10.4. Find most recent available GlobalAumPerAsset30D data
1282
+ // Looks back up to 7 days to find the most recent available data
1283
+ const findMostRecentGlobalAumData = async (db, startDateStr, maxLookbackDays = 7) => {
1284
+ try {
1285
+ const startDate = new Date(startDateStr);
1286
+
1287
+ // Look back through dates to find the most recent available data
1288
+ for (let i = 0; i < maxLookbackDays; i++) {
1289
+ const checkDate = new Date(startDate);
1290
+ checkDate.setDate(startDate.getDate() - i);
1291
+ const dateKey = checkDate.toISOString().split('T')[0];
1292
+
1293
+ try {
1294
+ const computationData = await getComputationResults(db, 'GlobalAumPerAsset30D', dateKey);
1295
+
1296
+ // If we got valid data (not null/empty), return it
1297
+ if (computationData && typeof computationData === 'object' && Object.keys(computationData).length > 0) {
1298
+ return {
1299
+ date: dateKey,
1300
+ data: computationData
1301
+ };
1302
+ }
1303
+ } catch (error) {
1304
+ // Missing data for this date - continue looking back
1305
+ console.log(`[GlobalAum] No data found for ${dateKey}, checking previous date...`);
1306
+ }
1307
+ }
1308
+
1309
+ // No data found in the lookback period
1310
+ return null;
1311
+ } catch (error) {
1312
+ console.error(`[GlobalAum] Error finding most recent data:`, error);
1313
+ throw error;
1314
+ }
1315
+ };
1316
+
1280
1317
  // 10.5. Fetch GlobalAumPerAsset30D with 7-day lookback
1281
1318
  // Returns the last 7 days of successfully stored data for a specific ticker or all assets
1282
1319
  const fetchGlobalAumPerAssetWithLookback = async (db, dateStr, ticker = null, lookbackDays = 7) => {
@@ -2973,6 +3010,7 @@ const subscribeToAllWatchlistPIs = async (db, userId, watchlistId, alertTypes =
2973
3010
  module.exports = {
2974
3011
  latestUserCentricSnapshot,
2975
3012
  pageCollection,
3013
+ findMostRecentGlobalAumData,
2976
3014
  fetchGlobalAumPerAssetWithLookback,
2977
3015
  getComputationResults,
2978
3016
  fetchPopularInvestorMasterList,
@@ -9,6 +9,7 @@ const {
9
9
  requestPopularInvestorAddition,
10
10
  trackPopularInvestorView,
11
11
  pageCollection,
12
+ findMostRecentGlobalAumData,
12
13
  fetchGlobalAumPerAssetWithLookback
13
14
  } = require('../helpers/data-fetchers/firestore.js');
14
15
  const { sanitizeCid } = require('../helpers/security_utils.js');
@@ -275,25 +276,25 @@ router.get('/global-aum', requireFirebaseAuth, async (req, res, next) => {
275
276
  // Default to today if no date provided
276
277
  const targetDate = date || new Date().toISOString().split('T')[0];
277
278
 
278
- // Fetch the latest computation result
279
- const computationData = await getComputationResults(db, 'GlobalAumPerAsset30D', targetDate);
279
+ // Find the most recent available data (looks back up to 7 days if today's data doesn't exist)
280
+ const result = await findMostRecentGlobalAumData(db, targetDate, 7);
280
281
 
281
- if (!computationData || typeof computationData !== 'object' || Object.keys(computationData).length === 0) {
282
+ if (!result || !result.data || Object.keys(result.data).length === 0) {
282
283
  return res.status(404).json({
283
284
  success: false,
284
- error: 'No AUM data available for the specified date'
285
+ error: 'No AUM data available in the last 7 days'
285
286
  });
286
287
  }
287
288
 
288
289
  // Convert to array and sort by AUM (descending)
289
- const assetsArray = Object.entries(computationData)
290
+ const assetsArray = Object.entries(result.data)
290
291
  .map(([ticker, aum]) => ({ ticker, aum: Number(aum) }))
291
292
  .sort((a, b) => b.aum - a.aum)
292
293
  .slice(0, parseInt(limit) || 10);
293
294
 
294
295
  res.json({
295
296
  success: true,
296
- date: targetDate,
297
+ date: result.date, // Return the actual date of the data found
297
298
  count: assetsArray.length,
298
299
  data: assetsArray
299
300
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bulltrackers-module",
3
- "version": "1.0.655",
3
+ "version": "1.0.657",
4
4
  "description": "Helper Functions for Bulltrackers.",
5
5
  "main": "index.js",
6
6
  "files": [