Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Commit

Permalink
upsert TEI, prettierrc
Browse files Browse the repository at this point in the history
  • Loading branch information
taylordowns2000 committed Oct 30, 2020
1 parent f59bd86 commit 09da441
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 61 deletions.
7 changes: 7 additions & 0 deletions .prettierrc
@@ -0,0 +1,7 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"singleQuote": true,
"arrowParens": "avoid",
"printWidth": 80
}
50 changes: 42 additions & 8 deletions lib/Adaptor.js
Expand Up @@ -11,6 +11,7 @@ exports.dataValueSet = dataValueSet;
exports.dataElement = dataElement;
exports.createTEI = createTEI;
exports.updateTEI = updateTEI;
exports.upsertTEI = upsertTEI;
exports.enroll = enroll;
exports.fetchAnalytics = fetchAnalytics;
Object.defineProperty(exports, "field", {
Expand Down Expand Up @@ -440,6 +441,39 @@ function updateTEI(tei, data) {
});
});
};
}
/**
* Create or update one or many new Tracked Entity Instances
* @public
* @example
* upsertTEI(data)
* @constructor
* @param {object} data - Payload data for new tracked entity instance(s)
* @returns {Operation}
*/


function upsertTEI(data) {
return function (state) {
var body = (0, _languageCommon.expandReferences)(data)(state);
var _state$configuration8 = state.configuration,
username = _state$configuration8.username,
password = _state$configuration8.password,
apiUrl = _state$configuration8.apiUrl;
var url = (0, _url4.resolve)(apiUrl + '/', 'api/trackedEntityInstances?strategy=CREATE_AND_UPDATE');
console.log("Upserting tracked entity instance of type '".concat(body.trackedEntityType, "' to org unit '").concat(body.orgUnit, "' with ").concat(body.attributes && body.attributes.length, " attributes and ").concat(body.enrollments && body.enrollments.length, " enrollments."));
return (0, _Client.post)({
username: username,
password: password,
body: body,
url: url
}).then(function (result) {
console.log('Success:', result);
return _objectSpread({}, state, {
references: [result].concat(_toConsumableArray(state.references))
});
});
};
} // /**
// * Create and enroll TrackedEntityInstances
// * @example
Expand Down Expand Up @@ -482,10 +516,10 @@ function enroll(tei, enrollmentData) {
return function (state) {
var body = (0, _languageCommon.expandReferences)(enrollmentData)(state);
body['trackedEntityInstance'] = tei;
var _state$configuration8 = state.configuration,
username = _state$configuration8.username,
password = _state$configuration8.password,
hostUrl = _state$configuration8.hostUrl;
var _state$configuration9 = state.configuration,
username = _state$configuration9.username,
password = _state$configuration9.password,
hostUrl = _state$configuration9.hostUrl;
var url = (0, _url4.resolve)(hostUrl + '/', 'api/enrollments');
console.log('Enrolling tracked entity instance.');
return (0, _Client.post)({
Expand Down Expand Up @@ -524,10 +558,10 @@ function enroll(tei, enrollmentData) {
function fetchAnalytics(params, postUrl) {
return function (state) {
var data = (0, _languageCommon.expandReferences)(params)(state);
var _state$configuration9 = state.configuration,
username = _state$configuration9.username,
password = _state$configuration9.password,
hostUrl = _state$configuration9.hostUrl;
var _state$configuration10 = state.configuration,
username = _state$configuration10.username,
password = _state$configuration10.password,
hostUrl = _state$configuration10.hostUrl;
var url = (0, _url4.resolve)(hostUrl + '/', 'api/26/analytics.json?');
var query = data.query || expandDataValues(params)(state);
console.log("Getting analytics data for query: ".concat(query));
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "language-dhis2",
"version": "1.2.0",
"version": "1.3.0",
"description": "DHIS2 Language Pack for OpenFn",
"main": "lib/index.js",
"scripts": {
Expand Down
100 changes: 49 additions & 51 deletions src/Adaptor.js
Expand Up @@ -23,7 +23,7 @@ export function execute(...operations) {
data: null,
};

return (state) => {
return state => {
return commonExecute(
configMigrationHelper,
...operations
Expand Down Expand Up @@ -70,7 +70,7 @@ function configMigrationHelper(state) {
* @returns {Operation}
*/
export function fetchData(params, postUrl) {
return (state) => {
return state => {
const data = expandReferences(params)(state);
const { username, password, hostUrl } = state.configuration;
const url = resolveUrl(hostUrl + '/', 'api/dataValueSets.json?');
Expand All @@ -79,16 +79,16 @@ export function fetchData(params, postUrl) {
console.log('Getting Data Value Sets:');

return get({ username, password, query, url })
.then((result) => {
.then(result => {
console.log('Get Result:', result.body);
return result;
})
.then((result) => {
.then(result => {
if (postUrl) {
const body = result.body;
const url = postUrl;

return post({ username, password, body, url }).then((result) => {
return post({ username, password, body, url }).then(result => {
console.log('Post Result:', result.statusCode);
return {
...state,
Expand Down Expand Up @@ -122,7 +122,7 @@ export function fetchData(params, postUrl) {
* @returns {Operation}
*/
export function fetchEvents(params, postUrl) {
return (state) => {
return state => {
const data = expandReferences(params)(state);
const { username, password, hostUrl } = state.configuration;
const url = resolveUrl(hostUrl + '/', 'api/events.json?');
Expand All @@ -131,16 +131,16 @@ export function fetchEvents(params, postUrl) {
console.log('Getting Events Data:');

return get({ username, password, query, url })
.then((result) => {
.then(result => {
console.log('Get Result:', result.body);
return result;
})
.then((result) => {
.then(result => {
if (postUrl) {
const body = result.body;
const url = postUrl;

return post({ username, password, body, url }).then((result) => {
return post({ username, password, body, url }).then(result => {
console.log('Post Result:', result.statusCode);
return {
...state,
Expand All @@ -167,7 +167,7 @@ export function fetchEvents(params, postUrl) {
* @returns {Operation}
*/
export function event(eventData) {
return (state) => {
return state => {
const body = expandReferences(eventData)(state);
const { username, password, hostUrl } = state.configuration;
const url = resolveUrl(hostUrl + '/', 'api/events');
Expand All @@ -179,18 +179,18 @@ export function event(eventData) {
password,
body,
url,
}).then((result) => {
}).then(result => {
console.log('Result:', JSON.stringify(result.body, null, 2));
return { ...state, references: [result, ...state.references] };
});
};
}

function expandDataValues(obj) {
return (state) => {
return state => {
return mapValues(function (value) {
if (typeof value == 'object') {
return value.map((item) => {
return value.map(item => {
return expandDataValues(item)(state);
});
} else {
Expand Down Expand Up @@ -220,7 +220,7 @@ function expandDataValues(obj) {
* @returns {Operation}
*/
export function dataValueSet(data) {
return (state) => {
return state => {
const body = expandDataValues(data)(state);
const { username, password, hostUrl } = state.configuration;
const url = resolveUrl(hostUrl + '/', 'api/dataValueSets');
Expand All @@ -236,7 +236,7 @@ export function dataValueSet(data) {
password,
body,
url,
}).then((result) => {
}).then(result => {
console.log('Result:', JSON.stringify(result.body, null, 2));
return { ...state, references: [result, ...state.references] };
});
Expand Down Expand Up @@ -268,7 +268,7 @@ export function dataElement(dataElement, value, comment) {
* @returns {Operation}
*/
export function createTEI(data) {
return (state) => {
return state => {
const body = expandReferences(data)(state);
const { username, password, hostUrl } = state.configuration;
const url = resolveUrl(hostUrl + '/', 'api/trackedEntityInstances');
Expand All @@ -288,7 +288,7 @@ export function createTEI(data) {
password,
body,
url,
}).then((result) => {
}).then(result => {
console.log('Result:', JSON.stringify(result.body, null, 2));
return { ...state, references: [result, ...state.references] };
});
Expand All @@ -306,7 +306,7 @@ export function createTEI(data) {
* @returns {Operation}
*/
export function updateTEI(tei, data) {
return (state) => {
return state => {
const body = expandReferences(data)(state);
const { username, password, hostUrl } = state.configuration;
const url = hostUrl.concat(`/api/trackedEntityInstances/${tei}`);
Expand All @@ -326,15 +326,15 @@ export function updateTEI(tei, data) {
password,
body,
url,
}).then((result) => {
}).then(result => {
console.log('Result:', JSON.stringify(result.body, null, 2));
return { ...state, references: [result, ...state.references] };
});
};
}

/**
* Create or updateone or many new Tracked Entity Instances
* Create or update one or many new Tracked Entity Instances
* @public
* @example
* upsertTEI(data)
Expand All @@ -343,40 +343,38 @@ export function updateTEI(tei, data) {
* @returns {Operation}
*/
export function upsertTEI(data) {

return state => {

const body = expandReferences(data)(state);

const {
username,
password,
apiUrl
} = state.configuration;
const { username, password, apiUrl } = state.configuration;

const url = resolveUrl(apiUrl + '/', 'api/trackedEntityInstances?strategy=CREATE_AND_UPDATE')
const url = resolveUrl(
apiUrl + '/',
'api/trackedEntityInstances?strategy=CREATE_AND_UPDATE'
);

console.log("Posting tracked entity instance data:");
console.log(body)
console.log(
`Upserting tracked entity instance of type '${
body.trackedEntityType
}' to org unit '${body.orgUnit}' with ${
body.attributes && body.attributes.length
} attributes and ${
body.enrollments && body.enrollments.length
} enrollments.`
);

return post({
username,
password,
body,
url
})
.then((result) => {
console.log("Success:", result);
return {...state,
references: [result, ...state.references]
}
})

}
username,
password,
body,
url,
}).then(result => {
console.log('Success:', result);
return { ...state, references: [result, ...state.references] };
});
};
}



// /**
// * Create and enroll TrackedEntityInstances
// * @example
Expand Down Expand Up @@ -414,7 +412,7 @@ export function upsertTEI(data) {
* @returns {Operation}
*/
export function enroll(tei, enrollmentData) {
return (state) => {
return state => {
const body = expandReferences(enrollmentData)(state);
body['trackedEntityInstance'] = tei;
const { username, password, hostUrl } = state.configuration;
Expand All @@ -427,7 +425,7 @@ export function enroll(tei, enrollmentData) {
password,
body,
url,
}).then((result) => {
}).then(result => {
console.log('Result:', JSON.stringify(result.body, null, 2));
return { ...state, references: [result, ...state.references] };
});
Expand All @@ -453,7 +451,7 @@ export function enroll(tei, enrollmentData) {
* @returns {Operation}
*/
export function fetchAnalytics(params, postUrl) {
return (state) => {
return state => {
const data = expandReferences(params)(state);
const { username, password, hostUrl } = state.configuration;
const url = resolveUrl(hostUrl + '/', 'api/26/analytics.json?');
Expand All @@ -462,16 +460,16 @@ export function fetchAnalytics(params, postUrl) {
console.log(`Getting analytics data for query: ${query}`);

return get({ username, password, query, url })
.then((result) => {
.then(result => {
console.log('Get Result:', result.body);
return result;
})
.then((result) => {
.then(result => {
if (postUrl) {
const body = result.body;
const url = postUrl;

return post({ username, password, body, url }).then((result) => {
return post({ username, password, body, url }).then(result => {
console.log('Post Result:', result.statusCode);
return {
...state,
Expand Down

0 comments on commit 09da441

Please sign in to comment.