bulltrackers-module 1.0.515 → 1.0.516

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.
@@ -30,6 +30,15 @@ async function requestUserSync(req, res, dependencies, config) {
30
30
  });
31
31
  }
32
32
 
33
+ // Check the referrer or source header to determine if this is from PI page or profile page
34
+ const referrer = req.headers.referer || req.headers.referrer || '';
35
+ const sourcePage = req.query?.sourcePage || req.body?.sourcePage || '';
36
+
37
+ // Determine user type based on where the request came from
38
+ // If from /popular-investors/{cid}, it's a PI
39
+ // If from /profile, it's a signed-in user
40
+ const isPI = referrer.includes('/popular-investors/') || sourcePage === 'popular-investor';
41
+
33
42
  // Check for dev override impersonation
34
43
  const { getEffectiveCid, getDevOverride } = require('../dev/dev_helpers');
35
44
  const effectiveCid = await getEffectiveCid(db, requestingUserCid, config, logger);
@@ -71,103 +80,17 @@ async function requestUserSync(req, res, dependencies, config) {
71
80
  logger.log('INFO', `[requestUserSync] Developer account ${requestingUserCid} bypassing rate limits${isImpersonating ? ` (impersonating ${effectiveCid})` : ''}`);
72
81
  }
73
82
 
74
- // Determine user type (PI or signed-in user)
75
- // Check if user is in rankings (PI) or has signed-in user data
76
- const { checkIfUserIsPI } = require('../data_helpers');
77
- let isPI = await checkIfUserIsPI(db, targetCidNum, config, logger);
78
-
79
- // Fallback: If not found in rankings, check if they have PI data (portfolio, history, or social)
80
- // This handles cases where a PI might not be in the latest rankings but has historical data
81
- if (!isPI) {
82
- try {
83
- const today = new Date().toISOString().split('T')[0];
84
- let hasPiData = false;
85
-
86
- // Check if they have PI portfolio data (any date in last 7 days)
87
- for (let i = 0; i <= 7; i++) {
88
- const checkDate = new Date();
89
- checkDate.setDate(checkDate.getDate() - i);
90
- const dateStr = checkDate.toISOString().split('T')[0];
91
-
92
- // Check portfolio data
93
- const portfolioRef = db.collection('PopularInvestorPortfolioData')
94
- .doc(dateStr)
95
- .collection(String(targetCidNum))
96
- .doc(String(targetCidNum));
97
- const portfolioDoc = await portfolioRef.get();
98
-
99
- if (portfolioDoc.exists) {
100
- logger.log('INFO', `[requestUserSync] User ${targetCidNum} has PI portfolio data from ${dateStr}, treating as PI`);
101
- hasPiData = true;
102
- break;
103
- }
104
-
105
- // Check trade history data
106
- const historyRef = db.collection('PopularInvestorTradeHistoryData')
107
- .doc(dateStr)
108
- .collection(String(targetCidNum))
109
- .doc(String(targetCidNum));
110
- const historyDoc = await historyRef.get();
111
-
112
- if (historyDoc.exists) {
113
- logger.log('INFO', `[requestUserSync] User ${targetCidNum} has PI trade history data from ${dateStr}, treating as PI`);
114
- hasPiData = true;
115
- break;
116
- }
117
-
118
- // Check social posts data
119
- const socialRef = db.collection('PopularInvestorSocialPostData')
120
- .doc(dateStr)
121
- .collection(String(targetCidNum))
122
- .doc(String(targetCidNum));
123
- const socialDoc = await socialRef.get();
124
-
125
- if (socialDoc.exists) {
126
- logger.log('INFO', `[requestUserSync] User ${targetCidNum} has PI social posts data from ${dateStr}, treating as PI`);
127
- hasPiData = true;
128
- break;
129
- }
130
- }
131
-
132
- if (hasPiData) {
133
- isPI = { CustomerId: targetCidNum, UserName: null }; // Fake ranking entry
134
- } else {
135
- logger.log('INFO', `[requestUserSync] User ${targetCidNum} not found in rankings and no PI data found, treating as signed-in user`);
136
- }
137
- } catch (fallbackErr) {
138
- logger.log('WARN', `[requestUserSync] Error checking PI data fallback for ${targetCidNum}:`, fallbackErr.message);
139
- }
140
- } else {
141
- logger.log('INFO', `[requestUserSync] User ${targetCidNum} found in rankings as PI`);
142
- }
143
-
144
- // Get username - for PIs, get from rankings; for signed-in users, we'll need to fetch from their profile
83
+ // Get username based on user type
145
84
  let username = null;
