Reports halfway through

Datatable now correctly handles the situation when user is not logged in and access protected resources
This commit is contained in:
Tomas Bures 2017-07-09 23:16:47 +02:00
parent aba42d94ac
commit 3f7b428546
28 changed files with 421 additions and 471 deletions

View file

@ -6,55 +6,93 @@ 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']);
const allowedKeys = new Set(['name', 'description', 'report_template', 'params']);
function hash(ns) {
return hasher.hash(filterObject(ns, allowedKeys));
const ReportState = {
SCHEDULED: 0,
PROCESSING: 1,
FINISHED: 2,
FAILED: 3,
MAX: 4
};
function hash(entity) {
return hasher.hash(filterObject(entity, allowedKeys));
}
async function getById(templateId) {
const template = await knex('report_templates').where('id', templateId).first();
if (!template) {
async function getById(id) {
const entity = await knex('reports').where('id', id).first();
if (!entity) {
throw new interoperableErrors.NotFoundError();
}
return template;
return entity;
}
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']);
return await dtHelpers.ajaxList(params, tx => tx('reports').innerJoin('report_templates', 'reports.report_template', 'report_templates.id'), ['reports.id', 'reports.name', 'report_templates.name', 'reports.description', 'reports.last_run', 'reports.state']);
}
async function create(template) {
const templateId = await knex('report_templates').insert(filterObject(template, allowedKeys));
return templateId;
}
async function updateWithConsistencyCheck(template) {
async function create(entity) {
await knex.transaction(async tx => {
const existingTemplate = await tx('report_templates').where('id', template.id).first();
if (!template) {
throw new interoperableErrors.NotFoundError();
const id = await tx('reports').insert(filterObject(entity, allowedKeys));
if (!await tx('report_templates').select(['id']).where('id', entity.report_template).first()) {
throw new interoperableErrors.DependencyNotFoundError();
}
const existingNsHash = hash(existingTemplate);
if (existingNsHash != template.originalHash) {
throw new interoperableErrors.ChangedError();
}
await tx('report_templates').where('id', template.id).update(filterObject(template, allowedKeys));
return id;
});
}
async function remove(templateId) {
await knex('report_templates').where('id', templateId).del();
async function updateWithConsistencyCheck(entity) {
await knex.transaction(async tx => {
const existing = await tx('reports').where('id', entity.id).first();
if (!entity) {
throw new interoperableErrors.NotFoundError();
}
const existingHash = hash(existing);
if (existingHash != entity.originalHash) {
throw new interoperableErrors.ChangedError();
}
if (!await tx('report_templates').select(['id']).where('id', entity.report_template).first()) {
throw new interoperableErrors.DependencyNotFoundError();
}
await tx('reports').where('id', entity.id).update(filterObject(entity, allowedKeys));
});
}
async function remove(id) {
await knex('reports').where('id', id).del();
}
async function updateFields(id, fields) {
return await knex('reports').where('id', id).update(fields);
}
async function listByState(state, limit) {
return await knex('reports').where('state', state).limit(limit);
}
async function bulkChangeState(oldState, newState) {
return await knex('reports').where('state', oldState).update('state', newState);
}
module.exports = {
ReportState,
hash,
getById,
listDTAjax,
create,
updateWithConsistencyCheck,
remove
remove,
updateFields,
listByState,
bulkChangeState
};