OpenFn/unicef-cambodia

f2-j2: upload to Primero fails because of age range validation error

aleksa-krolls opened this issue · 3 comments

Issue

In this "f2" flow, we get case data from OSCaR and upload to Primero. This recent run failed on prod because the calculated age was not within the range of 0 and 130: https://www.openfn.org/projects/pdngk6/runs/r89r3g3e

This could have been because we received a bad quality date value for date_of_birth or because the validation was only recently added.

function calcAge(str) {
var today = new Date();
var birthDate = new Date(str);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}

Please modify this age calculation logic to return 0 as a default value if age > 30 || age < 1 || date_of_birth === undefined.

adaptor

UPDATE: Note that this is Primero v1 so use language-primero@v1.2.0

expression

Flow job triggered by f2-j1...
https://github.com/OpenFn/unicef-cambodia/blob/master/jobs/f2-j2-upsertCasesToPrimero.js

state

Please be careful to only use these credentials for testing...
f2-j1 to get data: Oscar Staging API user
f2-j2 to upload to primero: Primero Alpha Cambodia - Staging API user

See here for f2-j1 state ... this is sample output.json from f2-j1 that you can use as state for f2-j2.
https://github.com/OpenFn/unicef-cambodia/blob/master/sample_data/sample-f2-j1-output.json
NOTE that this state includes 3 cases: 1 with valid date_of_birth, 1 with very old out of range dob, and 1 with null dob.

@lakhassane please prioritize this today, and lmk if any questions!

@aleksa-krolls I added the conditions to return 0 on dob.

@lakhassane looks like it's working now, thanks!