channeladvisor-localdb
Version:
library that keeps the local inventory database updated
217 lines (216 loc) • 7.96 kB
JavaScript
// Generated by LiveScript 1.3.1
(function(){
var queries, async, debug, common, inspect, ref$, UpdatesUpdateInfo, CatalogUpdateInfo, UpdateStartInfo, UpdateStopInfo, UpdateDoneInfo, UpdateProgressInfo, ItemUpdateInfo, ErrorInfo;
queries = require('../queries');
async = require('async');
debug = require('debug');
common = require('./common');
inspect = require('util').inspect;
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;
module.exports = function(comment, dateToFetchTo, force){
var d, dateToFetchFrom, currentPage, startDate, this$ = this;
force == null && (force = false);
d = debug("CALDB:updater:catalog");
d("start");
dateToFetchFrom = new Date("2000");
currentPage = 1;
startDate = new Date;
if (comment) {
comment = comment + " || ";
} else {
comment = "";
}
this.resetStats();
d("RunLog.get-last-catalog-update-progress");
return this.models.RunLog.getLastCatalogUpdateProgress(function(err, runlog){
var continuing;
if (err) {
return this$.errout({
error: err,
message: "could not run database query, at catalog:RunLog.get-last-catalog-update-progress",
stage: "catalog:pre-run-checks"
});
}
if (!force && dateToFetchTo && runlog.length !== 0) {
currentPage = runlog[0].pageId;
d("changing current-page to " + currentPage);
}
if (force) {
d("using force");
comment = comment + "Forceful update per request. Existing data will be truncated.";
}
continuing = false;
if (!dateToFetchTo) {
dateToFetchTo = new Date;
} else {
d("continuing catalog, " + dateToFetchTo);
continuing = true;
comment = comment + ("Continuing catalog update last run on " + dateToFetchTo.toGMTString() + " from page " + currentPage + ".");
}
return async.waterfall([
function(next){
if (!force) {
return next();
}
d("truncating tables");
return async.waterfall([
function(nxt){
return this$.unpromise(this$.db.query("SET FOREIGN_KEY_CHECKS = 0"), nxt);
}, function(_, nxt){
return this$.unpromise(this$.models.InventoryItemPrice.truncate(), nxt);
}, function(_, nxt){
return this$.unpromise(this$.models.InventoryItemQuantity.truncate(), nxt);
}, function(_, nxt){
return this$.unpromise(this$.models.InventoryItemAttribute.truncate(), nxt);
}, function(_, nxt){
return this$.unpromise(this$.models.InventoryItem.truncate(), nxt);
}, function(_, nxt){
return this$.unpromise(this$.models.RunLog.truncate(), nxt);
}, function(_, nxt){
return this$.unpromise(this$.db.query("SET FOREIGN_KEY_CHECKS = 1"), nxt);
}
], function(err){
if (err) {
return this$.errout({
error: err,
message: "could not run database query, at catalog:InventoryItem.truncate cascade: true",
stage: "catalog:truncate-inventory"
});
}
return next();
});
}, function(next){
if (!force) {
return next();
}
d("resetting catalog");
return this$.unpromise(this$.models.RunLog.create({
updater: 'catalog',
event: 'reset',
date: startDate,
comment: "Forceful reset of catalog requested."
}), function(err){
if (err) {
return this$.errout({
error: err,
message: "could not run database query, at catalog:RunLog.create",
stage: "catalog:insert-force-log"
});
}
return next();
});
}, function(next){
if (continuing) {
return next();
}
d("updates checkpoint");
return this$.unpromise(this$.models.RunLog.create({
updater: 'updates',
event: 'checkpoint',
date: startDate
}), function(err){
if (err) {
return this$.errout({
error: err,
message: "could not run database query, at catalog:RunLog.create",
stage: "catalog:update-checkpoint"
});
}
return next();
});
}, function(next){
d("insert catalog:start");
return this$.unpromise(this$.models.RunLog.create({
updater: 'catalog',
event: 'start',
date: startDate,
comment: comment
}), function(err){
if (err) {
return this$.errout({
error: err,
message: "could not run database query, at catalog:RunLog.create",
stage: "catalog:start-run"
});
}
return next();
});
}, function(next){
d("emit update-start");
this$.emit('update-start', new UpdateStartInfo({
type: 'catalog',
date: startDate,
info: new CatalogUpdateInfo({
dateFrom: dateToFetchFrom,
dateTo: dateToFetchTo,
page: currentPage
}),
comment: comment
}));
return next();
}, function(next){
var pages, page;
pages = function*(){
var conf;
for (;;) {
conf = {
accountID: this$.account,
itemCriteria: {
DateRangeField: 'CreateDate',
DateRangeStartGMT: dateToFetchFrom.toISOString(),
DateRangeEndGMT: dateToFetchTo.toISOString(),
PageNumber: currentPage++,
PageSize: 100
},
detailLevel: {
IncludeQuantityInfo: true,
IncludePriceInfo: true,
IncludeClassificationInfo: true
},
sortField: 'Sku',
sortDirection: 'Ascending'
};
yield conf;
}
};
page = pages();
d("initialized pages generator");
return next(null, page);
}, function(page, next){
var start, handleEnd;
start = common.get(this$);
handleEnd = function(err){
if (err !== "OKAY") {
d("err is NOT 'OKAY'");
return this$.emit('error', new ErrorInfo({
error: err,
message: "could not process/query items",
stage: "catalog:get-next-page",
fatal: true
}));
}
d("cleaning up; pushing catalog:done");
return this$.unpromise(this$.models.RunLog.create({
updater: 'catalog',
event: 'done'
}), function(err){
if (err) {
return this$.emit('error', new ErrorInfo({
error: err,
message: "could not push catalog:done",
stage: "catalog:cleanup",
fatal: true
}));
}
d("catalog-done");
return this$.catalogDone();
});
};
return start("catalog", page, currentPage, handleEnd);
}
], function(err){
throw err;
});
});
};
}).call(this);