UNPKG

channeladvisor-localdb

Version:

library that keeps the local inventory database updated

207 lines (206 loc) 8.15 kB
// 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){ var d, fetch, this$ = this; d = debug("CALDB:updater:catalog"); d("start"); this.resetStats(); fetch = this.client.InventoryService.GetFilteredInventoryItemList; d("RunLog.get-last-updates-checkpoint"); return this.models.RunLog.getLastUpdatesCheckpoint(function(err, runlog){ var checkpoint; if (err) { return this$.errout({ error: err, message: "could not run database query, at updates:RunLog.get-last-updates-checkpoint", stage: "updates:pre-run-checks" }); } if (runlog.length === 0) { return this$.errout({ message: "no update checkpoints; this should never happen", stage: "updates:pre-run-checks" }); } checkpoint = runlog[0]; runlog = null; d("RunLog.get-last-incomplete-updates-start"); return this$.models.RunLog.getLastIncompleteUpdatesStart(function(err, runlog){ var dateToFetchTo; if (err) { return this$.errout({ error: err, message: "could not run database query, at updates:RunLog.get-last-incomplete-updates-start", stage: "updates:pre-run-checks" }); } if (runlog.length !== 0) { dateToFetchTo = runlog[0].date; } runlog = null; d("RunLog.get-last-incomplete-updates-progress"); return this$.models.RunLog.getLastIncompleteUpdatesProgress(function(err, runlog){ var continuing, currentPage, dateToFetchFrom, startDate; if (err) { return this$.errout({ error: err, message: "could not run database query, at updates:RunLog.get-last-incomplete-updates-progress", stage: "updates:pre-run-checks" }); } if (dateToFetchTo && runlog.length !== 0) { continuing = true; currentPage = runlog[0].pageId; } runlog = null; dateToFetchFrom = checkpoint.date; if (currentPage == null) { currentPage = 1; } if (comment) { comment = comment + " || "; } else { comment = ""; } continuing = false; if (dateToFetchTo == null) { dateToFetchTo = new Date; } else { d("continuing updates, " + dateToFetchTo); continuing = true; comment = comment + ("Continuing 'updates' update last run on " + dateToFetchTo.toGMTString() + " from page " + currentPage + "."); } startDate = new Date; return async.waterfall([ function(next){ d("insert updates:start"); return this$.unpromise(this$.models.RunLog.create({ updater: 'updates', event: 'start', date: startDate, comment: comment }), function(err){ if (err) { return this$.errout({ error: err, message: "could not run database query, at updates:RunLog.create", stage: "updates:start-run" }); } return setTimeout(next); }); }, function(next){ d("emit update-start"); this$.emit('update-start', new UpdateStartInfo({ type: 'updates', date: startDate, info: new UpdatesUpdateInfo({ dateFrom: dateToFetchFrom, dateTo: dateToFetchTo, page: currentPage }), comment: comment })); return setTimeout(next); }, function(next){ var pages, page; pages = function*(){ var conf; for (;;) { conf = { accountID: this$.account, itemCriteria: { DateRangeField: 'LastUpdateDate', 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: "updates:get-next-page", fatal: true })); } d("cleaning up; pushing updates:checkpoint and updates:done"); return this$.unpromise(this$.models.RunLog.create({ updater: 'updates', event: 'checkpoint' }), function(err){ if (err) { return this$.emit('error', new ErrorInfo({ error: err, message: "could not push updates:checkpoint", stage: "updates:cleanup", fatal: true })); } return this$.unpromise(this$.models.RunLog.create({ updater: 'updates', event: 'done' }), function(err){ if (err) { return this$.emit('error', new ErrorInfo({ error: err, message: "could not push updates:done", stage: "updates:cleanup", fatal: true })); } if (!continuing) { this$.emit('update-done', new UpdateDoneInfo({ type: 'updates', date: new Date, comment: comment, changed: this$.stats.changed, deleted: this$.stats.deleted })); } if (!continuing) { d("selecting 'updates' updater, 5 minute delay"); this$.updatesDone(); } if (continuing) { return this$.updatesDone(true); } }); }); }; return start("updates", page, currentPage, handleEnd); } ], function(err){ throw err; }); }); }); }); }; }).call(this);