OpenFn / unicef-cambodia

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

Use different API endpoint for updating Oscar cases with no referrals #65

Closed aleksa-krolls closed 3 years ago

aleksa-krolls commented 3 years ago

Background, context, and business value

In Flow 2 (OSCaR cases --> Primero) we sync 2 types of cases...

  1. OSCaR cases with no referrals (this is simply to show the Primero case worker what's going on in the Oscar system)
  2. OSCaR cases with referrals to Primero

Supporting case type # 1 has been a more recent change. For these cases, when we run Flow 1 to sync Primero data back to --> Oscar, we only want to sync the Primero case_id (aka external_id in Oscar)... we do not want any other fields from Primero to sync back to Oscar for these cases to ensure no mistaken overwriting of data.

For this sync back to Oscar in Flow 1, currently we leverage the Oscar endpoint /organizations/clients/upsert/. However, we've learned that doesn't support true upsert operations and cannot be used to update cases originally registered in the Oscar system... it can only be leveraged to upsert referrals. If we try to update cases only (no referrals) via this endpoint, we will therefore create false referrals.

So to update cases only (with no referrals) that already exist in Oscar, we must leverage the Oscar endpoint /organizations/clients/update_links/ (see LP for Oscar API Docs swaggerhub).

The specific request, in as few words as possible

Update f1-j2-casesToOscar.js to determine which cases fetched from Primero (f1-j1) have services where "service_response_type": "referral_to_oscar". Then...

  1. If the cases do have services where "service_response_type": "referral_to_oscar" --> keep the flow as is, leveraging the Oscar endpoint POST '/organizations/clients/upsert/' to sync the full case payload received from Primero
  2. If the cases do NOT have such "referral_to_oscar" services --> then use the case case_id, case_id_display, and oscar_number to send a different request to PUT '/organizations/clients/update_links/'. The request body should look like the below snippet.
    [
    {
    "external_id": "string",
    "global_id": "string",
    "external_id_display": "string"
    }
    ]

state.json

For Oscar: See LP for Oscar Production Demo API user For Primero: See LP for UNICEF Primero Alpha API User Login

For state, use sample_data/getPrimeroOutput.json. This payload is the output of running f1-j1-getPrimeroCases.js and includes 3 cases:

  1. case 39b7d2e that is a case with no services and NO referrals
  2. case e22fae1 that is a case with services but NO referrals
  3. case 1913226 that is a case with 1 referral to Oscar

adaptor

language-http when accessing Oscar

See API docs: https://app.swaggerhub.com/apis/Ro51/OSCaRInterop/1.0.0#/client%20links/updateClientLinks

expression.js

f1-j2-casesToOscar.js

output.json

For state sample_data/getPrimeroOutput.json, the cases should trigger the following actions:

  1. case 39b7d2e that is a case with no services and NO referrals --> we access PUT '/organizations/clients/update_links/' to update the Oscar case with the Primero Ids
  2. case e22fae1 that is a case with services but NO referrals --> we access PUT '/organizations/clients/update_links/' to update the Oscar case with the Primero Ids
  3. case 1913226 that is a case with 1 referral to Oscar --> we follow the existing flow and POST '/organizations/clients/upsert/' to create this referral in Oscar
taylordowns2000 commented 3 years ago

@aleksa-krolls , see changes here: https://github.com/OpenFn/unicef-cambodia/commit/3dd3d6cb8b400a377c68dc26eaa4b57bd697c9b9

Please take a full look through that particular link; I tried to make it fairly comprehensible.

Right now, everything works except update_links at the end. We get a 500 from Oscar, despite matching the API docs provided for that endpoint. See Slack for details: https://openfn.slack.com/archives/G2LJCFFDW/p1605598940004500?thread_ts=1605594473.001000&cid=G2LJCFFDW

aleksa-krolls commented 3 years ago

@taylordowns2000 Resolved by adding the data attribute to the PUT request body. Thanks for the help, will keep you posted as we resume testing today.