Made routes more logical
parent
9d75471488
commit
ab35c2814f
|
@ -428,7 +428,7 @@ $(document).ready(function (){
|
||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/admin/product/addtocart',
|
url: '/product/addtocart',
|
||||||
data: {productId: $('#productId').val(), productQuantity: $('#product_quantity').val(), productOptions: JSON.stringify(productOptions)}
|
data: {productId: $('#productId').val(), productQuantity: $('#product_quantity').val(), productOptions: JSON.stringify(productOptions)}
|
||||||
})
|
})
|
||||||
.done(function(msg){
|
.done(function(msg){
|
||||||
|
@ -460,7 +460,7 @@ $(document).ready(function (){
|
||||||
}else{
|
}else{
|
||||||
$.ajax({
|
$.ajax({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/admin/product/addtocart',
|
url: '/product/addtocart',
|
||||||
data: {productId: $(this).attr('data-id')}
|
data: {productId: $(this).attr('data-id')}
|
||||||
})
|
})
|
||||||
.done(function(msg){
|
.done(function(msg){
|
||||||
|
@ -477,7 +477,7 @@ $(document).ready(function (){
|
||||||
$(document).on('click', '#empty-cart', function(e){
|
$(document).on('click', '#empty-cart', function(e){
|
||||||
$.ajax({
|
$.ajax({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/admin/product/emptycart'
|
url: '/product/emptycart'
|
||||||
})
|
})
|
||||||
.done(function(msg){
|
.done(function(msg){
|
||||||
$('#cart-count').text(msg.totalCartItems);
|
$('#cart-count').text(msg.totalCartItems);
|
||||||
|
@ -612,7 +612,7 @@ $(document).ready(function (){
|
||||||
function deleteFromCart(element){
|
function deleteFromCart(element){
|
||||||
$.ajax({
|
$.ajax({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/admin/product/removefromcart',
|
url: '/product/removefromcart',
|
||||||
data: {cart_index: element}
|
data: {cart_index: element}
|
||||||
})
|
})
|
||||||
.done(function(msg){
|
.done(function(msg){
|
||||||
|
@ -672,7 +672,7 @@ function updateCart(){
|
||||||
// update cart on server
|
// update cart on server
|
||||||
$.ajax({
|
$.ajax({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/admin/product/updatecart',
|
url: '/product/updatecart',
|
||||||
data: {items: JSON.stringify(cartItems)}
|
data: {items: JSON.stringify(cartItems)}
|
||||||
})
|
})
|
||||||
.done(function(msg){
|
.done(function(msg){
|
||||||
|
|
File diff suppressed because one or more lines are too long
221
routes/admin.js
221
routes/admin.js
|
@ -1,10 +1,8 @@
|
||||||
let express = require('express');
|
const express = require('express');
|
||||||
let common = require('./common');
|
const common = require('./common');
|
||||||
let escape = require('html-entities').AllHtmlEntities;
|
const escape = require('html-entities').AllHtmlEntities;
|
||||||
let async = require('async');
|
const colors = require('colors');
|
||||||
let colors = require('colors');
|
const router = express.Router();
|
||||||
let _ = require('lodash');
|
|
||||||
let router = express.Router();
|
|
||||||
|
|
||||||
// Admin section
|
// Admin section
|
||||||
router.get('/', common.restrict, (req, res, next) => {
|
router.get('/', common.restrict, (req, res, next) => {
|
||||||
|
@ -171,141 +169,106 @@ router.get('/products', common.restrict, (req, res, next) => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Admin section
|
// logout
|
||||||
router.post('/product/addtocart', (req, res, next) => {
|
router.get('/logout', (req, res) => {
|
||||||
const db = req.app.db;
|
req.session.user = null;
|
||||||
let productQuantity = req.body.productQuantity ? parseInt(req.body.productQuantity) : 1;
|
req.session.message = null;
|
||||||
|
req.session.messageType = null;
|
||||||
|
res.redirect('/');
|
||||||
|
});
|
||||||
|
|
||||||
// setup cart object if it doesn't exist
|
// login form
|
||||||
if(!req.session.cart){
|
router.get('/login', (req, res) => {
|
||||||
req.session.cart = [];
|
let db = req.app.db;
|
||||||
}
|
|
||||||
|
|
||||||
db.products.findOne({_id: common.getId(req.body.productId)}, (err, product) => {
|
db.users.count({}, (err, userCount) => {
|
||||||
if(err){
|
if(err){
|
||||||
console.error(colors.red('Error adding to cart', err));
|
// if there are no users set the "needsSetup" session
|
||||||
|
req.session.needsSetup = true;
|
||||||
|
res.redirect('/admin/setup');
|
||||||
}
|
}
|
||||||
|
// we check for a user. If one exists, redirect to login form otherwise setup
|
||||||
if(product){
|
if(userCount > 0){
|
||||||
let productPrice = parseFloat(product.productPrice).toFixed(2);
|
// set needsSetup to false as a user exists
|
||||||
|
req.session.needsSetup = false;
|
||||||
// doc used to test if existing in the cart with the options. If not found, we add new.
|
res.render('login', {
|
||||||
let options = {};
|
title: 'Login',
|
||||||
if(req.body.productOptions){
|
referringUrl: req.header('Referer'),
|
||||||
options = JSON.parse(req.body.productOptions);
|
config: common.getConfig(),
|
||||||
}
|
message: common.clearSessionValue(req.session, 'message'),
|
||||||
let findDoc = {
|
messageType: common.clearSessionValue(req.session, 'messageType'),
|
||||||
productId: req.body.productId,
|
helpers: req.handlebars.helpers,
|
||||||
options: options
|
showFooter: 'showFooter'
|
||||||
};
|
});
|
||||||
|
|
||||||
// if exists we add to the existing value
|
|
||||||
let cartIndex = _.findIndex(req.session.cart, findDoc);
|
|
||||||
if(cartIndex > -1){
|
|
||||||
req.session.cart[cartIndex].quantity = parseInt(req.session.cart[cartIndex].quantity) + productQuantity;
|
|
||||||
req.session.cart[cartIndex].totalItemPrice = productPrice * parseInt(req.session.cart[cartIndex].quantity);
|
|
||||||
}else{
|
}else{
|
||||||
// Doesnt exist so we add to the cart session
|
// if there are no users set the "needsSetup" session
|
||||||
req.session.cartTotalItems = req.session.cartTotalItems + productQuantity;
|
req.session.needsSetup = true;
|
||||||
|
res.redirect('/admin/setup');
|
||||||
// new product deets
|
|
||||||
let productObj = {};
|
|
||||||
productObj.productId = req.body.productId;
|
|
||||||
productObj.title = product.productTitle;
|
|
||||||
productObj.quantity = productQuantity;
|
|
||||||
productObj.totalItemPrice = productPrice * productQuantity;
|
|
||||||
productObj.options = options;
|
|
||||||
productObj.productImage = product.productImage;
|
|
||||||
if(product.productPermalink){
|
|
||||||
productObj.link = product.productPermalink;
|
|
||||||
}else{
|
|
||||||
productObj.link = product._id;
|
|
||||||
}
|
|
||||||
|
|
||||||
// merge into the current cart
|
|
||||||
req.session.cart.push(productObj);
|
|
||||||
}
|
|
||||||
|
|
||||||
// update total cart amount
|
|
||||||
common.updateTotalCartAmount(req, res);
|
|
||||||
|
|
||||||
// update how many products in the shopping cart
|
|
||||||
req.session.cartTotalItems = Object.keys(req.session.cart).length;
|
|
||||||
res.status(200).json({message: 'Cart successfully updated', totalCartItems: Object.keys(req.session.cart).length});
|
|
||||||
}else{
|
|
||||||
res.status(400).json({message: 'Error updating cart. Please try again.'});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Updates a single product quantity
|
// login the user and check the password
|
||||||
router.post('/product/updatecart', (req, res, next) => {
|
router.post('/login_action', (req, res) => {
|
||||||
const db = req.app.db;
|
let db = req.app.db;
|
||||||
let cartItems = JSON.parse(req.body.items);
|
let bcrypt = req.bcrypt;
|
||||||
let hasError = false;
|
|
||||||
|
|
||||||
async.eachSeries(cartItems, (cartItem, callback) => {
|
db.users.findOne({userEmail: req.body.email}, (err, user) => {
|
||||||
let productQuantity = cartItem.itemQuantity ? cartItem.itemQuantity : 1;
|
|
||||||
if(cartItem.itemQuantity === 0){
|
|
||||||
// quantity equals zero so we remove the item
|
|
||||||
req.session.cart.splice(cartItem.cartIndex, 1);
|
|
||||||
callback(null);
|
|
||||||
}else{
|
|
||||||
db.products.findOne({_id: common.getId(cartItem.productId)}, (err, product) => {
|
|
||||||
if(err){
|
if(err){
|
||||||
console.error(colors.red('Error updating cart', err));
|
req.session.message = 'Cannot find user.';
|
||||||
}
|
req.session.messageType = 'danger';
|
||||||
if(product){
|
res.redirect('/admin/login');
|
||||||
let productPrice = parseFloat(product.productPrice).toFixed(2);
|
return;
|
||||||
if(req.session.cart[cartItem.cartIndex]){
|
|
||||||
req.session.cart[cartItem.cartIndex].quantity = productQuantity;
|
|
||||||
req.session.cart[cartItem.cartIndex].totalItemPrice = productPrice * productQuantity;
|
|
||||||
callback(null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if user exists with that email
|
||||||
|
if(user === undefined || user === null){
|
||||||
|
req.session.message = 'A user with that email does not exist.';
|
||||||
|
req.session.messageType = 'danger';
|
||||||
|
res.redirect('/admin/login');
|
||||||
}else{
|
}else{
|
||||||
hasError = true;
|
// we have a user under that email so we compare the password
|
||||||
callback(null);
|
if(bcrypt.compareSync(req.body.password, user.userPassword) === true){
|
||||||
}
|
req.session.user = req.body.email;
|
||||||
});
|
req.session.usersName = user.usersName;
|
||||||
}
|
req.session.userId = user._id.toString();
|
||||||
}, () => {
|
req.session.isAdmin = user.isAdmin;
|
||||||
// update total cart amount
|
res.redirect('/admin');
|
||||||
common.updateTotalCartAmount(req, res);
|
|
||||||
|
|
||||||
// show response
|
|
||||||
if(hasError === false){
|
|
||||||
res.status(200).json({message: 'Cart successfully updated', totalCartItems: Object.keys(req.session.cart).length});
|
|
||||||
}else{
|
}else{
|
||||||
res.status(400).json({message: 'There was an error updating the cart', totalCartItems: Object.keys(req.session.cart).length});
|
// password is not correct
|
||||||
|
req.session.message = 'Access denied. Check password and try again.';
|
||||||
|
req.session.messageType = 'danger';
|
||||||
|
res.redirect('/admin/login');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Remove single product from cart
|
// setup form is shown when there are no users setup in the DB
|
||||||
router.post('/product/removefromcart', (req, res, next) => {
|
router.get('/setup', (req, res) => {
|
||||||
// remove item from cart
|
let db = req.app.db;
|
||||||
async.each(req.session.cart, (item, callback) => {
|
|
||||||
if(item){
|
|
||||||
if(item.productId === req.body.cart_index){
|
|
||||||
req.session.cart.splice(req.session.cart.indexOf(item), 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
callback();
|
|
||||||
}, () => {
|
|
||||||
// update total cart amount
|
|
||||||
common.updateTotalCartAmount(req, res);
|
|
||||||
res.status(200).json({message: 'Product successfully removed', totalCartItems: Object.keys(req.session.cart).length});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// Totally empty the cart
|
db.users.count({}, (err, userCount) => {
|
||||||
router.post('/product/emptycart', (req, res, next) => {
|
if(err){
|
||||||
delete req.session.cart;
|
console.error(colors.red('Error getting users for setup', err));
|
||||||
delete req.session.orderId;
|
}
|
||||||
|
// dont allow the user to "re-setup" if a user exists.
|
||||||
// update total cart amount
|
// set needsSetup to false as a user exists
|
||||||
common.updateTotalCartAmount(req, res);
|
req.session.needsSetup = false;
|
||||||
res.status(200).json({message: 'Cart successfully emptied', totalCartItems: 0});
|
if(userCount === 0){
|
||||||
|
req.session.needsSetup = true;
|
||||||
|
res.render('setup', {
|
||||||
|
title: 'Setup',
|
||||||
|
config: common.getConfig(),
|
||||||
|
helpers: req.handlebars.helpers,
|
||||||
|
message: common.clearSessionValue(req.session, 'message'),
|
||||||
|
messageType: common.clearSessionValue(req.session, 'messageType'),
|
||||||
|
showFooter: 'showFooter'
|
||||||
|
});
|
||||||
|
}else{
|
||||||
|
res.redirect('/admin/login');
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Admin section
|
// Admin section
|
||||||
|
@ -689,15 +652,15 @@ router.post('/setup_action', (req, res) => {
|
||||||
console.error(colors.red('Failed to insert user: ' + err));
|
console.error(colors.red('Failed to insert user: ' + err));
|
||||||
req.session.message = 'Setup failed';
|
req.session.message = 'Setup failed';
|
||||||
req.session.messageType = 'danger';
|
req.session.messageType = 'danger';
|
||||||
res.redirect('/setup');
|
res.redirect('/admin/setup');
|
||||||
}else{
|
}else{
|
||||||
req.session.message = 'User account inserted';
|
req.session.message = 'User account inserted';
|
||||||
req.session.messageType = 'success';
|
req.session.messageType = 'success';
|
||||||
res.redirect('/login');
|
res.redirect('/admin/login');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}else{
|
}else{
|
||||||
res.redirect('/login');
|
res.redirect('/admin/login');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -712,7 +675,7 @@ router.post('/user/insert', common.restrict, (req, res) => {
|
||||||
let urlParts = url.parse(req.header('Referer'));
|
let urlParts = url.parse(req.header('Referer'));
|
||||||
|
|
||||||
let isAdmin = 'false';
|
let isAdmin = 'false';
|
||||||
if(urlParts.path === '/setup'){
|
if(urlParts.path === '/admin/setup'){
|
||||||
isAdmin = 'true';
|
isAdmin = 'true';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -755,9 +718,9 @@ router.post('/user/insert', common.restrict, (req, res) => {
|
||||||
|
|
||||||
// if from setup we add user to session and redirect to login.
|
// if from setup we add user to session and redirect to login.
|
||||||
// Otherwise we show users screen
|
// Otherwise we show users screen
|
||||||
if(urlParts.path === '/setup'){
|
if(urlParts.path === '/admin/setup'){
|
||||||
req.session.user = req.body.userEmail;
|
req.session.user = req.body.userEmail;
|
||||||
res.redirect('/login');
|
res.redirect('/admin/login');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
res.redirect('/admin/users');
|
res.redirect('/admin/users');
|
||||||
|
|
|
@ -15,7 +15,7 @@ let ObjectId = require('mongodb').ObjectID;
|
||||||
exports.checkLogin = (req, res, next) => {
|
exports.checkLogin = (req, res, next) => {
|
||||||
// if not protecting we check for public pages and don't checkLogin
|
// if not protecting we check for public pages and don't checkLogin
|
||||||
if(req.session.needsSetup === true){
|
if(req.session.needsSetup === true){
|
||||||
res.redirect('/setup');
|
res.redirect('/admin/setup');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ exports.checkLogin = (req, res, next) => {
|
||||||
next();
|
next();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
res.redirect('/login');
|
res.redirect('/admin/login');
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.showCartCloseBtn = (page) => {
|
exports.showCartCloseBtn = (page) => {
|
||||||
|
|
208
routes/index.js
208
routes/index.js
|
@ -1,6 +1,7 @@
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const colors = require('colors');
|
const colors = require('colors');
|
||||||
|
const async = require('async');
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const common = require('./common');
|
const common = require('./common');
|
||||||
|
|
||||||
|
@ -134,104 +135,141 @@ router.get('/product/:id', (req, res) => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// logout
|
// Updates a single product quantity
|
||||||
router.get('/logout', (req, res) => {
|
router.post('/product/updatecart', (req, res, next) => {
|
||||||
req.session.user = null;
|
const db = req.app.db;
|
||||||
req.session.message = null;
|
let cartItems = JSON.parse(req.body.items);
|
||||||
req.session.messageType = null;
|
let hasError = false;
|
||||||
res.redirect('/');
|
|
||||||
});
|
|
||||||
|
|
||||||
// login form
|
async.eachSeries(cartItems, (cartItem, callback) => {
|
||||||
router.get('/login', (req, res) => {
|
let productQuantity = cartItem.itemQuantity ? cartItem.itemQuantity : 1;
|
||||||
let db = req.app.db;
|
if(cartItem.itemQuantity === 0){
|
||||||
|
// quantity equals zero so we remove the item
|
||||||
db.users.count({}, (err, userCount) => {
|
req.session.cart.splice(cartItem.cartIndex, 1);
|
||||||
|
callback(null);
|
||||||
|
}else{
|
||||||
|
db.products.findOne({_id: common.getId(cartItem.productId)}, (err, product) => {
|
||||||
if(err){
|
if(err){
|
||||||
// if there are no users set the "needsSetup" session
|
console.error(colors.red('Error updating cart', err));
|
||||||
req.session.needsSetup = true;
|
}
|
||||||
res.redirect('/setup');
|
if(product){
|
||||||
|
let productPrice = parseFloat(product.productPrice).toFixed(2);
|
||||||
|
if(req.session.cart[cartItem.cartIndex]){
|
||||||
|
req.session.cart[cartItem.cartIndex].quantity = productQuantity;
|
||||||
|
req.session.cart[cartItem.cartIndex].totalItemPrice = productPrice * productQuantity;
|
||||||
|
callback(null);
|
||||||
}
|
}
|
||||||
// we check for a user. If one exists, redirect to login form otherwise setup
|
|
||||||
if(userCount > 0){
|
|
||||||
// set needsSetup to false as a user exists
|
|
||||||
req.session.needsSetup = false;
|
|
||||||
res.render('login', {
|
|
||||||
title: 'Login',
|
|
||||||
referringUrl: req.header('Referer'),
|
|
||||||
config: common.getConfig(),
|
|
||||||
message: common.clearSessionValue(req.session, 'message'),
|
|
||||||
messageType: common.clearSessionValue(req.session, 'messageType'),
|
|
||||||
helpers: req.handlebars.helpers,
|
|
||||||
showFooter: 'showFooter'
|
|
||||||
});
|
|
||||||
}else{
|
}else{
|
||||||
// if there are no users set the "needsSetup" session
|
hasError = true;
|
||||||
req.session.needsSetup = true;
|
callback(null);
|
||||||
res.redirect('/setup');
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, () => {
|
||||||
|
// update total cart amount
|
||||||
|
common.updateTotalCartAmount(req, res);
|
||||||
|
|
||||||
|
// show response
|
||||||
|
if(hasError === false){
|
||||||
|
res.status(200).json({message: 'Cart successfully updated', totalCartItems: Object.keys(req.session.cart).length});
|
||||||
|
}else{
|
||||||
|
res.status(400).json({message: 'There was an error updating the cart', totalCartItems: Object.keys(req.session.cart).length});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// setup form is shown when there are no users setup in the DB
|
// Remove single product from cart
|
||||||
router.get('/setup', (req, res) => {
|
router.post('/product/removefromcart', (req, res, next) => {
|
||||||
let db = req.app.db;
|
// remove item from cart
|
||||||
|
async.each(req.session.cart, (item, callback) => {
|
||||||
|
if(item){
|
||||||
|
if(item.productId === req.body.cart_index){
|
||||||
|
req.session.cart.splice(req.session.cart.indexOf(item), 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
callback();
|
||||||
|
}, () => {
|
||||||
|
// update total cart amount
|
||||||
|
common.updateTotalCartAmount(req, res);
|
||||||
|
res.status(200).json({message: 'Product successfully removed', totalCartItems: Object.keys(req.session.cart).length});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
db.users.count({}, (err, userCount) => {
|
// Totally empty the cart
|
||||||
|
router.post('/product/emptycart', (req, res, next) => {
|
||||||
|
delete req.session.cart;
|
||||||
|
delete req.session.orderId;
|
||||||
|
|
||||||
|
// update total cart amount
|
||||||
|
common.updateTotalCartAmount(req, res);
|
||||||
|
res.status(200).json({message: 'Cart successfully emptied', totalCartItems: 0});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Add item to cart
|
||||||
|
router.post('/product/addtocart', (req, res, next) => {
|
||||||
|
const db = req.app.db;
|
||||||
|
let productQuantity = req.body.productQuantity ? parseInt(req.body.productQuantity) : 1;
|
||||||
|
|
||||||
|
// setup cart object if it doesn't exist
|
||||||
|
if(!req.session.cart){
|
||||||
|
req.session.cart = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the item from the DB
|
||||||
|
db.products.findOne({_id: common.getId(req.body.productId)}, (err, product) => {
|
||||||
if(err){
|
if(err){
|
||||||
console.error(colors.red('Error getting users for setup', err));
|
console.error(colors.red('Error adding to cart', err));
|
||||||
}
|
|
||||||
// dont allow the user to "re-setup" if a user exists.
|
|
||||||
// set needsSetup to false as a user exists
|
|
||||||
req.session.needsSetup = false;
|
|
||||||
if(userCount === 0){
|
|
||||||
req.session.needsSetup = true;
|
|
||||||
res.render('setup', {
|
|
||||||
title: 'Setup',
|
|
||||||
config: common.getConfig(),
|
|
||||||
helpers: req.handlebars.helpers,
|
|
||||||
message: common.clearSessionValue(req.session, 'message'),
|
|
||||||
messageType: common.clearSessionValue(req.session, 'messageType'),
|
|
||||||
showFooter: 'showFooter'
|
|
||||||
});
|
|
||||||
}else{
|
|
||||||
res.redirect('/login');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// login the user and check the password
|
|
||||||
router.post('/login_action', (req, res) => {
|
|
||||||
let db = req.app.db;
|
|
||||||
let bcrypt = req.bcrypt;
|
|
||||||
|
|
||||||
db.users.findOne({userEmail: req.body.email}, (err, user) => {
|
|
||||||
if(err){
|
|
||||||
req.session.message = 'Cannot find user.';
|
|
||||||
req.session.messageType = 'danger';
|
|
||||||
res.redirect('/login');
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if user exists with that email
|
// We item is found, add it to the cart
|
||||||
if(user === undefined || user === null){
|
if(product){
|
||||||
req.session.message = 'A user with that email does not exist.';
|
let productPrice = parseFloat(product.productPrice).toFixed(2);
|
||||||
req.session.messageType = 'danger';
|
|
||||||
res.redirect('/login');
|
// Doc used to test if existing in the cart with the options. If not found, we add new.
|
||||||
}else{
|
let options = {};
|
||||||
// we have a user under that email so we compare the password
|
if(req.body.productOptions){
|
||||||
if(bcrypt.compareSync(req.body.password, user.userPassword) === true){
|
options = JSON.parse(req.body.productOptions);
|
||||||
req.session.user = req.body.email;
|
|
||||||
req.session.usersName = user.usersName;
|
|
||||||
req.session.userId = user._id.toString();
|
|
||||||
req.session.isAdmin = user.isAdmin;
|
|
||||||
res.redirect('/admin');
|
|
||||||
}else{
|
|
||||||
// password is not correct
|
|
||||||
req.session.message = 'Access denied. Check password and try again.';
|
|
||||||
req.session.messageType = 'danger';
|
|
||||||
res.redirect('/login');
|
|
||||||
}
|
}
|
||||||
|
let findDoc = {
|
||||||
|
productId: req.body.productId,
|
||||||
|
options: options
|
||||||
|
};
|
||||||
|
|
||||||
|
// if exists we add to the existing value
|
||||||
|
let cartIndex = _.findIndex(req.session.cart, findDoc);
|
||||||
|
if(cartIndex > -1){
|
||||||
|
req.session.cart[cartIndex].quantity = parseInt(req.session.cart[cartIndex].quantity) + productQuantity;
|
||||||
|
req.session.cart[cartIndex].totalItemPrice = productPrice * parseInt(req.session.cart[cartIndex].quantity);
|
||||||
|
}else{
|
||||||
|
// Doesnt exist so we add to the cart session
|
||||||
|
req.session.cartTotalItems = req.session.cartTotalItems + productQuantity;
|
||||||
|
|
||||||
|
// new product deets
|
||||||
|
let productObj = {};
|
||||||
|
productObj.productId = req.body.productId;
|
||||||
|
productObj.title = product.productTitle;
|
||||||
|
productObj.quantity = productQuantity;
|
||||||
|
productObj.totalItemPrice = productPrice * productQuantity;
|
||||||
|
productObj.options = options;
|
||||||
|
productObj.productImage = product.productImage;
|
||||||
|
if(product.productPermalink){
|
||||||
|
productObj.link = product.productPermalink;
|
||||||
|
}else{
|
||||||
|
productObj.link = product._id;
|
||||||
|
}
|
||||||
|
|
||||||
|
// merge into the current cart
|
||||||
|
req.session.cart.push(productObj);
|
||||||
|
}
|
||||||
|
|
||||||
|
// update total cart amount
|
||||||
|
common.updateTotalCartAmount(req, res);
|
||||||
|
|
||||||
|
// update how many products in the shopping cart
|
||||||
|
req.session.cartTotalItems = Object.keys(req.session.cart).length;
|
||||||
|
res.status(200).json({message: 'Cart successfully updated', totalCartItems: Object.keys(req.session.cart).length});
|
||||||
|
}else{
|
||||||
|
res.status(400).json({message: 'Error updating cart. Please try again.'});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -107,7 +107,7 @@
|
||||||
{{/ifCond}}
|
{{/ifCond}}
|
||||||
{{/unless}}
|
{{/unless}}
|
||||||
{{#if session.user}}
|
{{#if session.user}}
|
||||||
<li><a href="/logout"><i class="fa fa-sign-out" aria-hidden="true"> </i>Logout</a></li>
|
<li><a href="/admin/logout"><i class="fa fa-sign-out" aria-hidden="true"> </i>Logout</a></li>
|
||||||
{{/if}}
|
{{/if}}
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue