@@ -81,12 +81,20 @@ export const cleanupSoftDeletedNotifications = onSchedule({
8181 } ,
8282 async ( ) => {
8383 try {
84+ let lastDocument :
85+ FirebaseFirestore . QueryDocumentSnapshot < FirebaseFirestore . DocumentData > | undefined ;
86+
8487 while ( true ) {
85- const snapshot = await admin . firestore ( )
88+ let query = admin . firestore ( )
8689 . collectionGroup ( "notifications" )
8790 . where ( "isDeleted" , "==" , true )
91+ . orderBy ( admin . firestore . FieldPath . documentId ( ) )
8892 . limit ( CLEANUP_BATCH_SIZE )
89- . get ( ) ;
93+ if ( lastDocument ) {
94+ query = query . startAfter ( lastDocument ) ;
95+ }
96+
97+ const snapshot = await query . get ( ) ;
9098
9199 if ( snapshot . empty ) { return ; }
92100
@@ -97,6 +105,7 @@ export const cleanupSoftDeletedNotifications = onSchedule({
97105 await batch . commit ( ) ;
98106
99107 if ( snapshot . size < CLEANUP_BATCH_SIZE ) { return ; }
108+ lastDocument = snapshot . docs [ snapshot . docs . length - 1 ] ;
100109 }
101110 } catch ( error ) {
102111 logger . error (
@@ -105,6 +114,7 @@ export const cleanupSoftDeletedNotifications = onSchedule({
105114 {
106115 collectionGroup : "notifications" ,
107116 filter : "isDeleted == true" ,
117+ orderBy : "documentId" ,
108118 cleanupBatchSize : CLEANUP_BATCH_SIZE
109119 }
110120 ) ;
@@ -127,6 +137,7 @@ export const cleanupUnusedTodoNotificationRecords = onSchedule({
127137 . where ( "isCompleted" , "==" , true )
128138 . where ( "dueDate" , "<" , admin . firestore . Timestamp . now ( ) )
129139 . orderBy ( "dueDate" )
140+ . orderBy ( admin . firestore . FieldPath . documentId ( ) )
130141 . limit ( QUERY_BATCH_SIZE ) ;
131142
132143 if ( lastDocument ) {
@@ -142,7 +153,7 @@ export const cleanupUnusedTodoNotificationRecords = onSchedule({
142153 {
143154 collectionGroup : "todoLists" ,
144155 filter : "isCompleted == true && dueDate < now" ,
145- orderBy : "dueDate" ,
156+ orderBy : [ "dueDate" , "documentId" ] ,
146157 queryBatchSize : QUERY_BATCH_SIZE
147158 }
148159 ) ;
0 commit comments