From 08a62dc9871d3bb1d453ef92ef36380404b07257 Mon Sep 17 00:00:00 2001 From: taylor Date: Fri, 29 Jul 2016 17:26:11 -0400 Subject: [PATCH 1/3] enrollments, update TEI, wip --- lib/Adaptor.js | 40 ++++++++++++++++++++++++++++--------- lib/Client.js | 18 +++++++++++++++++ src/Adaptor.js | 54 +++++++++++++++++++++++++++++++++++++++----------- src/Client.js | 18 +++++++++++++++++ 4 files changed, 109 insertions(+), 21 deletions(-) diff --git a/lib/Adaptor.js b/lib/Adaptor.js index 881139b..4ac018a 100644 --- a/lib/Adaptor.js +++ b/lib/Adaptor.js @@ -161,6 +161,17 @@ function dataValueSet(data) { }; } +/** + * 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 }; } @@ -203,13 +214,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); @@ -220,12 +232,12 @@ 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)) }); }); @@ -254,11 +266,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; @@ -268,7 +290,7 @@ 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) { diff --git a/lib/Client.js b/lib/Client.js index 701b868..33ea80c 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.post = post; +exports.put = put; var _superagent = require('superagent'); @@ -27,3 +28,20 @@ function post(_ref) { }); }); } + +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); + }); + }); +} diff --git a/src/Adaptor.js b/src/Adaptor.js index 0df902c..26376f6 100644 --- a/src/Adaptor.js +++ b/src/Adaptor.js @@ -1,5 +1,5 @@ import { execute as commonExecute, expandReferences } from 'language-common'; -import { post } from './Client'; +import { post, put } from './Client'; import { resolve as resolveUrl } from 'url'; /** @module Adaptor */ @@ -90,6 +90,17 @@ export function dataValueSet(data) { } } +/** + * 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} + */ export function dataElement(key, value) { return { "dataElement": key, "value": value } } @@ -130,25 +141,26 @@ export 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} */ -export function updateTEI(data) { +export function updateTEI(tei, data) { return state => { const body = expandReferences(data)(state); const { username, password, apiUrl } = state.configuration; - const url = resolveUrl(apiUrl + '/', 'api/trackedEntityInstances') + const 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 post({ username, password, body, url }) + return put({ username, password, body, url }) .then((result) => { console.log("Success:", result); return { ...state, references: [ result, ...state.references ] } @@ -157,7 +169,16 @@ export function updateTEI(data) { } } -// 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 => { @@ -179,18 +200,27 @@ export function updateTEI(data) { // } // } - -// Enroll a tracked entity instance in a program -export 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} + */ +export function enroll(tei, enrollmentData) { return state => { const body = expandReferences(enrollmentData)(state); + body["trackedEntityInstance"] = tei; const { username, password, apiUrl } = state.configuration; const url = resolveUrl(apiUrl + '/', 'api/enrollments') - console.log("Enrolling tracked entity instance:"); + console.log("Enrolling tracked entity instance with data:"); console.log(body) return post({ username, password, body, url }) diff --git a/src/Client.js b/src/Client.js index 6b310b0..bed6007 100644 --- a/src/Client.js +++ b/src/Client.js @@ -17,3 +17,21 @@ export function post({ username, password, body, url }) { }) } + +export function put({ username, password, body, url }) { + return new Promise((resolve, reject) => { + request.put(url) + .type('json') + .accept('json') + .auth(username, password) + .send(JSON.stringify(body)) + .end((error, res) => { + if (!!error || !res.ok) { + reject( error ) + } + + resolve( res ) + }) + + }) +} From c3dd757f4a76f827fc2851afee14ea14208276f3 Mon Sep 17 00:00:00 2001 From: taylor Date: Wed, 17 Aug 2016 16:29:31 -0400 Subject: [PATCH 2/3] import each --- src/Adaptor.js | 151 ++++++++++++++++++++++++++++++++++++------------- src/Client.js | 58 ++++++++++--------- 2 files changed, 145 insertions(+), 64 deletions(-) diff --git a/src/Adaptor.js b/src/Adaptor.js index 26376f6..0458c70 100644 --- a/src/Adaptor.js +++ b/src/Adaptor.js @@ -1,6 +1,14 @@ -import { execute as commonExecute, expandReferences } from 'language-common'; -import { post, put } from './Client'; -import { resolve as resolveUrl } from 'url'; +import { + execute as commonExecute, + expandReferences +} from 'language-common'; +import { + post, + put +} from './Client'; +import { + resolve as resolveUrl +} from 'url'; /** @module Adaptor */ @@ -23,7 +31,9 @@ export function execute(...operations) { } return state => { - return commonExecute(...operations)({ ...initialState, ...state }) + return commonExecute(...operations)({...initialState, + ...state + }) }; } @@ -43,18 +53,29 @@ export function event(eventData) { return state => { const body = expandReferences(eventData)(state); - const { username, password, apiUrl } = state.configuration; + const { + username, + password, + apiUrl + } = state.configuration; const url = resolveUrl(apiUrl + '/', 'api/events') console.log("Posting event:"); console.log(body) - return post({ username, password, body, url }) - .then((result) => { - console.log("Success:", result); - return { ...state, references: [ result, ...state.references ] } - }) + return post({ + username, + password, + body, + url + }) + .then((result) => { + console.log("Success:", result); + return {...state, + references: [result, ...state.references] + } + }) } } @@ -74,18 +95,29 @@ export function dataValueSet(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/dataValueSets') console.log("Posting data value set:"); console.log(body) - return post({ username, password, body, url }) - .then((result) => { - console.log("Success:", result); - return { ...state, references: [ result, ...state.references ] } - }) + return post({ + username, + password, + body, + url + }) + .then((result) => { + console.log("Success:", result); + return {...state, + references: [result, ...state.references] + } + }) } } @@ -102,7 +134,10 @@ export function dataValueSet(data) { * @returns {Operation} */ export function dataElement(key, value) { - return { "dataElement": key, "value": value } + return { + "dataElement": key, + "value": value + } } /** @@ -121,18 +156,29 @@ export function createTEI(data) { const body = expandReferences(data)(state); - const { username, password, apiUrl } = state.configuration; + const { + username, + password, + apiUrl + } = state.configuration; const url = resolveUrl(apiUrl + '/', 'api/trackedEntityInstances') console.log("Posting tracked entity instance data:"); console.log(body) - return post({ username, password, body, url }) - .then((result) => { - console.log("Success:", result); - return { ...state, references: [ result, ...state.references ] } - }) + return post({ + username, + password, + body, + url + }) + .then((result) => { + console.log("Success:", result); + return {...state, + references: [result, ...state.references] + } + }) } } @@ -153,18 +199,29 @@ export function updateTEI(tei, data) { return state => { const body = expandReferences(data)(state); - const { username, password, apiUrl } = state.configuration; + const { + username, + password, + apiUrl + } = state.configuration; const url = apiUrl.concat(`/api/trackedEntityInstances/${tei}`) console.log(`Updating tracked entity instance ${tei} with data:`); console.log(body) - return put({ username, password, body, url }) - .then((result) => { - console.log("Success:", result); - return { ...state, references: [ result, ...state.references ] } - }) + return put({ + username, + password, + body, + url + }) + .then((result) => { + console.log("Success:", result); + return {...state, + references: [result, ...state.references] + } + }) } } @@ -216,23 +273,41 @@ export function enroll(tei, enrollmentData) { const body = expandReferences(enrollmentData)(state); body["trackedEntityInstance"] = tei; - const { username, password, apiUrl } = state.configuration; + const { + username, + password, + apiUrl + } = state.configuration; const url = resolveUrl(apiUrl + '/', 'api/enrollments') console.log("Enrolling tracked entity instance with data:"); console.log(body) - return post({ username, password, body, url }) - .then((result) => { - console.log("Success:", result); - return { ...state, references: [ result, ...state.references ] } - }) + return post({ + username, + password, + body, + url + }) + .then((result) => { + console.log("Success:", result); + return {...state, + references: [result, ...state.references] + } + }) } } export { - field, fields, sourceValue, - merge, dataPath, dataValue, lastReferenceValue -} from 'language-common'; + field, + fields, + sourceValue, + merge, + each, + dataPath, + dataValue, + lastReferenceValue +} +from 'language-common'; diff --git a/src/Client.js b/src/Client.js index bed6007..c03907a 100644 --- a/src/Client.js +++ b/src/Client.js @@ -1,37 +1,43 @@ import request from 'superagent' -export function post({ username, password, body, url }) { +export function post({ + username, + password, + body, + url +}) { return new Promise((resolve, reject) => { request.post(url) - .type('json') - .accept('json') - .auth(username, password) - .send(JSON.stringify(body)) - .end((error, res) => { - if (!!error || !res.ok) { - reject( error ) - } - - resolve( res ) - }) - + .type('json') + .accept('json') + .auth(username, password) + .send(JSON.stringify(body)) + .end((error, res) => { + if (!!error || !res.ok) { + reject(error) + } + resolve(res) + }) }) } -export function put({ username, password, body, url }) { +export function put({ + username, + password, + body, + url +}) { return new Promise((resolve, reject) => { request.put(url) - .type('json') - .accept('json') - .auth(username, password) - .send(JSON.stringify(body)) - .end((error, res) => { - if (!!error || !res.ok) { - reject( error ) - } - - resolve( res ) - }) - + .type('json') + .accept('json') + .auth(username, password) + .send(JSON.stringify(body)) + .end((error, res) => { + if (!!error || !res.ok) { + reject(error) + } + resolve(res) + }) }) } From 20a73dc696ac0db8f9ff57d59e86665cff1942f6 Mon Sep 17 00:00:00 2001 From: taylor Date: Wed, 17 Aug 2016 16:29:41 -0400 Subject: [PATCH 3/3] add each --- lib/Adaptor.js | 79 +++++++++++++++++++++++++++++++++++++++++--------- lib/Client.js | 2 -- 2 files changed, 66 insertions(+), 15 deletions(-) diff --git a/lib/Adaptor.js b/lib/Adaptor.js index 4ac018a..b979d21 100644 --- a/lib/Adaptor.js +++ b/lib/Adaptor.js @@ -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; }; @@ -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() { @@ -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)) + }); }); }; } @@ -154,9 +167,16 @@ 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)) + }); }); }; } @@ -173,7 +193,10 @@ function dataValueSet(data) { * @returns {Operation} */ function dataElement(key, value) { - return { "dataElement": key, "value": value }; + return { + "dataElement": key, + "value": value + }; } /** @@ -203,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)) + }); }); }; } @@ -237,14 +267,30 @@ function updateTEI(tei, data) { console.log('Updating tracked entity instance ' + tei + ' with data:'); console.log(body); - return (0, _Client.put)({ 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 => { @@ -293,9 +339,16 @@ function enroll(tei, enrollmentData) { 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)) + }); }); }; } diff --git a/lib/Client.js b/lib/Client.js index 33ea80c..d9d2d4d 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -23,7 +23,6 @@ function post(_ref) { if (!!error || !res.ok) { reject(error); } - resolve(res); }); }); @@ -40,7 +39,6 @@ function put(_ref2) { if (!!error || !res.ok) { reject(error); } - resolve(res); }); });