@@ -288,10 +288,72 @@ const fetchAllLogs = async (query) => {
288
288
}
289
289
} ;
290
290
291
+ const updateLogs = async ( ) => {
292
+ const batchSize = 500 ;
293
+ let lastDoc = null ;
294
+ let isCompleted = false ;
295
+
296
+ const summary = {
297
+ totalLogsProcessed : 0 ,
298
+ totalLogsUpdated : 0 ,
299
+ totalOperationsFailed : 0 ,
300
+ failedLogDetails : [ ] ,
301
+ } ;
302
+
303
+ try {
304
+ while ( ! isCompleted ) {
305
+ let query = logsModel . orderBy ( "timestamp" ) . limit ( batchSize ) ;
306
+ if ( lastDoc ) {
307
+ query = query . startAfter ( lastDoc ) ;
308
+ }
309
+ const snapshot = await query . get ( ) ;
310
+
311
+ if ( snapshot . empty ) {
312
+ isCompleted = true ;
313
+ continue ;
314
+ }
315
+
316
+ const batch = firestore . batch ( ) ;
317
+ snapshot . forEach ( ( doc ) => {
318
+ const data = doc . data ( ) ;
319
+ if ( data . meta && data . meta . createdBy ) {
320
+ const updatedMeta = {
321
+ ...data . meta ,
322
+ userId : data . meta . createdBy ,
323
+ } ;
324
+ delete updatedMeta . createdBy ;
325
+
326
+ batch . update ( doc . ref , { meta : updatedMeta } ) ;
327
+ summary . totalLogsUpdated ++ ;
328
+ }
329
+ summary . totalLogsProcessed ++ ;
330
+ } ) ;
331
+
332
+ try {
333
+ await batch . commit ( ) ;
334
+ } catch ( err ) {
335
+ logger . error ( "Batch update failed for logs collection:" , err ) ;
336
+ summary . totalOperationsFailed += snapshot . docs . length ;
337
+ summary . failedLogDetails . push ( ...snapshot . docs . map ( ( doc ) => doc . id ) ) ;
338
+ }
339
+
340
+ lastDoc = snapshot . docs [ snapshot . docs . length - 1 ] ;
341
+ isCompleted = snapshot . docs . length < batchSize ;
342
+ }
343
+
344
+ logger . info ( "Migration completed:" , summary ) ;
345
+ return summary ;
346
+ } catch ( error ) {
347
+ logger . error ( "Error during logs migration:" , error ) ;
348
+ throw error ;
349
+ }
350
+ } ;
351
+
291
352
module . exports = {
292
353
addLog,
293
354
fetchLogs,
294
355
fetchCacheLogs,
295
356
fetchLastAddedCacheLog,
296
357
fetchAllLogs,
358
+ updateLogs,
297
359
} ;
0 commit comments