From c051497247a283270e56aed4fcdb5fca5b926473 Mon Sep 17 00:00:00 2001 From: Santan24 Date: Tue, 8 Jun 2021 16:09:01 +0530 Subject: [PATCH 1/9] Initial backend code --- app.js | 4 +++- controllers/survey.js | 17 +++++++++++++++++ models/survey.js | 24 ++++++++++++++++++++++++ models/user.js | 2 ++ routes/survey.js | 8 ++++++++ 5 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 controllers/survey.js create mode 100644 models/survey.js create mode 100644 routes/survey.js diff --git a/app.js b/app.js index 4c6575b..b973cee 100644 --- a/app.js +++ b/app.js @@ -6,11 +6,13 @@ const bcryptjs = require("bcryptjs"); const app = express(); require("dotenv").config(); require("./db/connectionDB"); + const authRoutes = require("./routes/auth"); +const surveyRoutes = require("./routes/survey"); app.use(express.urlencoded({ extended: true })); app.use(express.json()); app.use(cors()); - +app.use("/survey",surveyRoutes) app.use("/api", authRoutes); app.listen(process.env.PORT || 2000, () => { diff --git a/controllers/survey.js b/controllers/survey.js new file mode 100644 index 0000000..a1ac93f --- /dev/null +++ b/controllers/survey.js @@ -0,0 +1,17 @@ +const mongoose=require('mongoose') +const {Survey}=require('../models/survey') + +const createSurvey = async(req,res)=> { + + const survey = await Survey.create(req.body) + res.send(survey) + } + module.exports= {createSurvey} + + + + + + + + diff --git a/models/survey.js b/models/survey.js new file mode 100644 index 0000000..4114fb0 --- /dev/null +++ b/models/survey.js @@ -0,0 +1,24 @@ + +const mongoose=require('mongoose') + +const surveySchema=mongoose.Schema({ + title:{ + type:String + }, + type:{ + type:String + }, + questionsSet:[ + { + ques:{ + type: String + }, + ans:{ + type: [Boolean] + } + } +] + +}) +const Survey = mongoose.model("Survey",surveySchema) +module.exports={Survey} \ No newline at end of file diff --git a/models/user.js b/models/user.js index 1d0abbb..f75518a 100644 --- a/models/user.js +++ b/models/user.js @@ -34,5 +34,7 @@ const userSchema = mongoose.Schema({ } }); + + const User = mongoose.model("User", userSchema); module.exports = { User }; diff --git a/routes/survey.js b/routes/survey.js new file mode 100644 index 0000000..9337f09 --- /dev/null +++ b/routes/survey.js @@ -0,0 +1,8 @@ +const express = require('express') +const { createSurvey } = require('../controllers/survey') +const router= express.Router() + +router.post('/createSurvey',createSurvey) + + +module.exports =router \ No newline at end of file From 7a2f30ff9890f262b4ea006716f65cb36c83497e Mon Sep 17 00:00:00 2001 From: Santan24 Date: Tue, 8 Jun 2021 16:13:17 +0530 Subject: [PATCH 2/9] pull request --- controllers/survey.js | 1 + 1 file changed, 1 insertion(+) diff --git a/controllers/survey.js b/controllers/survey.js index a1ac93f..05094ef 100644 --- a/controllers/survey.js +++ b/controllers/survey.js @@ -5,6 +5,7 @@ const createSurvey = async(req,res)=> { const survey = await Survey.create(req.body) res.send(survey) + //survey } module.exports= {createSurvey} From 46765863eb170a39aa4e1a156ccaff69231af958 Mon Sep 17 00:00:00 2001 From: Santan24 Date: Wed, 9 Jun 2021 17:45:35 +0530 Subject: [PATCH 3/9] create survey api --- controllers/auth.js | 3 +-- models/survey.js | 3 ++- routes/survey.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/controllers/auth.js b/controllers/auth.js index 89122e0..10d5d71 100644 --- a/controllers/auth.js +++ b/controllers/auth.js @@ -103,7 +103,7 @@ const resetlink = async (req, res) => { }; try { await transporter.sendMail(data); - res.send("Email sent Successfully!"); + res.send("Email sent Successfully!"); } catch (err) { console.log("error occured while sending email!", err); } @@ -148,7 +148,6 @@ const login=async(req,res)=>{ { const token=jwt.sign({email ,_id},JWT_SECRET) res.send({token}) - } else { diff --git a/models/survey.js b/models/survey.js index 4114fb0..001a6e8 100644 --- a/models/survey.js +++ b/models/survey.js @@ -21,4 +21,5 @@ const surveySchema=mongoose.Schema({ }) const Survey = mongoose.model("Survey",surveySchema) -module.exports={Survey} \ No newline at end of file +module.exports={Survey} +//created the survey api \ No newline at end of file diff --git a/routes/survey.js b/routes/survey.js index 9337f09..245f68f 100644 --- a/routes/survey.js +++ b/routes/survey.js @@ -2,7 +2,7 @@ const express = require('express') const { createSurvey } = require('../controllers/survey') const router= express.Router() -router.post('/createSurvey',createSurvey) +router.post('/createSurvey',createSurvey,) module.exports =router \ No newline at end of file From 4ccfa84a16e35ad8846e626c50d2da7242f2ad57 Mon Sep 17 00:00:00 2001 From: Santan24 Date: Wed, 9 Jun 2021 18:22:45 +0530 Subject: [PATCH 4/9] created API FOR SURVEY --- models/survey.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/survey.js b/models/survey.js index 001a6e8..97448ff 100644 --- a/models/survey.js +++ b/models/survey.js @@ -22,4 +22,4 @@ const surveySchema=mongoose.Schema({ }) const Survey = mongoose.model("Survey",surveySchema) module.exports={Survey} -//created the survey api \ No newline at end of file +//created the survey api now \ No newline at end of file From b3ae0f15b77d29f75852242e56364ca2bc20ff89 Mon Sep 17 00:00:00 2001 From: Santan24 Date: Wed, 9 Jun 2021 18:26:06 +0530 Subject: [PATCH 5/9] created survey --- models/survey.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/survey.js b/models/survey.js index 97448ff..86d92b0 100644 --- a/models/survey.js +++ b/models/survey.js @@ -20,6 +20,6 @@ const surveySchema=mongoose.Schema({ ] }) +//created the survey api now const Survey = mongoose.model("Survey",surveySchema) module.exports={Survey} -//created the survey api now \ No newline at end of file From 2d24b6c2ab1d7526dbb1832dd2d5618e8ad3c9cf Mon Sep 17 00:00:00 2001 From: Santan24 Date: Wed, 9 Jun 2021 23:50:42 +0530 Subject: [PATCH 6/9] formatted the files --- .env | 1 + app.js | 8 ++-- controllers/auth.js | 87 +++++++++++++++++++------------------------ controllers/survey.js | 26 ++++--------- db/connectionDB.js | 16 +++++--- models/survey.js | 42 ++++++++++----------- models/user.js | 29 ++++++--------- routes/auth.js | 23 ++++++++---- routes/survey.js | 11 +++--- 9 files changed, 113 insertions(+), 130 deletions(-) diff --git a/.env b/.env index 880bad3..4c24d8d 100644 --- a/.env +++ b/.env @@ -1,3 +1,4 @@ PORT=2000 JWT_SECRET=askfeed1234 + diff --git a/app.js b/app.js index b973cee..426de16 100644 --- a/app.js +++ b/app.js @@ -1,18 +1,18 @@ const express = require("express"); - const mongoose = require("mongoose"); const cors = require("cors"); const bcryptjs = require("bcryptjs"); -const app = express(); require("dotenv").config(); require("./db/connectionDB"); - const authRoutes = require("./routes/auth"); const surveyRoutes = require("./routes/survey"); + +const app = express(); + app.use(express.urlencoded({ extended: true })); app.use(express.json()); app.use(cors()); -app.use("/survey",surveyRoutes) +app.use("/survey", surveyRoutes); app.use("/api", authRoutes); app.listen(process.env.PORT || 2000, () => { diff --git a/controllers/auth.js b/controllers/auth.js index 10d5d71..a5b7317 100644 --- a/controllers/auth.js +++ b/controllers/auth.js @@ -2,7 +2,6 @@ const { User } = require("../models/user"); const nodemailer = require("nodemailer"); const bcryptjs = require("bcryptjs"); const jwt = require("jsonwebtoken"); -JWT_SECRET = "askfeed1234"; const transporter = nodemailer.createTransport({ service: "gmail", @@ -17,7 +16,7 @@ const signup = async (req, res) => { const userexist = await User.findOne({ email }); if (!userexist) { const hashedPass = await bcryptjs.hashSync(password, 10); - const token = jwt.sign({ username, email }, JWT_SECRET, { + const token = jwt.sign({ username, email }, process.env.JWT_SECRET, { expiresIn: "30m", }); const newUser = { @@ -35,7 +34,7 @@ const signup = async (req, res) => { to: email, subject: "Account activation link", html: `

PLease click on given link to activate your account

-

http://localhost:2000/api/activate/?token=${token}

+

http://localhost:2000/api/activate/${token}

`, }; try { @@ -53,8 +52,8 @@ const signup = async (req, res) => { const verifyAccount = async (req, res) => { try { - const token = req.query.token; - const decodedUser = jwt.verify(token, JWT_SECRET); + const token = req.params.token; + const decodedUser = jwt.verify(token, process.env.JWT_SECRET); const user = await User.findOne({ email: decodedUser.email, token: token }); if (user) { @@ -81,7 +80,7 @@ const resetlink = async (req, res) => { const user = await User.findOne({ email }); // console.log("user", user, email); if (user) { - const token = jwt.sign({ email }, JWT_SECRET, { + const token = jwt.sign({ email }, process.env.JWT_SECRET, { expiresIn: "30m", }); @@ -103,7 +102,7 @@ const resetlink = async (req, res) => { }; try { await transporter.sendMail(data); - res.send("Email sent Successfully!"); + res.send("Email sent Successfully!"); } catch (err) { console.log("error occured while sending email!", err); } @@ -112,51 +111,41 @@ const resetlink = async (req, res) => { } }; - -const changepassword = async(req,res) =>{ - const { password}=req.body; +const changepassword = async (req, res) => { + const { password } = req.body; const token = req.query.token; - const decodedtoken = jwt.verify(token,JWT_SECRET) + const decodedtoken = jwt.verify(token, process.env.JWT_SECRET); const hashedPass = await bcryptjs.hashSync(password, 10); - const user= await User.findOneAndUpdate({email:decodedtoken.email, resetToken:token }, - {$set: - { - password: hashedPass, - resetToken:null - } - } - ) - if(user) + const user = await User.findOneAndUpdate( + { email: decodedtoken.email, resetToken: token }, { - res.send("Password updated successfully!") - } - else - { - res.send("Unable to reset password!") + $set: { + password: hashedPass, + resetToken: null, + }, } + ); + if (user) { + res.send("Password updated successfully!"); + } else { + res.send("Unable to reset password!"); } -const login=async(req,res)=>{ - const {email,password}=req.body - const user=await User.findOne({email}) - const {_id}=user - if(user) - - { - console.log("user",user) - const matchpassword= await bcryptjs.compare(password,user.password) - if(matchpassword && user.isVarified) - { - const token=jwt.sign({email ,_id},JWT_SECRET) - res.send({token}) - } - else - { - res.send("Login unsuccessful!") - } - } - else - { - res.send("Incorrect Email or password!") +}; +const login = async (req, res) => { + const { email, password } = req.body; + const user = await User.findOne({ email }); + const { _id } = user; + if (user) { + console.log("user", user); + const matchpassword = await bcryptjs.compare(password, user.password); + if (matchpassword && user.isVarified) { + const token = jwt.sign({ email, _id }, process.env.JWT_SECRET); + res.send({ token }); + } else { + res.send("Login unsuccessful!"); } -} -module.exports = { signup, verifyAccount, resetlink ,changepassword,login}; + } else { + res.send("Incorrect Email or password!"); + } +}; +module.exports = { signup, verifyAccount, resetlink, changepassword, login }; diff --git a/controllers/survey.js b/controllers/survey.js index 05094ef..9693089 100644 --- a/controllers/survey.js +++ b/controllers/survey.js @@ -1,18 +1,8 @@ -const mongoose=require('mongoose') -const {Survey}=require('../models/survey') - -const createSurvey = async(req,res)=> { - - const survey = await Survey.create(req.body) - res.send(survey) - //survey - } - module.exports= {createSurvey} - - - - - - - - +const mongoose = require("mongoose"); +const { Survey } = require("../models/survey"); + +const createSurvey = async (req, res) => { + const survey = await Survey.create(req.body); + res.send(survey); +}; +module.exports = { createSurvey }; diff --git a/db/connectionDB.js b/db/connectionDB.js index e5a64e2..c52293b 100644 --- a/db/connectionDB.js +++ b/db/connectionDB.js @@ -1,8 +1,12 @@ const mongoose = require("mongoose"); -mongoose.connect("mongodb+srv://santan:*Saheb13*@cluster0.cnnl3.mongodb.net/myFirstDatabase?retryWrites=true&w=majority", { - useNewUrlParser: true, - useUnifiedTopology: true, - -}) .then(() => console.log("DB connected established")) - .catch(err => console.log("DB Connection error: " , err)); \ No newline at end of file +mongoose + .connect( + "mongodb+srv://santan:*Saheb13*@cluster0.cnnl3.mongodb.net/myFirstDatabase?retryWrites=true&w=majority", + { + useNewUrlParser: true, + useUnifiedTopology: true, + } + ) + .then(() => console.log("DB connected established")) + .catch((err) => console.log("DB Connection error: ", err)); diff --git a/models/survey.js b/models/survey.js index 86d92b0..63b2656 100644 --- a/models/survey.js +++ b/models/survey.js @@ -1,25 +1,23 @@ +const mongoose = require("mongoose"); -const mongoose=require('mongoose') - -const surveySchema=mongoose.Schema({ - title:{ - type:String - }, - type:{ - type:String - }, - questionsSet:[ +const surveySchema = mongoose.Schema({ + title: { + type: String, + }, + type: { + type: String, + }, + questionsSet: [ { - ques:{ - type: String - }, - ans:{ - type: [Boolean] - } - } -] + ques: { + type: String, + }, + ans: { + type: String, + }, + }, + ], +}); -}) -//created the survey api now -const Survey = mongoose.model("Survey",surveySchema) -module.exports={Survey} +const Survey = mongoose.model("Survey", surveySchema); +module.exports = { Survey }; diff --git a/models/user.js b/models/user.js index f75518a..eb7de48 100644 --- a/models/user.js +++ b/models/user.js @@ -1,40 +1,35 @@ const mongoose = require("mongoose"); -// const crypto = require ('crypto') - const userSchema = mongoose.Schema({ - username: { type: String, - required:true, - max:64, + required: true, + max: 64, }, password: { type: String, - required:true, + required: true, }, email: { type: String, - unique:true, + unique: true, lowercase: true, }, phoneNo: { type: Number, }, - isVarified:{ + isVarified: { type: Boolean, - default:false + default: false, }, - token:{ - type: String + token: { + type: String, + }, + resetToken: { + type: String, + default: null, }, - resetToken:{ - type: String , - default:null - } - }); - const User = mongoose.model("User", userSchema); module.exports = { User }; diff --git a/routes/auth.js b/routes/auth.js index 54ba942..d1c6751 100644 --- a/routes/auth.js +++ b/routes/auth.js @@ -1,11 +1,18 @@ -const express =require("express") +const express = require("express"); const router = express.Router(); -const {signup, verifyAccount,resetlink,changepassword,login} = require("../controllers/auth") +const { + signup, + verifyAccount, + resetlink, + changepassword, + login, +} = require("../controllers/auth"); -router.post('/signup', signup) -router.post('/activate', verifyAccount) -router.post('/resetlink', resetlink) -router.post('/changepassword',changepassword) -router.post('/login',login) -module.exports = router; \ No newline at end of file +router.post("/signup", signup); +router.post("/activate", verifyAccount); +router.post("/resetlink", resetlink); +router.post("/changepassword", changepassword); +router.post("/login", login); + +module.exports = router; diff --git a/routes/survey.js b/routes/survey.js index 245f68f..3b48b47 100644 --- a/routes/survey.js +++ b/routes/survey.js @@ -1,8 +1,7 @@ -const express = require('express') -const { createSurvey } = require('../controllers/survey') -const router= express.Router() +const express = require("express"); +const { createSurvey } = require("../controllers/survey"); +const router = express.Router(); -router.post('/createSurvey',createSurvey,) +router.post("/createsurvey", createSurvey); - -module.exports =router \ No newline at end of file +module.exports = router; From e4769bf9cce56e148d9c5ecbb7053b8bab24a44d Mon Sep 17 00:00:00 2001 From: Santan24 Date: Thu, 10 Jun 2021 12:28:52 +0530 Subject: [PATCH 7/9] commit --- middlewares/requireCredits.js | 6 ++++++ middlewares/requireLogin.js | 8 ++++++++ models/recipient.js | 14 ++++++++++++++ models/survey.js | 2 +- 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 middlewares/requireCredits.js create mode 100644 middlewares/requireLogin.js create mode 100644 models/recipient.js diff --git a/middlewares/requireCredits.js b/middlewares/requireCredits.js new file mode 100644 index 0000000..2093acf --- /dev/null +++ b/middlewares/requireCredits.js @@ -0,0 +1,6 @@ +module.exports = (req, res, next) => { + if (!req.user.credits <1) { + return res.status(403).send({ error: " You do Not have enough Credits!" }); + } + next(); +}; diff --git a/middlewares/requireLogin.js b/middlewares/requireLogin.js new file mode 100644 index 0000000..09fa251 --- /dev/null +++ b/middlewares/requireLogin.js @@ -0,0 +1,8 @@ +module.exports = (req, res, next) => { + if (!req.user) { + return res.status(401).send({ error: 'You must log in!' }); + } + + next(); + }; + \ No newline at end of file diff --git a/models/recipient.js b/models/recipient.js new file mode 100644 index 0000000..5429923 --- /dev/null +++ b/models/recipient.js @@ -0,0 +1,14 @@ +const mongoose= require('mongoose') + +const recipientSchema=mongoose.Schema({ + email: + { + type: String + }, + responded:{ + type:Boolean,default:false + } +}) + +const Recipient = mongoose.model("Recipient", recipientSchema); +module.exports = {Recipient}; diff --git a/models/survey.js b/models/survey.js index 1b56b7f..03486fc 100644 --- a/models/survey.js +++ b/models/survey.js @@ -21,6 +21,6 @@ const surveySchema=mongoose.Schema({ ] }) -//created the survey api now + const Survey = mongoose.model("Survey",surveySchema) module.exports={Survey} From 55b2ca8c4f4286ccd4490519827d0eb3967df1f1 Mon Sep 17 00:00:00 2001 From: Santan24 Date: Thu, 10 Jun 2021 13:12:22 +0530 Subject: [PATCH 8/9] survey recipient models added --- app.js | 4 +++ models/recipient.js | 19 ++++++-------- models/survey.js | 60 +++++++++++++++++++++++++++------------------ routes/survey.js | 26 +++++++++++++++++--- 4 files changed, 70 insertions(+), 39 deletions(-) diff --git a/app.js b/app.js index 426de16..c74d533 100644 --- a/app.js +++ b/app.js @@ -4,6 +4,10 @@ const cors = require("cors"); const bcryptjs = require("bcryptjs"); require("dotenv").config(); require("./db/connectionDB"); + +require('./models/User') +require('./models/Survey') + const authRoutes = require("./routes/auth"); const surveyRoutes = require("./routes/survey"); diff --git a/models/recipient.js b/models/recipient.js index 5429923..992a4b4 100644 --- a/models/recipient.js +++ b/models/recipient.js @@ -1,14 +1,9 @@ -const mongoose= require('mongoose') +const mongoose = require('mongoose'); +const { Schema } = mongoose; -const recipientSchema=mongoose.Schema({ - email: - { - type: String - }, - responded:{ - type:Boolean,default:false - } -}) +const recipientSchema = new Schema({ + email: String, + responded: { type: Boolean, default: false } +}); -const Recipient = mongoose.model("Recipient", recipientSchema); -module.exports = {Recipient}; +module.exports = recipientSchema; diff --git a/models/survey.js b/models/survey.js index 03486fc..0ae4371 100644 --- a/models/survey.js +++ b/models/survey.js @@ -1,26 +1,38 @@ +const mongoose = require("mongoose"); +const recipientSchema= require('./recipient') +const surveySchema = mongoose.Schema({ + title: { + type: String, + }, + category: { + type: String, + }, + body: { + type: String, + }, + subject: { + type: String, + }, + recipients: + [ + recipientSchema + ], + yes: { + type: Number, + default: 0, + }, + No: { + type: Number, + default: 0, + }, +// _user:{ + // type:mongoose.Schema.Types.ObjectId, + // ref:'User},' + //relationship between survey and an individual user + dateSent: Date, + lastResponded: Date +}); -const mongoose=require('mongoose') -const surveySchema=mongoose.Schema({ - title:{ - type:String - }, - type:{ - type:String - }, - - questionsSet:[ - { - ques:{ - type: String - }, - ans:{ - type: [Boolean] - } - } -] - -}) - -const Survey = mongoose.model("Survey",surveySchema) -module.exports={Survey} +const Survey = mongoose.model("Survey", surveySchema); +module.exports = { Survey }; diff --git a/routes/survey.js b/routes/survey.js index 3b48b47..0fdad6c 100644 --- a/routes/survey.js +++ b/routes/survey.js @@ -1,7 +1,27 @@ const express = require("express"); -const { createSurvey } = require("../controllers/survey"); -const router = express.Router(); +const mongoose = require("mongoose"); +const requireLogin = require("../middlewares/requireLogin"); +const requireCredits = require("../middlewares/requireCredits"); +// const { createSurvey } = require("../controllers/survey"); +// const router = express.Router(); +// router.post("/createsurvey", createSurvey); +const Survey = mongoose.model("surveys"); +//first need to cheak user is actually logged in +module.exports = (app) => { + app.post("api/surveys", requireLogin, requireCredits, (req, res) => { + const { title, category, body, recipients } = req.body; -router.post("/createsurvey", createSurvey); + const survey = new Survey({ + title, + category, + subject, + body, + recipients: recipients.split(',').map(email => ({ email: email.trim() })), + _user: req.user.id, + dateSent: Date.now() + + }); + }); +}; module.exports = router; From 3009f9f27c1cb119a08109847a905c8f4d309056 Mon Sep 17 00:00:00 2001 From: Santan24 Date: Thu, 10 Jun 2021 16:44:41 +0530 Subject: [PATCH 9/9] Added middleware --- models/survey.js | 17 +++++++---------- models/user.js | 5 +---- routes/survey.js | 11 +++++------ 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/models/survey.js b/models/survey.js index 0ae4371..9df0dbf 100644 --- a/models/survey.js +++ b/models/survey.js @@ -13,9 +13,8 @@ const surveySchema = mongoose.Schema({ subject: { type: String, }, - recipients: - [ - recipientSchema + recipients:[ + recipientSchema ], yes: { type: Number, @@ -25,14 +24,12 @@ const surveySchema = mongoose.Schema({ type: Number, default: 0, }, -// _user:{ - // type:mongoose.Schema.Types.ObjectId, - // ref:'User},' - //relationship between survey and an individual user + _user:{ + type:mongoose.Schema.Types.ObjectId, + ref:'User' + }, dateSent: Date, lastResponded: Date }); - - -const Survey = mongoose.model("Survey", surveySchema); +const Survey = mongoose.models.Survey||mongoose.model("Survey", surveySchema); module.exports = { Survey }; diff --git a/models/user.js b/models/user.js index eb7de48..04f831e 100644 --- a/models/user.js +++ b/models/user.js @@ -1,10 +1,8 @@ const mongoose = require("mongoose"); - const userSchema = mongoose.Schema({ username: { type: String, required: true, - max: 64, }, password: { type: String, @@ -30,6 +28,5 @@ const userSchema = mongoose.Schema({ default: null, }, }); - -const User = mongoose.model("User", userSchema); +const User = mongoose.models.User || mongoose.model("User", userSchema); module.exports = { User }; diff --git a/routes/survey.js b/routes/survey.js index 0fdad6c..890e51e 100644 --- a/routes/survey.js +++ b/routes/survey.js @@ -2,15 +2,15 @@ const express = require("express"); const mongoose = require("mongoose"); const requireLogin = require("../middlewares/requireLogin"); const requireCredits = require("../middlewares/requireCredits"); +const {Survey} = require('../models/survey') // const { createSurvey } = require("../controllers/survey"); // const router = express.Router(); // router.post("/createsurvey", createSurvey); -const Survey = mongoose.model("surveys"); //first need to cheak user is actually logged in -module.exports = (app) => { + +module.exports = (app) => { app.post("api/surveys", requireLogin, requireCredits, (req, res) => { const { title, category, body, recipients } = req.body; - const survey = new Survey({ title, category, @@ -19,9 +19,8 @@ module.exports = (app) => { recipients: recipients.split(',').map(email => ({ email: email.trim() })), _user: req.user.id, dateSent: Date.now() - }); + survey.save() }); }; - -module.exports = router; +// module.exports = router;