146
85
  if (isPI) {
147
- // Get PI username from rankings (if available)
148
- if (isPI.UserName || isPI.username) {
149
- username = isPI.UserName || isPI.username;
150
- } else {
151
- // Try to get from rankings if we have a fake entry
152
- const { findLatestRankingsDate } = require('../data_helpers');
153
- const rankingsCollection = config.popularInvestorRankingsCollection || 'popular_investor_rankings';
154
- const rankingsDate = await findLatestRankingsDate(db, rankingsCollection, 30);
155
-
156
- if (rankingsDate) {
157
- const rankingsRef = db.collection(rankingsCollection).doc(rankingsDate);
158
- const rankingsDoc = await rankingsRef.get();
159
- if (rankingsDoc.exists) {
160
- const rankingsData = rankingsDoc.data();
161
- const rankingsItems = rankingsData.Items || [];
162
- const rankingEntry = rankingsItems.find(item => Number(item.CustomerId) === targetCidNum);
163
- if (rankingEntry) {
164
- username = rankingEntry.UserName || rankingEntry.username || null;
165
- }
166
- }
167
- }
86
+ // For PIs, get username from rankings
87
+ const { checkIfUserIsPI } = require('../core/user_status_helpers');
88
+ const rankEntry = await checkIfUserIsPI(db, targetCidNum, config, logger);
89
+ if (rankEntry && (rankEntry.UserName || rankEntry.username)) {
90
+ username = rankEntry.UserName || rankEntry.username;
168
91
  }
169
92
  } else {
170
- // For signed-in users, try to get username from verification data
93
+ // For signed-in users, try verification data
171
94
  const signedInUsersCollection = config.signedInUsersCollection || 'signed_in_users';
172
95
  const userDoc = await db.collection(signedInUsersCollection).doc(String(targetCidNum)).get();
173
96
  if (userDoc.exists) {
@@ -177,8 +100,7 @@ async function requestUserSync(req, res, dependencies, config) {
177
100
  }
178
101
 
179
102
  if (!username) {
180
- logger.log('WARN', `[requestUserSync] Could not find username for user ${targetCidNum}, will use CID as fallback`);
181
- username = String(targetCidNum);
103
+ username = String(targetCidNum); // Fallback to CID
182
104
  }
183
105
 
184
106
  // Create request document
@@ -832,9 +832,9 @@ async function handleOnDemandUserUpdate(taskData, config, dependencies) {
832
832
  logger.log('INFO', `[On-Demand Update] Skipping history fetch (portfolioOnly=false) for ${username}`);
833
833
  }
834
834
 
835
- // Fetch social data if requested (for user signup or explicit request)
835
+ // Fetch social data - always for on-demand syncs
836
836
  let socialFetched = false;
837
- if (includeSocial && username) {
837
+ if (username) {
838
838
  try {
839
839
  logger.log('INFO', `[On-Demand Update] Fetching social data for ${username} (${cid})...`);
840
840
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bulltrackers-module",
3
- "version": "1.0.515",
3
+ "version": "1.0.516",
4
4
  "description": "Helper Functions for Bulltrackers.",
5
5
  "main": "index.js",
6
6
  "files": [