channeladvisor-localdb
Version:
library that keeps the local inventory database updated
311 lines (310 loc) • 10.3 kB
JavaScript
// Generated by LiveScript 1.3.1
(function(){
var queries, async, debug, ref$, UpdatesUpdateInfo, CatalogUpdateInfo, UpdateStartInfo, UpdateStopInfo, UpdateDoneInfo, UpdateProgressInfo, ItemUpdateInfo, ErrorInfo, get, TIMER_ID, TIMER_THIS, restartTimer, stopTimer, startFetching, processIndividualItem, createItem, setItemAttributes, out$ = typeof exports != 'undefined' && exports || this;
queries = require('../queries');
async = require('async');
debug = require('debug');
ref$ = require('../info-objects/'), UpdatesUpdateInfo = ref$.UpdatesUpdateInfo, CatalogUpdateInfo = ref$.CatalogUpdateInfo, UpdateStartInfo = ref$.UpdateStartInfo, UpdateStopInfo = ref$.UpdateStopInfo, UpdateDoneInfo = ref$.UpdateDoneInfo, UpdateProgressInfo = ref$.UpdateProgressInfo, ItemUpdateInfo = ref$.ItemUpdateInfo, ErrorInfo = ref$.ErrorInfo;
out$.get = get = function(bindee){
return startFetching.bind(bindee);
};
restartTimer = function(){
var d, e, killCaldb;
d = debug("CALDB:common:timer");
if (TIMER_THIS._stop) {
d("NOOP: timer");
return;
}
try {
clearTimeout(TIMER_ID);
} catch (e$) {
e = e$;
}
TIMER_ID = setTimeout(killCaldb, 1000 * 60 * 10);
d("started or restarted timer");
return killCaldb = function(){
if (TIMER_THIS._stop) {
d("NOOP: timer timeout");
return;
}
d("TIMEOUT: No request has been received for a long time. Killing.");
TIMER_THIS.emit('error', new ErrorInfo({
fatal: true,
error: new Error("CALDB fetch timeout; no response for 10 minutes"),
message: "CALDB fetch timeout; no response for 10 minutes",
stage: "fetch"
}));
return TIMER_THIS.stop("CALDB fetch timeout");
};
};
stopTimer = function(){
var e;
try {
return clearTimeout(TIMER_ID);
} catch (e$) {
return e = e$;
}
};
startFetching = function(type, page, currentPage, callback){
var startDate, fetch, fetchNextPage, this$ = this;
startDate = new Date;
fetch = this.client.InventoryService.GetFilteredInventoryItemList;
TIMER_THIS = this;
fetchNextPage = function(callback){
var d;
restartTimer();
d = debug("CALDB:common:fetch-next-page");
if (this$._stop) {
d("STOP CALLED, QUITTING");
return;
}
d("fetching page " + currentPage);
d("inserting " + type + ":progress");
return this$.unpromise(this$.models.RunLog.create({
updater: type,
event: 'progress',
pageId: currentPage
}), function(err){
if (err) {
stopTimer();
return callback(err);
}
this$.emit('update-progress', new UpdateProgressInfo({
type: type,
date: new Date,
dateStarted: startDate,
comment: '',
currentPage: currentPage,
changed: this$.stats.changed,
deleted: this$.stats.deleted
}));
d("fetching next page");
return fetch(page.next().value, function(err, result){
var data, ref$, q, itemsLeftToFetch;
if (err) {
stopTimer();
return callback(err);
}
if (this$._stop) {
d("STOP CALLED, QUITTING @ PAGE LOAD");
return;
}
d("fetched page");
restartTimer();
result = result.GetFilteredInventoryItemListResult;
if (result.MessageCode !== 0) {
stopTimer();
return callback(new Error("MessageCode is not 0, but is " + result.MessageCode + " with message: " + result.Message));
}
data = (ref$ = result.ResultData || (result.ResultData = {})).InventoryItemResponse || (ref$.InventoryItemResponse = []);
if (data.length === 0) {
d("all items fetched");
stopTimer();
return callback("OKAY");
}
q = async.queue(function(item, done){
return processIndividualItem.call(this$, item, done);
}, 100);
q.drain = function(){
d("done processing items, fetching next page");
currentPage++;
return callback();
};
itemsLeftToFetch = data.length;
return q.push(data, function(err, item){
var d;
restartTimer();
if (this$._stop) {
d("STOP: NO-OP AT ITEM DONE CALLBACK");
return;
}
d = debug("CALDB:common:" + type + ":queue");
if (err) {
d("got error from an item during processing");
q.kill();
stopTimer();
return callback(err);
}
this$.stats.changed++;
d("item done fetching, " + --itemsLeftToFetch + " left");
return this$.emit('item-update', new ItemUpdateInfo({
type: type,
date: new Date
}));
});
});
});
};
return async.forever(fetchNextPage, callback);
};
processIndividualItem = function(itemData, callback){
var d, this$ = this;
d = debug("CALDB:common:process-individual-item");
if (this._stop) {
d("STOP: NOOP AT PROCESS-INDIVIDUAL-ITEM");
return;
}
d("inserting data");
return createItem.call(this, itemData, function(err, item){
if (err) {
return callback(err);
}
d("inserting attributes");
return setItemAttributes.call(this$, item, function(err, item){
if (err) {
return callback(err);
}
d("processed item");
return callback(null, item);
});
});
};
createItem = function(itemData, callback){
var d, quantityData, ref$, priceData, this$ = this;
d = debug("CALDB:common:create-item");
if (this._stop) {
d("STOP: NOOP AT CREATE-ITEM");
return;
}
quantityData = (ref$ = itemData.Quantity, delete itemData.Quantity, ref$);
priceData = (ref$ = itemData.PriceInfo, delete itemData.PriceInfo, ref$);
return this.unpromise.spread(this.models.InventoryItem.findOrCreate({
where: {
Sku: (ref$ = itemData.Sku, delete itemData.Sku, ref$)
}
}), function(err, item){
if (err) {
return callback(err);
}
item = item[0];
return this$.unpromise(item.update(itemData), function(err){
var createPrice, createQuantity;
if (err) {
return callback(err);
}
createPrice = function(callback){
if (this$._stop) {
d("STOP: NOOP AT CREATE-PRICE");
return;
}
return this$.unpromise(item.getPrice(), function(err, price){
if (err) {
return callback(err);
}
if (!price[0]) {
return this$.unpromise(item.createPrice(priceData), function(err){
return callback(err);
});
} else {
return this$.unpromise(price[0].update(priceData), function(err){
return callback(err);
});
}
});
};
createQuantity = function(callback){
if (this$._stop) {
d("STOP: NOOP AT CREATE-QUANTITY");
return;
}
return this$.unpromise(item.getQuantity(), function(err, quantity){
if (err) {
return callback(err);
}
if (!quantity[0]) {
return this$.unpromise(item.createQuantity(quantityData), function(err){
return callback(err);
});
} else {
return this$.unpromise(quantity[0].update(quantityData), function(err){
return callback(err);
});
}
});
};
return createPrice(function(err){
if (err) {
return callback;
}
return createQuantity(function(err){
if (err) {
return callback;
}
return callback(null, item);
});
});
});
});
};
setItemAttributes = function(item, callback){
var d, fetch, this$ = this;
d = debug("CALDB:common:set-item-attributes");
if (this._stop) {
d("STOP: NOOP AT SET-ITEM-ATTRIBUTES");
return;
}
d("making api call");
fetch = this.client.InventoryService.GetInventoryItemAttributeList;
restartTimer();
return fetch({
accountID: this.account,
sku: item.Sku
}, function(err, result){
var data, q;
if (err) {
return callback(err);
}
restartTimer();
d("got api response");
result = result.GetInventoryItemAttributeListResult;
if (result.MessageCode !== 0) {
return callback(new Error("MessageCode is not 0, but is " + result.MessageCode + " with message: " + result.Message));
}
data = result.ResultData.AttributeInfo;
q = async.queue(function(attr, done){
var _stop;
if (this$._stop) {
d("STOP: NOOP AT SET-ITEM-ATTRIBUTES.QUEUE");
return;
}
_stop = function(err){
q.kill();
return callback(err);
};
return this$.unpromise(item.getAttributes({
where: {
Name: attr.Name
}
}), function(err, attributes){
var attribute;
if (err) {
return _stop(err);
}
attributes = attributes[0];
if (attributes.length === 1) {
attribute = attributes[0];
return this$.unpromise(attribute.update(attr), function(err){
if (err) {
return _stop(err);
}
return done();
});
} else if (!attributes.length) {
return this$.unpromise(item.createAttribute(attr), function(err){
if (err) {
return _stop(err);
}
return done();
});
} else {
return _stop(new Error("Found more than one attribute for an item with the same name!"));
}
});
});
q.drain = function(){
return callback(null, item);
};
return q.push(data);
});
};
}).call(this);