OpenFn / unicef-cambodia

UNICEF Cambodia - Primero Interoperability
https://openfn.github.io/unicef-cambodia/
1 stars 2 forks source link

function to split 1 case with 2 services into 2 cases (with 1 service each) #121

Closed aleksa-krolls closed 1 year ago

aleksa-krolls commented 1 year ago

Request

Assuming an input state like this, where there is an array of cases... where 1 case has 2 services.

[ { case_id: 1, services_section: [ { s1 },  { s2 }]} ]

Can you please write a fn(...) block that takes this input and returns an output like this ... so that 1 case is never sent with 2 services, but rather a separate request will be sent for each service. (Meaning that the services should be split across 2 different instances, but the case data will be the same across both instances.)

[ { case_id: 1, services_section: [ { s1 }]} ]
[ { case_id: 1, services_section: [ { s2 }]} ]

Input: https://github.com/OpenFn/unicef-cambodia/blob/master/sample_data/primero_2referrals_input.json Desired output: https://github.com/OpenFn/unicef-cambodia/blob/master/sample_data/primero_2referrals_output.json

aleksa-krolls commented 1 year ago

From Slack thread: https://openfn.slack.com/archives/C017ELVRSM8/p1666007345359699?thread_ts=1665420539.586769&cid=C017ELVRSM8

Job assuming an input like this {organization: { casedata, services: [ {s1}, {s2}]}}

fn(state => {
  const { cases } = state;

  const transformedCases = [];

  cases.map(wrapper => {
    wrapper.organization.services_section.map(s => {
      transformedCases.push({
        ...wrapper,
        organization: { ...wrapper.organization, services_section: [s] },
      });
    });
  });

  return { ...state, cases: transformedCases };
});

this returns this output:

{
  "references": [],
  "data": null,
  "cases": [
    {
      "organization": {
        "case_id": 1,
        "services_section": [
          {
            "s": 1,
            "name": "some person"
          }
        ]
      }
    },
    {
      "organization": {
        "case_id": 1,
        "services_section": [
          {
            "s": 2,
            "name": "someone else"
          }
        ]
      }
    }
  ]
}