OpenFn/lwala

Only insert deceased Person if deaths >0

Closed this issue · 5 comments

//New logic to insert child Person records if person is marked as deceased in HH form
each(
merge(
dataPath("$.form.household_deaths.deaths[*]"),
fields(
field("caseId", dataValue("form.case.@case_id")),
field("catchment", dataValue("form.catchment")),
field("Date", dataValue("form.Date"))
)
),
upsert("Person__c", "CommCare_ID__c", fields(
field("CommCare_ID__c", (state) => {
var age = dataValue("age_dead")(state)
return `${state.data.caseId}${age}`;
}),
field("CommCare_HH_Code__c", dataValue("caseId")),
relationship("RecordType", "Name", (state) => {
var age = dataValue("age_dead")(state)
var gender = dataValue("gender_dead")(state)
var rt = '';
if (age < 5) {
rt = "Child";
} else if (age < 18) {
rt = "Youth";
//Youth
} else if (gender === "female") {
rt = "Female Adult";
} else {
rt = "Male Adult";
}
return rt;
}),
field("Name", "Deceased Person"),
field("Source__c", true),
relationship("Catchment__r", "Name", dataValue("catchment")),
field("Client_Status__c", "Deceased"),
field("Dead_age__c", dataValue("age_dead")),
field("Cause_of_Death__c", (state) => {
var cause = dataValue("cause_of_death_dead")(state);
return (cause !== undefined ? cause.toString().replace(/_/g, " ") : null);
}),
field("Verbal_autopsy__c", dataValue("verbal_autopsy")),
field("Client_Status__c", "Deceased"),
field("Active_in_Thrive_Thru_5__c", "No"),
field("Active_in_HAWI__c", "No"),
field("Active_TT5_Mother__c", "No"),
field("TT5_Mother_Registrant__c", "No"),
field("Date_of_Death__c", dataValue("Date")),
field("Inactive_Date__c", dataValue("Date"))
))
);

Need to add a line to only run this part of the job if deaths > 0 (where deaths is the array we defined in alterState). Can you help me out here? Have tried a few variations and can't seem to get it to work :-)

@taylordowns2000 I'm still struggling to getting upsertIf() to work here and spending way too much time. Can we walk through tomorrow?

I'm finding the upsert always executes even if the logical is false. I'm wondering if it has something to do with this upsertIf being nested under merge()...

If I try this expression but add in upsertIf (see one of many variations I tried below), and run it with this state.json (where "deaths_in_past_6_months" : "0")--> it still upserts a Person record when it should not because logical is false.

  upsertIf(state => {
      var deaths = state.data.form.household_deaths.deaths_in_past_6_months;
      return (deaths > 0 ? true : false);
    },
    "Person__c", "CommCare_ID__c", fields(


Resolved with TD suggested change. Added to new job to run in parallel with this job to support 2 live CommCare form versions: bd2f041