Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dhis2 update examples #617

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 29 additions & 16 deletions adaptors/library/jobs/DHIS2-DataValues-API.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
// ----
// Add data to data value sets in DHIS2 using a generic JSON message, submitted
// by Taylor Downs @ OpenFn.
// by Taylor Downs @ OpenFn. Co-authored by @mtuchi
// ---

dataValueSet(
fields(
field('dataSet', 'pBOMPrpg1QX'),
field('orgUnit', 'DiszpKrYNg8'),
field('period', '201401'),
field('completeData', dataValue('form.date')),
field('dataValues', function (state) {
return [
dataElement('qrur9Dvnyt5', dataValue('form.prop_a')(state)),
dataElement('oZg33kd9taw', dataValue('form.prop_b')(state)),
dataElement('msodh3rEMJa', dataValue('form.prop_c')(state)),
];
})
)
);
fn(state => {
const { form } = state.data;
// Create a data value set mapping
state.dataValueSetMap = {
dataSet: 'pBOMPrpg1QX',
completeDate: form.date,
period: '201401',
orgUnit: 'DiszpKrYNg8',
dataValues: [
{
dataElement: 'f7n9E0hX8qk',
value: form.prop_a,
},
{
dataElement: 'Ix2HsbDMLea',
value: form.prop_b,
},
{
dataElement: 'eY5ehpbEsB7',
value: form.prop_c,
},
],
};
return state;
});

// Create dataValueSets
create('dataValueSets', state => state.dataValueSetMap);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't best practice be more like:

create('dataValueSets', {
  dataSet: 'pBOMPrpg1QX',
  completeDate: $.form.date,
  period: '201401',
  orgUnit: 'DiszpKrYNg8',
  dataValues: [
    {
      dataElement: 'f7n9E0hX8qk',
      value: $.form.prop_a,
    },
    {
      dataElement: 'Ix2HsbDMLea',
      value: $.form.prop_b,
    },
    {
      dataElement: 'eY5ehpbEsB7',
      value: $.form.prop_c,
    },
  ],
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's clean and simple. I have updated the examples to use that pattern

55 changes: 34 additions & 21 deletions adaptors/library/jobs/DHIS2-Events-API.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
// ----
// Create new events in DHIS2 using a generic JSON message, submitted by
// Taylor Downs @ OpenFn for demonstration porpoises.
// Taylor Downs @ OpenFn, Co-authored by @mtuchi
// ---

event(
fields(
field('program', 'eBAyeGv0exc'),
field('orgUnit', 'DiszpKrYNg8'),
field('eventDate', dataValue('meta.date')),
field('status', 'COMPLETED'),
field('storedBy', 'admin'),
field('coordinate', {
latitude: '59.8',
longitude: '10.9',
}),
field('dataValues', function (state) {
return [
dataElement('qrur9Dvnyt5', dataValue('form.prop_a')(state)),
dataElement('oZg33kd9taw', dataValue('form.prop_b')(state)),
dataElement('msodh3rEMJa', dataValue('form.prop_c')(state)),
];
})
)
);
fn(state => {
const { meta, form } = state.data;

state.events = {
program: 'eBAyeGv0exc',
orgUnit: 'DiszpKrYNg8',
occurredAt: meta.date,
status: 'COMPLETED',
storedBy: 'admin',
geometry: {
type: 'POINT',
coordinates: [59.8, 10.9],
},
dataValues: [
{
dataElement: 'qrur9Dvnyt5',
value: form.prop_a,
},
{
dataElement: 'oZg33kd9taw',
value: form.prop_b,
},
{
dataElement: 'msodh3rEMJa',
value: form.prop_c,
},
],
};

return state;
});

create('events', state => state.events);
4 changes: 2 additions & 2 deletions adaptors/library/staticExamples.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@
{
"expressionPath": "jobs/DHIS2-DataValues-API",
"adaptor": "dhis2",
"name": "Add data values"
"name": "Create data values"
},
{
"expressionPath": "jobs/DHIS2-Events-API",
"adaptor": "dhis2",
"name": "Add events"
"name": "Create new events"
},
{
"expressionPath": "jobs/ODK-Create-Many-Records-Moving-In-And-Out-Of-Repeat-Blocks",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"generate-library": "docusaurus generate-library",
"generate-adaptors": "docusaurus generate-adaptors",
"start": "docusaurus generate-adaptors & docusaurus start",
"start:dev": "docusaurus generate-adaptors -m && docusaurus start",
"start:dev": "docusaurus generate-adaptors -m && docusaurus generate-library && docusaurus start",
"start-offline": "docusaurus start",
"build": "docusaurus build",
"swizzle": "docusaurus swizzle",
Expand Down Expand Up @@ -62,4 +62,4 @@
"cytoscape": "3.28.1"
},
"packageManager": "[email protected]"
}
}
Loading