This is a preview of the Reports functionality.
It allows defining report templates and then create reports based on the templates. A template defines: - parameters - to be set in the report (currently only selection of campaigns, in the future to be extended to selection of lists/segments, and selection from pre-defined options) - data retrieval / processing code (in Javascript) - rendering template (in Handlebars) This main functionality is accompanied by a few minor tweaks here and there. Worth notice is the ability to use server-side ajax table s for multi-selection of campaigns. This is meant for reports that compare data across multiple campaigns. This could possibly be even used for some poor man's A/B testing. Note that the execution of custom JavaScript in the data retrieval / processing code and definition of custom Handlebars templates is a security issue. This should however be OK in the general case once proper user management with granular permissions is in. This is because definition of a report template is anyway such an expert task that it would normally be performed only by admin. Instantiation of reports based on report templates can be then done by any user because this should no longer be any security problem.
This commit is contained in:
parent
2afeb74e68
commit
6ba04d7ff4
31 changed files with 1737 additions and 13 deletions
161
lib/models/report-templates.js
Normal file
161
lib/models/report-templates.js
Normal file
|
@ -0,0 +1,161 @@
|
|||
'use strict';
|
||||
|
||||
const db = require('../db');
|
||||
const tableHelpers = require('../table-helpers');
|
||||
const tools = require('../tools');
|
||||
const _ = require('../translate')._;
|
||||
|
||||
const allowedKeys = ['name', 'description', 'mime_type', 'user_fields', 'js', 'hbs'];
|
||||
|
||||
module.exports.list = (start, limit, callback) => {
|
||||
tableHelpers.list('report_templates', ['*'], 'name', start, limit, callback);
|
||||
};
|
||||
|
||||
module.exports.quicklist = callback => {
|
||||
tableHelpers.quicklist('report_templates', ['id', 'name'], 'name', callback);
|
||||
};
|
||||
|
||||
module.exports.filter = (request, callback) => {
|
||||
tableHelpers.filter('report_templates', ['*'], request, ['#', 'name', 'description', 'created'], ['name'], 'created DESC', null, callback);
|
||||
};
|
||||
|
||||
module.exports.get = (id, callback) => {
|
||||
id = Number(id) || 0;
|
||||
|
||||
if (id < 1) {
|
||||
return callback(new Error(_('Missing report template ID')));
|
||||
}
|
||||
|
||||
db.getConnection((err, connection) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
connection.query('SELECT * FROM report_templates WHERE id=?', [id], (err, rows) => {
|
||||
connection.release();
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (!rows || !rows.length) {
|
||||
return callback(null, false);
|
||||
}
|
||||
|
||||
const template = tools.convertKeys(rows[0]);
|
||||
|
||||
const userFields = template.userFields.trim();
|
||||
if (userFields != '') {
|
||||
try {
|
||||
template.userFieldsObject = JSON.parse(userFields);
|
||||
} catch (err) {
|
||||
// This is to handle situation when for some reason we get corrupted JSON in the DB.
|
||||
template.userFieldsObject = {};
|
||||
template.userFields = "{}";
|
||||
}
|
||||
} else {
|
||||
template.userFieldsObject = {};
|
||||
}
|
||||
|
||||
return callback(null, template);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.createOrUpdate = (createMode, data, callback) => {
|
||||
data = data || {};
|
||||
|
||||
const id = 'id' in data ? Number(data.id) : 0;
|
||||
|
||||
if (!createMode && id < 1) {
|
||||
return callback(new Error(_('Missing report template ID')));
|
||||
}
|
||||
|
||||
const template = tools.convertKeys(data);
|
||||
const name = (template.name || '').toString().trim();
|
||||
|
||||
if (!name) {
|
||||
return callback(new Error(_('Report template name must be set')));
|
||||
}
|
||||
|
||||
const keys = ['name'];
|
||||
const values = [name];
|
||||
|
||||
Object.keys(template).forEach(key => {
|
||||
let value = typeof template[key] === 'number' ? template[key] : (template[key] || '').toString().trim();
|
||||
key = tools.toDbKey(key);
|
||||
|
||||
if (key === 'description') {
|
||||
value = tools.purifyHTML(value);
|
||||
}
|
||||
|
||||
if (key === 'user_fields') {
|
||||
value = value.trim();
|
||||
|
||||
if (value != '') {
|
||||
try {
|
||||
JSON.parse(value);
|
||||
} catch (err) {
|
||||
return callback(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (allowedKeys.indexOf(key) >= 0 && keys.indexOf(key) < 0) {
|
||||
keys.push(key);
|
||||
values.push(value);
|
||||
}
|
||||
});
|
||||
|
||||
db.getConnection((err, connection) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
let query;
|
||||
|
||||
if (createMode) {
|
||||
query = 'INSERT INTO report_templates (`' + keys.join('`, `') + '`) VALUES (' + values.map(() => '?').join(',') + ')';
|
||||
} else {
|
||||
query = 'UPDATE report_templates SET ' + keys.map(key => '`' + key + '`=?').join(', ') + ' WHERE id=? LIMIT 1';
|
||||
values.push(id);
|
||||
}
|
||||
|
||||
connection.query(query, values, (err, result) => {
|
||||
connection.release();
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (createMode) {
|
||||
return callback(null, result && result.insertId || false);
|
||||
} else {
|
||||
return callback(null, result && result.affectedRows || false)
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.delete = (id, callback) => {
|
||||
id = Number(id) || 0;
|
||||
|
||||
if (id < 1) {
|
||||
return callback(new Error(_('Missing report template ID')));
|
||||
}
|
||||
|
||||
db.getConnection((err, connection) => {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
connection.query('DELETE FROM report_templates WHERE id=? LIMIT 1', [id], (err, result) => {
|
||||
connection.release();
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
const affected = result && result.affectedRows || 0;
|
||||
return callback(err, affected);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue