Skip to content

Worked on email templete #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ const cors = require("cors");
const bcryptjs = require("bcryptjs");
require("dotenv").config();
require("./db/connectionDB");
require('./models/user')
require('./models/Survey')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be /survey. Filename case should match always.

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());
Expand Down
10 changes: 2 additions & 8 deletions controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@ const nodemailer = require("nodemailer");
const bcryptjs = require("bcryptjs");
const jwt = require("jsonwebtoken");

const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "[email protected]",
pass: "*Saheb13*",
},
});
const {transporter} = require('../utils/transporter')

const signup = async (req, res) => {
const { username, password, email, phoneNo } = req.body;
Expand All @@ -33,7 +27,7 @@ const signup = async (req, res) => {
from: "[email protected]",
to: email,
subject: "Account activation link",
html: `<h2>PLease click on given link to activate your account</h2>
html: `<h2>Account activation link</h2>
<p>http://localhost:2000/api/activate/${token}</p>
`,
};
Expand Down
42 changes: 40 additions & 2 deletions controllers/survey.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,46 @@
const mongoose = require("mongoose");
const { Survey } = require("../models/survey");
const {transporter} = require('../utils/transporter')

const createSurvey = async (req, res) => {
const survey = await Survey.create(req.body);
const { title, category,subject, body, recipients } = req.body;
const survey = await Survey.create({
title,
category,
subject,
body,
recipients,
dateSent: Date.now(),
});

const data = {
from: "[email protected]",
to: recipients,
subject: "Account activation link",
html: ` <html>
<body>
<div style="text-align: center;">
<h3>I'd like your input!</h3>
<p>Please answer the following question:</p>
<p>${survey.body}</p>
<div>
<a href="/">Yes</a>
</div>
<div>
<a href="/">No</a>
</div>
</div>
</body>
</html>
`,
};

try {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should try-catch the whole function, and return the error. Anything could fail not just the mail.

await transporter.sendMail(data);
console.log("Email sent Successfully! survey");
} catch (err) {
console.log("error occured while sending email!", err);
}
res.send(survey);
};
module.exports = { createSurvey };
module.exports = { createSurvey };
6 changes: 6 additions & 0 deletions middlewares/requireCredits.js
Original file line number Diff line number Diff line change
@@ -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();
};
8 changes: 8 additions & 0 deletions middlewares/requireLogin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = (req, res, next) => {
if (!req.user) {
return res.status(401).send({ error: 'You must log in!' });
}

next();
};

8 changes: 8 additions & 0 deletions models/recipient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const mongoose = require("mongoose");
const { Schema } = mongoose;
const recipientSchema = new Schema({
email: String,
responded: { type: Boolean, default: false },
});

module.exports = recipientSchema;
61 changes: 35 additions & 26 deletions models/survey.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@

const mongoose=require('mongoose')

const surveySchema=mongoose.Schema({
title:{
type:String
},
type:{
type:String
},

questionsSet:[
{
ques:{
type: String
},
ans:{
type: [Boolean]
}
}
]

})
//created the survey api now
const Survey = mongoose.model("Survey",surveySchema)
module.exports={Survey}
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:[
String
],
yes: {
type: Number,
default: 0,
},
No: {
type: Number,
default: 0,
},
_user:{
type:mongoose.Schema.Types.ObjectId,
ref:'User'
},
dateSent: Date,
lastResponded: Date
});
const Survey = mongoose.models.Survey||mongoose.model("Survey", surveySchema);
module.exports = { Survey };
5 changes: 1 addition & 4 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
const mongoose = require("mongoose");

const userSchema = mongoose.Schema({
username: {
type: String,
required: true,
max: 64,
},
password: {
type: String,
Expand All @@ -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 };
4 changes: 2 additions & 2 deletions routes/survey.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const express = require("express");
const { createSurvey } = require("../controllers/survey");
const router = express.Router();
const { createSurvey } = require("../controllers/survey");

router.post("/createsurvey", createSurvey);

module.exports = router;
module.exports = router;
13 changes: 13 additions & 0 deletions utils/transporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const nodemailer = require('nodemailer')

const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "[email protected]",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to use any credentials used in config files or default.json. Can be used at multiple places and will have to update at one place only, if needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

pass: "*Saheb13*",
},
});

module.exports ={
transporter
}