Refactoring

master
Mark Moffat 2019-10-26 11:38:53 +10:30
parent f0f3c56deb
commit 7070cdef7e
5 changed files with 786 additions and 888 deletions

View File

@ -10,9 +10,9 @@ const restrictedRoutes = [
{ route: '/admin/product/published_state', response: 'json' },
{ route: '/admin/product/setasmainimage', response: 'json' },
{ route: '/admin/product/deleteimage', response: 'json' },
{ route: '/admin/product/removeoption', response: 'json' },
{ route: '/admin/order/statusupdate', response: 'json' },
{ route: '/admin/settings/update', response: 'json' },
{ route: '/admin/settings/option/remove', response: 'json' },
{ route: '/admin/settings/pages/new', response: 'redirect' },
{ route: '/admin/settings/pages/edit/:page', response: 'redirect' },
{ route: '/admin/settings/pages/update', response: 'json' },

View File

@ -147,16 +147,17 @@ const getThemes = () => {
return fs.readdirSync(path.join(__dirname, '../', 'views', 'themes')).filter(file => fs.statSync(path.join(path.join(__dirname, '../', 'views', 'themes'), file)).isDirectory());
};
const getImages = (dir, req, res, callback) => {
const getImages = async (dir, req, res, callback) => {
const db = req.app.db;
db.products.findOne({ _id: getId(dir) }, (err, product) => {
if(err){
console.error(colors.red('Error getting images', err));
const product = await db.products.findOne({ _id: getId(dir) });
if(!product){
return[];
}
// loop files in /public/uploads/
glob('public/uploads/' + product.productPermalink + '/**', { nosort: true }, (er, files) => {
const files = await glob.sync(`public/uploads/${product.productPermalink}/**`, { nosort: true });
// sort array
files.sort();
@ -179,9 +180,7 @@ const getImages = (dir, req, res, callback) => {
fileList.push(file);
}
}
callback(fileList);
});
});
return fileList;
};
const getConfig = () => {

View File

@ -26,17 +26,12 @@ router.get('/admin/logout', (req, res) => {
});
// login form
router.get('/admin/login', (req, res) => {
router.get('/admin/login', async (req, res) => {
const db = req.app.db;
db.users.count({}, (err, userCount) => {
if(err){
// if there are no users set the "needsSetup" session
req.session.needsSetup = true;
res.redirect('/admin/setup');
}
const userCount = await db.users.count({});
// we check for a user. If one exists, redirect to login form otherwise setup
if(userCount > 0){
if(userCount && userCount > 0){
// set needsSetup to false as a user exists
req.session.needsSetup = false;
res.render('login', {
@ -54,22 +49,17 @@ router.get('/admin/login', (req, res) => {
res.redirect('/admin/setup');
}
});
});
// login the user and check the password
router.post('/admin/login_action', (req, res) => {
router.post('/admin/login_action', async (req, res) => {
const db = req.app.db;
db.users.findOne({ userEmail: common.mongoSanitize(req.body.email) }, (err, user) => {
if(err){
const user = await db.users.findOne({ userEmail: common.mongoSanitize(req.body.email) });
if(!user || user === null){
res.status(400).json({ message: 'A user with that email does not exist.' });
return;
}
// check if user exists with that email
if(user === undefined || user === null){
res.status(400).json({ message: 'A user with that email does not exist.' });
}else{
// we have a user under that email so we compare the password
bcrypt.compare(req.body.password, user.userPassword)
.then((result) => {
@ -79,27 +69,22 @@ router.post('/admin/login_action', (req, res) => {
req.session.userId = user._id.toString();
req.session.isAdmin = user.isAdmin;
res.status(200).json({ message: 'Login successful' });
}else{
return;
}
// password is not correct
res.status(400).json({ message: 'Access denied. Check password and try again.' });
}
});
}
});
});
// setup form is shown when there are no users setup in the DB
router.get('/admin/setup', (req, res) => {
router.get('/admin/setup', async (req, res) => {
const db = req.app.db;
db.users.count({}, (err, userCount) => {
if(err){
console.error(colors.red('Error getting users for setup', err));
}
const userCount = await db.users.count({});
// 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){
if(userCount && userCount === 0){
req.session.needsSetup = true;
res.render('setup', {
title: 'Setup',
@ -109,14 +94,13 @@ router.get('/admin/setup', (req, res) => {
messageType: common.clearSessionValue(req.session, 'messageType'),
showFooter: 'showFooter'
});
}else{
res.redirect('/admin/login');
return;
}
});
res.redirect('/admin/login');
});
// insert a user
router.post('/admin/setup_action', (req, res) => {
router.post('/admin/setup_action', async (req, res) => {
const db = req.app.db;
const doc = {
@ -127,29 +111,24 @@ router.post('/admin/setup_action', (req, res) => {
};
// check for users
db.users.count({}, (err, userCount) => {
if(err){
console.info(err.stack);
}
if(userCount === 0){
const userCount = await db.users.count({});
if(userCount && userCount === 0){
// email is ok to be used.
db.users.insert(doc, (err, doc) => {
// show the view
if(err){
console.error(colors.red('Failed to insert user: ' + err));
req.session.message = 'Setup failed';
req.session.messageType = 'danger';
res.redirect('/admin/setup');
}else{
try{
await db.users.insert(doc);
req.session.message = 'User account inserted';
req.session.messageType = 'success';
res.redirect('/admin/login');
return;
}catch(ex){
console.error(colors.red('Failed to insert user: ' + ex));
req.session.message = 'Setup failed';
req.session.messageType = 'danger';
res.redirect('/admin/setup');
return;
}
}
});
}else{
res.redirect('/admin/login');
}
});
});
// settings update
@ -200,33 +179,6 @@ router.post('/admin/settings/update', restrict, checkAccess, (req, res) => {
res.status(400).json({ message: 'Permission denied' });
});
// settings update
router.post('/admin/settings/option/remove', restrict, checkAccess, (req, res) => {
const db = req.app.db;
db.products.findOne({ _id: common.getId(req.body.productId) }, (err, product) => {
if(err){
console.info(err.stack);
}
if(product && product.productOptions){
const optJson = JSON.parse(product.productOptions);
delete optJson[req.body.optName];
db.products.update({ _id: common.getId(req.body.productId) }, { $set: { productOptions: JSON.stringify(optJson) } }, (err, numReplaced) => {
if(err){
console.info(err.stack);
}
if(numReplaced.result.nModified === 1){
res.status(200).json({ message: 'Option successfully removed' });
}else{
res.status(400).json({ message: 'Failed to remove option. Please try again.' });
}
});
}else{
res.status(400).json({ message: 'Product not found. Try saving before removing.' });
}
});
});
// settings update
router.get('/admin/settings/menu', restrict, async (req, res) => {
const db = req.app.db;
@ -243,12 +195,9 @@ router.get('/admin/settings/menu', restrict, async (req, res) => {
});
// settings page list
router.get('/admin/settings/pages', restrict, (req, res) => {
router.get('/admin/settings/pages', restrict, async (req, res) => {
const db = req.app.db;
db.pages.find({}).toArray(async (err, pages) => {
if(err){
console.info(err.stack);
}
const pages = await db.pages.find({}).toArray();
res.render('settings_pages', {
title: 'Static pages',
@ -262,7 +211,6 @@ router.get('/admin/settings/pages', restrict, (req, res) => {
menu: common.sortMenu(await common.getMenu(db))
});
});
});
// settings pages new
router.get('/admin/settings/pages/new', restrict, checkAccess, async (req, res) => {
@ -282,15 +230,22 @@ router.get('/admin/settings/pages/new', restrict, checkAccess, async (req, res)
});
// settings pages editor
router.get('/admin/settings/pages/edit/:page', restrict, checkAccess, (req, res) => {
router.get('/admin/settings/pages/edit/:page', restrict, checkAccess, async (req, res) => {
const db = req.app.db;
db.pages.findOne({ _id: common.getId(req.params.page) }, async (err, page) => {
if(err){
console.info(err.stack);
}
// page found
const page = await db.pages.findOne({ _id: common.getId(req.params.page) });
const menu = common.sortMenu(await common.getMenu(db));
if(page){
if(!page){
res.status(404).render('error', {
title: '404 Error - Page not found',
config: req.app.config,
message: '404 Error - Page not found',
helpers: req.handlebars.helpers,
showFooter: 'showFooter',
menu
});
return;
}
res.render('settings_page_edit', {
title: 'Static pages',
page: page,
@ -303,22 +258,10 @@ router.get('/admin/settings/pages/edit/:page', restrict, checkAccess, (req, res)
config: req.app.config,
menu
});
}else{
// 404 it!
res.status(404).render('error', {
title: '404 Error - Page not found',
config: req.app.config,
message: '404 Error - Page not found',
helpers: req.handlebars.helpers,
showFooter: 'showFooter',
menu
});
}
});
});
// settings update page
router.post('/admin/settings/pages/update', restrict, checkAccess, (req, res) => {
router.post('/admin/settings/pages/update', restrict, checkAccess, async (req, res) => {
const db = req.app.db;
const doc = {
@ -330,47 +273,43 @@ router.post('/admin/settings/pages/update', restrict, checkAccess, (req, res) =>
if(req.body.page_id){
// existing page
db.pages.findOne({ _id: common.getId(req.body.page_id) }, (err, page) => {
if(err){
console.info(err.stack);
}
if(page){
db.pages.update({ _id: common.getId(req.body.page_id) }, { $set: doc }, {}, (err, numReplaced) => {
if(err){
console.info(err.stack);
}
res.status(200).json({ message: 'Page updated successfully', page_id: req.body.page_id });
});
}else{
const page = await db.pages.findOne({ _id: common.getId(req.body.page_id) });
if(!page){
res.status(400).json({ message: 'Page not found' });
}
});
try{
await db.pages.update({ _id: common.getId(req.body.page_id) }, { $set: doc }, {});
res.status(200).json({ message: 'Page updated successfully', page_id: req.body.page_id });
}catch(ex){
res.status(400).json({ message: 'Error updating page. Please try again.' });
}
}else{
// insert page
db.pages.insert(doc, (err, newDoc) => {
if(err){
res.status(400).json({ message: 'Error creating page. Please try again.' });
}else{
try{
const newDoc = await db.pages.insert(doc);
res.status(200).json({ message: 'New page successfully created', page_id: newDoc._id });
return;
}catch(ex){
res.status(400).json({ message: 'Error creating page. Please try again.' });
}
});
}
});
// settings delete page
router.get('/admin/settings/pages/delete/:page', restrict, checkAccess, (req, res) => {
router.get('/admin/settings/pages/delete/:page', restrict, checkAccess, async (req, res) => {
const db = req.app.db;
db.pages.remove({ _id: common.getId(req.params.page) }, {}, (err, numRemoved) => {
if(err){
req.session.message = 'Error deleting page. Please try again.';
req.session.messageType = 'danger';
res.redirect('/admin/settings/pages');
return;
}
try{
await db.pages.remove({ _id: common.getId(req.params.page) }, {});
req.session.message = 'Page successfully deleted';
req.session.messageType = 'success';
res.redirect('/admin/settings/pages');
});
return;
}catch(ex){
req.session.message = 'Error deleting page. Please try again.';
req.session.messageType = 'danger';
res.redirect('/admin/settings/pages');
}
});
// new menu item
@ -414,7 +353,7 @@ router.post('/admin/settings/menu/save_order', restrict, checkAccess, (req, res)
});
// validate the permalink
router.post('/admin/api/validate_permalink', (req, res) => {
router.post('/admin/api/validate_permalink', async (req, res) => {
// if doc id is provided it checks for permalink in any products other that one provided,
// else it just checks for any products with that permalink
const db = req.app.db;
@ -426,21 +365,17 @@ router.post('/admin/api/validate_permalink', (req, res) => {
query = { productPermalink: req.body.permalink, _id: { $ne: common.getId(req.body.docId) } };
}
db.products.count(query, (err, products) => {
if(err){
console.info(err.stack);
}
if(products > 0){
const products = await db.products.count(query);
if(products && products > 0){
res.status(400).json({ message: 'Permalink already exists' });
}else{
res.status(200).json({ message: 'Permalink validated successfully' });
return;
}
});
res.status(200).json({ message: 'Permalink validated successfully' });
});
// upload the file
const upload = multer({ dest: 'public/uploads/' });
router.post('/admin/file/upload', restrict, checkAccess, upload.single('upload_file'), (req, res, next) => {
router.post('/admin/file/upload', restrict, checkAccess, upload.single('upload_file'), async (req, res, next) => {
const db = req.app.db;
if(req.file){
@ -462,9 +397,8 @@ router.post('/admin/file/upload', restrict, checkAccess, upload.single('upload_f
}
// get the product form the DB
db.products.findOne({ _id: common.getId(req.body.productId) }, (err, product) => {
if(err){
console.info(err.stack);
const product = await db.products.findOne({ _id: common.getId(req.body.productId) });
if(!product){
// delete the temp file.
fs.unlinkSync(file.path);
@ -495,26 +429,21 @@ router.post('/admin/file/upload', restrict, checkAccess, upload.single('upload_f
// if there isn't a product featured image, set this one
if(!product.productImage){
db.products.update({ _id: common.getId(req.body.productId) }, { $set: { productImage: imagePath } }, { multi: false }, (err, numReplaced) => {
if(err){
console.info(err.stack);
await db.products.update({ _id: common.getId(req.body.productId) }, { $set: { productImage: imagePath } }, { multi: false });
req.session.message = 'File uploaded successfully';
req.session.messageType = 'success';
res.redirect('/admin/product/edit/' + req.body.productId);
return;
}
req.session.message = 'File uploaded successfully';
req.session.messageType = 'success';
res.redirect('/admin/product/edit/' + req.body.productId);
});
}else{
req.session.message = 'File uploaded successfully';
req.session.messageType = 'success';
res.redirect('/admin/product/edit/' + req.body.productId);
return;
}
});
}else{
// Redirect to error
req.session.message = 'File upload error. Please select a file.';
req.session.messageType = 'danger';
res.redirect('/admin/product/edit/' + req.body.productId);
}
});
// delete a file via ajax request
@ -526,25 +455,25 @@ router.post('/admin/testEmail', restrict, (req, res) => {
});
// delete a file via ajax request
router.post('/admin/file/delete', restrict, checkAccess, (req, res) => {
router.post('/admin/file/delete', restrict, checkAccess, async (req, res) => {
req.session.message = null;
req.session.messageType = null;
fs.unlink('public/' + req.body.img, (err) => {
if(err){
console.error(colors.red('File delete error: ' + err));
res.writeHead(400, { 'Content-Type': 'application/text' });
res.end('Failed to delete file: ' + err);
}else{
try{
await fs.unlinkSync('public/' + req.body.img);
res.writeHead(200, { 'Content-Type': 'application/text' });
res.end('File deleted successfully');
}catch(ex){
console.error(colors.red('File delete error: ' + ex));
res.writeHead(400, { 'Content-Type': 'application/text' });
res.end('Failed to delete file: ' + ex);
}
});
});
router.get('/admin/files', restrict, (req, res) => {
router.get('/admin/files', restrict, async (req, res) => {
// loop files in /public/uploads/
glob('public/uploads/**', { nosort: true }, (er, files) => {
const files = await glob.sync('public/uploads/**', { nosort: true });
// sort array
files.sort();
@ -587,6 +516,5 @@ router.get('/admin/files', restrict, (req, res) => {
messageType: common.clearSessionValue(req.session, 'messageType')
});
});
});
module.exports = router;

View File

@ -22,10 +22,11 @@ router.get('/payment/:orderId', async (req, res, next) => {
const db = req.app.db;
const config = req.app.config;
// render the payment complete message
db.orders.findOne({ _id: getId(req.params.orderId) }, async (err, order) => {
if(err){
console.info(err.stack);
// Get the order
const order = await db.orders.findOne({ _id: getId(req.params.orderId) });
if(!order){
res.render('error', { title: 'Not found', message: 'Order not found', helpers: req.handlebars.helpers, config });
return;
}
// If stock management is turned on payment approved update stock level
@ -66,7 +67,6 @@ router.get('/payment/:orderId', async (req, res, next) => {
menu: sortMenu(await getMenu(db))
});
});
});
router.get('/checkout', async (req, res, next) => {
const config = req.app.config;
@ -135,35 +135,37 @@ router.get('/cartPartial', (req, res) => {
});
// show an individual product
router.get('/product/:id', (req, res) => {
router.get('/product/:id', async (req, res) => {
const db = req.app.db;
const config = req.app.config;
db.products.findOne({ $or: [{ _id: getId(req.params.id) }, { productPermalink: req.params.id }] }, (err, result) => {
// render 404 if page is not published
if(err){
res.render('error', { title: 'Not found', message: 'Product not found', helpers: req.handlebars.helpers, config });
const product = await db.products.findOne({ $or: [{ _id: getId(req.params.id) }, { productPermalink: req.params.id }] });
if(!product){
res.render('error', { title: 'Not found', message: 'Order not found', helpers: req.handlebars.helpers, config });
return;
}
if(err || result == null || result.productPublished === false){
if(product.productPublished === false){
res.render('error', { title: 'Not found', message: 'Product not found', helpers: req.handlebars.helpers, config });
}else{
const productOptions = result.productOptions;
return;
}
const productOptions = product.productOptions;
// If JSON query param return json instead
if(req.query.json === 'true'){
res.status(200).json(result);
res.status(200).json(product);
return;
}
// show the view
getImages(result._id, req, res, async (images) => {
const images = await getImages(product._id, req, res);
res.render(`${config.themeViews}product`, {
title: result.productTitle,
result: result,
title: product.productTitle,
result: product,
productOptions: productOptions,
images: images,
productDescription: result.productDescription,
metaDescription: config.cartTitle + ' - ' + result.productTitle,
productDescription: product.productDescription,
metaDescription: config.cartTitle + ' - ' + product.productTitle,
pageCloseBtn: showCartCloseBtn('product'),
config: config,
session: req.session,
@ -175,9 +177,6 @@ router.get('/product/:id', (req, res) => {
menu: sortMenu(await getMenu(db))
});
});
}
});
});
// Updates a single product quantity
router.post('/product/updatecart', (req, res, next) => {
@ -187,17 +186,14 @@ router.post('/product/updatecart', (req, res, next) => {
let hasError = false;
let stockError = false;
async.eachSeries(cartItems, (cartItem, callback) => {
async.eachSeries(cartItems, async (cartItem, callback) => {
const 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: getId(cartItem.productId) }, (err, product) => {
if(err){
console.error(colors.red('Error updating cart', err));
}
const product = await db.products.findOne({ _id: getId(cartItem.productId) });
if(product){
// If stock management on check there is sufficient stock for this product
if(config.trackStock){
@ -219,7 +215,6 @@ router.post('/product/updatecart', (req, res, next) => {
hasError = true;
callback(null);
}
});
}
}, async () => {
// update total cart amount
@ -289,7 +284,7 @@ router.post('/product/emptycart', async (req, res, next) => {
});
// Add item to cart
router.post('/product/addtocart', (req, res, next) => {
router.post('/product/addtocart', async (req, res, next) => {
const db = req.app.db;
const config = req.app.config;
let productQuantity = req.body.productQuantity ? parseInt(req.body.productQuantity) : 1;
@ -306,12 +301,7 @@ router.post('/product/addtocart', (req, res, next) => {
}
// Get the item from the DB
db.products.findOne({ _id: getId(req.body.productId) }, async (err, product) => {
if(err){
console.error(colors.red('Error adding to cart', err));
return res.status(400).json({ message: 'Error updating cart. Please try again.' });
}
const product = await db.products.findOne({ _id: getId(req.body.productId) });
// No product found
if(!product){
return res.status(400).json({ message: 'Error updating cart. Please try again.' });
@ -408,7 +398,6 @@ router.post('/product/addtocart', (req, res, next) => {
req.session.cartTotalItems = req.session.cart.reduce((a, b) => +a + +b.quantity, 0);
return res.status(200).json({ message: 'Cart successfully updated', totalCartItems: req.session.cartTotalItems });
});
});
// search products
router.get('/search/:searchTerm/:pageNum?', (req, res) => {
@ -594,7 +583,7 @@ router.get('/page/:pageNum', (req, res, next) => {
});
// The main entry point of the shop
router.get('/:page?', (req, res, next) => {
router.get('/:page?', async (req, res, next) => {
const db = req.app.db;
const config = req.app.config;
const numberProducts = config.productsPerPage ? config.productsPerPage : 6;
@ -639,10 +628,7 @@ router.get('/:page?', (req, res, next) => {
return;
}
// lets look for a page
db.pages.findOne({ pageSlug: req.params.page, pageEnabled: 'true' }, async (err, page) => {
if(err){
console.error(colors.red('Error getting page', err));
}
const page = db.pages.findOne({ pageSlug: req.params.page, pageEnabled: 'true' });
// if we have a page lets render it, else throw 404
if(page){
res.render(`${config.themeViews}page`, {
@ -669,7 +655,6 @@ router.get('/:page?', (req, res, next) => {
menu: sortMenu(await getMenu(db))
});
}
});
}
});

View File

@ -9,13 +9,10 @@ const fs = require('fs');
const path = require('path');
const router = express.Router();
router.get('/admin/products', restrict, (req, res, next) => {
router.get('/admin/products', restrict, async (req, res, next) => {
const db = req.app.db;
// get the top results
db.products.find({}).sort({ productAddedDate: -1 }).limit(10).toArray((err, topResults) => {
if(err){
console.info(err.stack);
}
const topResults = await db.products.find({}).sort({ productAddedDate: -1 }).limit(10).toArray();
res.render('products', {
title: 'Cart',
top_results: topResults,
@ -27,9 +24,8 @@ router.get('/admin/products', restrict, (req, res, next) => {
helpers: req.handlebars.helpers
});
});
});
router.get('/admin/products/filter/:search', (req, res, next) => {
router.get('/admin/products/filter/:search', async (req, res, next) => {
const db = req.app.db;
const searchTerm = req.params.search;
const productsIndex = req.app.productsIndex;
@ -40,10 +36,7 @@ router.get('/admin/products/filter/:search', (req, res, next) => {
});
// we search on the lunr indexes
db.products.find({ _id: { $in: lunrIdArray } }).toArray((err, results) => {
if(err){
console.error(colors.red('Error searching', err));
}
const results = await db.products.find({ _id: { $in: lunrIdArray } }).toArray();
res.render('products', {
title: 'Results',
results: results,
@ -56,7 +49,6 @@ router.get('/admin/products/filter/:search', (req, res, next) => {
helpers: req.handlebars.helpers
});
});
});
// insert form
router.get('/admin/product/new', restrict, checkAccess, (req, res) => {
@ -77,7 +69,7 @@ router.get('/admin/product/new', restrict, checkAccess, (req, res) => {
});
// insert new product form action
router.post('/admin/product/insert', restrict, checkAccess, (req, res) => {
router.post('/admin/product/insert', restrict, checkAccess, async (req, res) => {
const db = req.app.db;
// Process supplied options
@ -131,10 +123,7 @@ router.post('/admin/product/insert', restrict, checkAccess, (req, res) => {
return;
}
db.products.count({ productPermalink: req.body.productPermalink }, (err, product) => {
if(err){
console.info(err.stack);
}
const product = await db.products.count({ productPermalink: req.body.productPermalink });
if(product > 0 && req.body.productPermalink !== ''){
// permalink exits
req.session.message = 'Permalink already exists. Pick a new one.';
@ -160,33 +149,9 @@ router.post('/admin/product/insert', restrict, checkAccess, (req, res) => {
res.redirect('/admin/product/new');
return;
}
db.products.insert(doc, (err, newDoc) => {
if(err){
console.log(colors.red('Error inserting document: ' + err));
// keep the current stuff
req.session.productTitle = req.body.productTitle;
req.session.productDescription = req.body.productDescription;
req.session.productPrice = req.body.productPrice;
req.session.productPermalink = req.body.productPermalink;
req.session.productOptions = productOptions;
req.session.productComment = common.checkboxBool(req.body.productComment);
req.session.productTags = req.body.productTags;
req.session.productStock = req.body.productStock ? parseInt(req.body.productStock) : null;
req.session.message = 'Error: Inserting product';
req.session.messageType = 'danger';
// If API request, return json
if(req.apiAuthenticated){
res.status(400).json({ error: `Error inserting document: ${err}` });
return;
}
// redirect to insert
res.redirect('/admin/product/new');
return;
}
try{
const newDoc = await db.products.insert(doc);
// get the new ID
const newId = newDoc.insertedIds[0];
@ -205,27 +170,53 @@ router.post('/admin/product/insert', restrict, checkAccess, (req, res) => {
// redirect to new doc
res.redirect('/admin/product/edit/' + newId);
});
});
});
}catch(ex){
console.log(colors.red('Error inserting document: ' + ex));
// keep the current stuff
req.session.productTitle = req.body.productTitle;
req.session.productDescription = req.body.productDescription;
req.session.productPrice = req.body.productPrice;
req.session.productPermalink = req.body.productPermalink;
req.session.productOptions = productOptions;
req.session.productComment = common.checkboxBool(req.body.productComment);
req.session.productTags = req.body.productTags;
req.session.productStock = req.body.productStock ? parseInt(req.body.productStock) : null;
req.session.message = 'Error: Inserting product';
req.session.messageType = 'danger';
// If API request, return json
if(req.apiAuthenticated){
res.status(400).json({ error: 'Error inserting document' });
return;
}
// redirect to insert
res.redirect('/admin/product/new');
}
});
// render the editor
router.get('/admin/product/edit/:id', restrict, checkAccess, (req, res) => {
router.get('/admin/product/edit/:id', restrict, checkAccess, async (req, res) => {
const db = req.app.db;
common.getImages(req.params.id, req, res, (images) => {
db.products.findOne({ _id: common.getId(req.params.id) }, (err, result) => {
if(err){
console.info(err.stack);
const images = await common.getImages(req.params.id, req, res);
const product = await db.products.findOne({ _id: common.getId(req.params.id) });
if(!product){
req.session.message = 'Product not found';
req.session.messageType = 'danger';
res.redirect('/admin/products');
return;
}
let options = {};
if(result.productOptions){
options = result.productOptions;
if(product.productOptions){
options = product.productOptions;
}
res.render('product_edit', {
title: 'Edit product',
result: result,
result: product,
images: images,
options: options,
admin: true,
@ -237,16 +228,38 @@ router.get('/admin/product/edit/:id', restrict, checkAccess, (req, res) => {
helpers: req.handlebars.helpers
});
});
});
// Remove option from product
router.post('/admin/product/removeoption', restrict, checkAccess, async (req, res) => {
const db = req.app.db;
const product = await db.products.findOne({ _id: common.getId(req.body.productId) });
if(product && product.productOptions){
const opts = product.productOptions;
delete opts[req.body.optName];
try{
const updateOption = await db.products.update({ _id: common.getId(req.body.productId) }, { $set: { productOptions: opts } });
if(updateOption.result.nModified === 1){
res.status(200).json({ message: 'Option successfully removed' });
return;
}
res.status(400).json({ message: 'Failed to remove option. Please try again.' });
return;
}catch(ex){
res.status(400).json({ message: 'Failed to remove option. Please try again.' });
return;
}
}
res.status(400).json({ message: 'Product not found. Try saving before removing.' });
});
// Update an existing product form action
router.post('/admin/product/update', restrict, checkAccess, (req, res) => {
router.post('/admin/product/update', restrict, checkAccess, async (req, res) => {
const db = req.app.db;
db.products.findOne({ _id: common.getId(req.body.productId) }, (err, product) => {
if(err){
console.info(err.stack);
const product = await db.products.findOne({ _id: common.getId(req.body.productId) });
if(!product){
req.session.message = 'Failed updating product.';
req.session.messageType = 'danger';
@ -259,22 +272,7 @@ router.post('/admin/product/update', restrict, checkAccess, (req, res) => {
res.redirect('/admin/product/edit/' + req.body.productId);
return;
}
db.products.count({ productPermalink: req.body.productPermalink, _id: { $ne: common.getId(product._id) } }, (err, count) => {
if(err){
console.info(err.stack);
// If API request, return json
if(req.apiAuthenticated){
res.status(400).json({ messge: 'Failed to update product' });
return;
}
req.session.message = 'Failed updating product.';
req.session.messageType = 'danger';
res.redirect('/admin/product/edit/' + req.body.productId);
return;
}
const count = await db.products.count({ productPermalink: req.body.productPermalink, _id: { $ne: common.getId(product._id) } });
if(count > 0 && req.body.productPermalink !== ''){
// If API request, return json
if(req.apiAuthenticated){
@ -298,8 +296,9 @@ router.post('/admin/product/update', restrict, checkAccess, (req, res) => {
// redirect to insert
res.redirect('/admin/product/edit/' + req.body.productId);
}else{
common.getImages(req.body.productId, req, res, (images) => {
return;
}
const images = await common.getImages(req.body.productId, req, res);
// Process supplied options
let productOptions = req.body.productOptions;
if(productOptions && typeof productOptions !== 'object'){
@ -364,19 +363,8 @@ router.post('/admin/product/update', restrict, checkAccess, (req, res) => {
productDoc['productImage'] = product.productImage;
}
db.products.update({ _id: common.getId(req.body.productId) }, { $set: productDoc }, {}, (err, numReplaced) => {
if(err){
// If API request, return json
if(req.apiAuthenticated){
res.status(400).json({ messge: 'Failed to save. Please try again' });
return;
}
console.error(colors.red('Failed to save product: ' + err));
req.session.message = 'Failed to save. Please try again';
req.session.messageType = 'danger';
res.redirect('/admin/product/edit/' + req.body.productId);
}else{
try{
await db.products.update({ _id: common.getId(req.body.productId) }, { $set: productDoc }, {});
// Update the index
indexProducts(req.app)
.then(() => {
@ -390,30 +378,34 @@ router.post('/admin/product/update', restrict, checkAccess, (req, res) => {
req.session.messageType = 'success';
res.redirect('/admin/product/edit/' + req.body.productId);
});
}catch(ex){
// If API request, return json
if(req.apiAuthenticated){
res.status(400).json({ messge: 'Failed to save. Please try again' });
return;
}
});
});
console.error(colors.red('Failed to save product: ' + ex));
req.session.message = 'Failed to save. Please try again';
req.session.messageType = 'danger';
res.redirect('/admin/product/edit/' + req.body.productId);
}
});
});
});
// delete product
router.get('/admin/product/delete/:id', restrict, checkAccess, (req, res) => {
router.get('/admin/product/delete/:id', restrict, checkAccess, async (req, res) => {
const db = req.app.db;
// remove the article
db.products.remove({ _id: common.getId(req.params.id) }, {}, (err, numRemoved) => {
if(err){
console.info(err.stack);
}
// remove the product
await db.products.remove({ _id: common.getId(req.params.id) }, {});
// delete any images and folder
rimraf('public/uploads/' + req.params.id, (err) => {
if(err){
console.info(err.stack);
}
// remove the index
// re-index products
indexProducts(req.app)
.then(() => {
// redirect home
@ -423,51 +415,47 @@ router.get('/admin/product/delete/:id', restrict, checkAccess, (req, res) => {
});
});
});
});
// update the published state based on an ajax call from the frontend
router.post('/admin/product/published_state', restrict, checkAccess, (req, res) => {
router.post('/admin/product/published_state', restrict, checkAccess, async (req, res) => {
const db = req.app.db;
db.products.update({ _id: common.getId(req.body.id) }, { $set: { productPublished: common.convertBool(req.body.state) } }, { multi: false }, (err, numReplaced) => {
if(err){
console.error(colors.red('Failed to update the published state: ' + err));
res.status(400).json('Published state not updated');
}else{
try{
await db.products.update({ _id: common.getId(req.body.id) }, { $set: { productPublished: common.convertBool(req.body.state) } }, { multi: false });
res.status(200).json('Published state updated');
}catch(ex){
console.error(colors.red('Failed to update the published state: ' + ex));
res.status(400).json('Published state not updated');
}
});
});
// set as main product image
router.post('/admin/product/setasmainimage', restrict, checkAccess, (req, res) => {
router.post('/admin/product/setasmainimage', restrict, checkAccess, async (req, res) => {
const db = req.app.db;
try{
// update the productImage to the db
db.products.update({ _id: common.getId(req.body.product_id) }, { $set: { productImage: req.body.productImage } }, { multi: false }, (err, numReplaced) => {
if(err){
res.status(400).json({ message: 'Unable to set as main image. Please try again.' });
}else{
await db.products.update({ _id: common.getId(req.body.product_id) }, { $set: { productImage: req.body.productImage } }, { multi: false });
res.status(200).json({ message: 'Main image successfully set' });
}catch(ex){
res.status(400).json({ message: 'Unable to set as main image. Please try again.' });
}
});
});
// deletes a product image
router.post('/admin/product/deleteimage', restrict, checkAccess, (req, res) => {
router.post('/admin/product/deleteimage', restrict, checkAccess, async (req, res) => {
const db = req.app.db;
// get the productImage from the db
db.products.findOne({ _id: common.getId(req.body.product_id) }, (err, product) => {
if(err){
console.info(err.stack);
const product = await db.products.findOne({ _id: common.getId(req.body.product_id) });
if(!product){
res.status(400).json({ message: 'Product not found' });
return;
}
if(req.body.productImage === product.productImage){
// set the produt_image to null
db.products.update({ _id: common.getId(req.body.product_id) }, { $set: { productImage: null } }, { multi: false }, (err, numReplaced) => {
if(err){
console.info(err.stack);
}
// set the productImage to null
await db.products.update({ _id: common.getId(req.body.product_id) }, { $set: { productImage: null } }, { multi: false });
// remove the image from disk
fs.unlink(path.join('public', req.body.productImage), (err) => {
if(err){
@ -476,7 +464,6 @@ router.post('/admin/product/deleteimage', restrict, checkAccess, (req, res) => {
res.status(200).json({ message: 'Image successfully deleted' });
}
});
});
}else{
// remove the image from disk
fs.unlink(path.join('public', req.body.productImage), (err) => {
@ -488,6 +475,5 @@ router.post('/admin/product/deleteimage', restrict, checkAccess, (req, res) => {
});
}
});
});
module.exports = router;