OpenFn / unicef-cambodia

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

A duplicate case is created in Primero when I run the F2-J1 job twice #87

Closed daissatou2 closed 2 years ago

daissatou2 commented 2 years ago

Bug Report

Background, context, and business value

A duplicate case is created in Primero when I run the F2-J1 job twice. Run 1: https://www.openfn.org/projects/primero-oscar-cambodia-io-staging/runs/0619f6ef-3c8e-7d34-bf8b-ae2fc425262d Run 2: https://www.openfn.org/projects/primero-oscar-cambodia-io-staging/runs/0619f6ff-5835-7ae6-82d3-d104de132ea2

Primero case create 1: https://demo-kh.primero.org/v2/cases/5eeda33b-b7c7-4dab-9f8d-ee9b4af57d25 Primero case create 2: https://demo-kh.primero.org/v2/cases/3189bac6-34c8-4849-9084-22e34b657a3d"

state.json

Primero V2 Cambodia Demo API user Oscar Staging (api only user)

lakhassane commented 2 years ago

@daissatou2 released a new version.

That should solve the duplicate issue https://www.openfn.org/projects/primero-oscar-cambodia-io-staging/runs/0619f962-6f4f-78fe-af54-08843e5d05ad

aleksa-krolls commented 2 years ago

Thanks @lakhassane ... what was the issue?

@daissatou2 please re-test first thing on Monday.

lakhassane commented 2 years ago

@aleksa-krolls I think there were no case_id and it was set to undefined. But now I have doubt that it's corrected with that release. @daissatou2 could you ping me when testing so I can see it with you?

aleksa-krolls commented 2 years ago

@daissatou2 please coordinate with Mamadou this morning to test together.

@lakhassane if case_id is undefined, we shouldn't search by it... only then need to search by oscar_number.

daissatou2 commented 2 years ago

@lakhassane @aleksa-krolls still seeing the same issue: run 1: https://www.openfn.org/projects/primero-oscar-cambodia-io-staging/runs/061a4a58-bd07-7021-9794-2de9660fb6b9 run 2: https://www.openfn.org/projects/primero-oscar-cambodia-io-staging/runs/061a4a55-be47-7c1d-a2c9-e516b31329bc

image

daissatou2 commented 2 years ago

Hey @lakhassane it looks like this might still be an issue. The job is no longer creating duplicates for cases that were created in OSCaR and sent to Primero.

But it is creating a new case for the scenario where we update the global_id:

  1. I created a case in Primero and synced with OSCaR: https://www.openfn.org/projects/primero-oscar-cambodia-io-staging/runs/061a4cdd-8e86-7f57-8365-0b6cae10fdc2
  2. Then I ran F2-J2 to sync the global id for the Primero case from above. (Reminder that Flow 2 is syncing not only new OSCaR cases but also the global id for existing OSCaR cases sent by Primero). But instead of updating the existing Primero case with the OSCaR global_id, it is creating a new blank case with just the global_id field filled in: image

This is kind of confusing...let me know if a call helps!

aleksa-krolls commented 2 years ago

@daissatou2 I took a look here... and the OpenFn job was linked to the openfn_cp_user instead of the apiuser. I don't remember why... but I just changed the credential on the f2-j2 job to apiuser: https://www.openfn.org/projects/primero-oscar-cambodia-io-staging/jobs/jv79xn

Then, when I re-ran the run... it looks like it does find the existing case, so I wonder if this was a permissions thing: https://www.openfn.org/projects/primero-oscar-cambodia-io-staging/runs/061a4d6f-2e49-780f-a760-378a248f8d9a

Sorry to do this, but can you and @lakhassane please do a fresh test to see if this is indeed still an issue?

daissatou2 commented 2 years ago

@aleksa-krolls I am not seeing a difference in behavior. It's creating a new case instead of updating the original one. image primero->oscar : https://www.openfn.org/projects/primero-oscar-cambodia-io-staging/runs/061a4ea9-4cc8-7bd8-9f64-30d50c61ca62 oscar->primero: https://www.openfn.org/projects/primero-oscar-cambodia-io-staging/runs/061a4eba-ba27-7c21-9df2-dc1df435760f oscar_short_id: ardv-1858

It worked for you because it had already created a new case when I tested...

aleksa-krolls commented 2 years ago

@lakhassane Back over to you... please jump on a call with @daissatou2 as needed to test & resolve.

daissatou2 commented 2 years ago

@aleksa-krolls let's discuss with @lakhassane tomorrow morning

aleksa-krolls commented 2 years ago

@lakhassane @daissatou2 and I are able to look up existing cases using the following GET request...

GET https://demo-kh.primero.org/api/v2/cases?case_id=168bd287-227f-44f6-a4f7-13287c29845b

This returns results. However, if we look at this run log (L63), it looks like the upsertCase(...) helper does NOT find existing cases... why? What does the GET request query look like in our upsertCase function?

Data provided to Primero for upload `upsertCase`:  {
  "oscar_number": "01FNP38VDS7W06JKB150Y1195A",
  "case_id": "168bd287-227f-44f6-a4f7-13287c29845b",
  "oscar_short_id": "ardv-1858",
  "owned_by": "mgrpnh",
  "risk_level": "medium",
  "module_id": "primeromodule-cp"
}
No case found. Performing create.
Post succeeded: 200 OK

^ a case does exist with this case_id...

taylordowns2000 commented 2 years ago

@aleksa-krolls , @daissatou2 , just off the phone with @lakhassane . He has found that the case which already exists and shares the same case_id has a different (actually, has no) OSCaR Number.

It looks like we're being asked to match on oscar_number or case_id. @lakhassane does not think this is possible via the Primero API, so the suggestion is that we'd need to re-write this matching function to make multiple requests, each with its own exclusive filter.

We're going to explore this further right now, but if you've got extra context here please chime in!

lakhassane commented 2 years ago

@daissatou2 @aleksa-krolls I made a temporary fix now as per Aleksa's suggestion in slack to use oscar_number only if case_id does not exist in state. I am working on a more permanent solution that needs adaptor updates! Will keep you posted

aleksa-krolls commented 2 years ago

@lakhassane please message @daissatou2 when this is ready for re-testing.

lakhassane commented 2 years ago

@aleksa-krolls @daissatou2 it was ready for testing. Check last comment. Sorry if it wasn't clear :(