Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for cht-conf v3.19.0 and above #244

Merged
merged 5 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cht-conf-test-harness",
"version": "3.0.12",
"version": "3.0.13",
"description": "Test Framework for CHT Projects",
"main": "./src/harness.js",
"scripts": {
Expand Down
30 changes: 26 additions & 4 deletions src/core-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

const md5 = require('md5');
const PouchDB = require('pouchdb');
const semver = require('semver');
const uuid = require('uuid/v4');

PouchDB.plugin(require('pouchdb-adapter-memory'));
Expand Down Expand Up @@ -82,7 +83,7 @@ const prepare = async (chtCore, rulesEngine, appSettings, pouchdb, pouchdbStateH
};

const prepareRulesEngine = async (chtCore, rulesEngine, appSettings, user, userRoles, sessionId) => {
const rulesSettings = getRulesSettings(appSettings, user, userRoles, sessionId, chtCore.ChtScriptApi);
const rulesSettings = getRulesSettings(chtCore, appSettings, user, userRoles, sessionId);

if (!rulesEngine.isEnabled()) {
await rulesEngine.initialize(rulesSettings);
Expand Down Expand Up @@ -197,13 +198,14 @@ const chtScriptApiWithDefaults = (chtScriptApi, settingsDoc, defaultUserRoles) =
};
};

const getRulesSettings = (settingsDoc, userContactDoc, userRoles, sessionId, chtScriptApi) => {
const getRulesSettings = (chtCore, settingsDoc, userContactDoc, userRoles, sessionId) => {
const settingsTasks = settingsDoc && settingsDoc.tasks || {};
// https://github.com/medic/cht-conf-test-harness/issues/106
// const filterTargetByContext = (target) => target.context ? !!this.parseProvider.parse(target.context)({ user: userContactDoc }) : true;
const targets = settingsTasks.targets && settingsTasks.targets.items || [];
const rules = getRules(chtCore.version, settingsTasks);
return {
rules: settingsTasks.rules,
...rules,
taskSchedules: settingsTasks.schedules,
targets: targets,
enableTasks: true,
Expand All @@ -215,8 +217,28 @@ const getRulesSettings = (settingsDoc, userContactDoc, userRoles, sessionId, cht
},
monthStartDate: getMonthStartDate(settingsDoc),
sessionId,
chtScriptApi: chtScriptApiWithDefaults(chtScriptApi, settingsDoc, userRoles),
chtScriptApi: chtScriptApiWithDefaults(chtCore.ChtScriptApi, settingsDoc, userRoles),
};
};

const getRules = (coreVersion, settingsTasks) => {
const addNoolsBoilerplateToCode = code => `define Target { _id: null, contact: null, deleted: null, type: null, pass: null, date: null, groupBy: null }
define Contact { contact: null, reports: null, tasks: null }
define Task {
_id: null, deleted: null, doc: null, contact: null, icon: null, date: null, readyStart: null, readyEnd: null,
title: null, fields: null, resolved: null, priority: null, priorityLabel: null, reports: null, actions: null
}
rule GenerateEvents {
when { c: Contact } then { ${code} }
}`;

// rules mutation added in cht-conf 3.19.0
const actualCoreVersion = semver.coerce(coreVersion);
const addNoolsBoilerplate = settingsTasks.isDeclarative && semver.lt(actualCoreVersion, '4.2.0-dev');
const rules = addNoolsBoilerplate ? addNoolsBoilerplateToCode(settingsTasks.rules) : settingsTasks.rules;
// do not set the isDeclarative flag when the code has nools boilerplate
const rulesAreDeclarative = addNoolsBoilerplate && !!settingsTasks.isDeclarative;
return { rules, rulesAreDeclarative };
};

module.exports = CoreAdapter;
15 changes: 15 additions & 0 deletions test/collateral/legacy-cht-conf/app_settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"permissions": {
"chw_permission": [
"chw_role"
]
},
"contact_summary": "var ContactSummary = {}; !function(e,t){if('object'==typeof exports&&'object'==typeof module)module.exports=t();else if('function'==typeof define&&define.amd)define([],t);else{var n=t();for(var r in n)('object'==typeof exports?exports:e)[r]=n[r]}}(ContactSummary,(function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){'undefined'!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:'Module'}),Object.defineProperty(e,'__esModule',{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&'object'==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,'default',{enumerable:!0,value:e}),2&t&&'string'!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,'a',t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p='',n(n.s=0)}([function(e,t){e.exports={context:{}}}])})); return ContactSummary;",
"tasks": {
"rules": "define Target { _id: null, contact: null, deleted: null, type: null, pass: null, date: null, groupBy: null }\ndefine Contact { contact: null, reports: null, tasks: null }\ndefine Task { _id: null, deleted: null, doc: null, contact: null, icon: null, date: null, readyStart: null, readyEnd: null, title: null, fields: null, resolved: null, priority: null, priorityLabel: null, reports: null, actions: null }\nrule GenerateEvents {\n when { c: Contact } then { !function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){'undefined'!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:'Module'}),Object.defineProperty(e,'__esModule',{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&'object'==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,'default',{enumerable:!0,value:e}),2&t&&'string'!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,'a',t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p='',n(n.s=1)}([function(e,t){e.exports=function(e){var t={};!function e(t,n){var r=Object.keys(t);for(var o in r){var a=r[o];switch(typeof t[a]){case'object':e(t[a],n);break;case'function':t[a]=t[a].bind(n)}}}(e,t),t.definition=function e(t){var n=Object.assign({},t),r=Object.keys(n);for(var o in r){var a=r[o];if(Array.isArray(n[a])){n[a]=n[a].slice(0);for(var i=0;i<n[a].length;++i)'object'==typeof n[a][i]&&(n[a][i]=e(n[a][i]))}else'object'==typeof n[a]&&(n[a]=e(n[a]))}return n}(e)}},function(e,t,n){var r=n(2),o=n(3),a=n(4);n(6)(o,c,Utils,Target,emit),a(r,c,Utils,Task,emit),emit('_complete',{_id:!0})},function(e,t){e.exports=[]},function(e,t){e.exports=[]},function(e,t,n){var r=n(0),o=n(5);function a(e,t,n,r,o,a){var i;if(e.appliesToType){var c;if('contacts'===e.appliesTo){if(!o.contact)return;c='contact'===o.contact.type?o.contact.contact_type:o.contact.type}else{if(!a)return;c=a.form}if(-1===e.appliesToType.indexOf(c))return}if('scheduled_tasks'===e.appliesTo||!e.appliesIf||e.appliesIf(o,a))if('scheduled_tasks'===e.appliesTo){if(a&&e.appliesIf){if(!a.scheduled_tasks)return;for(i=0;i<a.scheduled_tasks.length;i++)e.appliesIf(o,a,i)&&d(i)}}else d();function s(e,t,n){var r;return(r='function'==typeof e.contactLabel?e.contactLabel(t,n):e.contactLabel)?{name:r}:t.contact}function d(i){var c,d,p,u,l=null;for(c=0;c<e.events.length;c++){if((d=e.events[c]).dueDate)l=d.dueDate(d,o,a,i);else if(a)l=void 0!==i?new Date(t.addDate(new Date(a.scheduled_tasks[i].due),d.days)):new Date(t.addDate(new Date(a.reported_date),d.days));else if(d.dueDate)l=d.dueDate(d,o);else{var y=o.contact&&o.contact.reported_date?new Date(o.contact.reported_date):new Date;l=new Date(t.addDate(y,d.days))}t.isTimely(l,d)&&(u={_id:(a?a._id:o.contact&&o.contact._id)+'~'+(d.id||c)+'~'+e.name,deleted:!(!(o.contact&&o.contact.deleted||a)||!a.deleted),doc:o,contact:s(e,o,a),icon:e.icon,date:l,readyStart:d.start||0,readyEnd:d.end||0,title:e.title,resolved:e.resolvedIf(o,a,d,l,i),actions:f(e.actions,d)},void 0!==i&&(u._id+='-'+i),'function'==typeof(p=e.priority)&&(p=p(o,a)),p&&(u.priority=p.level,u.priorityLabel=p.label),r('task',new n(u)))}}function f(t,n){return e.actions.map((function(e){return function(e,t){var n={source:'task',source_id:a?a._id:o.contact&&o.contact._id,contact:o.contact};e.modifyContent&&e.modifyContent(n,o,a,t);return{type:e.type||'report',form:e.form,label:e.label||'Follow up',content:n}}(e,n)}))}}e.exports=function(e,t,n,i,c){if(e)for(var s,d,f=0;f<e.length;++f)switch('function'!=typeof(s=Object.assign({},e[f],o)).resolvedIf&&(s.resolvedIf=function(e,t,r,o){return s.defaultResolvedIf(e,t,r,o,n)}),r(s),s.appliesTo){case'reports':case'scheduled_tasks':for(var p=0;p<t.reports.length;++p)d=t.reports[p],a(s,n,i,c,t,d);break;case'contacts':t.contact&&a(s,n,i,c,t);break;default:throw new Error('Unrecognised task.appliesTo: '+s.appliesTo)}}},function(e,t){e.exports={defaultResolvedIf:function(e,t,n,r,o){var a,i;o||(o=Utils);var c=function(e){var t;if(!e||!e.actions)return;return(t=e.actions.find((function(e){return!e.type||'report'===e.type})))&&t.form}(this.definition);if(!c)throw new Error('Could not find the default resolving form!');return a=0,a=t?Math.max(o.addDate(r,-n.start).getTime(),t.reported_date+1):o.addDate(r,-n.start).getTime(),i=o.addDate(r,n.end+1).getTime(),o.isFormSubmittedInWindow(e.reports,c,a,i)}}},function(e,t,n){var r=n(0);function o(e,t,n,r,o,a){var i=!!a;if(o.contact){var c='contact'===o.contact.type?o.contact.contact_type:o.contact.type,s=i?a.form:c;if(!(e.appliesToType&&e.appliesToType.indexOf(s)<0)&&(!e.appliesIf||e.appliesIf(o,a)))for(var d=i?a:o.contact,f=function(e,t,n){var r;return r='function'==typeof e.idType?e.idType(t,n):'report'===e.idType?n&&n._id:t.contact&&t.contact._id,Array.isArray(r)||(r=[r]),r}(e,o,a),p=!e.passesIf||!!e.passesIf(o,a),u=function(e,t,n,r){if('function'==typeof e.date)return e.date(n,r);if(void 0===e.date||'now'===e.date)return t.now().getTime();if('reported'===e.date)return r?r.reported_date:n.contact.reported_date;throw new Error('Unrecognised value for target.date: '+e.date)}(e,n,o,a),l=e.groupBy&&e.groupBy(o,a),y=0;y<f.length;++y){var v=new t({_id:f[y]+'~'+e.id,contact:o.contact,deleted:!!d.deleted,type:e.id,pass:p,groupBy:l,date:u});e.emitCustom?e.emitCustom(b,v,o,a):b(v)}}function b(e){r('target',e)}}e.exports=function(e,t,n,a,i){for(var c=0;c<e.length;++c){var s=e[c];switch(r(s),s.appliesTo){case'contacts':o(s,a,n,i,t);break;case'reports':for(var d=0;d<t.reports.length;++d){o(s,a,n,i,t,t.reports[d])}break;default:throw new Error('Unrecognised target.appliesTo: '+s.appliesTo)}}}}]); }\n}",
"targets": {
"enabled": true,
"items": []
}
}
}
3 changes: 3 additions & 0 deletions test/collateral/legacy-cht-conf/contact-summary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
context: {}
};
Loading
Loading