Discrepancy between target count and server count (couchDB)

On CHT 5.1.0 (on Samsung Galaxy Tab A9+), a CHWs target shows services-this-month = 35 for June (screenshot
attached).

But when I count the actual report docs backing that count, I get 24 in both places:

  • Live iteration of her report docs in the same PouchDB (db.allDocs(), filtered by form + reported_date, same filter the target uses): 24
  • Same reports on the server (CouchDB medic, via _find and reports_by_form_and_parent): 24

Total docs in CouchDB medic (June 2026, contact._id = ): 24

By form:
infant_child 9
health_assessment 6
pregnancy 4
referral_follow_up 3
ntd 1
family_planning 1

Target shows 35, but neither the tablet nor the server has more than 24 actual report docs to back it.

Would appreciate any guidance on where to look.

Thanks.

Hi @iesmail

Would you mind sharing the code for this target, so we have an idea of what it’s counting exactly?

Hi @diana, here it is:

{
	id: 'services-last-month',
	translation_key: 'targets.all_services.title',
	subtitle_translation_key: 'targets.last_month.subtitle',
	type: 'count',
	icon: 'icon-people-person-visits',
	goal: -1,
	appliesTo: 'reports',
	context: 'user.parent.type === "health_center"',
	idType: 'report',
	appliesIf: function (c, r) {
		return (extras.isShortModule(r) || extras.isLongModule(r)) && extras.hasConsentbeenGiven(r) && extras.isInLastMonth(r.reported_date);
	},
	visible: false,
	date: 'now',
	aggregate: false, // for the moment set to false until we decide how to use the aggregate functionality,
},

Thanks @iesmail

date: "now" is used for counting continuous (all-time) targets, not monthly targets, so I see where this can yield incorrect results. I see that you added extras.isInLastMonth(r.reported_date);, but that will be dependent on when the calculation is executed, and targets are not calculated all the time.

For a correct monthly target, remove the extras.isInLastMonth(r.reported_date) condition and change the date to use reported.
Can you please try enabling these changes and sharing the new target count?

Hi @diana, thanks for spotting this! Just to add, in our contact-summary, we are using the last month’s target doc count to compute and show last month’s chw pyment on the chw profile. Please see this function:

Hence, will your suggested change affect this logic?

isCHWPayforPerformanceLastmonth(targetDoc, contact) {
if (
contact.type === "person" && (contact.role === "chw" || contact.role === "chv") &&
!!targetDoc
) {
//console.log("TargetDoc:", targetDoc); // Debugging line to check targetDoc
const target_in_array = targetDoc.targets;
let payforservices = 0;
let payformeetings = 0;


  // Services: Find the number of short services
  const short_services_details = target_in_array.find(
    (u) => u.id === "short-services-last-month"
  );
  if (short_services_details !== undefined) {
    const short_services_number = short_services_details.value.pass;
    payforservices += short_services_number*CHW_P4P.shortService;
  }

  // Services: Find the number of long services
  const long_services_details = target_in_array.find(
    (u) => u.id === "long-services-last-month"
  );
  if (long_services_details !== undefined) {
    const long_services_number = long_services_details.value.pass;
    payforservices += long_services_number*CHW_P4P.longService;
  }

  // Meetings: Check whether CHW attended Supervisor meeting
  const supervisory_meeting_details = target_in_array.find(
    (u) => u.id === "supervisory-meeting-last-month"
  );
  if (supervisory_meeting_details !== undefined) {
    const supervisory_meeting_number = supervisory_meeting_details.value.pass;
    
    if(supervisory_meeting_number > 0){
      payformeetings += CHW_P4P.supervisorMeeting
    }
  }

  // Meetings: Check whether CHW organised community outreach meetings/services
  const community_meeting_details = target_in_array.find(
    (u) => u.id === "community-meeting-last-month"
  );
  if (community_meeting_details !== undefined) {
    const community_meeting_number = community_meeting_details.value.pass;
    
    if(community_meeting_number > 0){
      payformeetings += CHW_P4P.communityOutreachMeeting
    }
  }

  return [
    payforservices,
    payformeetings,
  ];
}
return [0, 0]; // Return 0 for both values if conditions are not met},

I;m not sure I understand the question @iesmail . the changes should guarantee that the correct reports are counted towards your target, regardless of when the target is calculated.