Skip to content

Commit

Permalink
added caching to product list valid for one day
Browse files Browse the repository at this point in the history
  • Loading branch information
schaechinger committed Mar 25, 2018
1 parent 6094210 commit e290a92
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 26 deletions.
15 changes: 10 additions & 5 deletions lib/Internetmarke.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,25 +137,30 @@ class Internetmarke {
* @param {Object} data
* @param {Client} data.client - The credentials of the client registered for
* the product service (prod ws).
* @returns {Promise.<boolean>}
*/
enableProductList({ client }) {
if (!this._productList) {
this._productList = new ProductList({ client });

return this._productList.init();
}
else {
return Promise.resolve(true);
}
}

/**
* Retrieves the whole list of products available at the product service.
*
* @param {boolean} forceLoad - Determine whether the product list should be
* updated first.
* @returns {Promise.<Product[]>}
*/
getProductList() {
getProductList(forceLoad = false) {
this._checkProductList();

return this._productList.loadProducts()
.then(success => {
return this._productList._products;
});
return Promise.resolve(this._productList._products);
}

/**
Expand Down
77 changes: 56 additions & 21 deletions lib/ProductList/ProductList.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

const Client = require('./Client'),
Product = require('./Product'),
ProductService = require('../Service/Soap/Product');
ProductService = require('../Service/Soap/Product'),
Temp = require('../helper/Temp');

const UPDATE_INTERVAL = 86400000;

class ProductList {
/**
Expand All @@ -18,19 +21,16 @@ class ProductList {
* @constructor
* @param {Object} config
* @param {Client} config.client - The product service client object.
* @param {boolean} config.forceLoad - Indicates whether the product list
* should be updated immediately.
*/
constructor({ client, forceLoad = false }) {
constructor({ client }) {
/** @type {ProductService} */
this._productService = new ProductService({ client });

/** @type {Product[]} */
this._products = [];

if (forceLoad) {
this.loadProducts();
}
/** @type {(Date|null)} */
this._lastUpdate = null;
/** @type {Temp} */
this._temp = new Temp({ file: 'productlist.json' });
}

/**
Expand Down Expand Up @@ -62,27 +62,62 @@ class ProductList {
* Loads the list of products from the service to make it available to the
* internetmarke module.
*
* @param {boolean} forceLoad - Determine whether the product list should be
* forced to be updated from the web service.
* @returns {Promise.<boolean>}
*/
loadProducts() {
return this._productService.getProductList()
.then(products => {
if (!products) {
return false;
}
.then(data => {
this._temp.update(JSON.stringify(data));

products.salesProductList.SalesProduct.forEach(data => {
const product = new Product(data);
if (product.isValid()) {
this._products[product.getId()] = product;
}
});
return this._parseData(data);
});
}

init() {
return this._temp.get()
.then(data => {
data = data.toString();

// TODO: what about other prod types?
if (data) {
data = JSON.parse(data);
this._parseData(data);
}

return true;
if (!data || UPDATE_INTERVAL < new Date() - this._lastUpdate) {
return this.loadProducts();
}
else {
return true;
}
});
}

/**
* Runs through the product object and generates the product list.
*
* @param {Object} products - The raw data of the product list with meta
* information.
*/
_parseData(products) {
if (!products) {
return false;
}

this._lastUpdate = new Date(products.date);

products.salesProductList.SalesProduct.forEach(data => {
const product = new Product(data);
if (product.isValid()) {
this._products[product.getId()] = product;
}
});

// TODO: what about other prod types?

return true;
}
}

module.exports = ProductList;
2 changes: 2 additions & 0 deletions lib/helper/Temp.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class Temp {

if (file) {
this._file = path.join(this._tmpDir, file);

fs.closeSync(fs.openSync(this._file, 'a'));
}
}

Expand Down

0 comments on commit e290a92

Please sign in to comment.