diff --git a/src/main/datasets/datasetgroup-service.js b/src/main/datasets/datasetgroup-service.js index 35d2c12..ce8b704 100644 --- a/src/main/datasets/datasetgroup-service.js +++ b/src/main/datasets/datasetgroup-service.js @@ -2,6 +2,7 @@ function dataSetGroupService(d2Api, $q, periodService, Restangular, errorHandler var service = this; var dataSetGroups = {}; var dataSetGroupNames = []; + var datasetDisplayRules = []; this.getGroups = function () { return dataSetGroups; @@ -94,6 +95,10 @@ function dataSetGroupService(d2Api, $q, periodService, Restangular, errorHandler } }; + this.getDataSetDisplayRules = function () { + return datasetDisplayRules; + }; + // Configure the api endpoints we use d2Api.addEndPoint('systemSettings'); d2Api.addEndPoint('dataSets'); @@ -102,8 +107,11 @@ function dataSetGroupService(d2Api, $q, periodService, Restangular, errorHandler d2Api.addEndPoint('categoryCombos'); // Load the dataSetGroups that are available from system settings - d2Api.systemSettings.get('keyApprovalDataSetGroups') - .then(function (resultDataSetGroups) { + $q.all([d2Api.systemSettings.get('keyApprovalsDataSetDisplayRules'), d2Api.systemSettings.get('keyApprovalDataSetGroups')]) + .then(function (data) { + datasetDisplayRules = data[0]; + var resultDataSetGroups = data[1]; + if (!Array.isArray(resultDataSetGroups)) { return $q.reject('Dataset groups not defined in systemsettings (key: keyApprovalDataSetGroups), see the deployment manual on how to configure the app.'); } diff --git a/src/main/datasets/datasetview-directive.js b/src/main/datasets/datasetview-directive.js index 8af4543..0b304e0 100644 --- a/src/main/datasets/datasetview-directive.js +++ b/src/main/datasets/datasetview-directive.js @@ -1,5 +1,5 @@ /* global jQuery */ -function datasetViewDirective(AppManifest, $translate) { +function datasetViewDirective(AppManifest, $translate, dataSetGroupService, $log) { var dataSetReportWrapSelector = '.dataset-report-wrap'; function loadDataSetReport(details, ds, element, scope) { @@ -151,6 +151,73 @@ function datasetViewDirective(AppManifest, $translate) { return result; }); + function appliesToCurrentWorkflow(dataSets) { + return function (rulesDefinition) { + return dataSets.every(function (dataSet) { + return (new RegExp(rulesDefinition.workflow)).test(dataSet.name); + }); + }; + } + + function hasValidRulesDefinition(rulesDefinition) { + if (rulesDefinition && + rulesDefinition.workflow && + rulesDefinition.matchPeriodOn.test && + rulesDefinition.matchPeriodOn.comparator && + rulesDefinition.matchPeriodOn.value + ) { + return true; + } + return false; + } + + function buildRulesFromConfig(currentPeriod) { + return dataSetGroupService + .getDataSetDisplayRules() + .filter(hasValidRulesDefinition) + .filter(appliesToCurrentWorkflow(scope.details.dataSetsFilteredByMechanisms)) + // Check if rule applies to the current periodType + .filter(function (rulesDefinition) { + var periodTest = new RegExp(rulesDefinition.matchPeriodOn.test); + + return periodTest.test(currentPeriod); + }) + // Check if the rule applies to the current period + .filter(function (rulesDefinition) { + var fullYear = parseInt(currentPeriod.substring(0, 4), 10); + + switch (rulesDefinition.matchPeriodOn.comparator) { + case "gte": + return (fullYear >= rulesDefinition.matchPeriodOn.value) + case "lt": + return (fullYear < rulesDefinition.matchPeriodOn.value) + } + + return false; + }) + .map(function (ruleDefinition) { + return function (dataSet) { + // We only want dataSets that are in the list of dataSets from the ruleDefinition + return ruleDefinition.dataSets.indexOf(dataSet.id) >= 0; + }; + }) + } + + function applyDataSetDisplayRules(dataSets) { + var rules = buildRulesFromConfig(details.period); + + // When rules are available run them + if (rules.length) { + return rules.reduce(function (dataSets, ruleFn) { + return dataSets.filter(ruleFn); + }, dataSets); + } + + return dataSets; + } + + scope.details.dataSetsFilteredByMechanisms = applyDataSetDisplayRules(scope.details.dataSetsFilteredByMechanisms); + //Move this out jQuery(dataSetReportWrapSelector).html(''); diff --git a/src/main/manifest.webapp b/src/main/manifest.webapp index ee21dea..73f1be2 100644 --- a/src/main/manifest.webapp +++ b/src/main/manifest.webapp @@ -1 +1 @@ -{"version":"0.4.8","name":"Data Approval","description":"Approvals app for PEPFAR","icons":{"48":"img/icons/dataapproval.png"},"developer":{"url":"http://www.dhis2.org","name":"Mark Polak","company":"DHIS2 Core Team","email":"markpo@ifi.uio.no"},"launch_path":"index.html?v=0.4.8","default_locale":"en","activities":{"dhis":{"href":"*"}}} \ No newline at end of file +{"version":"0.4.9","name":"Data Approval","description":"Approvals app for PEPFAR","icons":{"48":"img/icons/dataapproval.png"},"developer":{"url":"http://www.dhis2.org","name":"Mark Polak","company":"DHIS2 Core Team","email":"markpo@ifi.uio.no"},"launch_path":"index.html?v=0.4.9","default_locale":"en","activities":{"dhis":{"href":"*"}}} \ No newline at end of file