Commit 841bc8dd authored by antoine masson's avatar antoine masson
Browse files

v0.9

- Add download survey
- Add download token
- Add survey dates verification
- Add survey verification when token login
parent 8b2cd759
......@@ -107,7 +107,7 @@ exports.list_all_surveys = async (req, res) => {
exports.get_a_survey = async (req, res) => {
try {
const survey = await Survey.findOne({ _id: req.params.id });
const survey = await Survey.findById(req.params.id);
await res.status(201).json({survey:survey,message:"",status:"OK"});
} catch (err) {
res.status(400).json({ message: err,status:"Error"});
......@@ -153,6 +153,20 @@ exports.delete_a_survey = async (req, res) => {
res.status(400).json({ message: err,status:"Error"});
}
};
exports.download_survey = async (req,res) =>{
try{
Survey.findById(req.params.id).
exec(async function (err, survey) {
res.set("Content-Type", "application/octet-stream");
res.set("Content-Disposition", "attachment;filename="+survey.general.short_name+".json");
await res.send(survey);
});
} catch (err) {
res.status(400).json({ message: err,status:"Error"});
}
};
// language definition
exports.createNewLangdef = async (req, res) => {
try {
......
......@@ -77,7 +77,7 @@ exports.test_survey_token = async (req,res)=>{
exports.get_a_token = async (req, res) => {
try {
const token = await Token.findOne({ _id: req.params.id });
const token = await Token.findById(req.params.id);
await res.status(201).json({token:token,message:"",status:"OK"});
} catch (err) {
res.status(400).json({ message: err,status:"Error"});
......@@ -111,6 +111,22 @@ exports.delete_a_token = (req, res) => {
res.status(400).json({ message: err,status:"Error"});
}
};
exports.down_survey_tokens = async (req,res)=>{
try{
console.log(req.params.survey_id)
const survey=await Survey.findById(req.params.survey_id)
Token.find({survey_id:req.params.survey_id,test:false},).
exec(async function (err, tokens) {
res.set("Content-Type", "application/octet-stream");
res.set("Content-Disposition", "attachment;filename="+survey.general.short_name+"-token.json");
await res.send(tokens);
});
} catch (err) {
res.status(400).json({ message: err,status:"Error"});
}
};
// client
exports.loginToken = async (req, res) => {
......@@ -126,7 +142,18 @@ exports.loginToken = async (req, res) => {
.status(201)
.json({ message: "Token disabled!" ,status: "Error"});
}
//TODO : add survey verification
const survey = await Survey.findById(token.survey_id)
if(!survey.status && !token.test){
return res
.status(201)
.json({ message: "Survey not online" ,status: "Error"});
}
if(!survey.enable && !token.test){
return res
.status(201)
.json({ message: "Survey disabled" ,status: "Error"});
}
if(token.status==="finished"){
return res
.status(201)
......@@ -141,7 +168,17 @@ exports.loginToken = async (req, res) => {
exports.accessToken = async (req, res) => {
if(await Survey.isAccessToken(req.body.survey_id)){
try{
//TODO : add survey verification
const survey = await Survey.findById(req.body.survey_id)
if(!survey.status && !token.test){
return res
.status(201)
.json({ message: "Survey not online" ,status: "Error"});
}
if(!survey.enable && !token.test){
return res
.status(201)
.json({ message: "Survey disabled" ,status: "Error"});
}
let token = await Token.findOne({ token:req.body.token });
if (!token) { //create new token if doesn't exist
const newtoken = new Token({
......@@ -199,6 +236,7 @@ exports.update_status = async (req, res) => {
.json({ message: "Nothing saved : test token" ,status: "Error"});
}
token.status = req.body.status;
if(token.status==="finished") token.submitedAt = Date.now();
await token.save();
res.status(201).json({message:"Token status modified",status: "OK"});
} catch (err) {
......
......@@ -234,9 +234,16 @@ SurveySchema.statics.isAccessToken = async function(survey_id){
}
};
SurveySchema.virtual('status').get(function() {
return "TODO";
});
const dates = this.general.dates;
let check = true
if(dates.date_start_enable && dates.date_start.valueOf() > new Date().valueOf()){
check = false
} else if(dates.date_stop_enable && dates.date_stop.valueOf() < new Date().valueOf()){
check = false
}
return check;
});
SurveySchema.methods.toJSON = function () {
const survey = this;
const Obj = survey.toObject({virtuals:true});
......
......@@ -8,6 +8,8 @@ module.exports = app => {
app.post("/surveys/create", auth(true), Builder.createNewSurvey);
app.get("/surveys/survey/:id", auth(true), Builder.get_a_survey);
app.get("/surveys/downsurvey/:id", auth(true), Builder.download_survey);
app.delete("/surveys/survey/:id", auth(true), Builder.delete_a_survey);
app.put("/surveys/survey/:id", auth(true), Builder.update_a_survey);
app.put("/surveys/survey/enable/:id", auth(true), Builder.enable_a_survey);
......
......@@ -8,6 +8,7 @@ module.exports = app => {
app.get("/tokens/survey/:survey_id", auth(true), Builder.list_survey_tokens);
app.get("/tokens/survey/:survey_id/test", auth(true), Builder.test_survey_token);
app.get("/tokens/down/:survey_id", auth(true), Builder.down_survey_tokens);
app.get("/tokens/token/:id", auth(true), Builder.get_a_token);
app.delete("/tokens/token/:id", auth(true), Builder.delete_a_token);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment