Fix ajax for challenge creation
[vchess.git] / server / routes / challenges.js
index faa38c5..acec3c1 100644 (file)
@@ -24,9 +24,8 @@ router.post("/challenges", access.logged, access.ajax, (req,res) => {
     to: req.body.chall.to, //string: user name (may be empty)
   };
   const insertChallenge = () => {
-    ChallengeModel.create(challenge, (err) => {
-      if (!!err)
-        return res.json(err);
+    ChallengeModel.create(challenge, (err,ret) => {
+      return res.json(err || {cid:ret.cid});
     });
   };
   if (!!req.body.chall.to)
@@ -35,24 +34,13 @@ router.post("/challenges", access.logged, access.ajax, (req,res) => {
       if (!!err || !user)
         return res.json(err | {errmsg: "Typo in player name"});
       challenge.to = user.id; //ready now to insert challenge
+      insertChallenge();
     });
-    insertChallenge();
   }
   else
     insertChallenge();
 });
 
-// TODO: either like that, or remove challenge in /games POST ?
-// "Challenge update" --> someone accepted a challenge
-router.put("/challenges", access.logged, access.ajax, (req,res) => {
-  // launchGame(cid, uid) //req.body.chall
-  // TODO: gather challenge infos
-  // Then create game, and remove challenge:
-  ChallengeModel.remove(cid, req.userId, err => {
-    res.json(err || {});
-  });
-});
-
 router.delete("/challenges", access.logged, access.ajax, (req,res) => {
   const cid = req.query.id;
   ChallengeModel.remove(cid, req.userId, err => {