Attempt to clarify installation instructions a little
[vchess.git] / server / routes / challenges.js
CommitLineData
ab4f4bf2
BA
1let router = require("express").Router();
2const access = require("../utils/access");
3const ChallengeModel = require("../models/Challenge");
052d17ea 4const UserModel = require("../models/User"); //for name check
fe4c7e67 5const params = require("../config/parameters");
8d7e2786 6
052d17ea 7router.post("/challenges", access.logged, access.ajax, (req,res) => {
0234201f
BA
8 if (ChallengeModel.checkChallenge(req.body.chall)) {
9 let challenge = {
866842c3
BA
10 fen: req.body.chall.fen,
11 cadence: req.body.chall.cadence,
eb2d61de 12 options: req.body.chall.options,
7ba4a5bc 13 randomness: req.body.chall.randomness,
866842c3
BA
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 = () => {
0234201f
BA
19 ChallengeModel.create(challenge, (err, ret) => {
20 res.json(err || ret);
866842c3
BA
21 });
22 };
0234201f 23 if (req.body.chall.to) {
fccaa878
BA
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 }
866842c3 37 }
fccaa878 38 );
e8ea1e35
BA
39 }
40 else insertChallenge();
866842c3
BA
41 }
42});
43
44router.get("/challenges", access.ajax, (req,res) => {
45 const uid = req.query.uid;
0234201f 46 if (uid.match(/^[0-9]+$/)) {
866842c3 47 ChallengeModel.getByUser(uid, (err,challenges) => {
0234201f 48 res.json(err || { challenges: challenges });
bf20f404 49 });
bf20f404 50 }
4edfed6c
BA
51});
52
1f49533d 53router.delete("/challenges", access.logged, access.ajax, (req,res) => {
77fd7298 54 const cid = req.query.id;
0234201f 55 if (cid.match(/^[0-9]+$/)) {
866842c3
BA
56 ChallengeModel.safeRemove(cid, req.userId);
57 res.json({});
58 }
1f49533d 59});
8d7e2786
BA
60
61module.exports = router;