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

Commit

Permalink
Merge pull request #2 from OpenFn/more_tracker_support
Browse files Browse the repository at this point in the history
More tracker support
  • Loading branch information
taylordowns2000 committed Aug 18, 2016
2 parents 83df84b + 20a73dc commit 6a25d79
Show file tree
Hide file tree
Showing 4 changed files with 301 additions and 81 deletions.
117 changes: 96 additions & 21 deletions lib/Adaptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.lastReferenceValue = exports.dataValue = exports.dataPath = exports.merge = exports.sourceValue = exports.fields = exports.field = undefined;
exports.lastReferenceValue = exports.dataValue = exports.dataPath = exports.each = exports.merge = exports.sourceValue = exports.fields = exports.field = undefined;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

Expand Down Expand Up @@ -41,6 +41,12 @@ Object.defineProperty(exports, 'merge', {
return _languageCommon.merge;
}
});
Object.defineProperty(exports, 'each', {
enumerable: true,
get: function get() {
return _languageCommon.each;
}
});
Object.defineProperty(exports, 'dataPath', {
enumerable: true,
get: function get() {
Expand Down Expand Up @@ -121,9 +127,16 @@ function event(eventData) {
console.log("Posting event:");
console.log(body);

return (0, _Client.post)({ username: username, password: password, body: body, url: url }).then(function (result) {
return (0, _Client.post)({
username: username,
password: password,
body: body,
url: url
}).then(function (result) {
console.log("Success:", result);
return _extends({}, state, { references: [result].concat(_toConsumableArray(state.references)) });
return _extends({}, state, {
references: [result].concat(_toConsumableArray(state.references))
});
});
};
}
Expand Down Expand Up @@ -154,15 +167,36 @@ function dataValueSet(data) {
console.log("Posting data value set:");
console.log(body);

return (0, _Client.post)({ username: username, password: password, body: body, url: url }).then(function (result) {
return (0, _Client.post)({
username: username,
password: password,
body: body,
url: url
}).then(function (result) {
console.log("Success:", result);
return _extends({}, state, { references: [result].concat(_toConsumableArray(state.references)) });
return _extends({}, state, {
references: [result].concat(_toConsumableArray(state.references))
});
});
};
}

/**
* Create a "dataElement" pairing for DHIS2.
* @example
* execute(
* dataElement(key, value)
* )(state)
* @constructor
* @param {object} key - Payload data for the Data Element key
* @param {object} value - Payload data for the Data Element value
* @returns {Operation}
*/
function dataElement(key, value) {
return { "dataElement": key, "value": value };
return {
"dataElement": key,
"value": value
};
}

/**
Expand Down Expand Up @@ -192,9 +226,16 @@ function createTEI(data) {
console.log("Posting tracked entity instance data:");
console.log(body);

return (0, _Client.post)({ username: username, password: password, body: body, url: url }).then(function (result) {
return (0, _Client.post)({
username: username,
password: password,
body: body,
url: url
}).then(function (result) {
console.log("Success:", result);
return _extends({}, state, { references: [result].concat(_toConsumableArray(state.references)) });
return _extends({}, state, {
references: [result].concat(_toConsumableArray(state.references))
});
});
};
}
Expand All @@ -203,13 +244,14 @@ function createTEI(data) {
* Update existing Tracked Entity Instances
* @example
* execute(
* updateTEI(data)
* updateTEI(tei, data)
* )(state)
* @constructor
* @param {object} data - Payload data for updating tracked entity instance(s)
* @param {object} tei - Payload data for the TEI to be updated
* @param {object} data - Payload data for updating a TEI
* @returns {Operation}
*/
function updateTEI(data) {
function updateTEI(tei, data) {

return function (state) {
var body = (0, _languageCommon.expandReferences)(data)(state);
Expand All @@ -220,19 +262,35 @@ function updateTEI(data) {
var apiUrl = _state$configuration4.apiUrl;


var url = (0, _url.resolve)(apiUrl + '/', 'api/trackedEntityInstances');
var url = apiUrl.concat('/api/trackedEntityInstances/' + tei);

console.log("Posting tracked entity instance data:");
console.log('Updating tracked entity instance ' + tei + ' with data:');
console.log(body);

return (0, _Client.post)({ username: username, password: password, body: body, url: url }).then(function (result) {
return (0, _Client.put)({
username: username,
password: password,
body: body,
url: url
}).then(function (result) {
console.log("Success:", result);
return _extends({}, state, { references: [result].concat(_toConsumableArray(state.references)) });
return _extends({}, state, {
references: [result].concat(_toConsumableArray(state.references))
});
});
};
}

// Create and enroll TrackedEntityInstances
// /**
// * Create and enroll TrackedEntityInstances
// * @example
// * execute(
// * createEnrollTEI(te, orgUnit, attributes, enrollments)
// * )(state)
// * @constructor
// * @param {object} enrollmentData - Payload data for new enrollment
// * @returns {Operation}
// */
// export function upsertEnroll(upsertData) {
//
// return state => {
Expand All @@ -254,11 +312,21 @@ function updateTEI(data) {
// }
// }

// Enroll a tracked entity instance in a program
function enroll(enrollmentData) {
/**
* Enroll a tracked entity instance in a program
* @example
* execute(
* enroll(enrollmentData)
* )(state)
* @constructor
* @param {object} enrollmentData - Payload data for new enrollment
* @returns {Operation}
*/
function enroll(tei, enrollmentData) {

return function (state) {
var body = (0, _languageCommon.expandReferences)(enrollmentData)(state);
body["trackedEntityInstance"] = tei;

var _state$configuration5 = state.configuration;
var username = _state$configuration5.username;
Expand All @@ -268,12 +336,19 @@ function enroll(enrollmentData) {

var url = (0, _url.resolve)(apiUrl + '/', 'api/enrollments');

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

return (0, _Client.post)({ username: username, password: password, body: body, url: url }).then(function (result) {
return (0, _Client.post)({
username: username,
password: password,
body: body,
url: url
}).then(function (result) {
console.log("Success:", result);
return _extends({}, state, { references: [result].concat(_toConsumableArray(state.references)) });
return _extends({}, state, {
references: [result].concat(_toConsumableArray(state.references))
});
});
};
}
16 changes: 16 additions & 0 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
value: true
});
exports.post = post;
exports.put = put;

var _superagent = require('superagent');

Expand All @@ -22,7 +23,22 @@ function post(_ref) {
if (!!error || !res.ok) {
reject(error);
}
resolve(res);
});
});
}

function put(_ref2) {
var username = _ref2.username;
var password = _ref2.password;
var body = _ref2.body;
var url = _ref2.url;

return new Promise(function (resolve, reject) {
_superagent2.default.put(url).type('json').accept('json').auth(username, password).send(JSON.stringify(body)).end(function (error, res) {
if (!!error || !res.ok) {
reject(error);
}
resolve(res);
});
});
Expand Down

0 comments on commit 6a25d79

Please sign in to comment.