Thank you @diana for the updates on targets in the new CHT versions during the round up. I decided to post some lingering questions on the forum, given the remaining time on that call.
- Are there any built in ways to quickly debug targets? Maybe through console, similar to how
window.CHTCore.debugFormModel()
is used for a form. At the moment we’ve created a “hidden” task to print out general info such as “user”, and use a print method that checks if acan_debug_targets
permission has been granted in order to output various pieces of info to the console.
Debug task:
{
id: 'debug',
icon: 'debug',
translation_key: 'targets.debug',
type: 'count',
goal: -1,
date: 'reported',
appliesTo: 'contacts',
appliesToType: ['indawo'], // For a CHW this will only fire once since it's the top-most place
context: 'false', // We don't want this to display a block
appliesIf: passthrough(
() => false, // Should never apply
['Target: ', '[General] debug info'],
['User:', () => user],
)
},
Print logic:
{
id: 'dwelling-registrations-weekly',
icon: 'wcg-dwelling',
translation_key: 'targets.dwelling.registration.weekly.count.title',
type: 'count',
goal: -1,
date: 'reported',
appliesTo: 'contacts',
appliesToType: ['dwelling'],
context: 'user.role === "chw" || user.contact_type === "chw"',
appliesIf: passthrough(
({ contact: { reported_date, visit_date } }) =>
visit_date ? isISOWithinWeek(visit_date) : isMilliWithinWeek(reported_date),
['Target: ', '[Weekly] Dwelling registration'],
['Reported date:', ({ contact: { reported_date } }) => reported_date],
['Visit date:', ({ contact: { visit_date } }) => visit_date]
)
},
...
const passthrough = (func, ...logArgs) => (...args) => {
if (hasDebugPermission()) {
for (const logArg of logArgs) {
print(...logArg.map(arg => (typeof arg === 'function' ? arg(...args) : arg)));
}
}
return func(...args);
};
// We're doing this because the 'cht' variable is only available in function target function calls, not globally
let _canDebugTargets;
function hasDebugPermission(){
// eslint-disable-next-line eqeqeq
if(_canDebugTargets == null){
// The current role is used by default
// https://docs.communityhealthtoolkit.org/building/targets/targets-js/#cht-api
_canDebugTargets = cht.v1.hasPermissions('can_debug_targets');
}
return _canDebugTargets;
}
function print(...args) {
console.log(...args);
}
-
Is there an appetite to have “weekly” targets? Speaking under correction, we still only have “monthly” and “all” - with the option to provide further fine tuning through the
appliesIf
. Just wondering if anyone else requires “weekly” as an easy config option, and if that’s something CHT plans to implement.
Related to this thread:
Displaying targets weekly - Technical Support - Community Health Toolkit -
Why does the “context” prop take a js string and not a function? In our case, most of our targets are, essentially, duplicated. Each would show up as a weekly and a monthly target for either a CHW or an OTL. What this means is that we define the same string in multiple places, where a function or variable could have been referenced - making it easy to change in a single place.
To add a bit more context, we recently had a user that for some reason had no ‘role’ property on their login linked hierarchy CHW, this resulted in NO target blocks showing up. Since we couldn’t replicate it locally, we had to add some sort of mechanism of enabling and obtaining debug info on our instance. After figuring out the issue, as a fallback, we added the `contact_type’ prop check on every context string.