1 // AJAX methods to get, create, update or delete a challenge
3 let router
= require("express").Router();
4 const access
= require("../utils/access");
5 const ChallengeModel
= require("../models/Challenge");
6 const UserModel
= require("../models/User"); //for name check
8 router
.post("/challenges", access
.logged
, access
.ajax
, (req
,res
) => {
9 const error
= ChallengeModel
.checkChallenge(req
.body
.chall
);
10 // TODO: treat "to" field separately (search users by name)
11 // --> replace "to" by an array of uid (in chall), then call:
12 const from = req
.userId
;
13 // let to = !!req.body.to ? ObjectID(req.body.to) : undefined;
14 // let nameTo = !!req.body.nameTo ? req.body.nameTo : undefined;
15 // let vid = ObjectID(req.body.vid);
16 // if (!to && !!nameTo)
18 // UserModel.getByName(nameTo, (err,user) => {
19 // access.checkRequest(res, err, user, "Opponent not found", () => {
20 // createChallenge(vid, from, user._id, res);
25 // createChallenge(vid, from, to, res);
27 // createChallenge(vid, from, undefined, res); //automatch
28 ChallengeModel
.create(req
.body
.chall
, (err
,lastId
) => {
29 res
.json(err
|| {cid: lastId
["rowid"]});
34 //router.get("/challenges", access.logged, access.ajax, (req,res) => {
35 // if (req.query["uid"] != req.user._id)
36 // return res.json({errmsg: "Not your challenges"});
37 // let uid = ObjectID(req.query["uid"]);
38 // ChallengeModel.getByPlayer(uid, (err, challengeArray) => {
39 // res.json(err || {challenges: challengeArray});
43 //function createChallenge(vid, from, to, res)
45 // ChallengeModel.create(vid, from, to, (err, chall) => {
47 // // A challenge can be sent using only name, thus 'to' is returned
54 //router.delete("/challenges", access.logged, access.ajax, (req,res) => {
55 // let cid = ObjectID(req.query.cid);
56 // ChallengeModel.getById(cid, (err,chall) => {
57 // access.checkRequest(res, err, chall, "Challenge not found", () => {
58 // if (!chall.from.equals(req.user._id) && !!chall.to && !chall.to.equals(req.user._id))
59 // return res.json({errmsg: "Not your challenge"});
60 // ChallengeModel.remove(cid, err => {
61 // res.json(err || {});
67 module
.exports
= router
;