2019-12-18 17:10:54 +10:00
|
|
|
const fs = require('fs');
|
|
|
|
const yenv = require('yenv');
|
|
|
|
if(fs.existsSync('./env.yaml')){
|
|
|
|
process.env = yenv('env.yaml', { strict: false });
|
|
|
|
}
|
2018-01-07 04:55:48 +10:00
|
|
|
const path = require('path');
|
2019-12-18 17:10:54 +10:00
|
|
|
const express = require('express');
|
2018-01-07 04:55:48 +10:00
|
|
|
const logger = require('morgan');
|
|
|
|
const cookieParser = require('cookie-parser');
|
|
|
|
const bodyParser = require('body-parser');
|
|
|
|
const session = require('express-session');
|
|
|
|
const moment = require('moment');
|
2019-06-01 22:22:14 +10:00
|
|
|
const _ = require('lodash');
|
2018-01-07 04:55:48 +10:00
|
|
|
const MongoStore = require('connect-mongodb-session')(session);
|
|
|
|
const numeral = require('numeral');
|
|
|
|
const helmet = require('helmet');
|
|
|
|
const colors = require('colors');
|
2019-06-01 22:22:14 +10:00
|
|
|
const cron = require('node-cron');
|
2019-10-29 19:00:47 +10:00
|
|
|
const crypto = require('crypto');
|
2018-02-06 04:20:30 +10:00
|
|
|
const common = require('./lib/common');
|
2019-06-15 14:46:08 +10:00
|
|
|
const { runIndexing } = require('./lib/indexing');
|
2019-06-17 20:21:45 +10:00
|
|
|
const { addSchemas } = require('./lib/schema');
|
2020-01-21 17:36:46 +10:00
|
|
|
const { initDb, getDbUri } = require('./lib/db');
|
2018-01-07 04:55:48 +10:00
|
|
|
let handlebars = require('express-handlebars');
|
2019-11-06 18:31:25 +10:00
|
|
|
const i18n = require('i18n');
|
2018-01-07 04:55:48 +10:00
|
|
|
|
2018-01-11 06:20:36 +10:00
|
|
|
// Validate our settings schema
|
|
|
|
const Ajv = require('ajv');
|
2019-06-15 14:46:08 +10:00
|
|
|
const ajv = new Ajv({ useDefaults: true });
|
2018-01-11 06:20:36 +10:00
|
|
|
|
2019-06-15 16:05:31 +10:00
|
|
|
// get config
|
2019-11-03 09:18:34 +10:00
|
|
|
const config = common.getConfig();
|
2019-06-15 16:05:31 +10:00
|
|
|
|
|
|
|
const baseConfig = ajv.validate(require('./config/baseSchema'), config);
|
2018-01-11 06:20:36 +10:00
|
|
|
if(baseConfig === false){
|
|
|
|
console.log(colors.red(`settings.json incorrect: ${ajv.errorsText()}`));
|
|
|
|
process.exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the payment gateway config
|
2019-10-25 19:08:47 +10:00
|
|
|
switch(config.paymentGateway){
|
|
|
|
case'paypal':
|
2019-11-03 09:18:34 +10:00
|
|
|
if(ajv.validate(require('./config/paypalSchema'), require('./config/paypal.json')) === false){
|
2019-07-25 05:17:19 +10:00
|
|
|
console.log(colors.red(`PayPal config is incorrect: ${ajv.errorsText()}`));
|
|
|
|
process.exit(2);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-10-25 19:08:47 +10:00
|
|
|
case'stripe':
|
2019-11-03 09:18:34 +10:00
|
|
|
if(ajv.validate(require('./config/stripeSchema'), require('./config/stripe.json')) === false){
|
2019-07-25 05:17:19 +10:00
|
|
|
console.log(colors.red(`Stripe config is incorrect: ${ajv.errorsText()}`));
|
|
|
|
process.exit(2);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-10-25 19:08:47 +10:00
|
|
|
case'authorizenet':
|
2019-11-03 09:18:34 +10:00
|
|
|
if(ajv.validate(require('./config/authorizenetSchema'), require('./config/authorizenet.json')) === false){
|
2019-07-25 05:17:19 +10:00
|
|
|
console.log(colors.red(`Authorizenet config is incorrect: ${ajv.errorsText()}`));
|
|
|
|
process.exit(2);
|
|
|
|
}
|
|
|
|
break;
|
2019-11-11 17:47:48 +10:00
|
|
|
|
|
|
|
case'adyen':
|
|
|
|
if(ajv.validate(require('./config/adyenSchema'), require('./config/adyen.json')) === false){
|
|
|
|
console.log(colors.red(`adyen config is incorrect: ${ajv.errorsText()}`));
|
|
|
|
process.exit(2);
|
|
|
|
}
|
|
|
|
break;
|
2020-01-01 14:27:42 +10:00
|
|
|
case'instore':
|
|
|
|
if(ajv.validate(require('./config/instoreSchema'), require('./config/instore.json')) === false){
|
|
|
|
console.log(colors.red(`instore config is incorrect: ${ajv.errorsText()}`));
|
|
|
|
process.exit(2);
|
|
|
|
}
|
|
|
|
break;
|
2018-02-05 07:39:42 +10:00
|
|
|
}
|
2018-01-11 06:20:36 +10:00
|
|
|
|
2018-01-07 04:55:48 +10:00
|
|
|
// require the routes
|
|
|
|
const index = require('./routes/index');
|
|
|
|
const admin = require('./routes/admin');
|
2018-02-05 23:20:53 +10:00
|
|
|
const product = require('./routes/product');
|
2018-02-03 23:26:09 +10:00
|
|
|
const customer = require('./routes/customer');
|
2018-02-05 23:20:53 +10:00
|
|
|
const order = require('./routes/order');
|
|
|
|
const user = require('./routes/user');
|
2018-01-08 04:34:20 +10:00
|
|
|
const paypal = require('./routes/payments/paypal');
|
|
|
|
const stripe = require('./routes/payments/stripe');
|
2018-02-05 07:39:42 +10:00
|
|
|
const authorizenet = require('./routes/payments/authorizenet');
|
2019-11-11 17:47:48 +10:00
|
|
|
const adyen = require('./routes/payments/adyen');
|
2020-01-01 14:27:42 +10:00
|
|
|
const instore = require('./routes/payments/instore');
|
2018-01-07 04:55:48 +10:00
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
2019-11-06 18:31:25 +10:00
|
|
|
// Language initialize
|
|
|
|
i18n.configure({
|
|
|
|
locales: config.availableLanguages,
|
|
|
|
defaultLocale: config.defaultLocale,
|
|
|
|
cookie: 'locale',
|
|
|
|
queryParameter: 'lang',
|
|
|
|
directory: `${__dirname}/locales`,
|
|
|
|
directoryPermissions: '755',
|
|
|
|
api: {
|
|
|
|
__: '__', // now req.__ becomes req.__
|
|
|
|
__n: '__n' // and req.__n can be called as req.__n
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-01-07 04:55:48 +10:00
|
|
|
// view engine setup
|
|
|
|
app.set('views', path.join(__dirname, '/views'));
|
2018-02-12 05:47:26 +10:00
|
|
|
app.engine('hbs', handlebars({
|
|
|
|
extname: 'hbs',
|
|
|
|
layoutsDir: path.join(__dirname, 'views', 'layouts'),
|
|
|
|
defaultLayout: 'layout.hbs',
|
2019-07-12 18:06:34 +10:00
|
|
|
partialsDir: [path.join(__dirname, 'views')]
|
2018-02-12 05:47:26 +10:00
|
|
|
}));
|
2018-01-07 04:55:48 +10:00
|
|
|
app.set('view engine', 'hbs');
|
|
|
|
|
|
|
|
// helpers for the handlebar templating platform
|
|
|
|
handlebars = handlebars.create({
|
|
|
|
helpers: {
|
2019-11-06 18:31:25 +10:00
|
|
|
// Language helper
|
2019-11-06 18:33:51 +10:00
|
|
|
__: () => { return i18n.__(this, arguments); }, // eslint-disable-line no-undef
|
|
|
|
__n: () => { return i18n.__n(this, arguments); }, // eslint-disable-line no-undef
|
2019-11-06 18:31:25 +10:00
|
|
|
availableLanguages: (block) => {
|
2019-11-06 18:33:51 +10:00
|
|
|
let total = '';
|
2019-11-06 18:31:25 +10:00
|
|
|
for(const lang of i18n.getLocales()){
|
|
|
|
total += block.fn(lang);
|
|
|
|
}
|
|
|
|
return total;
|
|
|
|
},
|
2019-06-17 20:21:45 +10:00
|
|
|
perRowClass: (numProducts) => {
|
2018-01-07 04:55:48 +10:00
|
|
|
if(parseInt(numProducts) === 1){
|
2019-12-28 15:43:20 +10:00
|
|
|
return'col-6 col-md-12 product-item';
|
2018-01-07 04:55:48 +10:00
|
|
|
}
|
|
|
|
if(parseInt(numProducts) === 2){
|
2019-12-28 15:43:20 +10:00
|
|
|
return'col-6 col-md-6 product-item';
|
2018-01-07 04:55:48 +10:00
|
|
|
}
|
|
|
|
if(parseInt(numProducts) === 3){
|
2019-12-28 15:43:20 +10:00
|
|
|
return'col-6 col-md-4 product-item';
|
2018-01-07 04:55:48 +10:00
|
|
|
}
|
|
|
|
if(parseInt(numProducts) === 4){
|
2019-12-28 15:43:20 +10:00
|
|
|
return'col-6 col-md-3 product-item';
|
2018-01-07 04:55:48 +10:00
|
|
|
}
|
|
|
|
|
2019-12-26 13:23:21 +10:00
|
|
|
return'col-md-6 product-item';
|
2018-02-14 06:21:45 +10:00
|
|
|
},
|
2019-06-17 20:21:45 +10:00
|
|
|
menuMatch: (title, search) => {
|
2018-02-17 23:30:28 +10:00
|
|
|
if(!title || !search){
|
|
|
|
return'';
|
|
|
|
}
|
|
|
|
if(title.toLowerCase().startsWith(search.toLowerCase())){
|
|
|
|
return'class="navActive"';
|
|
|
|
}
|
|
|
|
return'';
|
|
|
|
},
|
2019-06-17 20:21:45 +10:00
|
|
|
getTheme: (view) => {
|
2018-02-14 06:21:45 +10:00
|
|
|
return`themes/${config.theme}/${view}`;
|
2018-01-07 04:55:48 +10:00
|
|
|
},
|
2019-06-17 20:21:45 +10:00
|
|
|
formatAmount: (amt) => {
|
2018-01-07 04:55:48 +10:00
|
|
|
if(amt){
|
|
|
|
return numeral(amt).format('0.00');
|
|
|
|
}
|
|
|
|
return'0.00';
|
|
|
|
},
|
2019-06-17 20:21:45 +10:00
|
|
|
amountNoDecimal: (amt) => {
|
2018-01-07 04:55:48 +10:00
|
|
|
if(amt){
|
|
|
|
return handlebars.helpers.formatAmount(amt).replace('.', '');
|
|
|
|
}
|
|
|
|
return handlebars.helpers.formatAmount(amt);
|
|
|
|
},
|
2019-06-17 20:21:45 +10:00
|
|
|
getStatusColor: (status) => {
|
2018-01-07 04:55:48 +10:00
|
|
|
switch(status){
|
2019-11-06 18:31:25 +10:00
|
|
|
case'Paid':
|
|
|
|
return'success';
|
|
|
|
case'Approved':
|
|
|
|
return'success';
|
|
|
|
case'Approved - Processing':
|
|
|
|
return'success';
|
|
|
|
case'Failed':
|
|
|
|
return'danger';
|
|
|
|
case'Completed':
|
|
|
|
return'success';
|
|
|
|
case'Shipped':
|
|
|
|
return'success';
|
|
|
|
case'Pending':
|
|
|
|
return'warning';
|
|
|
|
default:
|
|
|
|
return'danger';
|
2018-01-07 04:55:48 +10:00
|
|
|
}
|
|
|
|
},
|
2019-06-17 20:21:45 +10:00
|
|
|
checkProductOptions: (opts) => {
|
2018-01-07 04:55:48 +10:00
|
|
|
if(opts){
|
|
|
|
return'true';
|
|
|
|
}
|
|
|
|
return'false';
|
|
|
|
},
|
2019-06-17 20:21:45 +10:00
|
|
|
currencySymbol: (value) => {
|
2018-01-07 04:55:48 +10:00
|
|
|
if(typeof value === 'undefined' || value === ''){
|
|
|
|
return'$';
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
},
|
2019-06-17 20:21:45 +10:00
|
|
|
objectLength: (obj) => {
|
2018-01-07 04:55:48 +10:00
|
|
|
if(obj){
|
|
|
|
return Object.keys(obj).length;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
},
|
2019-06-17 20:21:45 +10:00
|
|
|
stringify: (obj) => {
|
|
|
|
if(obj){
|
|
|
|
return JSON.stringify(obj);
|
|
|
|
}
|
|
|
|
return'';
|
|
|
|
},
|
|
|
|
checkedState: (state) => {
|
2018-01-07 04:55:48 +10:00
|
|
|
if(state === 'true' || state === true){
|
|
|
|
return'checked';
|
|
|
|
}
|
|
|
|
return'';
|
|
|
|
},
|
2019-06-17 20:21:45 +10:00
|
|
|
selectState: (state, value) => {
|
2018-01-07 04:55:48 +10:00
|
|
|
if(state === value){
|
|
|
|
return'selected';
|
|
|
|
}
|
|
|
|
return'';
|
|
|
|
},
|
2019-06-17 20:21:45 +10:00
|
|
|
isNull: (value, options) => {
|
2018-01-07 04:55:48 +10:00
|
|
|
if(typeof value === 'undefined' || value === ''){
|
|
|
|
return options.fn(this);
|
|
|
|
}
|
|
|
|
return options.inverse(this);
|
|
|
|
},
|
2019-06-17 20:21:45 +10:00
|
|
|
toLower: (value) => {
|
2018-02-12 05:47:26 +10:00
|
|
|
if(value){
|
|
|
|
return value.toLowerCase();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
2019-06-17 20:21:45 +10:00
|
|
|
formatDate: (date, format) => {
|
2018-01-07 04:55:48 +10:00
|
|
|
return moment(date).format(format);
|
|
|
|
},
|
2020-01-21 17:36:46 +10:00
|
|
|
discountExpiry: (start, end) => {
|
|
|
|
return moment().isBetween(moment(start), moment(end));
|
|
|
|
},
|
2019-06-17 20:21:45 +10:00
|
|
|
ifCond: (v1, operator, v2, options) => {
|
2018-01-07 04:55:48 +10:00
|
|
|
switch(operator){
|
2019-11-06 18:31:25 +10:00
|
|
|
case'==':
|
|
|
|
return(v1 === v2) ? options.fn(this) : options.inverse(this);
|
|
|
|
case'!=':
|
|
|
|
return(v1 !== v2) ? options.fn(this) : options.inverse(this);
|
|
|
|
case'===':
|
|
|
|
return(v1 === v2) ? options.fn(this) : options.inverse(this);
|
|
|
|
case'<':
|
|
|
|
return(v1 < v2) ? options.fn(this) : options.inverse(this);
|
|
|
|
case'<=':
|
|
|
|
return(v1 <= v2) ? options.fn(this) : options.inverse(this);
|
|
|
|
case'>':
|
|
|
|
return(v1 > v2) ? options.fn(this) : options.inverse(this);
|
|
|
|
case'>=':
|
|
|
|
return(v1 >= v2) ? options.fn(this) : options.inverse(this);
|
|
|
|
case'&&':
|
|
|
|
return(v1 && v2) ? options.fn(this) : options.inverse(this);
|
|
|
|
case'||':
|
|
|
|
return(v1 || v2) ? options.fn(this) : options.inverse(this);
|
|
|
|
default:
|
|
|
|
return options.inverse(this);
|
2018-01-07 04:55:48 +10:00
|
|
|
}
|
|
|
|
},
|
2019-06-17 20:21:45 +10:00
|
|
|
isAnAdmin: (value, options) => {
|
2018-02-04 00:08:51 +10:00
|
|
|
if(value === 'true' || value === true){
|
2018-01-07 04:55:48 +10:00
|
|
|
return options.fn(this);
|
|
|
|
}
|
|
|
|
return options.inverse(this);
|
2020-01-01 14:27:42 +10:00
|
|
|
},
|
|
|
|
paymentMessage: (status) => {
|
|
|
|
if(status === 'Paid'){
|
|
|
|
return'<h2 class="text-success">Your payment has been successfully processed</h2>';
|
|
|
|
}
|
|
|
|
if(status === 'Pending'){
|
|
|
|
const paymentConfig = common.getPaymentConfig();
|
|
|
|
if(config.paymentGateway === 'instore'){
|
|
|
|
return`<h2 class="text-warning">${paymentConfig.resultMessage}</h2>`;
|
|
|
|
}
|
|
|
|
return'<h2 class="text-warning">The payment for this order is pending. We will be in contact shortly.</h2>';
|
|
|
|
}
|
|
|
|
return'<h2 class="text-success">Your payment has failed. Please try again or contact us.</h2>';
|
|
|
|
},
|
|
|
|
paymentOutcome: (status) => {
|
|
|
|
if(status === 'Paid' || status === 'Pending'){
|
2020-01-03 22:21:20 +10:00
|
|
|
return'<h5 class="text-warning">Please retain the details above as a reference of payment</h5>';
|
2020-01-01 14:27:42 +10:00
|
|
|
}
|
|
|
|
return'';
|
2020-01-02 13:37:53 +10:00
|
|
|
},
|
|
|
|
upperFirst: (value) => {
|
2020-01-07 20:40:22 +10:00
|
|
|
if(value){
|
|
|
|
return value.replace(/^\w/, (chr) => {
|
|
|
|
return chr.toUpperCase();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return value;
|
2020-01-03 22:29:00 +10:00
|
|
|
},
|
|
|
|
math: (lvalue, operator, rvalue, options) => {
|
|
|
|
lvalue = parseFloat(lvalue);
|
|
|
|
rvalue = parseFloat(rvalue);
|
|
|
|
|
|
|
|
return{
|
|
|
|
'+': lvalue + rvalue,
|
|
|
|
'-': lvalue - rvalue,
|
|
|
|
'*': lvalue * rvalue,
|
|
|
|
'/': lvalue / rvalue,
|
|
|
|
'%': lvalue % rvalue
|
|
|
|
}[operator];
|
2020-01-04 12:37:35 +10:00
|
|
|
},
|
|
|
|
showCartButtons: (cart) => {
|
|
|
|
if(!cart){
|
|
|
|
return'd-none';
|
|
|
|
}
|
|
|
|
return'';
|
2020-01-23 20:58:21 +10:00
|
|
|
},
|
|
|
|
snip: (text) => {
|
2020-01-28 16:31:28 +10:00
|
|
|
if(text && text.length > 155){
|
2020-01-23 20:58:21 +10:00
|
|
|
return text.substring(0, 155) + '...';
|
|
|
|
}
|
|
|
|
return text;
|
2018-01-07 04:55:48 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-01-15 07:11:22 +10:00
|
|
|
// session store
|
2019-07-12 18:06:34 +10:00
|
|
|
const store = new MongoStore({
|
2020-01-21 17:36:46 +10:00
|
|
|
uri: getDbUri(config.databaseConnectionString),
|
2018-01-07 04:55:48 +10:00
|
|
|
collection: 'sessions'
|
|
|
|
});
|
|
|
|
|
2019-10-29 19:00:47 +10:00
|
|
|
// Setup secrets
|
|
|
|
if(!config.secretCookie || config.secretCookie === ''){
|
|
|
|
const randomString = crypto.randomBytes(20).toString('hex');
|
|
|
|
config.secretCookie = randomString;
|
|
|
|
common.updateConfigLocal({ secretCookie: randomString });
|
|
|
|
}
|
|
|
|
if(!config.secretSession || config.secretSession === ''){
|
|
|
|
const randomString = crypto.randomBytes(20).toString('hex');
|
|
|
|
config.secretSession = randomString;
|
|
|
|
common.updateConfigLocal({ secretSession: randomString });
|
|
|
|
}
|
|
|
|
|
2018-01-07 04:55:48 +10:00
|
|
|
app.enable('trust proxy');
|
|
|
|
app.use(helmet());
|
2020-01-13 18:01:11 +10:00
|
|
|
app.set('port', process.env.PORT || 1111);
|
2018-01-07 04:55:48 +10:00
|
|
|
app.use(logger('dev'));
|
2019-06-15 14:46:08 +10:00
|
|
|
app.use(bodyParser.urlencoded({ extended: false }));
|
2019-10-29 19:00:47 +10:00
|
|
|
app.use(cookieParser(config.secretCookie));
|
2018-01-07 04:55:48 +10:00
|
|
|
app.use(session({
|
|
|
|
resave: true,
|
|
|
|
saveUninitialized: true,
|
2019-10-29 19:00:47 +10:00
|
|
|
secret: config.secretSession,
|
2018-01-07 04:55:48 +10:00
|
|
|
cookie: {
|
|
|
|
path: '/',
|
|
|
|
httpOnly: true,
|
2019-06-01 22:22:14 +10:00
|
|
|
maxAge: 900000
|
2018-01-07 04:55:48 +10:00
|
|
|
},
|
|
|
|
store: store
|
|
|
|
}));
|
|
|
|
|
2019-11-07 19:43:38 +10:00
|
|
|
app.use(bodyParser.json({
|
|
|
|
// Only on Stripe URL's which need the rawBody
|
|
|
|
verify: (req, res, buf) => {
|
|
|
|
if(req.originalUrl === '/stripe/subscription_update'){
|
|
|
|
req.rawBody = buf.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2019-11-06 18:31:25 +10:00
|
|
|
// Set locales from session
|
|
|
|
app.use(i18n.init);
|
|
|
|
|
2018-01-07 04:55:48 +10:00
|
|
|
// serving static content
|
|
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
2018-02-12 05:47:26 +10:00
|
|
|
app.use(express.static(path.join(__dirname, 'views', 'themes')));
|
2018-01-07 04:55:48 +10:00
|
|
|
|
|
|
|
// Make stuff accessible to our router
|
|
|
|
app.use((req, res, next) => {
|
|
|
|
req.handlebars = handlebars;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2018-05-21 23:36:12 +10:00
|
|
|
// Ran on all routes
|
|
|
|
app.use((req, res, next) => {
|
|
|
|
res.setHeader('Cache-Control', 'no-cache, no-store');
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2018-01-07 04:55:48 +10:00
|
|
|
// setup the routes
|
|
|
|
app.use('/', index);
|
2018-02-03 23:26:09 +10:00
|
|
|
app.use('/', customer);
|
2018-02-05 23:20:53 +10:00
|
|
|
app.use('/', product);
|
|
|
|
app.use('/', order);
|
|
|
|
app.use('/', user);
|
2018-02-06 05:51:04 +10:00
|
|
|
app.use('/', admin);
|
2018-01-07 04:55:48 +10:00
|
|
|
app.use('/paypal', paypal);
|
|
|
|
app.use('/stripe', stripe);
|
2018-02-05 07:39:42 +10:00
|
|
|
app.use('/authorizenet', authorizenet);
|
2019-11-11 17:47:48 +10:00
|
|
|
app.use('/adyen', adyen);
|
2020-01-01 14:27:42 +10:00
|
|
|
app.use('/instore', instore);
|
2018-01-07 04:55:48 +10:00
|
|
|
|
|
|
|
// catch 404 and forward to error handler
|
|
|
|
app.use((req, res, next) => {
|
2019-07-12 18:06:34 +10:00
|
|
|
const err = new Error('Not Found');
|
2018-01-07 04:55:48 +10:00
|
|
|
err.status = 404;
|
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
// error handlers
|
|
|
|
|
|
|
|
// development error handler
|
|
|
|
// will print stacktrace
|
|
|
|
if(app.get('env') === 'development'){
|
|
|
|
app.use((err, req, res, next) => {
|
|
|
|
console.error(colors.red(err.stack));
|
|
|
|
res.status(err.status || 500);
|
|
|
|
res.render('error', {
|
|
|
|
message: err.message,
|
|
|
|
error: err,
|
|
|
|
helpers: handlebars.helpers
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// production error handler
|
|
|
|
// no stacktraces leaked to user
|
|
|
|
app.use((err, req, res, next) => {
|
|
|
|
console.error(colors.red(err.stack));
|
|
|
|
res.status(err.status || 500);
|
|
|
|
res.render('error', {
|
|
|
|
message: err.message,
|
|
|
|
error: {},
|
|
|
|
helpers: handlebars.helpers
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-01-15 03:02:10 +10:00
|
|
|
// Nodejs version check
|
2018-02-23 05:16:55 +10:00
|
|
|
const nodeVersionMajor = parseInt(process.version.split('.')[0].replace('v', ''));
|
|
|
|
if(nodeVersionMajor < 7){
|
|
|
|
console.log(colors.red(`Please use Node.js version 7.x or above. Current version: ${nodeVersionMajor}`));
|
2018-01-15 03:02:10 +10:00
|
|
|
process.exit(2);
|
|
|
|
}
|
|
|
|
|
2018-01-07 04:55:48 +10:00
|
|
|
app.on('uncaughtException', (err) => {
|
|
|
|
console.error(colors.red(err.stack));
|
2018-01-15 03:02:10 +10:00
|
|
|
process.exit(2);
|
2018-01-07 04:55:48 +10:00
|
|
|
});
|
|
|
|
|
2019-12-16 17:28:37 +10:00
|
|
|
initDb(config.databaseConnectionString, async (err, db) => {
|
2018-01-07 04:55:48 +10:00
|
|
|
// On connection error we display then exit
|
|
|
|
if(err){
|
|
|
|
console.log(colors.red('Error connecting to MongoDB: ' + err));
|
2018-01-15 03:02:10 +10:00
|
|
|
process.exit(2);
|
2018-01-07 04:55:48 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// add db to app for routes
|
|
|
|
app.db = db;
|
2018-02-23 03:41:24 +10:00
|
|
|
app.config = config;
|
|
|
|
app.port = app.get('port');
|
2018-01-07 04:55:48 +10:00
|
|
|
|
2019-06-01 22:22:14 +10:00
|
|
|
// Fire up the cron job to clear temp held stock
|
|
|
|
cron.schedule('*/1 * * * *', async () => {
|
|
|
|
const validSessions = await db.sessions.find({}).toArray();
|
|
|
|
const validSessionIds = [];
|
|
|
|
_.forEach(validSessions, (value) => {
|
|
|
|
validSessionIds.push(value._id);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Remove any invalid cart holds
|
2019-10-29 18:26:30 +10:00
|
|
|
await db.cart.deleteMany({
|
2019-06-15 14:46:08 +10:00
|
|
|
sessionId: { $nin: validSessionIds }
|
2019-06-01 22:22:14 +10:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-06-09 22:49:35 +10:00
|
|
|
// Set trackStock for testing
|
|
|
|
if(process.env.NODE_ENV === 'test'){
|
|
|
|
config.trackStock = true;
|
|
|
|
}
|
|
|
|
|
2019-06-17 20:21:45 +10:00
|
|
|
// Process schemas
|
|
|
|
await addSchemas();
|
|
|
|
|
2019-06-11 20:03:42 +10:00
|
|
|
// We index when not in test env
|
|
|
|
if(process.env.NODE_ENV !== 'test'){
|
|
|
|
try{
|
2019-06-15 14:46:08 +10:00
|
|
|
await runIndexing(app);
|
2019-06-11 20:03:42 +10:00
|
|
|
}catch(ex){
|
|
|
|
console.error(colors.red('Error setting up indexes:' + err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the app
|
|
|
|
try{
|
|
|
|
await app.listen(app.get('port'));
|
2018-02-23 03:41:24 +10:00
|
|
|
app.emit('appStarted');
|
2019-11-09 10:27:20 +10:00
|
|
|
if(process.env.NODE_ENV !== 'test'){
|
|
|
|
console.log(colors.green('expressCart running on host: http://localhost:' + app.get('port')));
|
|
|
|
}
|
2019-06-11 20:03:42 +10:00
|
|
|
}catch(ex){
|
|
|
|
console.error(colors.red('Error starting expressCart app:' + err));
|
2018-01-15 03:02:10 +10:00
|
|
|
process.exit(2);
|
2019-06-11 20:03:42 +10:00
|
|
|
}
|
2018-01-07 04:55:48 +10:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = app;
|