bulltrackers-module 1.0.562 → 1.0.563

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.
@@ -6,6 +6,7 @@
6
6
  /**
7
7
  * Find the latest available date for signed-in user portfolio data
8
8
  * Searches backwards from today up to maxDaysBack days
9
+ * Checks both new root data path and legacy path for compatibility
9
10
  * @param {Firestore} db - Firestore instance
10
11
  * @param {string} signedInUsersCollection - Collection name (legacy)
11
12
  * @param {string|number} userCid - User CID
@@ -13,9 +14,51 @@
13
14
  * @returns {Promise<string|null>} - Date string (YYYY-MM-DD) or null if not found
14
15
  */
15
16
  async function findLatestPortfolioDate(db, signedInUsersCollection, userCid, maxDaysBack = 30) {
16
- const CANARY_BLOCK_ID = '19M';
17
17
  const today = new Date();
18
+ const userCidStr = String(userCid);
19
+
20
+ // First, check the user-centric latest snapshot (fastest check)
21
+ try {
22
+ const latestRef = db.collection('SignedInUsers')
23
+ .doc(userCidStr)
24
+ .collection('portfolio')
25
+ .doc('latest');
26
+
27
+ const latestDoc = await latestRef.get();
28
+ if (latestDoc.exists) {
29
+ const data = latestDoc.data();
30
+ if (data && data.date) {
31
+ return data.date; // Found in latest snapshot
32
+ }
33
+ }
34
+ } catch (error) {
35
+ // Continue to other checks if this fails
36
+ }
37
+
38
+ // Second, check the new root data path: SignedInUserPortfolioData/{date}/{cid}/{cid}
39
+ for (let i = 0; i < maxDaysBack; i++) {
40
+ const checkDate = new Date(today);
41
+ checkDate.setDate(checkDate.getDate() - i);
42
+ const dateStr = checkDate.toISOString().split('T')[0];
43
+
44
+ try {
45
+ const rootDataRef = db.collection('SignedInUserPortfolioData')
46
+ .doc(dateStr)
47
+ .collection(userCidStr)
48
+ .doc(userCidStr);
49
+
50
+ const rootDataDoc = await rootDataRef.get();
51
+ if (rootDataDoc.exists) {
52
+ return dateStr; // Found data in root data collection
53
+ }
54
+ } catch (error) {
55
+ // Continue to next date if error
56
+ continue;
57
+ }
58
+ }
18
59
 
60
+ // Third, check legacy path for backward compatibility: {signedInUsersCollection}/19M/snapshots/{date}/parts
61
+ const CANARY_BLOCK_ID = '19M';
19
62
  for (let i = 0; i < maxDaysBack; i++) {
20
63
  const checkDate = new Date(today);
21
64
  checkDate.setDate(checkDate.getDate() - i);
@@ -33,8 +76,8 @@ async function findLatestPortfolioDate(db, signedInUsersCollection, userCid, max
33
76
  // Check if user's CID exists in any part document
34
77
  for (const partDoc of partsSnapshot.docs) {
35
78
  const partData = partDoc.data();
36
- if (partData && partData[String(userCid)]) {
37
- return dateStr; // Found data for this date
79
+ if (partData && partData[userCidStr]) {
80
+ return dateStr; // Found data for this date in legacy path
38
81
  }
39
82
  }
40
83
  } catch (error) {
@@ -147,64 +147,115 @@ async function getUserDataStatus(req, res, dependencies, config) {
147
147
  }
148
148
 
149
149
  // Check history data with fallback
150
+ // Check new root data path first: SignedInUsers/{cid}/tradeHistory/latest
150
151
  let historyExists = false;
151
152
  let historyDate = null;
152
153
  let isHistoryFallback = false;
153
154
 
154
- const historyCollection = signedInHistoryCollection || 'signed_in_user_history';
155
-
156
- // Check today first
157
- const todayHistoryRef = db.collection(historyCollection)
158
- .doc(CANARY_BLOCK_ID)
159
- .collection('snapshots')
160
- .doc(today)
161
- .collection('parts');
162
-
163
- const todayHistorySnapshot = await todayHistoryRef.get();
164
-
165
- if (!todayHistorySnapshot.empty) {
166
- for (const partDoc of todayHistorySnapshot.docs) {
167
- const partData = partDoc.data();
168
- if (partData && partData[String(userCid)]) {
155
+ try {
156
+ const latestHistoryRef = db.collection('SignedInUsers')
157
+ .doc(String(userCid))
158
+ .collection('tradeHistory')
159
+ .doc('latest');
160
+
161
+ const latestHistoryDoc = await latestHistoryRef.get();
162
+ if (latestHistoryDoc.exists) {
163
+ const data = latestHistoryDoc.data();
164
+ if (data && data.date) {
169
165
  historyExists = true;
170
- historyDate = today;
171
- break;
166
+ historyDate = data.date;
167
+ isHistoryFallback = data.date !== today;
168
+ logger.log('INFO', `[getUserDataStatus] Found history data in latest snapshot: ${historyDate}`);
172
169
  }
173
170
  }
171
+ } catch (error) {
172
+ // Continue to other checks if this fails
174
173
  }
175
174
 
176
- // If not found today, search backwards
175
+ // If not found in latest snapshot, check new root data path: SignedInUserTradeHistoryData/{date}/{cid}/{cid}
177
176
  if (!historyExists) {
178
- for (let i = 1; i <= 30; i++) {
177
+ for (let i = 0; i <= 30; i++) {
179
178
  const checkDate = new Date();
180
179
  checkDate.setDate(checkDate.getDate() - i);
181
180
  const dateStr = checkDate.toISOString().split('T')[0];
182
181
 
183
182
  try {
184
- const historyPartsRef = db.collection(historyCollection)
185
- .doc(CANARY_BLOCK_ID)
186
- .collection('snapshots')
183
+ const rootHistoryRef = db.collection('SignedInUserTradeHistoryData')
187
184
  .doc(dateStr)
188
- .collection('parts');
185
+ .collection(String(userCid))
186
+ .doc(String(userCid));
189
187
 
190
- const historyPartsSnapshot = await historyPartsRef.get();
188
+ const rootHistoryDoc = await rootHistoryRef.get();
189
+ if (rootHistoryDoc.exists) {
190
+ historyExists = true;
191
+ historyDate = dateStr;
192
+ isHistoryFallback = dateStr !== today;
193
+ logger.log('INFO', `[getUserDataStatus] Found history data in root data collection: ${historyDate}`);
194
+ break;
195
+ }
196
+ } catch (error) {
197
+ continue;
198
+ }
199
+ }
200
+ }
201
+
202
+ // If still not found, check legacy path for backward compatibility
203
+ if (!historyExists) {
204
+ const historyCollection = signedInHistoryCollection || 'signed_in_user_history';
205
+
206
+ // Check today first
207
+ const todayHistoryRef = db.collection(historyCollection)
208
+ .doc(CANARY_BLOCK_ID)
209
+ .collection('snapshots')
210
+ .doc(today)
211
+ .collection('parts');
212
+
213
+ const todayHistorySnapshot = await todayHistoryRef.get();
214
+
215
+ if (!todayHistorySnapshot.empty) {
216
+ for (const partDoc of todayHistorySnapshot.docs) {
217
+ const partData = partDoc.data();
218
+ if (partData && partData[String(userCid)]) {
219
+ historyExists = true;
220
+ historyDate = today;
221
+ break;
222
+ }
223
+ }
224
+ }
225
+
226
+ // If not found today, search backwards
227
+ if (!historyExists) {
228
+ for (let i = 1; i <= 30; i++) {
229
+ const checkDate = new Date();
230
+ checkDate.setDate(checkDate.getDate() - i);
231
+ const dateStr = checkDate.toISOString().split('T')[0];
191
232
 
192
- if (!historyPartsSnapshot.empty) {
193
- for (const partDoc of historyPartsSnapshot.docs) {
194
- const partData = partDoc.data();
195
- if (partData && partData[String(userCid)]) {
196
- historyExists = true;
197
- historyDate = dateStr;
198
- isHistoryFallback = true;
199
- logger.log('INFO', `[getUserDataStatus] Found history data in fallback date ${dateStr}`);
200
- break;
233
+ try {
234
+ const historyPartsRef = db.collection(historyCollection)
235
+ .doc(CANARY_BLOCK_ID)
236
+ .collection('snapshots')
237
+ .doc(dateStr)
238
+ .collection('parts');
239
+
240
+ const historyPartsSnapshot = await historyPartsRef.get();
241
+
242
+ if (!historyPartsSnapshot.empty) {
243
+ for (const partDoc of historyPartsSnapshot.docs) {
244
+ const partData = partDoc.data();
245
+ if (partData && partData[String(userCid)]) {
246
+ historyExists = true;
247
+ historyDate = dateStr;
248
+ isHistoryFallback = true;
249
+ logger.log('INFO', `[getUserDataStatus] Found history data in legacy fallback date ${dateStr}`);
250
+ break;
251
+ }
201
252
  }
202
253
  }
254
+
255
+ if (historyExists) break;
256
+ } catch (error) {
257
+ continue;
203
258
  }
204
-
205
- if (historyExists) break;
206
- } catch (error) {
207
- continue;
208
259
  }
209
260
  }
210
261
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bulltrackers-module",
3
- "version": "1.0.562",
3
+ "version": "1.0.563",
4
4
  "description": "Helper Functions for Bulltrackers.",
5
5
  "main": "index.js",
6
6
  "files": [