diff --git a/.env b/.env new file mode 100644 index 0000000..10d3d4a --- /dev/null +++ b/.env @@ -0,0 +1,6 @@ +STORMPATH_API_KEY_ID=5PC4RW41LP6BJN3WN2X6HN3SU +STORMPATH_API_KEY_SECRET=518jzRbEilz/v2wVyoUD8gmBSzVDusjNLXFh+UyNkFk +STORMPATH_SECRET_KEY=asefasd +STORMPATH_URL= https://api.stormpath.com/v1/applications/7jp6sl4JWZ6kSWzMpXkEFg +MAIL_PASS=krishanmarya1 +MAIL_ADDRESS=dummyacct101390 diff --git a/index.js b/index.js index c9e0c57..0a03694 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,36 @@ var express = require("express"), - stormpath = require("express-stormpath"), - bodyParser = require("body-parser"), - mongoose = require("mongoose"); - app = express(); + stormpath = require("express-stormpath"), + bodyParser = require("body-parser"), + MongoClient = require('mongodb').MongoClient, + mongoose = require("mongoose"); +app = express(); +require('dotenv').config(); +nodemailer = require('nodemailer'); +const transportString = `smtps://${process.env.MAIL_ADDRESS}%40gmail.com:${process.env.MAIL_PASS}@smtp.gmail.com`; +const transporter = nodemailer.createTransport(transportString); +console.log(transportString); -console.log("HERE I AM!!!",process.env.STORMPATH_API_KEY_ID) +mongoose.Promise = require('bluebird'); + +let URL = "mongodb://heroku_j1647s3l:tkmie8pbencj50ljm1d688h87e@ds117919.mlab.com:17919/heroku_j1647s3l"; + + + +MongoClient.connect(URL, function(err, db) { + if (err) { + URL = 'mongodb://localhost:27017/mydatabase'; + } else { + URL = process.env.URL; + } + db.close(); +}); +mongoose.connect(URL); + +console.log("HERE I AM!!!", process.env.STORMPATH_API_KEY_ID) app.set("views", __dirname + "/views"); app.set("view engine", "ejs"); +app.use(bodyParser.urlencoded()); +app.use(bodyParser.json()); app.use(express.static(__dirname + "/public")); app.use(stormpath.init(app, { apiKeyFile: '/.stormpath/apiKey.properties', @@ -14,18 +38,261 @@ app.use(stormpath.init(app, { apiKeySecret: process.env.STORMPATH_API_KEY_SECRET || 'secret', secretKey: process.env.STORMPATH_SECRET_KEY || "key", application: process.env.STORMPATH_URL || "url", + postLogoutRedirectUrl: '/' + })); +var organizationSchema = new mongoose.Schema({ + orgName: String, + givenName: String, + surname: String, + website: String, +}); + +var Organization = mongoose.model("Organization", organizationSchema); + +var customerSchema = new mongoose.Schema({ + custFirstName: String, + custLastName: String, + custAddress: String, + cusEmail: String, + organization: String, + clicks: Array, + lastEmailed: Number, + nextScheduled:Number, +}); + +const organizationalDataAlreadyGiven = (req, res, next) => { + console.log('running middleware!'); + Organization.findOne({ + givenName: req.user.givenName, + surname: req.user.surname + }, function(err, Organization) { + console.log("Organization", Organization); + if (Organization) { + res.redirect('/customer' + Organization.orgName) + } else { + next(); + } + }) +}; + + + +var Customer = mongoose.model("Customer", customerSchema); app.get("/", function(req, res) { res.render("landing"); }); -app.get("/newCustomer", function(req, res) { - res.send() +app.post("/getCustomerInfo", stormpath.loginRequired, function(req, res) { + Organization.findOne({ + givenName: req.user.givenName, + surname: req.user.surname + }, function(err, Organization) { + Customer.find({ + organization: Organization.orgName + }, function(err, people) { + res.send(people); + }) + }) +}); + +app.get("/customer:id", stormpath.loginRequired, function(req, res) { + res.render("customer", { + organization: req.params.orgName + }); }); -app.listen(process.env.PORT || 3000, function() { - console.log("The CRM Server is running"); +app.get("/newOrganization", stormpath.loginRequired, organizationalDataAlreadyGiven,function(req, res) { + res.render("newOrganization"); +}); + +app.post("/newOrganization", stormpath.loginRequired, function(req, res) { + console.log("MY BODY!!!", req.body); + var orgName = req.body.orgName; + var givenName = req.body.givenName; + var surname = req.body.surname; + var website = req.body.website; + + var newOrganization = { + orgName, + givenName, + surname, + website, + }; + + Organization.create(newOrganization, function(err, newlyCreated) { + if (err) { + console.log(err); + } else { + res.redirect("/customer" + orgName); + } + }); +}); + +app.post('/logout', (req, res) => { + console.log('logigin out') + res.redirect('/'); +}) + +app.post("/newCustomer", stormpath.loginRequired, function(req, res) { + console.log('JOHNBULLISWRONG!', req.body); + var custFirstName = req.body.cusFirstName; + var custLastName = req.body.cusLastName; + var custAddress = req.body.cusAddress; + var cusEmail = req.body.cusEmail; + Organization.findOne({ + givenName: req.user.givenName, + surname: req.user.surname + }, function(err, organization) { + console.log("here is ORG!", organization); + var newCustomer = { + custFirstName, + custLastName, + custAddress, + cusEmail, + organization: organization.orgName + }; + if (err) { + console.log(err); + } else { + Customer.create(newCustomer, function(err, newlyCreated) { + if (err) { + console.log(err); + } else { + //res.redirect("/customer" + orgName); + } + }); + + }; + + }) }) + +app.get("/newEmail", stormpath.loginRequired, function(req, res) { + Organization.findOne({ + givenName: req.user.givenName, + surname: req.user.surname + }, function(err, organization) { + res.render(("newEmail"), { + organization, + }) + }) +}); + +app.post("/newEmail", stormpath.loginRequired, function(req, res) { + + const message = req.body.message; + const header = req.body.header; + const people = req.body.people; + const website = req.body.website; + console.log(message, header, people); + + for (let i = 0; i < people.length; i++) { + + //check if click are preasent in customer DB + Customer.find({ + cusEmail: people[i][3] + }, function(err, person) { + var clickArray = person[0].clicks; + var lastEmailed = person[0].lastEmailed; + var nextScheduled = person[0].nextScheduled; + var currentMill = (new Date()).getTime(); + var dontEmail = (currentMill - lastEmailed) < 604800000; + if (!!clickArray.length) { + + var latestTime = clickArray[clickArray.length - 1]; + var milliseconds = latestTime.getTime(); + + while (milliseconds < currentMill) { + milliseconds += 604800000; + }; + var nextEmail = (new Date(milliseconds)); + // if (nextScheduled-currentMill<604800000){ + // return; + // } + Customer.update({cusEmail: people[i][3]}, {nextScheduled:milliseconds},function(err, response){ + console.log(err,response); + }); + + console.log(latestTime, "', // sender address + to: people[i][3], // list of receivers + subject: header, // Subject line + text: `Dear ${people[i][0]}\n` + message, // plaintext body + html: `Dear ${people[i][0]},
` + message + "
Interested", // html body + }; + + transporter.sendMail(mailOptions, function(error, info) { + if (error) { + return console.log(error); + } + console.log('Message sent: ' + info.response); + Customer.update({cusEmail: people[i][3]}, {lastEmailed:nextEmail}, function(err,affected) { + console.log('affected rows %d', affected); + }) + }); + }, function() { + + console.log("Done"); + }, true) + } else if(!dontEmail) { + + var mailOptions = { + from: '"Krishan Arya :busts_in_silhouette:" ', // sender address + to: people[i][3], // list of receivers + subject: header, // Subject line + text: `Dear ${people[i][0]}\n` + message, // plaintext body + html: `Dear ${people[i][0]},
` + message + "
Interested", // html body + }; + + transporter.sendMail(mailOptions, function(error, info) { + if (error) { + return console.log(error); + } + console.log('Message sent: ' + info.response); + Customer.update({cusEmail: people[i][3]}, {lastEmailed:currentMill}, function(err,affected) { + console.log('affected rows %d', affected); + }) + }); + } + }); + + } + +}); + + + +app.get('/redirect/*', function(req, res) { + console.log("visited2"); + var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl; + var email = fullUrl.slice(fullUrl.lastIndexOf('/')+1); + console.log("EMAIL", email,"fu",fullUrl,"website", website); + time = new Date(); + Customer.update({cusEmail: email}, {$push: {clicks: time}}, function(err, model) { + console.log(err); + }); + res.redirect("google.com"); + }); + + app.get('*', (req, res) => { + res.redirect("http://google.com"); + console.log('caught a case!') + var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl; +console.log(fullUrl); + res.redirect('/'); + }); + + app.listen(process.env.PORT || 3000, function() { + console.log("The CRM Server is running"); + }); diff --git a/package.json b/package.json index f6dd30b..c3446f0 100644 --- a/package.json +++ b/package.json @@ -21,11 +21,15 @@ "bluebird": "^3.4.7", "body-parser": "^1.16.0", "client-sessions": "^0.7.0", + "cron": "^1.2.1", + "dotenv": "^4.0.0", "ejs": "^2.5.5", "express": "^4.14.0", "express-stormpath": "^3.1.9", "mo": "^1.7.3", - "mongoose": "^4.7.7" + "mongoose": "^4.7.7", + "node-cron": "^1.1.2", + "nodemailer": "^2.7.0" }, "devDependencies": { "grunt": "^1.0.1", diff --git a/public/johnbull.png b/public/johnbull.png new file mode 100644 index 0000000..16b1d40 Binary files /dev/null and b/public/johnbull.png differ diff --git a/public/styles/styles.css b/public/styles/styles.css index 874517f..70d57cd 100644 --- a/public/styles/styles.css +++ b/public/styles/styles.css @@ -1,4 +1,44 @@ + #orgNameForm { + width: 550px; + } + + #emailForm { + margin-top: 50px; + width: 80%; + margin: 0 auto; + } + body { + background-image: url('../johnbull.png'); + } + div#customerEntryFull { + top: 20%; + position: relative; + } + + + body { + background-repeat: no-repeat; + background-size: cover; + } + + + div#emailForm { + text-align: center; + } + + div#orgNameForm { + top: 30%; + position: relative; + } + + + div#newCusFormMain { + text-align: center; +} + form.ui.form { + text-align: center; + } .hidden.menu { display: none; } @@ -77,4 +117,3 @@ font-size: 1.5em; } } - \ No newline at end of file diff --git a/views/customer.ejs b/views/customer.ejs index de01340..8a9918b 100644 --- a/views/customer.ejs +++ b/views/customer.ejs @@ -1,22 +1,86 @@ +<% include partials/header %> + + +
+
Create a New Customer
-
+
- +
- +
- +
- +
- + +
Blast
+
logout
+ + + + + + + + + + + + + +
NameLastAddressEmail
+ +
+ + +<%include partials/footer %> diff --git a/views/landing.ejs b/views/landing.ejs index f08c86a..89f4073 100644 --- a/views/landing.ejs +++ b/views/landing.ejs @@ -1,97 +1,81 @@ - - - - - - - - - - - - - - - Homepage - Semantic - - - - - - - - - -