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 options: req
.body
.chall
.options
,
13 randomness: req
.body
.chall
.randomness
,
14 vid: req
.body
.chall
.vid
,
16 to: req
.body
.chall
.to
, //string: user name (may be empty)
18 const insertChallenge
= () => {
19 ChallengeModel
.create(challenge
, (err
, ret
) => {
23 if (req
.body
.chall
.to
) {
25 "name", challenge
.to
, "id, name, email, notify",
27 if (err
|| !user
) res
.json(err
|| {errmsg: "Typo in player name"});
29 challenge
.to
= user
.id
; //ready now to insert challenge
34 "New challenge : " + params
.siteURL
+ "/#/?disp=corr");
40 else insertChallenge();
44 router
.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
});
53 router
.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
);
61 module
.exports
= router
;