Reports halfway through.
Editing report parameters and execution/monitoring of reports is TBD.
This commit is contained in:
parent
d4cea46f07
commit
aba42d94ac
10 changed files with 493 additions and 0 deletions
60
models/reports.js
Normal file
60
models/reports.js
Normal file
|
@ -0,0 +1,60 @@
|
|||
'use strict';
|
||||
|
||||
const knex = require('../lib/knex');
|
||||
const hasher = require('node-object-hash')();
|
||||
const { enforce, filterObject } = require('../lib/helpers');
|
||||
const dtHelpers = require('../lib/dt-helpers');
|
||||
const interoperableErrors = require('../shared/interoperable-errors');
|
||||
|
||||
const allowedKeys = new Set(['name', 'description', 'mime_type', 'user_fields', 'js', 'hbs']);
|
||||
|
||||
function hash(ns) {
|
||||
return hasher.hash(filterObject(ns, allowedKeys));
|
||||
}
|
||||
|
||||
async function getById(templateId) {
|
||||
const template = await knex('report_templates').where('id', templateId).first();
|
||||
if (!template) {
|
||||
throw new interoperableErrors.NotFoundError();
|
||||
}
|
||||
|
||||
return template;
|
||||
}
|
||||
|
||||
async function listDTAjax(params) {
|
||||
return await dtHelpers.ajaxList(params, tx => tx('report_templates'), ['report_templates.id', 'report_templates.name', 'report_templates.description', 'report_templates.created']);
|
||||
}
|
||||
|
||||
async function create(template) {
|
||||
const templateId = await knex('report_templates').insert(filterObject(template, allowedKeys));
|
||||
return templateId;
|
||||
}
|
||||
|
||||
async function updateWithConsistencyCheck(template) {
|
||||
await knex.transaction(async tx => {
|
||||
const existingTemplate = await tx('report_templates').where('id', template.id).first();
|
||||
if (!template) {
|
||||
throw new interoperableErrors.NotFoundError();
|
||||
}
|
||||
|
||||
const existingNsHash = hash(existingTemplate);
|
||||
if (existingNsHash != template.originalHash) {
|
||||
throw new interoperableErrors.ChangedError();
|
||||
}
|
||||
|
||||
await tx('report_templates').where('id', template.id).update(filterObject(template, allowedKeys));
|
||||
});
|
||||
}
|
||||
|
||||
async function remove(templateId) {
|
||||
await knex('report_templates').where('id', templateId).del();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
hash,
|
||||
getById,
|
||||
listDTAjax,
|
||||
create,
|
||||
updateWithConsistencyCheck,
|
||||
remove
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue