bulltrackers-module 1.0.411 → 1.0.413

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.
@@ -66,9 +66,12 @@ async function findLatestPortfolioDate(db, signedInUsersCollection, userCid, max
66
66
  * Helper function to find the latest available date for computation results
67
67
  * Searches backwards from today up to 30 days
68
68
  */
69
- async function findLatestComputationDate(db, insightsCollection, category, computationName, userCid, maxDaysBack = 30) {
69
+ async function findLatestComputationDate(db, insightsCollection, resultsSub, compsSub, category, computationName, userCid, maxDaysBack = 30) {
70
70
  const today = new Date();
71
71
 
72
+ // Log the path structure being checked
73
+ console.log(`[findLatestComputationDate] Searching for: ${insightsCollection}/{date}/${resultsSub}/${category}/${compsSub}/${computationName}`);
74
+
72
75
  for (let i = 0; i < maxDaysBack; i++) {
73
76
  const checkDate = new Date(today);
74
77
  checkDate.setDate(checkDate.getDate() - i);
@@ -77,36 +80,34 @@ async function findLatestComputationDate(db, insightsCollection, category, compu
77
80
  try {
78
81
  const computationRef = db.collection(insightsCollection)
79
82
  .doc(dateStr)
80
- .collection('results')
83
+ .collection(resultsSub)
81
84
  .doc(category)
82
- .collection('computations')
85
+ .collection(compsSub)
83
86
  .doc(computationName);
84
87
 
85
88
  const computationDoc = await computationRef.get();
86
89
 
90
+ // Log each date being checked
91
+ if (i < 3) { // Only log first 3 to avoid spam
92
+ console.log(`[findLatestComputationDate] Checking date ${dateStr}: exists=${computationDoc.exists}`);
93
+ }
94
+
95
+ // Just check if document exists - don't check for CID here
96
+ // We'll check for CID in the calling function after decompression
87
97
  if (computationDoc.exists) {
88
- const rawData = computationDoc.data();
89
- // Decompress if needed (handles byte string storage)
90
- const computationData = tryDecompress(rawData);
91
- // Check if user's CID exists in the computation result
92
- const cidStr = String(userCid);
93
- const hasCid = computationData && computationData[cidStr] !== undefined;
94
-
95
- if (hasCid) {
96
- return dateStr; // Found data for this date
97
- } else if (computationData && typeof computationData === 'object') {
98
- // Log debug info if document exists but CID not found
99
- const sampleKeys = Object.keys(computationData).slice(0, 5);
100
- console.log(`[findLatestComputationDate] Date ${dateStr}: Document exists but CID ${cidStr} not found. Sample keys:`, sampleKeys);
101
- }
98
+ console.log(`[findLatestComputationDate] Found document at date: ${dateStr}`);
99
+ return dateStr; // Found document for this date
102
100
  }
103
101
  } catch (error) {
102
+ // Log errors for debugging
103
+ console.error(`[findLatestComputationDate] Error checking date ${dateStr}:`, error.message);
104
104
  // Continue to next date if error
105
105
  continue;
106
106
  }
107
107
  }
108
108
 
109
- return null; // No data found in the last maxDaysBack days
109
+ console.log(`[findLatestComputationDate] No document found in last ${maxDaysBack} days`);
110
+ return null; // No document found in the last maxDaysBack days
110
111
  }
111
112
 
112
113
  /**
@@ -672,7 +673,9 @@ async function getUserComputations(req, res, dependencies, config) {
672
673
  const firstCompName = computationNames[0];
673
674
  const latestDate = await findLatestComputationDate(
674
675
  db,
675
- insightsCollection,
676
+ insightsCollection,
677
+ resultsSub,
678
+ compsSub,
676
679
  category,
677
680
  firstCompName,
678
681
  userCid,
@@ -796,9 +799,14 @@ async function autoGenerateWatchlist(req, res, dependencies, config) {
796
799
  const computationName = 'SignedInUserCopiedPIs';
797
800
 
798
801
  // Try to find latest computation date (with fallback)
802
+ const insightsCollection = config.unifiedInsightsCollection || 'unified_insights';
803
+ const resultsSub = config.resultsSubcollection || 'results';
804
+ const compsSub = config.computationsSubcollection || 'computations';
799
805
  const computationDate = await findLatestComputationDate(
800
806
  db,
801
- insightsCollection,
807
+ insightsCollection,
808
+ resultsSub,
809
+ compsSub,
802
810
  category,
803
811
  computationName,
804
812
  userCid,
@@ -1387,17 +1395,24 @@ async function getPiProfile(req, res, dependencies, config) {
1387
1395
  const category = 'popular_investor';
1388
1396
  const today = new Date().toISOString().split('T')[0];
1389
1397
 
1390
- // Find latest available computation date
1398
+ logger.log('INFO', `[getPiProfile] Starting search for PI CID: ${cid}`);
1399
+
1400
+ // Find latest available computation date (just check if document exists, not CID)
1391
1401
  const latestDate = await findLatestComputationDate(
1392
1402
  db,
1393
- insightsCollection,
1403
+ insightsCollection,
1404
+ resultsSub,
1405
+ compsSub,
1394
1406
  category,
1395
1407
  computationName,
1396
- cid,
1408
+ null, // Don't pass CID - just find if document exists
1397
1409
  30
1398
1410
  );
1399
1411
 
1412
+ logger.log('INFO', `[getPiProfile] Latest computation date found: ${latestDate || 'NONE'}`);
1413
+
1400
1414
  if (!latestDate) {
1415
+ logger.log('WARN', `[getPiProfile] No computation document found for ${computationName} in last 30 days`);
1401
1416
  return res.status(404).json({
1402
1417
  error: "Profile data not available",
1403
1418
  message: "No computation results found for this Popular Investor"
@@ -1412,9 +1427,12 @@ async function getPiProfile(req, res, dependencies, config) {
1412
1427
  .collection(compsSub)
1413
1428
  .doc(computationName);
1414
1429
 
1430
+ logger.log('INFO', `[getPiProfile] Fetching document at path: ${insightsCollection}/${latestDate}/${resultsSub}/${category}/${compsSub}/${computationName}`);
1431
+
1415
1432
  const computationDoc = await computationRef.get();
1416
1433
 
1417
1434
  if (!computationDoc.exists) {
1435
+ logger.log('WARN', `[getPiProfile] Document does not exist at expected path`);
1418
1436
  return res.status(404).json({
1419
1437
  error: "Profile data not found",
1420
1438
  message: "Computation document does not exist"
@@ -1425,6 +1443,7 @@ async function getPiProfile(req, res, dependencies, config) {
1425
1443
  const rawData = computationDoc.data();
1426
1444
  const wasCompressed = rawData && rawData._compressed === true;
1427
1445
  logger.log('INFO', `[getPiProfile] Document exists. Was compressed: ${wasCompressed}, CID being searched: ${cid}`);
1446
+ logger.log('INFO', `[getPiProfile] Raw data keys:`, rawData ? Object.keys(rawData).slice(0, 10) : 'NO DATA');
1428
1447
 
1429
1448
  const computationData = tryDecompress(rawData);
1430
1449
 
@@ -1443,26 +1462,30 @@ async function getPiProfile(req, res, dependencies, config) {
1443
1462
  // Log full decompressed data (will be large but needed for debugging)
1444
1463
  logger.log('INFO', `[getPiProfile] FULL DECOMPRESSED DOCUMENT CONTENTS:`, JSON.stringify(computationData, null, 2));
1445
1464
 
1446
- const profileData = computationData[String(cid)];
1465
+ const cidStr = String(cid);
1466
+ const profileData = computationData && typeof computationData === 'object' ? computationData[cidStr] : undefined;
1447
1467
 
1448
1468
  if (!profileData) {
1449
1469
  // Additional debug info in error response
1450
1470
  const availableCids = computationData && typeof computationData === 'object' ? Object.keys(computationData).slice(0, 20) : [];
1451
- logger.log('WARN', `[getPiProfile] CID ${cid} not found in computation data. Available CIDs (sample):`, availableCids);
1471
+ logger.log('WARN', `[getPiProfile] CID ${cid} (as string: "${cidStr}") not found in computation data. Available CIDs (sample):`, availableCids);
1452
1472
 
1453
1473
  return res.status(404).json({
1454
1474
  error: "Profile data not found",
1455
1475
  message: "No data for this Popular Investor in computation results",
1456
1476
  debug: {
1457
- searchedCid: String(cid),
1477
+ searchedCid: cidStr,
1458
1478
  wasCompressed: wasCompressed,
1459
1479
  totalKeysInDocument: computationData && typeof computationData === 'object' ? Object.keys(computationData).length : 0,
1460
1480
  sampleAvailableCids: availableCids,
1461
- dataDate: latestDate
1481
+ dataDate: latestDate,
1482
+ documentPath: `${insightsCollection}/${latestDate}/${resultsSub}/${category}/${compsSub}/${computationName}`
1462
1483
  }
1463
1484
  });
1464
1485
  }
1465
1486
 
1487
+ logger.log('SUCCESS', `[getPiProfile] Found profile data for CID ${cid} from date ${latestDate}`);
1488
+
1466
1489
  return res.status(200).json({
1467
1490
  status: 'success',
1468
1491
  cid: String(cid),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bulltrackers-module",
3
- "version": "1.0.411",
3
+ "version": "1.0.413",
4
4
  "description": "Helper Functions for Bulltrackers.",
5
5
  "main": "index.js",
6
6
  "files": [