Function to update DHIS2 organization units

Can I use the upsert function of the dhis2 adaptor to add/update organization unit metadata in a DHIS2 instance?

Thanks.

@dsurrao Technically speaking, yes you should be able to upsert but let me know how it goes if you experiment with this example and its implementation here:

upsert(
     'organisationUnits',
     {
        attributeId: 'some_attribute_id_from_dhis2' //hypothetically, I would use the `id` for attributes like `name`?,
        attributeValue: 'some_attribute_value_from_state'
    },
     state.data,
    { ou: 'some_org_unit_id' } //UPDATE: You may not need this param since you are upserting the `orgUnit` based on the `2nd` and `3rd` arguments. But you can send additional import params, if need be, see [here](https://docs.dhis2.org/en/develop/using-the-api/dhis-core-version-master/metadata.html#webapi_metadata_create_update).
  );

Or you can try and achieve the same with create with the help of api docs here and here. UPDATE: If you use create to upsert, then send CREATE_AND_UPDATE as the importStrategy . See docs.

2 Likes

Thanks @chaiwa, I ended up using the CSV metadata import API call with a direct HTTP request.

@dsurrao Great, that is a good hack! I, and many other users, would be curious to learn from your solution. Would you share the sample job/high level steps here or consider writing an OpenFn Operation that imports metadata as csv, for the dhis2 adaptor and raise a Pull Request?

@chaiwa my solution was to use the OpenFn http adaptor with a POST request to the DHIS2 server:

post(
    'api/metadata?classKey=ORGANISATION_UNIT', 
    {
        body: state => {
            return state.metadataCSV;
        },
        headers: {
            'Content-Type': 'application/csv'
        }
    }
);

where state.metadataCSV is a string with format:

name,uid,code,parent
"West province","uid12345678","WESTP","ImspTQPwCqd"
"East province",,"EASTP","ImspTQPwCqd"

uid is a string of exactly 11 alphanumeric characters that can be left blank, in which case DHIS2 will create a uid value for you.

2 Likes