bulltrackers-module 1.0.559 → 1.0.561

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.
@@ -98,18 +98,60 @@ async function getSignedInUserPIPersonalizedMetrics(req, res, dependencies, conf
98
98
  try {
99
99
  const devOverride = await getDevOverride(db, userCid, config, logger);
100
100
 
101
+ // Also read raw Firestore data to check pretendToBePI value directly (in case of normalization issues)
102
+ let rawPretendToBePI = null;
103
+ if (devOverride && devOverride.enabled) {
104
+ try {
105
+ const devOverridesCollection = config.devOverridesCollection || 'dev_overrides';
106
+ const overrideDoc = await db.collection(devOverridesCollection).doc(String(userCid)).get();
107
+ if (overrideDoc.exists) {
108
+ const rawData = overrideDoc.data();
109
+ rawPretendToBePI = rawData.pretendToBePI; // Get raw value (could be false, true, undefined, etc.)
110
+ }
111
+ } catch (err) {
112
+ logger.log('WARN', `[getSignedInUserPIPersonalizedMetrics] Error reading raw dev override: ${err.message}`);
113
+ }
114
+ }
115
+
101
116
  // For PI-related checks, respect pretendToBePI flag
102
- // If pretendToBePI is false, use actual userCid even if impersonateCid is set
117
+ // If pretendToBePI is false (either normalized or raw), use actual userCid even if impersonateCid is set
103
118
  let effectiveCid;
104
- if (devOverride && devOverride.enabled && devOverride.pretendToBePI === false) {
105
- // User explicitly doesn't want to pretend to be a PI, use actual CID
119
+ let shouldCheckPI = true;
120
+
121
+ // Check if user explicitly doesn't want to pretend to be a PI
122
+ // Check both normalized value and raw value to be safe
123
+ const pretendToBePIValue = rawPretendToBePI !== undefined ? rawPretendToBePI : (devOverride ? devOverride.pretendToBePI : false);
124
+ const explicitlyNotPretending = pretendToBePIValue === false;
125
+
126
+ if (devOverride && devOverride.enabled && explicitlyNotPretending) {
127
+ // User explicitly doesn't want to pretend to be a PI
128
+ // Use actual CID and don't check PI status (user doesn't want to be treated as PI)
106
129
  effectiveCid = Number(userCid);
130
+ shouldCheckPI = false; // Skip PI check since user doesn't want to pretend to be PI
131
+
132
+ logger.log('INFO', `[getSignedInUserPIPersonalizedMetrics] DEV OVERRIDE: User ${userCid} has pretendToBePI=false (raw: ${rawPretendToBePI}, normalized: ${devOverride.pretendToBePI}), skipping PI check and returning 404`);
107
133
  } else {
108
134
  // Use normal effective CID logic (may use impersonateCid if set)
109
135
  effectiveCid = await getEffectiveCid(db, userCid, config, logger);
110
136
  }
111
137
 
112
- const isImpersonating = devOverride && devOverride.enabled && devOverride.impersonateCid && effectiveCid !== Number(userCid);
138
+ // Calculate isImpersonating based on whether we're actually using impersonation
139
+ // If pretendToBePI is false, we're not impersonating for PI purposes
140
+ // Only consider it impersonating if pretendToBePI is not explicitly false AND effectiveCid differs from userCid
141
+ const isImpersonating = shouldCheckPI &&
142
+ devOverride && devOverride.enabled && devOverride.impersonateCid &&
143
+ !explicitlyNotPretending && effectiveCid !== Number(userCid);
144
+
145
+ // If user explicitly doesn't want to pretend to be a PI, return 404 immediately
146
+ if (!shouldCheckPI) {
147
+ return res.status(404).json({
148
+ error: "Not a Popular Investor",
149
+ message: "This endpoint is only available for users who are Popular Investors",
150
+ effectiveCid: effectiveCid,
151
+ isImpersonating: false, // Not impersonating if pretendToBePI is false
152
+ actualCid: Number(userCid)
153
+ });
154
+ }
113
155
 
114
156
  // Check if user is a PI
115
157
  const rankEntry = await checkIfUserIsPI(db, effectiveCid, config, logger);
@@ -118,7 +160,8 @@ async function getSignedInUserPIPersonalizedMetrics(req, res, dependencies, conf
118
160
  error: "Not a Popular Investor",
119
161
  message: "This endpoint is only available for users who are Popular Investors",
120
162
  effectiveCid: effectiveCid,
121
- isImpersonating: isImpersonating || false
163
+ isImpersonating: isImpersonating || false,
164
+ actualCid: Number(userCid)
122
165
  });
123
166
  }
124
167
 
@@ -355,7 +398,8 @@ async function getSignedInUserPIPersonalizedMetrics(req, res, dependencies, conf
355
398
  isFallback: foundDate !== today,
356
399
  daysBackFromLatest: checkedDates.indexOf(foundDate),
357
400
  isImpersonating: isImpersonating || false,
358
- actualCid: Number(userCid)
401
+ actualCid: Number(userCid),
402
+ pretendToBePI: devOverride && devOverride.enabled ? (devOverride.pretendToBePI || false) : false
359
403
  });
360
404
 
