1 let router
= require("express").Router();
2 const access
= require("../utils/access");
3 const ChallengeModel
= require("../models/Challenge");
4 const UserModel
= require("../models/User"); //for name check
5 const params
= require("../config/parameters");
7 router
.post("/challenges", access
.logged
, access
.ajax
, (req
,res
) => {
8 if (ChallengeModel
.checkChallenge(req
.body
.chall
)) {
10 fen: req
.body
.chall
.fen
,
11 cadence: req
.body
.chall
.cadence
,
12 randomness: req
.body
.chall
.randomness
,
13 vid: req
.body
.chall
.vid
,
15 to: req
.body
.chall
.to
, //string: user name (may be empty)
17 const insertChallenge
= () => {
18 ChallengeModel
.create(challenge
, (err
, ret
) => {
22 if (req
.body
.chall
.to
) {
24 "name", challenge
.to
, "id, name, email, notify",
26 if (err
|| !user
) res
.json(err
|| {errmsg: "Typo in player name"});
28 challenge
.to
= user
.id
; //ready now to insert challenge
33 "New challenge : " + params
.siteURL
+ "/#/?disp=corr");
38 } else insertChallenge();
42 router
.get("/challenges", access
.ajax
, (req
,res
) => {
43 const uid
= req
.query
.uid
;
44 if (uid
.match(/^[0-9]+$/)) {
45 ChallengeModel
.getByUser(uid
, (err
,challenges
) => {
46 res
.json(err
|| { challenges: challenges
});
51 router
.delete("/challenges", access
.logged
, access
.ajax
, (req
,res
) => {
52 const cid
= req
.query
.id
;
53 if (cid
.match(/^[0-9]+$/)) {
54 ChallengeModel
.safeRemove(cid
, req
.userId
);
59 module
.exports
= router
;