bulltrackers-module 1.0.568 → 1.0.569

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.
@@ -42,17 +42,47 @@ async function getUpdateTargets(userType, thresholds, config, dependencies) {
42
42
 
43
43
  /**
44
44
  * Sub-pipe: pipe.orchestrator.dispatchUpdates
45
+ * UPDATED: Added real-time validation to skip users already updated today.
45
46
  */
46
47
  async function dispatchUpdates(targets, userType, config, dependencies) {
47
- const { logger, pubsubUtils } = dependencies;
48
+ const { logger, pubsubUtils, db } = dependencies;
48
49
  const { dispatcherTopicName, taskBatchSize, pubsubBatchSize } = config;
49
50
 
50
- if (targets.length === 0) {
51
- logger.log('INFO', `[Orchestrator Helpers] No ${userType} update targets to dispatch.`);
52
- return;
53
- }
51
+ if (targets.length === 0) return;
52
+
53
+ // --- NEW VALIDATION BLOCK ---
54
+ const startOfToday = new Date();
55
+ startOfToday.setUTCHours(0, 0, 0, 0);
56
+
57
+ logger.log('INFO', `[Orchestrator Validation] Verifying ${targets.length} targets for ${userType}...`);
54
58
 
55
- logger.log('INFO', `[Orchestrator Helpers] Dispatching ${targets.length} update tasks for ${userType} to ${dispatcherTopicName}...`);
59
+ const validTargets = [];
60
+ for (const target of targets) {
61
+ const cid = target.userId || target.cid || (typeof target === 'string' ? target : null);
62
+ if (!cid) { validTargets.push(target); continue; }
63
+
64
+ // Check the actual Firestore record for today's completion
65
+ const collection = userType === 'popular_investor' ? 'PopularInvestors' :
66
+ userType === 'signed_in_user' ? 'SignedInUsers' : 'NormalUsers';
67
+
68
+ const doc = await db.collection(collection).doc(String(cid)).get();
69
+ const lastUpdated = doc.data()?.lastUpdated?.updatedAt?.toDate?.() ||
70
+ doc.data()?.lastUpdate?.toDate?.();
71
+
72
+ if (lastUpdated && lastUpdated >= startOfToday) {
73
+ logger.log('TRACE', `[Orchestrator Validation] Skipping ${cid} - Already updated today.`);
74
+ continue;
75
+ }
76
+ validTargets.push(target);
77
+ }
78
+
79
+ if (validTargets.length === 0) {
80
+ logger.log('INFO', `[Orchestrator Helpers] All ${targets.length} targets already updated. Skipping dispatch.`);
81
+ return;
82
+ }
83
+ // --- END VALIDATION BLOCK ---
84
+
85
+ logger.log('INFO', `[Orchestrator Helpers] Dispatching ${validTargets.length} validated update tasks for ${userType}...`);
56
86
 
57
87
  const individualTasks = targets.map(target => {
58
88
  let task = { userType };
@@ -133,7 +133,7 @@ async function handleRequest(message, context, configObj, dependencies) {
133
133
  if (updateTasksCount > 0) {
134
134
  try {
135
135
  // This batch counter is critical for triggering the Root Data Indexer
136
- batchCounterRef = db.collection('task_engine_batch_counters').doc(`${today}-${taskId}`);
136
+ batchCounterRef = db.collection('system_task_counts').doc(`${today}-${taskId}`);
137
137
  await batchCounterRef.set({
138
138
  totalTasks: updateTasksCount,
139
139
  remainingTasks: updateTasksCount,
@@ -308,7 +308,7 @@ async function handleGenericUserUpdate(taskData, config, dependencies, isPI) {
308
308
 
309
309
  // 5. Batch Counter Decrement (Critical for Scheduled Runs)
310
310
  if (batchCounterId) {
311
- const counterRef = db.collection('task_engine_batch_counters').doc(batchCounterId);
311
+ const counterRef = db.collection('system_task_counts').doc(batchCounterId);
312
312
  await counterRef.update({ remainingTasks: FieldValue.increment(-1) });
313
313
 
314
314
  // Check if we need to trigger root indexer for the batch
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bulltrackers-module",
3
- "version": "1.0.568",
3
+ "version": "1.0.569",
4
4
  "description": "Helper Functions for Bulltrackers.",
5
5
  "main": "index.js",
6
6
  "files": [