2017-07-09 20:38:57 +00:00
|
|
|
'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');
|
|
|
|
|
2017-07-09 21:16:47 +00:00
|
|
|
const allowedKeys = new Set(['name', 'description', 'report_template', 'params']);
|
2017-07-09 20:38:57 +00:00
|
|
|
|
2017-07-09 21:16:47 +00:00
|
|
|
const ReportState = {
|
|
|
|
SCHEDULED: 0,
|
|
|
|
PROCESSING: 1,
|
|
|
|
FINISHED: 2,
|
|
|
|
FAILED: 3,
|
|
|
|
MAX: 4
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function hash(entity) {
|
|
|
|
return hasher.hash(filterObject(entity, allowedKeys));
|
2017-07-09 20:38:57 +00:00
|
|
|
}
|
|
|
|
|
2017-07-09 21:16:47 +00:00
|
|
|
async function getById(id) {
|
|
|
|
const entity = await knex('reports').where('id', id).first();
|
|
|
|
if (!entity) {
|
2017-07-09 20:38:57 +00:00
|
|
|
throw new interoperableErrors.NotFoundError();
|
|
|
|
}
|
|
|
|
|
2017-07-09 21:16:47 +00:00
|
|
|
return entity;
|
2017-07-09 20:38:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function listDTAjax(params) {
|
2017-07-09 21:16:47 +00:00
|
|
|
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']);
|
2017-07-09 20:38:57 +00:00
|
|
|
}
|
|
|
|
|
2017-07-09 21:16:47 +00:00
|
|
|
async function create(entity) {
|
|
|
|
await knex.transaction(async tx => {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
});
|
2017-07-09 20:38:57 +00:00
|
|
|
}
|
|
|
|
|
2017-07-09 21:16:47 +00:00
|
|
|
async function updateWithConsistencyCheck(entity) {
|
2017-07-09 20:38:57 +00:00
|
|
|
await knex.transaction(async tx => {
|
2017-07-09 21:16:47 +00:00
|
|
|
const existing = await tx('reports').where('id', entity.id).first();
|
|
|
|
if (!entity) {
|
2017-07-09 20:38:57 +00:00
|
|
|
throw new interoperableErrors.NotFoundError();
|
|
|
|
}
|
|
|
|
|
2017-07-09 21:16:47 +00:00
|
|
|
const existingHash = hash(existing);
|
|
|
|
if (existingHash != entity.originalHash) {
|
2017-07-09 20:38:57 +00:00
|
|
|
throw new interoperableErrors.ChangedError();
|
|
|
|
}
|
|
|
|
|
2017-07-09 21:16:47 +00:00
|
|
|
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));
|
2017-07-09 20:38:57 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-09 21:16:47 +00:00
|
|
|
async function remove(id) {
|
|
|
|
await knex('reports').where('id', id).del();
|
2017-07-09 20:38:57 +00:00
|
|
|
}
|
|
|
|
|
2017-07-09 21:16:47 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-07-09 20:38:57 +00:00
|
|
|
module.exports = {
|
2017-07-09 21:16:47 +00:00
|
|
|
ReportState,
|
2017-07-09 20:38:57 +00:00
|
|
|
hash,
|
|
|
|
getById,
|
|
|
|
listDTAjax,
|
|
|
|
create,
|
|
|
|
updateWithConsistencyCheck,
|
2017-07-09 21:16:47 +00:00
|
|
|
remove,
|
|
|
|
updateFields,
|
|
|
|
listByState,
|
|
|
|
bulkChangeState
|
2017-07-09 20:38:57 +00:00
|
|
|
};
|