Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #31 from afirlejczyk/feature/sync-reviews
Browse files Browse the repository at this point in the history
Review sync
  • Loading branch information
pkarw authored Aug 29, 2018
2 parents ad046f0 + cb62dd3 commit cce69d6
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ At this point synchronization works with following entities:
- Taxrules
- Attributes
- Product-to-categories
- Reviews (require custom module Divante/ReviewApi to work)

Categories and Product-to-categories links are additionaly stored in Redis cache for rapid-requests (for example from your WebAPI). Our other project [vue-storefront-api](https://github.com/DivanteLtd/vue-storefront-api) exposes this databse to be used in PWA/JS webapps.

Expand Down Expand Up @@ -74,6 +75,7 @@ node --harmony cli.js productcategories --partitions=1
node --harmony cli.js attributes --partitions=1 --removeNonExistient=true
node --harmony cli.js taxrule --partitions=1 --removeNonExistient=true
node --harmony cli.js products --partitions=1 --removeNonExistient=true
node --harmony cli.js reviews
```

Please note:
Expand Down
19 changes: 19 additions & 0 deletions doc/source_magento_review.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"id": 0,
"title": "string",
"detail": "string",
"nickname": "string",
"ratings": [{
"id": 0,
"review_id": 0,
"attribute_code": "string",
"value": 0
}],
"customer_id": null,
"review_entity": "product",
"review_type": 0,
"review_status": 0,
"created_at": "string",
"entity_pk_value": 0,
"stores": []
}
6 changes: 3 additions & 3 deletions src/adapters/magento/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let AbstractMagentoAdapter = require('./abstract');
const CacheKeys = require('./cache_keys');
const util = require('util');

class CategoryAdapter extends AbstractMagentoAdapter {
class AttributeAdapter extends AbstractMagentoAdapter {


getEntityType() {
Expand All @@ -20,7 +20,7 @@ class CategoryAdapter extends AbstractMagentoAdapter {
return this.api.attributes.list();
}

/** Regarding Magento2 api docs and reality we do have an exception here that items aren't listed straight in the response but under "items" key */
/** Regarding Magento2 api docs and reality we do have an exception here that items aren't listed straight in the response but under "items" key */
prepareItems(items) {

if(!items)
Expand Down Expand Up @@ -76,4 +76,4 @@ class CategoryAdapter extends AbstractMagentoAdapter {

}

module.exports = CategoryAdapter;
module.exports = AttributeAdapter;
5 changes: 3 additions & 2 deletions src/adapters/magento/magento2-rest-client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var taxRates = require('./lib/tax_rates');
var taxRules = require('./lib/tax_rules');
var stockItems = require('./lib/stock_items');
var productLinks = require('./lib/product_links');

var reviews = require('./lib/reviews');

const MAGENTO_API_VERSION = 'V1';

Expand All @@ -38,6 +38,7 @@ module.exports.Magento2Client = function (options) {
instance.customOptions = customOptions(client);
instance.bundleOptions = bundleOptions(client);
instance.productLinks = productLinks(client);

instance.reviews = reviews(client);

return instance;
}
27 changes: 27 additions & 0 deletions src/adapters/magento/magento2-rest-client/lib/reviews.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const util = require('util');

module.exports = function (restClient) {
var module = {};

module.getByProductSku = function (sku) {
const endpointUrl = util.format('/products/%s/review', encodeURIComponent(sku));
return restClient.get(endpointUrl);
};

module.list = function(searchCriteria) {
const query = 'searchCriteria=' + searchCriteria;
const endpointUrl = util.format('/reviews/?%s', query);
return restClient.get(endpointUrl);
};

module.create = function (reviewData) {
return restClient.post('/reviews', {review: reviewData})
}

module.delete = function (reviewId) {
var endpointUrl = util.format('/reviews/%d', reviewId);
return restClient.delete(endpointUrl);
}

return module;
};
87 changes: 87 additions & 0 deletions src/adapters/magento/review.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
'use strict';

let AbstractMagentoAdapter = require('./abstract');
const CacheKeys = require('./cache_keys');
const util = require('util');

class ReviewAdapter extends AbstractMagentoAdapter {
constructor(config) {
super(config);
this.use_paging = false;
}

getEntityType() {
return 'review';
}

getName() {
return 'adapters/magento/ReviewAdapter';
}

getSourceData(context) {
if (this.use_paging) {
return this.api.reviews.list('&searchCriteria[currentPage]=' + this.page + '&searchCriteria[pageSize]=' + this.page_size + (query ? '&' + query : '')).catch(function (err) {
throw new Error(err);
});
}

return this.api.reviews.list().catch(function (err) {
throw new Error(err);
});
}

/**
* Regarding Magento2 api docs and reality
* we do have an exception here that items aren't listed straight
* in the response but under "items" key */
prepareItems(items) {
if (!items) {
return items;
}

if (items.total_count) {
this.total_count = items.total_count;
}

if (this.use_paging) {
this.page_count = this.total_count / this.page_size;
}

if (items.items) {
items = items.items; // this is an exceptional behavior for Magento2 api for lists
}

return items;
}

isFederated() {
return false;
}

preProcessItem(item) {
logger.debug(item);
//
return new Promise((function (done, reject) {
if (item) {
item.product_id = item.entity_pk_value;

delete item.entity_pk_value;
delete item.ratings;

logger.debug(util.format('Review %s', item.id));
}

return done(item);
}).bind(this));
}

/**
* We're transorming the data structure of item to be compliant with Smile.fr Elastic Search Suite
* @param {object} item document to be updated in elastic search
*/
normalizeDocumentFormat(item) {
return item;
}
}

module.exports = ReviewAdapter;
36 changes: 31 additions & 5 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,35 @@ let factory = new AdapterFactory(config);
const jsonFile = require('jsonfile')
const INDEX_META_PATH = '.lastIndex.json'


// for partitioning purposes
let cluster = require('cluster')
let numCPUs = require('os').cpus().length;

let kue = require('kue');
let queue = kue.createQueue(Object.assign(config.kue, { redis: config.redis }));
let queue = kue.createQueue(Object.assign(config.kue, { redis: config.redis }));

function commandReviews(next, reject) {
let adapter = factory.getAdapter(cli.options.adapter, 'review');
let tsk = new Date().getTime();

adapter.cleanUp(tsk);

adapter.run({
transaction_key: tsk,
done_callback: () => {

if(cli.options.removeNonExistient){
adapter.cleanUp(tsk);
}

if(!next){
logger.info('Task done! Exiting in 30s ...');
setTimeout(process.exit, TIME_TO_EXIT); // let ES commit all changes made
} else next();
}
});
}

/**
* Re-index categories
Expand Down Expand Up @@ -222,12 +245,12 @@ function commandProducts(updatedAfter = null) {
if(cli.options.removeNonExistient){
logger.info('CleaningUp products!');
let adapter = factory.getAdapter(cli.options.adapter, 'product'); // to avoid multi threading mongo error
adapter.cleanUp(transaction_key);
adapter.cleanUp(transaction_key);
}

logger.info('Queue processed. Exiting!');
setTimeout(process.exit, TIME_TO_EXIT); // let ES commit all changes made

}
});
}, 2000);
Expand All @@ -241,7 +264,7 @@ function commandProducts(updatedAfter = null) {
let context = { updated_after: updatedAfter,
transaction_key: tsk,
done_callback: () => {

if(cli.options.removeNonExistient){
adapter.cleanUp(tsk);
}
Expand Down Expand Up @@ -358,6 +381,9 @@ cli.command('fullreindex', function () {
commandFullreindex();
})

cli.command('reviews', function() {
commandReviews();
});

/**
* Sync categories
Expand Down

0 comments on commit cce69d6

Please sign in to comment.