2017-04-20 23:42:01 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-04-27 20:35:53 +00:00
|
|
|
const reports = require('../../lib/models/reports');
|
|
|
|
const reportTemplates = require('../../lib/models/report-templates');
|
|
|
|
const lists = require('../../lib/models/lists');
|
|
|
|
const subscriptions = require('../../lib/models/subscriptions');
|
|
|
|
const campaigns = require('../../lib/models/campaigns');
|
2017-04-25 22:49:31 +00:00
|
|
|
const handlebars = require('handlebars');
|
2017-04-27 20:35:53 +00:00
|
|
|
const handlebarsHelpers = require('../../lib/handlebars-helpers');
|
|
|
|
const _ = require('../../lib/translate')._;
|
2017-04-25 22:49:31 +00:00
|
|
|
const hbs = require('hbs');
|
|
|
|
const vm = require('vm');
|
|
|
|
const log = require('npmlog');
|
2017-04-20 23:42:01 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
|
2017-04-25 22:49:31 +00:00
|
|
|
handlebarsHelpers.registerHelpers(handlebars);
|
2017-04-20 23:42:01 +00:00
|
|
|
|
2017-04-25 22:49:31 +00:00
|
|
|
let reportId = Number(process.argv[2]);
|
|
|
|
let reportDir;
|
2017-04-20 23:42:01 +00:00
|
|
|
|
2017-04-25 22:49:31 +00:00
|
|
|
function resolveEntities(getter, ids, callback) {
|
|
|
|
const idsRemaining = ids.slice();
|
|
|
|
const resolved = [];
|
2017-04-20 23:42:01 +00:00
|
|
|
|
2017-04-25 22:49:31 +00:00
|
|
|
function doWork() {
|
|
|
|
if (idsRemaining.length == 0) {
|
|
|
|
return callback(null, resolved);
|
2017-04-20 23:42:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 22:49:31 +00:00
|
|
|
getter(idsRemaining.shift(), (err, entity) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2017-04-20 23:42:01 +00:00
|
|
|
|
2017-04-25 22:49:31 +00:00
|
|
|
resolved.push(entity);
|
|
|
|
return doWork();
|
|
|
|
});
|
|
|
|
}
|
2017-04-20 23:42:01 +00:00
|
|
|
|
2017-04-25 22:49:31 +00:00
|
|
|
setImmediate(doWork);
|
|
|
|
}
|
2017-04-20 23:42:01 +00:00
|
|
|
|
2017-04-25 22:49:31 +00:00
|
|
|
const userFieldTypeToGetter = {
|
|
|
|
'campaign': (id, callback) => campaigns.get(id, false, callback),
|
|
|
|
'list': lists.get
|
|
|
|
};
|
2017-04-20 23:42:01 +00:00
|
|
|
|
2017-04-25 22:49:31 +00:00
|
|
|
function resolveUserFields(userFields, params, callback) {
|
|
|
|
const userFieldsRemaining = userFields.slice();
|
|
|
|
const resolved = {};
|
2017-04-20 23:42:01 +00:00
|
|
|
|
2017-04-25 22:49:31 +00:00
|
|
|
function doWork() {
|
|
|
|
if (userFieldsRemaining.length == 0) {
|
|
|
|
return callback(null, resolved);
|
|
|
|
}
|
2017-04-20 23:42:01 +00:00
|
|
|
|
2017-04-25 22:49:31 +00:00
|
|
|
const spec = userFieldsRemaining.shift();
|
|
|
|
const getter = userFieldTypeToGetter[spec.type];
|
2017-04-20 23:42:01 +00:00
|
|
|
|
2017-04-25 22:49:31 +00:00
|
|
|
if (getter) {
|
|
|
|
return resolveEntities(getter, params[spec.id], (err, entities) => {
|
|
|
|
if (spec.minOccurences == 1 && spec.maxOccurences == 1) {
|
|
|
|
resolved[spec.id] = entities[0];
|
2017-04-20 23:42:01 +00:00
|
|
|
} else {
|
2017-04-25 22:49:31 +00:00
|
|
|
resolved[spec.id] = entities;
|
2017-04-20 23:42:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 22:49:31 +00:00
|
|
|
doWork();
|
2017-04-20 23:42:01 +00:00
|
|
|
});
|
2017-04-25 22:49:31 +00:00
|
|
|
} else {
|
|
|
|
return callback(new Error(_('Unknown user field type "' + spec.type + '".')));
|
2017-04-20 23:42:01 +00:00
|
|
|
}
|
2017-04-25 22:49:31 +00:00
|
|
|
}
|
2017-04-20 23:42:01 +00:00
|
|
|
|
2017-04-25 22:49:31 +00:00
|
|
|
setImmediate(doWork);
|
|
|
|
}
|
2017-04-20 23:42:01 +00:00
|
|
|
|
2017-04-25 22:49:31 +00:00
|
|
|
function doneSuccess() {
|
2017-04-27 20:35:53 +00:00
|
|
|
process.exit(0);
|
2017-04-25 22:49:31 +00:00
|
|
|
}
|
2017-04-20 23:42:01 +00:00
|
|
|
|
2017-04-25 22:49:31 +00:00
|
|
|
function doneFail() {
|
2017-04-27 20:35:53 +00:00
|
|
|
process.exit(1)
|
2017-04-25 22:49:31 +00:00
|
|
|
}
|
2017-04-20 23:42:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-25 22:49:31 +00:00
|
|
|
reports.get(reportId, (err, report) => {
|
|
|
|
if (err || !report) {
|
|
|
|
log.error('reports', err && err.message || err || _('Could not find report with specified ID'));
|
|
|
|
doneFail();
|
2017-04-20 23:42:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 22:49:31 +00:00
|
|
|
reportTemplates.get(report.reportTemplate, (err, reportTemplate) => {
|
2017-04-20 23:42:01 +00:00
|
|
|
if (err) {
|
2017-04-25 22:49:31 +00:00
|
|
|
log.error('reports', err && err.message || err || _('Could not find report template'));
|
|
|
|
doneFail();
|
2017-04-20 23:42:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 22:49:31 +00:00
|
|
|
resolveUserFields(reportTemplate.userFieldsObject, report.paramsObject, (err, inputs) => {
|
|
|
|
if (err) {
|
|
|
|
log.error('reports', err.message || err);
|
|
|
|
doneFail();
|
|
|
|
}
|
|
|
|
|
|
|
|
const campaignsProxy = {
|
|
|
|
results: reports.getCampaignResults,
|
|
|
|
list: campaigns.list,
|
|
|
|
get: campaigns.get
|
|
|
|
};
|
|
|
|
|
|
|
|
const subscriptionsProxy = {
|
|
|
|
list: subscriptions.list
|
|
|
|
};
|
2017-04-20 23:42:01 +00:00
|
|
|
|
2017-04-25 22:49:31 +00:00
|
|
|
const sandbox = {
|
|
|
|
console,
|
|
|
|
campaigns: campaignsProxy,
|
|
|
|
subscriptions: subscriptionsProxy,
|
|
|
|
inputs,
|
|
|
|
|
|
|
|
callback: (err, outputs) => {
|
2017-04-20 23:42:01 +00:00
|
|
|
if (err) {
|
2017-04-25 22:49:31 +00:00
|
|
|
log.error('reports', err.message || err);
|
|
|
|
doneFail();
|
2017-04-20 23:42:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 22:49:31 +00:00
|
|
|
const hbsTmpl = handlebars.compile(reportTemplate.hbs);
|
|
|
|
const reportText = hbsTmpl(outputs);
|
2017-04-20 23:42:01 +00:00
|
|
|
|
2017-04-27 20:35:53 +00:00
|
|
|
process.stdout.write(reportText);
|
|
|
|
doneSuccess();
|
2017-04-25 22:49:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const script = new vm.Script(reportTemplate.js);
|
2017-04-20 23:42:01 +00:00
|
|
|
|
2017-04-27 20:35:53 +00:00
|
|
|
try {
|
|
|
|
script.runInNewContext(sandbox, {displayErrors: true, timeout: 120000});
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
doneFail();
|
|
|
|
}
|
2017-04-25 22:49:31 +00:00
|
|
|
});
|
2017-04-20 23:42:01 +00:00
|
|
|
});
|
2017-04-25 22:49:31 +00:00
|
|
|
});
|
|
|
|
|