2019-06-15 14:46:08 +10:00
|
|
|
const colors = require('colors');
|
|
|
|
const lunr = require('lunr');
|
|
|
|
|
|
|
|
const indexProducts = (app) => {
|
|
|
|
// index all products in lunr on startup
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
app.db.products.find({}).toArray((err, productsList) => {
|
|
|
|
if(err){
|
|
|
|
console.error(colors.red(err.stack));
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup lunr indexing
|
|
|
|
const productsIndex = lunr(function(){
|
|
|
|
this.field('productTitle', { boost: 10 });
|
|
|
|
this.field('productTags', { boost: 5 });
|
|
|
|
this.field('productDescription');
|
|
|
|
|
|
|
|
const lunrIndex = this;
|
|
|
|
|
|
|
|
// add to lunr index
|
|
|
|
productsList.forEach((product) => {
|
2019-07-12 18:06:34 +10:00
|
|
|
const doc = {
|
|
|
|
productTitle: product.productTitle,
|
|
|
|
productTags: product.productTags,
|
|
|
|
productDescription: product.productDescription,
|
|
|
|
id: product._id
|
2019-06-15 14:46:08 +10:00
|
|
|
};
|
|
|
|
lunrIndex.add(doc);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.productsIndex = productsIndex;
|
2019-11-09 10:27:20 +10:00
|
|
|
if(process.env.NODE_ENV !== 'test'){
|
|
|
|
console.log(colors.cyan('- Product indexing complete'));
|
|
|
|
}
|
2019-06-15 14:46:08 +10:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const indexCustomers = (app) => {
|
|
|
|
// index all products in lunr on startup
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
app.db.customers.find({}).toArray((err, customerList) => {
|
|
|
|
if(err){
|
|
|
|
console.error(colors.red(err.stack));
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup lunr indexing
|
|
|
|
const customersIndex = lunr(function(){
|
|
|
|
this.field('email', { boost: 10 });
|
|
|
|
this.field('name', { boost: 5 });
|
|
|
|
this.field('phone');
|
|
|
|
|
|
|
|
const lunrIndex = this;
|
|
|
|
|
|
|
|
// add to lunr index
|
|
|
|
customerList.forEach((customer) => {
|
2019-07-12 18:06:34 +10:00
|
|
|
const doc = {
|
|
|
|
email: customer.email,
|
|
|
|
name: `${customer.firstName} ${customer.lastName}`,
|
|
|
|
phone: customer.phone,
|
|
|
|
id: customer._id
|
2019-06-15 14:46:08 +10:00
|
|
|
};
|
|
|
|
lunrIndex.add(doc);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.customersIndex = customersIndex;
|
2019-11-09 10:27:20 +10:00
|
|
|
if(process.env.NODE_ENV !== 'test'){
|
|
|
|
console.log(colors.cyan('- Customer indexing complete'));
|
|
|
|
}
|
2019-06-15 14:46:08 +10:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const indexOrders = (app, cb) => {
|
|
|
|
// index all orders in lunr on startup
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
app.db.orders.find({}).toArray((err, ordersList) => {
|
|
|
|
if(err){
|
|
|
|
console.error(colors.red('Error setting up products index: ' + err));
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup lunr indexing
|
|
|
|
const ordersIndex = lunr(function(){
|
|
|
|
this.field('orderEmail', { boost: 10 });
|
|
|
|
this.field('orderLastname', { boost: 5 });
|
|
|
|
this.field('orderPostcode');
|
|
|
|
|
|
|
|
const lunrIndex = this;
|
|
|
|
|
|
|
|
// add to lunr index
|
|
|
|
ordersList.forEach((order) => {
|
2019-07-12 18:06:34 +10:00
|
|
|
const doc = {
|
|
|
|
orderLastname: order.orderLastname,
|
|
|
|
orderEmail: order.orderEmail,
|
|
|
|
orderPostcode: order.orderPostcode,
|
|
|
|
id: order._id
|
2019-06-15 14:46:08 +10:00
|
|
|
};
|
|
|
|
lunrIndex.add(doc);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.ordersIndex = ordersIndex;
|
2019-11-09 10:27:20 +10:00
|
|
|
if(process.env.NODE_ENV !== 'test'){
|
|
|
|
console.log(colors.cyan('- Order indexing complete'));
|
|
|
|
}
|
2019-06-15 14:46:08 +10:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const fixProductDates = (products) => {
|
|
|
|
let index = 0;
|
|
|
|
products.forEach(() => {
|
|
|
|
products[index].productAddedDate = new Date();
|
|
|
|
index++;
|
|
|
|
});
|
|
|
|
return products;
|
|
|
|
};
|
|
|
|
|
|
|
|
// start indexing products and orders
|
|
|
|
const runIndexing = (app) => {
|
2019-11-09 10:27:20 +10:00
|
|
|
if(process.env.NODE_ENV !== 'test'){
|
|
|
|
console.info(colors.yellow('Setting up indexes..'));
|
|
|
|
}
|
2019-06-15 14:46:08 +10:00
|
|
|
|
|
|
|
return Promise.all([
|
|
|
|
indexProducts(app),
|
|
|
|
indexOrders(app),
|
|
|
|
indexCustomers(app)
|
|
|
|
])
|
|
|
|
.catch((err) => {
|
|
|
|
console.info(colors.yellow('Error setting up indexes', err));
|
|
|
|
process.exit(2);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
indexProducts,
|
|
|
|
indexCustomers,
|
|
|
|
indexOrders,
|
|
|
|
fixProductDates,
|
|
|
|
runIndexing
|
|
|
|
};
|