bulltrackers-module 1.0.549 → 1.0.550

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.
@@ -285,11 +285,22 @@ async function readWithMigration(db, category, subcategory, params, options = {}
285
285
  try {
286
286
  if (isCollection) {
287
287
  // Collection path must have odd number of segments
288
- const collectionRef = db.collection(newPath);
289
- const snapshot = await collectionRef.get();
290
- if (!snapshot.empty) {
291
- if (logger) logger.log('INFO', `[readWithMigration] Found data in new path: ${newPath}`);
292
- return { snapshot, source: 'new', path: newPath };
288
+ if (documentId) {
289
+ // Read specific document from collection
290
+ const docRef = db.collection(newPath).doc(documentId);
291
+ const doc = await docRef.get();
292
+ if (doc.exists) {
293
+ if (logger) logger.log('INFO', `[readWithMigration] Found document in new path: ${docRef.path}`);
294
+ return { data: doc.data(), exists: true, source: 'new', path: docRef.path };
295
+ }
296
+ } else {
297
+ // Read entire collection
298
+ const collectionRef = db.collection(newPath);
299
+ const snapshot = await collectionRef.get();
300
+ if (!snapshot.empty) {
301
+ if (logger) logger.log('INFO', `[readWithMigration] Found data in new path: ${newPath}`);
302
+ return { snapshot, source: 'new', path: newPath };
303
+ }
293
304
  }
294
305
  } else {
295
306
  // Document path: if newPath has even segments, it's already a document path
@@ -345,17 +356,34 @@ async function readWithMigration(db, category, subcategory, params, options = {}
345
356
  legacyPath = legacyPath.replace(/{version}/g, params.version || '{version}');
346
357
 
347
358
  if (isCollection) {
348
- const legacyCollectionRef = db.collection(legacyPath);
349
- const legacySnapshot = await legacyCollectionRef.get();
350
- if (!legacySnapshot.empty) {
351
- if (logger) logger.log('INFO', `[readWithMigration] Found data in legacy path: ${legacyPath}, will migrate`);
352
-
353
- // Auto-migrate if new path is available
354
- if (newPath) {
355
- await migrateCollectionData(db, legacySnapshot, newPath, dataType, logger);
359
+ if (documentId) {
360
+ // Read specific document from legacy collection
361
+ const legacyDocRef = db.collection(legacyPath).doc(documentId);
362
+ const legacyDoc = await legacyDocRef.get();
363
+ if (legacyDoc.exists) {
364
+ if (logger) logger.log('INFO', `[readWithMigration] Found document in legacy path: ${legacyDocRef.path}, will migrate`);
365
+
366
+ // Auto-migrate if new path is available
367
+ if (newPath) {
368
+ await migrateDocumentData(db, legacyDoc, newPath, documentId, logger);
369
+ }
370
+
371
+ return { data: legacyDoc.data(), exists: true, source: 'legacy', path: legacyDocRef.path, migrated: !!newPath };
372
+ }
373
+ } else {
374
+ // Read entire legacy collection
375
+ const legacyCollectionRef = db.collection(legacyPath);
376
+ const legacySnapshot = await legacyCollectionRef.get();
377
+ if (!legacySnapshot.empty) {
378
+ if (logger) logger.log('INFO', `[readWithMigration] Found data in legacy path: ${legacyPath}, will migrate`);
379
+
380
+ // Auto-migrate if new path is available
381
+ if (newPath) {
382
+ await migrateCollectionData(db, legacySnapshot, newPath, dataType, logger);
383
+ }
384
+
385
+ return { snapshot: legacySnapshot, source: 'legacy', path: legacyPath, migrated: !!newPath };
356
386
  }
357
-
358
- return { snapshot: legacySnapshot, source: 'legacy', path: legacyPath, migrated: !!newPath };
359
387
  }
360
388
  } else {
361
389
  const legacyDocRef = documentId
@@ -225,11 +225,24 @@ async function updateWatchlist(req, res, dependencies, config) {
225
225
  }
226
226
  );
227
227
 
228
- if (!readResult || !readResult.exists) {
228
+ if (!readResult) {
229
229
  return res.status(404).json({ error: "Watchlist not found" });
230
230
  }
231
231
 
232
- const existingData = readResult.data();
232
+ // Handle both document read (with data) and collection read (with snapshot)
233
+ let existingData;
234
+ if (readResult.data) {
235
+ existingData = readResult.data;
236
+ } else if (readResult.snapshot && !readResult.snapshot.empty) {
237
+ // If we got a snapshot, find the specific document
238
+ const doc = readResult.snapshot.docs.find(d => d.id === id);
239
+ if (!doc) {
240
+ return res.status(404).json({ error: "Watchlist not found" });
241
+ }
242
+ existingData = doc.data();
243
+ } else {
244
+ return res.status(404).json({ error: "Watchlist not found" });
245
+ }
233
246
 
234
247
  // Verify ownership
235
248
  if (existingData.createdBy !== Number(userCid)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bulltrackers-module",
3
- "version": "1.0.549",
3
+ "version": "1.0.550",
4
4
  "description": "Helper Functions for Bulltrackers.",
5
5
  "main": "index.js",
6
6
  "files": [