Commit 76a2d988 authored by antoine masson's avatar antoine masson
Browse files

v0.25

- Add possiblity to check if an user exist and create one if not the case
- Correct enable/disable survey
- correct startup.sh
parent 141c128d
......@@ -67,7 +67,27 @@ exports.get_a_survey = async (req, res) => {
res.status(400).json({ message: err,status:"Error"});
}
};
exports.update_a_survey = async (req, res) => { //TODO: update here
exports.disable_a_survey = async (req, res) => {
Survey.findByIdAndUpdate(req.params.id, {
"$set": {
"general.enable": false
}},{},(err) => {
if (err) {res.send(err);}
else{res.json({ id: req.params.id,message:"Survey disabled",status: "OK"});}
}
);
};
exports.enable_a_survey = async (req, res) => {
Survey.findByIdAndUpdate(req.params.id, {
"$set": {
"general.enable": true
}},{},(err) => {
if (err) {res.send(err);}
else{res.json({ id: req.params.id,message:"Survey enabled",status: "OK"});}
}
);
};
exports.update_a_survey = async (req, res) => {
Survey.findByIdAndUpdate(req.params.id,req.body,{},(err) => {
if (err) {res.send(err);}
else{res.json({ id: req.params.id,message:"Survey modified",status: "OK"});}
......
......@@ -11,16 +11,9 @@ exports.registerNewUser = async (req, res) => {
status: "Error"
});
}
const user = new AdminUser({
username: req.body.username,
password: req.body.password,
enable: req.body.enable,
lastname:req.body.lastname,
firstname:req.body.firstname,
comments:req.body.comments,
});
let newuser = await user.save();
const token = await user.generateAuthToken(); // here it is calling the method that we created in the model
const user = new AdminUser(req.body);
const newuser = await user.save();
const token = await user.generateAuthToken();
res.status(201).json({token:token,user:newuser,message:"User created",status: "OK"});
} catch (err) {
res.status(400).json({ message: err,status:"Error" });
......@@ -114,14 +107,35 @@ exports.update_a_user = async (req, res) => {
exports.delete_a_user = (req, res) => {
try {
AdminUser.deleteOne({ _id: req.params.userId }, err => {
res.json({
id: req.params.userId,
message: 'User successfully deleted',
status :"OK"
AdminUser.deleteOne({_id: req.params.userId}, err => {
res.json({
id: req.params.userId,
message: 'User successfully deleted',
status: "OK"
});
});
});
} catch (err) {
res.status(400).json({ message: err,status:"Error"});
res.status(400).json({message: err, status: "Error"});
}
};
exports.is_admin_exist = async (req, res) => {
const number = await AdminUser.countDocuments();
res.json({
adminpresent: number>0,
message: '',
status: "OK"
});
};
exports.create_firstadmin = async (req, res) => {
if(await AdminUser.countDocuments() > 0) {
res.json({
message: 'Operation not possible, admin user are already in the database',
status: "Error"
});
}else {
const user = new AdminUser(req.body);
const newuser = await user.save();
const token = await user.generateAuthToken();
res.status(201).json({token:token,user:newuser,message:"User created",status: "OK"});
}
};
......@@ -13,4 +13,8 @@ module.exports = app => {
app.get("/adminusers/me", auth(true), userBuilder.getUserDetails);
app.put("/adminusers/me", auth(true), userBuilder.update_user);
app.get("/adminusers/exist", userBuilder.is_admin_exist);
app.post("/adminusers/createfirst", userBuilder.create_firstadmin);
};
......@@ -9,6 +9,8 @@ module.exports = app => {
app.get("/surveys/survey/:id", auth(true), Builder.get_a_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);
app.put("/surveys/survey/disable/:id", auth(true), Builder.disable_a_survey);
app.get("/surveys/langdef", auth(true), Builder.list_all_langdef);
......
#!/bin/sh
if [ "$NODE_ENV" = "production" ]; then
node server.js;
node index.js;
else
nodemon server.js;
nodemon index.js;
fi
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