Fix CRON tasks
authorBenjamin Auder <benjamin.auder@somewhere>
Fri, 7 Feb 2020 12:39:12 +0000 (13:39 +0100)
committerBenjamin Auder <benjamin.auder@somewhere>
Fri, 7 Feb 2020 12:39:12 +0000 (13:39 +0100)
server/models/Challenge.js
server/models/Game.js

index fa0407d..7ccaa76 100644 (file)
@@ -104,14 +104,14 @@ const ChallengeModel =
     const day = 86400000;
     db.serialize(function() {
       const query =
-        "SELECT id, target " +
+        "SELECT id, target, added " +
         "FROM Challenges";
       db.all(query, (err, challenges) => {
         challenges.forEach(c => {
           if ((!c.target && tsNow - c.added > 30*day) ||
             (!!c.target && tsNow - c.added > 2*day))
           {
-            db.run("DELETE FROM CHallenges WHERE id = " + c.id);
+            db.run("DELETE FROM Challenges WHERE id = " + c.id);
           }
         });
       });
index d57aac8..79bf071 100644 (file)
@@ -268,7 +268,7 @@ const GameModel =
     const day = 86400000;
     db.serialize(function() {
       let query =
-        "SELECT id,score " +
+        "SELECT id,score,created " +
         "FROM Games ";
       db.all(query, (err,games) => {
         games.forEach(g => {
@@ -277,13 +277,19 @@ const GameModel =
             "FROM Moves " +
             "WHERE gid = " + g.id;
           db.get(query, (err2,updated) => {
-            if (!updated && tsNow - g.created > 7*day)
-              return GameModel.remove(g.id);
-            const lastMaj = updated.lastMaj;
-            if (g.score != "*" && tsNow - lastMaj > 7*day ||
-              g.score == "*" && tsNow - lastMaj > 91*day)
+            if (!updated.lastMaj)
             {
-              GameModel.remove(g.id);
+              if (tsNow - g.created > 7*day)
+                return GameModel.remove(g.id);
+            }
+            else //at least one move
+            {
+              const lastMaj = updated.lastMaj;
+              if (g.score != "*" && tsNow - lastMaj > 7*day ||
+                g.score == "*" && tsNow - lastMaj > 91*day)
+              {
+                GameModel.remove(g.id);
+              }
             }
           });
         });