Commit b868a717 authored by antoine masson's avatar antoine masson
Browse files

v0.4

- Correct model bug for empty id in Survey
- Add possibility to drop survey database
- Get token related to one survey_id
- Get test token of survey_id
parent 8c96c6d7
...@@ -23,6 +23,20 @@ async function json2mango(datajson,mongo){ ...@@ -23,6 +23,20 @@ async function json2mango(datajson,mongo){
await mongo.create(datajson); await mongo.create(datajson);
} }
exports.dropSurvey = async (req, res) => {
try {
await Survey.collection.drop();
await Survey.syncIndexes();
return res.status(201).json({
message: "Survey drop",
status: "OK"
});
} catch (err) {
res.status(400).json({ message: err,status:"Error"});
}
}
exports.initSurveyTemplate = async()=>{ exports.initSurveyTemplate = async()=>{
if(await LanguageTrans.countDocuments() === 0){ if(await LanguageTrans.countDocuments() === 0){
fs.readFile(API.template_dir+'LanguageTrans.json', 'utf-8', async (err, data) => { fs.readFile(API.template_dir+'LanguageTrans.json', 'utf-8', async (err, data) => {
......
...@@ -52,6 +52,28 @@ exports.list_all_tokens = async (req, res) => { //TODO : add number of attached ...@@ -52,6 +52,28 @@ exports.list_all_tokens = async (req, res) => { //TODO : add number of attached
} }
}; };
exports.list_survey_tokens = async (req,res)=>{
try {
Token.find({survey_id:req.params.survey_id,test:false},).
exec(async function (err, tokens) {
await res.status(201).json({tokens:tokens,message:"",status:"OK"});
});
} catch (err) {
res.status(400).json({ message: err,status:"Error"});
}
};
exports.test_survey_token = async (req,res)=>{
try {
console.log(req.params.survey_id)
Token.findOne({survey_id:req.params.survey_id,test:true},).
exec(async function (err, tokens) {
await res.status(201).json({tokens:tokens,message:"",status:"OK"});
});
} catch (err) {
res.status(400).json({ message: err,status:"Error"});
}
};
exports.get_a_token = async (req, res) => { exports.get_a_token = async (req, res) => {
try { try {
const token = await Token.findOne({ _id: req.params.id }); const token = await Token.findOne({ _id: req.params.id });
......
...@@ -3,6 +3,54 @@ const mongoose = require("mongoose"); ...@@ -3,6 +3,54 @@ const mongoose = require("mongoose");
const { Schema } = mongoose; const { Schema } = mongoose;
const validator = require( "validator"); const validator = require( "validator");
const textinputs = new Schema(
{
language:String,
html:{
content:String,
inputs:String,
},
}
);
const text = new Schema(
{
language:String,
html:{
content:String,
},
}
);
const altschema = new Schema(
{
_id:mongoose.ObjectId,
short_name: {
type: String,
index: { unique: true, sparse: true }
},
description: {
type: String,
},
comments: {
type: String,
},
enable: {
type: Boolean,
default: true
},
contents:[textinputs],
items:[{
picture: String,
contents:[text],
}],
}
)
const POCSchema = new Schema({ const POCSchema = new Schema({
firstname: { firstname: {
type: String, type: String,
...@@ -40,11 +88,11 @@ SurveyGlobalOptSSchema.statics.checkExistingField = async function(field, value) ...@@ -40,11 +88,11 @@ SurveyGlobalOptSSchema.statics.checkExistingField = async function(field, value)
module.exports = mongoose.model("SurveyGlobalOptions", SurveyGlobalOptSSchema); module.exports = mongoose.model("SurveyGlobalOptions", SurveyGlobalOptSSchema);
const PrePostSurveySchema = new Schema({ const PrePostSurveySchema = new Schema({
_id:String, _id:mongoose.ObjectId,
short_name: { short_name: {
type: String, type: String,
required: true, required: true,
unique:true, index: { unique: true, sparse: true }
}, },
description: { description: {
type: String, type: String,
...@@ -72,16 +120,8 @@ const PrePostSurveySchema = new Schema({ ...@@ -72,16 +120,8 @@ const PrePostSurveySchema = new Schema({
required: true required: true
}, },
type_attribut:Object, type_attribut:Object,
contents: contents: [textinputs],
[
{
language:String,
html:{
content:String,
inputs:String,
},
}
]
} }
) )
...@@ -120,8 +160,7 @@ const SurveySchema = new Schema({ ...@@ -120,8 +160,7 @@ const SurveySchema = new Schema({
} }
}, },
welcome: welcome:
[ [{
{
language:String, language:String,
html:{ html:{
content:String content:String
...@@ -129,18 +168,19 @@ const SurveySchema = new Schema({ ...@@ -129,18 +168,19 @@ const SurveySchema = new Schema({
movie:{ movie:{
url:String url:String
} }
} }],
], presurvey:[PrePostSurveySchema],
presurvey:[PrePostSurveySchema], postsurvey:[PrePostSurveySchema],
postsurvey:[PrePostSurveySchema], rational:{
swing:{ general: {
type: Object learnloop:Boolean
}, },
tradeoff:{ alternatives:[altschema],
type: Object objectives:Array,
}, },
},{ timestamps: true }); },{ timestamps: true });
SurveySchema.statics.checkExistingField = async function(field, value) { SurveySchema.statics.checkExistingField = async function(field, value) {
......
...@@ -2,6 +2,7 @@ const Builder = require("../controllers/SurveyController"); ...@@ -2,6 +2,7 @@ const Builder = require("../controllers/SurveyController");
const auth = require("../middleware/auth"); const auth = require("../middleware/auth");
module.exports = app => { module.exports = app => {
app.get("/surveys/drop", auth(true), Builder.dropSurvey);
app.get("/surveys/all", auth(true), Builder.list_all_surveys); app.get("/surveys/all", auth(true), Builder.list_all_surveys);
app.post("/surveys/create", auth(true), Builder.createNewSurvey); app.post("/surveys/create", auth(true), Builder.createNewSurvey);
......
...@@ -6,6 +6,9 @@ module.exports = app => { ...@@ -6,6 +6,9 @@ module.exports = app => {
app.get("/tokens/all", auth(true), Builder.list_all_tokens); app.get("/tokens/all", auth(true), Builder.list_all_tokens);
app.post("/tokens/register", auth(true), Builder.registerNewToken); app.post("/tokens/register", auth(true), Builder.registerNewToken);
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/token/:id", auth(true), Builder.get_a_token); app.get("/tokens/token/:id", auth(true), Builder.get_a_token);
app.delete("/tokens/token/:id", auth(true), Builder.delete_a_token); app.delete("/tokens/token/:id", auth(true), Builder.delete_a_token);
app.put("/tokens/token/:id", auth(true), Builder.update_a_token); app.put("/tokens/token/:id", auth(true), Builder.update_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