@@ -4,40 +4,36 @@ const admin = require("firebase-admin");
4
4
admin . initializeApp ( ) ;
5
5
const db = admin . firestore ( ) ;
6
6
7
- // // Create and deploy your first functions
8
- // // https://firebase.google.com/docs/functions/get-started
9
- //
10
- // exports.helloWorld = functions.https.onRequest((request, response) => {
11
- // functions.logger.info("Hello logs!", {structuredData: true});
12
- // response.send("Hello from Firebase!");
13
- // });
14
-
15
7
const resetStreaksAndDeleteTodos = async ( ) => {
16
8
// get all users
17
9
const users = await db . collection ( "users" ) . get ( ) ;
18
10
// loop through all users
19
- return Promise . all ( users . docs . map ( async ( user ) => {
20
- // get user data
21
- const userData = user . data ( ) ;
22
- // if user has a partner
23
- if ( userData . partner ) {
24
- // if not all todos are verified, reset streak
25
- // get todos subcollection ref
26
- const todosRef = user . ref . collection ( "todos" ) ;
27
- // query todos subcollection for all todos
28
- const todos = await todosRef . get ( ) ;
29
- // check if all todos are verified
30
- const allTodosVerified = todos . docs . every ( ( todo ) => todo . data ( ) . verified ) ;
31
- // if not all todos are verified, reset streak
32
- if ( ! allTodosVerified ) {
33
- await user . ref . update ( {
34
- streak : 0 ,
35
- } ) ;
36
- }
37
- // delete all todos
38
- return Promise . all ( todos . docs . map ( ( todo ) => todo . ref . delete ( ) ) ) ;
39
- }
40
- } ) ) ;
11
+ return Promise . all (
12
+ users . docs . map ( async ( user ) => {
13
+ // get user data
14
+ const userData = user . data ( ) ;
15
+ // if user has a partner
16
+ if ( userData . partner ) {
17
+ // if not all todos are verified, reset streak
18
+ // get todos subcollection ref
19
+ const todosRef = user . ref . collection ( "todos" ) ;
20
+ // query todos subcollection for all todos
21
+ const todos = await todosRef . get ( ) ;
22
+ // check if all todos are verified
23
+ const allTodosVerified = todos . docs . every (
24
+ ( todo ) => todo . data ( ) . verified ,
25
+ ) ;
26
+ // if not all todos are verified, reset streak
27
+ if ( ! allTodosVerified ) {
28
+ await user . ref . update ( {
29
+ streak : 0 ,
30
+ } ) ;
31
+ }
32
+ // delete all todos
33
+ return Promise . all ( todos . docs . map ( ( todo ) => todo . ref . delete ( ) ) ) ;
34
+ }
35
+ } ) ,
36
+ ) ;
41
37
} ;
42
38
43
39
const deleteImages = async ( ) => {
@@ -48,8 +44,8 @@ const deleteImages = async () => {
48
44
49
45
// runs every day at 5:00am LA time
50
46
exports . dailyReset = functions . pubsub
51
- . schedule ( "0 5 * * *" )
52
- . timeZone ( "America/Los_Angeles" )
53
- . onRun ( async ( context ) => {
54
- await Promise . all ( [ resetStreaksAndDeleteTodos ( ) , deleteImages ( ) ] ) ;
55
- } ) ;
47
+ . schedule ( "0 5 * * *" )
48
+ . timeZone ( "America/Los_Angeles" )
49
+ . onRun ( async ( context ) => {
50
+ await Promise . all ( [ resetStreaksAndDeleteTodos ( ) , deleteImages ( ) ] ) ;
51
+ } ) ;
0 commit comments