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

Commit

Permalink
limit dhis2 reponse in logs for security and readability
Browse files Browse the repository at this point in the history
  • Loading branch information
taylordowns2000 committed Jul 31, 2020
1 parent 0a32d9f commit 891b700
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 74 deletions.
38 changes: 13 additions & 25 deletions lib/Adaptor.js
Expand Up @@ -82,7 +82,7 @@ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableTo

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }

Expand Down Expand Up @@ -283,15 +283,14 @@ function event(eventData) {
password = _state$configuration4.password,
hostUrl = _state$configuration4.hostUrl;
var url = (0, _url4.resolve)(hostUrl + '/', 'api/events');
console.log('Posting event:');
console.log(body);
console.log("Posting event to org unit '".concat(body.orgUnit, "'."));
return (0, _Client.post)({
username: username,
password: password,
body: body,
url: url
}).then(function (result) {
console.log('Success:', result);
console.log('Result:', JSON.stringify(result.body, null, 2));
return _objectSpread({}, state, {
references: [result].concat(_toConsumableArray(state.references))
});
Expand Down Expand Up @@ -341,20 +340,17 @@ function dataValueSet(data) {
password = _state$configuration5.password,
hostUrl = _state$configuration5.hostUrl;
var url = (0, _url4.resolve)(hostUrl + '/', 'api/dataValueSets');
console.log('Posting data value set:');
console.log(body);
console.log("Posting data value set ".concat(body.dataSet, " to org unit '").concat(body.orgUnit, "' with ").concat(body.dataValues && body.dataValues.length, " data values."));
return (0, _Client.post)({
username: username,
password: password,
body: body,
url: url
}).then(function (result) {
console.log('Success:', result.body);
console.log('Result:', JSON.stringify(result.body, null, 2));
return _objectSpread({}, state, {
references: [result].concat(_toConsumableArray(state.references))
});
state.references = result;
return state;
});
};
}
Expand Down Expand Up @@ -397,15 +393,14 @@ function createTEI(data) {
password = _state$configuration6.password,
hostUrl = _state$configuration6.hostUrl;
var url = (0, _url4.resolve)(hostUrl + '/', 'api/trackedEntityInstances');
console.log('Posting tracked entity instance data:');
console.log(body);
console.log("Posting 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);
console.log('Result:', JSON.stringify(result.body, null, 2));
return _objectSpread({}, state, {
references: [result].concat(_toConsumableArray(state.references))
});
Expand All @@ -418,7 +413,7 @@ function createTEI(data) {
* @example
* updateTEI(tei, data)
* @constructor
* @param {object} tei - Payload data for the TEI to be updated
* @param {object} tei - identifier for the TEI to be updated
* @param {object} data - Payload data for updating a TEI
* @returns {Operation}
*/
Expand All @@ -432,15 +427,14 @@ function updateTEI(tei, data) {
password = _state$configuration7.password,
hostUrl = _state$configuration7.hostUrl;
var url = hostUrl.concat("/api/trackedEntityInstances/".concat(tei));
console.log("Updating tracked entity instance ".concat(tei, " with data:"));
console.log(body);
console.log("Updating 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.put)({
username: username,
password: password,
body: body,
url: url
}).then(function (result) {
console.log('Success:', result);
console.log('Result:', JSON.stringify(result.body, null, 2));
return _objectSpread({}, state, {
references: [result].concat(_toConsumableArray(state.references))
});
Expand All @@ -460,17 +454,12 @@ function updateTEI(tei, data) {
//
// return state => {
// const body = expandReferences(trackedEntityInstanceData)(state);
//
// const { username, password, hostUrl } = state.configuration;
//
// const url = resolveUrl(hostUrl + '/', 'api/trackedEntityInstances')
//
// console.log("Posting tracked entity instance data:");
// console.log(body)
//
// return post({ username, password, body, url })
// .then((result) => {
// console.log("Success:", result);
// console.log("Result:", JSON.stringify(result.body, null, 2));
// return { ...state, references: [ result, ...state.references ] }
// })
//
Expand Down Expand Up @@ -498,15 +487,14 @@ function enroll(tei, enrollmentData) {
password = _state$configuration8.password,
hostUrl = _state$configuration8.hostUrl;
var url = (0, _url4.resolve)(hostUrl + '/', 'api/enrollments');
console.log('Enrolling tracked entity instance with data:');
console.log(body);
console.log('Enrolling tracked entity instance.');
return (0, _Client.post)({
username: username,
password: password,
body: body,
url: url
}).then(function (result) {
console.log('Success:', result);
console.log('Result:', JSON.stringify(result.body, null, 2));
return _objectSpread({}, state, {
references: [result].concat(_toConsumableArray(state.references))
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "language-dhis2",
"version": "1.1.2",
"version": "1.2.0",
"description": "DHIS2 Language Pack for OpenFn",
"main": "lib/index.js",
"scripts": {
Expand Down
83 changes: 35 additions & 48 deletions src/Adaptor.js
Expand Up @@ -43,7 +43,9 @@ export function execute(...operations) {
function configMigrationHelper(state) {
const { hostUrl, apiUrl } = state.configuration;
if (!hostUrl) {
console.log('DEPRECATION WARNING: Please migrate instance address from `apiUrl` to `hostUrl`.')
console.log(
'DEPRECATION WARNING: Please migrate instance address from `apiUrl` to `hostUrl`.'
);
state.configuration.hostUrl = apiUrl;
return state;
}
Expand All @@ -70,11 +72,8 @@ function configMigrationHelper(state) {
export function fetchData(params, postUrl) {
return (state) => {
const data = expandReferences(params)(state);

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

const url = resolveUrl(hostUrl + '/', 'api/dataValueSets.json?');

const query = data.fields || expandDataValues(params)(state);

console.log('Getting Data Value Sets:');
Expand All @@ -87,7 +86,6 @@ export function fetchData(params, postUrl) {
.then((result) => {
if (postUrl) {
const body = result.body;

const url = postUrl;

return post({ username, password, body, url }).then((result) => {
Expand Down Expand Up @@ -126,11 +124,8 @@ export function fetchData(params, postUrl) {
export function fetchEvents(params, postUrl) {
return (state) => {
const data = expandReferences(params)(state);

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

const url = resolveUrl(hostUrl + '/', 'api/events.json?');

const query = data.fields || expandDataValues(params)(state);

console.log('Getting Events Data:');
Expand All @@ -143,7 +138,6 @@ export function fetchEvents(params, postUrl) {
.then((result) => {
if (postUrl) {
const body = result.body;

const url = postUrl;

return post({ username, password, body, url }).then((result) => {
Expand Down Expand Up @@ -175,21 +169,18 @@ export function fetchEvents(params, postUrl) {
export function event(eventData) {
return (state) => {
const body = expandReferences(eventData)(state);

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

const url = resolveUrl(hostUrl + '/', 'api/events');

console.log('Posting event:');
console.log(body);
console.log(`Posting event to org unit '${body.orgUnit}'.`);

return post({
username,
password,
body,
url,
}).then((result) => {
console.log('Success:', result);
console.log('Result:', JSON.stringify(result.body, null, 2));
return { ...state, references: [result, ...state.references] };
});
};
Expand Down Expand Up @@ -231,25 +222,23 @@ function expandDataValues(obj) {
export function dataValueSet(data) {
return (state) => {
const body = expandDataValues(data)(state);

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

const url = resolveUrl(hostUrl + '/', 'api/dataValueSets');

console.log('Posting data value set:');
console.log(body);
console.log(
`Posting data value set ${body.dataSet} to org unit '${
body.orgUnit
}' with ${body.dataValues && body.dataValues.length} data values.`
);

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

state.references = result;
return state;
});
};
}
Expand Down Expand Up @@ -281,21 +270,26 @@ export function dataElement(dataElement, value, comment) {
export function createTEI(data) {
return (state) => {
const body = expandReferences(data)(state);

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

const url = resolveUrl(hostUrl + '/', 'api/trackedEntityInstances');

console.log('Posting tracked entity instance data:');
console.log(body);
console.log(
`Posting 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);
console.log('Result:', JSON.stringify(result.body, null, 2));
return { ...state, references: [result, ...state.references] };
});
};
Expand All @@ -307,28 +301,33 @@ export function createTEI(data) {
* @example
* updateTEI(tei, data)
* @constructor
* @param {object} tei - Payload data for the TEI to be updated
* @param {object} tei - identifier for the TEI to be updated
* @param {object} data - Payload data for updating a TEI
* @returns {Operation}
*/
export function updateTEI(tei, data) {
return (state) => {
const body = expandReferences(data)(state);

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

const url = hostUrl.concat(`/api/trackedEntityInstances/${tei}`);

console.log(`Updating tracked entity instance ${tei} with data:`);
console.log(body);
console.log(
`Updating 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 put({
username,
password,
body,
url,
}).then((result) => {
console.log('Success:', result);
console.log('Result:', JSON.stringify(result.body, null, 2));
return { ...state, references: [result, ...state.references] };
});
};
Expand All @@ -348,17 +347,12 @@ export function updateTEI(tei, data) {
//
// return state => {
// const body = expandReferences(trackedEntityInstanceData)(state);
//
// const { username, password, hostUrl } = state.configuration;
//
// const url = resolveUrl(hostUrl + '/', 'api/trackedEntityInstances')
//
// console.log("Posting tracked entity instance data:");
// console.log(body)
//
// return post({ username, password, body, url })
// .then((result) => {
// console.log("Success:", result);
// console.log("Result:", JSON.stringify(result.body, null, 2));
// return { ...state, references: [ result, ...state.references ] }
// })
//
Expand All @@ -379,21 +373,18 @@ export function enroll(tei, enrollmentData) {
return (state) => {
const body = expandReferences(enrollmentData)(state);
body['trackedEntityInstance'] = tei;

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

const url = resolveUrl(hostUrl + '/', 'api/enrollments');

console.log('Enrolling tracked entity instance with data:');
console.log(body);
console.log('Enrolling tracked entity instance.');

return post({
username,
password,
body,
url,
}).then((result) => {
console.log('Success:', result);
console.log('Result:', JSON.stringify(result.body, null, 2));
return { ...state, references: [result, ...state.references] };
});
};
Expand All @@ -420,11 +411,8 @@ export function enroll(tei, enrollmentData) {
export function fetchAnalytics(params, postUrl) {
return (state) => {
const data = expandReferences(params)(state);

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

const url = resolveUrl(hostUrl + '/', 'api/26/analytics.json?');

const query = data.query || expandDataValues(params)(state);

console.log(`Getting analytics data for query: ${query}`);
Expand All @@ -437,7 +425,6 @@ export function fetchAnalytics(params, postUrl) {
.then((result) => {
if (postUrl) {
const body = result.body;

const url = postUrl;

return post({ username, password, body, url }).then((result) => {
Expand Down

0 comments on commit 891b700

Please sign in to comment.