@@ -46,64 +46,86 @@ async function updateNotifications(
4646 todoId : string ,
4747 todoCategory : string
4848) : Promise < void > {
49- let lastDocument :
50- FirebaseFirestore . QueryDocumentSnapshot < FirebaseFirestore . DocumentData > | undefined ;
51-
52- while ( true ) {
53- let query = admin . firestore ( )
54- . collection ( `users/${ userId } /notifications` )
55- . where ( "todoId" , "==" , todoId )
56- . orderBy ( admin . firestore . FieldPath . documentId ( ) )
57- . limit ( BATCH_SIZE ) ;
58-
59- if ( lastDocument ) {
60- query = query . startAfter ( lastDocument ) ;
61- }
62-
63- const snapshot = await query . get ( ) ;
64- if ( snapshot . empty ) { return ; }
65-
66- const batch = admin . firestore ( ) . batch ( ) ;
67- snapshot . docs . forEach ( ( document ) => {
68- batch . update ( document . ref , { todoCategory } ) ;
69- } ) ;
70- await batch . commit ( ) ;
71-
72- if ( snapshot . size < BATCH_SIZE ) { return ; }
73- lastDocument = snapshot . docs [ snapshot . docs . length - 1 ] ;
74- }
49+ await updateNotificationBatch ( userId , todoId , todoCategory )
7550}
7651
7752async function updateNotificationTasks (
7853 userId : string ,
7954 todoId : string ,
8055 todoCategory : string
8156) : Promise < void > {
82- let lastDocument :
83- FirebaseFirestore . QueryDocumentSnapshot < FirebaseFirestore . DocumentData > | undefined ;
84-
85- while ( true ) {
86- let query = admin . firestore ( )
87- . collection ( "notificationTasks" )
88- . where ( "userId" , "==" , userId )
89- . where ( "todoId" , "==" , todoId )
90- . orderBy ( admin . firestore . FieldPath . documentId ( ) )
91- . limit ( BATCH_SIZE ) ;
92-
93- if ( lastDocument ) {
94- query = query . startAfter ( lastDocument ) ;
95- }
57+ await updateNotificationTaskBatch ( userId , todoId , todoCategory )
58+ }
59+
60+ async function updateNotificationBatch (
61+ userId : string ,
62+ todoId : string ,
63+ todoCategory : string ,
64+ lastDocument ?:
65+ FirebaseFirestore . QueryDocumentSnapshot < FirebaseFirestore . DocumentData >
66+ ) : Promise < void > {
67+ let query = admin . firestore ( )
68+ . collection ( `users/${ userId } /notifications` )
69+ . where ( "todoId" , "==" , todoId )
70+ . orderBy ( admin . firestore . FieldPath . documentId ( ) )
71+ . limit ( BATCH_SIZE ) ;
72+
73+ if ( lastDocument ) {
74+ query = query . startAfter ( lastDocument ) ;
75+ }
76+
77+ const snapshot = await query . get ( ) ;
78+ if ( snapshot . empty ) { return ; }
79+
80+ const batch = admin . firestore ( ) . batch ( ) ;
81+ snapshot . docs . forEach ( ( document ) => {
82+ batch . update ( document . ref , { todoCategory } ) ;
83+ } ) ;
84+ await batch . commit ( ) ;
9685
97- const snapshot = await query . get ( ) ;
98- if ( snapshot . empty ) { return ; }
86+ if ( snapshot . size < BATCH_SIZE ) { return ; }
9987
100- const batch = admin . firestore ( ) . batch ( ) ;
101- snapshot . docs . forEach ( ( document ) => {
102- batch . update ( document . ref , { todoCategory } ) ;
103- } ) ;
104- await batch . commit ( ) ;
88+ await updateNotificationBatch (
89+ userId ,
90+ todoId ,
91+ todoCategory ,
92+ snapshot . docs [ snapshot . docs . length - 1 ]
93+ ) ;
94+ }
10595
106- if ( snapshot . size < BATCH_SIZE ) { return ; }
107- lastDocument = snapshot . docs [ snapshot . docs . length - 1 ] ;
96+ async function updateNotificationTaskBatch (
97+ userId : string ,
98+ todoId : string ,
99+ todoCategory : string ,
100+ lastDocument ?:
101+ FirebaseFirestore . QueryDocumentSnapshot < FirebaseFirestore . DocumentData >
102+ ) : Promise < void > {
103+ let query = admin . firestore ( )
104+ . collection ( "notificationTasks" )
105+ . where ( "userId" , "==" , userId )
106+ . where ( "todoId" , "==" , todoId )
107+ . orderBy ( admin . firestore . FieldPath . documentId ( ) )
108+ . limit ( BATCH_SIZE ) ;
109+
110+ if ( lastDocument ) {
111+ query = query . startAfter ( lastDocument ) ;
108112 }
113+
114+ const snapshot = await query . get ( ) ;
115+ if ( snapshot . empty ) { return ; }
116+
117+ const batch = admin . firestore ( ) . batch ( ) ;
118+ snapshot . docs . forEach ( ( document ) => {
119+ batch . update ( document . ref , { todoCategory } ) ;
120+ } ) ;
121+ await batch . commit ( ) ;
122+
123+ if ( snapshot . size < BATCH_SIZE ) { return ; }
124+
125+ await updateNotificationTaskBatch (
126+ userId ,
127+ todoId ,
128+ todoCategory ,
129+ snapshot . docs [ snapshot . docs . length - 1 ]
130+ ) ;
109131}
0 commit comments