361
405
  } catch (error) {
@@ -170,14 +170,48 @@ async function finalizeVerification(req, res, dependencies, config) {
170
170
  // Send unified request to task engine that handles both portfolio/history AND social data
171
171
  const pubsubUtils = new PubSubUtils(dependencies);
172
172
 
173
+ // Generate a requestId for tracking and to ensure finalizeOnDemandRequest runs
174
+ // This is critical - without requestId, the root data indexer and computations won't be triggered
175
+ const requestId = `signup-${realCID}-${Date.now()}`;
176
+ const { signedInUsersCollection } = config;
177
+
178
+ // Create request tracking document (similar to user sync requests)
179
+ try {
180
+ const requestRef = db.collection('user_sync_requests')
181
+ .doc(String(realCID))
182
+ .collection('requests')
183
+ .doc(requestId);
184
+
185
+ await requestRef.set({
186
+ targetUserCid: realCID,
187
+ username: profileData.username,
188
+ status: 'pending',
189
+ source: 'user_signup',
190
+ requestedAt: FieldValue.serverTimestamp(),
191
+ createdAt: FieldValue.serverTimestamp(),
192
+ metadata: {
193
+ isNewUser: true,
194
+ verificationSource: 'otp_verification'
195
+ }
196
+ });
197
+
198
+ logger.log('INFO', `[Verification] Created request tracking document for ${username} (${realCID}): ${requestId}`);
199
+ } catch (reqError) {
200
+ logger.log('WARN', `[Verification] Failed to create request tracking document: ${reqError.message}`);
201
+ // Continue anyway - the requestId is still set
202
+ }
203
+
173
204
  // Create a unified on-demand request that includes both portfolio and social
174
205
  // The task engine will process both, update root data, then trigger computations
175
206
  const unifiedTask = {
176
207
  type: 'ON_DEMAND_USER_UPDATE', // Matches Task Engine handler
208
+ cid: realCID, // Top-level CID for handler
209
+ username: profileData.username, // Top-level username for handler
210
+ requestId: requestId, // CRITICAL: Required for finalizeOnDemandRequest to run
211
+ source: 'user_signup', // Mark as signup to ensure computations are triggered
177
212
  data: {
178
213
  cid: realCID,
179
214
  username: profileData.username,
180
- source: 'user_signup', // Mark as signup to ensure computations are triggered
181
215
  includeSocial: true, // Flag to include social data fetch
182
216
  since: new Date(Date.now() - (7 * 24 * 60 * 60 * 1000)).toISOString() // Last 7 days for social
183
217
  },
@@ -185,22 +219,26 @@ async function finalizeVerification(req, res, dependencies, config) {
185
219
  onDemand: true,
186
220
  targetCid: realCID, // Optimization: only process this user
187
221
  isNewUser: true, // Flag to add to daily update queue
188
- requestedAt: new Date().toISOString()
222
+ requestedAt: new Date().toISOString(),
223
+ userType: 'SIGNED_IN_USER' // Explicitly set userType for computation selection
189
224
  }
190
225
  };
191
226
 
192
227
  // Only trigger if user is public (has portfolio data)
193
228
  if (!isOptOut) {
194
229
  await pubsubUtils.publish(taskEngineTopic, unifiedTask);
195
- logger.log('INFO', `[Verification] Triggered unified data fetch (portfolio + social) for ${username} (${realCID}) via on-demand topic`);
230
+ logger.log('INFO', `[Verification] Triggered unified data fetch (portfolio + social) for ${username} (${realCID}) via on-demand topic with requestId: ${requestId}`);
196
231
  } else {
197
232
  // For private users, still fetch social data but no portfolio
198
233
  const socialOnlyTask = {
199
234
  type: 'ON_DEMAND_USER_UPDATE',
235
+ cid: realCID,
236
+ username: profileData.username,
237
+ requestId: requestId, // CRITICAL: Required for finalizeOnDemandRequest to run
238
+ source: 'user_signup',
200
239
  data: {
201
240
  cid: realCID,
202
241
  username: profileData.username,
203
- source: 'user_signup',
204
242
  includeSocial: true,
205
243
  portfolioOnly: false, // Skip portfolio for private users
206
244
  since: new Date(Date.now() - (7 * 24 * 60 * 60 * 1000)).toISOString()
@@ -209,11 +247,12 @@ async function finalizeVerification(req, res, dependencies, config) {
209
247
  onDemand: true,
210
248
  targetCid: realCID,
211
249
  isNewUser: true,
212
- requestedAt: new Date().toISOString()
250
+ requestedAt: new Date().toISOString(),
251
+ userType: 'SIGNED_IN_USER' // Explicitly set userType for computation selection
213
252
  }
214
253
  };
215
254
  await pubsubUtils.publish(taskEngineTopic, socialOnlyTask);
216
- logger.log('INFO', `[Verification] Triggered social-only fetch for private user ${username} (${realCID}) via on-demand topic`);
255
+ logger.log('INFO', `[Verification] Triggered social-only fetch for private user ${username} (${realCID}) via on-demand topic with requestId: ${requestId}`);
217
256
  }
218
257
 
219
258
  return res.status(200).json({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bulltrackers-module",
3
- "version": "1.0.559",
3
+ "version": "1.0.561",
4
4
  "description": "Helper Functions for Bulltrackers.",
5
5
  "main": "index.js",
6
6
  "files": [