Attempt to clarify installation instructions a little
[vchess.git] / server / routes / challenges.js
... / ...
CommitLineData
1let router = require("express").Router();
2const access = require("../utils/access");
3const ChallengeModel = require("../models/Challenge");
4const UserModel = require("../models/User"); //for name check
5const params = require("../config/parameters");
6
7router.post("/challenges", access.logged, access.ajax, (req,res) => {
8 if (ChallengeModel.checkChallenge(req.body.chall)) {
9 let challenge = {
10 fen: req.body.chall.fen,
11 cadence: req.body.chall.cadence,
12 options: req.body.chall.options,
13 randomness: req.body.chall.randomness,
14 vid: req.body.chall.vid,
15 uid: req.userId,
16 to: req.body.chall.to, //string: user name (may be empty)
17 };
18 const insertChallenge = () => {
19 ChallengeModel.create(challenge, (err, ret) => {
20 res.json(err || ret);
21 });
22 };
23 if (req.body.chall.to) {
24 UserModel.getOne(
25 "name", challenge.to, "id, name, email, notify",
26 (err, user) => {
27 if (err || !user) res.json(err || {errmsg: "Typo in player name"});
28 else {
29 challenge.to = user.id; //ready now to insert challenge
30 insertChallenge();
31 if (user.notify) {
32 UserModel.notify(
33 user,
34 "New challenge : " + params.siteURL + "/#/?disp=corr");
35 }
36 }
37 }
38 );
39 }
40 else insertChallenge();
41 }
42});
43
44router.get("/challenges", access.ajax, (req,res) => {
45 const uid = req.query.uid;
46 if (uid.match(/^[0-9]+$/)) {
47 ChallengeModel.getByUser(uid, (err,challenges) => {
48 res.json(err || { challenges: challenges });
49 });
50 }
51});
52
53router.delete("/challenges", access.logged, access.ajax, (req,res) => {
54 const cid = req.query.id;
55 if (cid.match(/^[0-9]+$/)) {
56 ChallengeModel.safeRemove(cid, req.userId);
57 res.json({});
58 }
59});
60
61module.exports = router;