diff --git a/lib/Adaptor.js b/lib/Adaptor.js index 881139b..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,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 + }; } /** @@ -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)) + }); }); }; } @@ -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); @@ -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 => { @@ -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; @@ -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)) + }); }); }; } diff --git a/lib/Client.js b/lib/Client.js index 701b868..d9d2d4d 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'); @@ -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); }); }); diff --git a/src/Adaptor.js b/src/Adaptor.js index 0df902c..0458c70 100644 --- a/src/Adaptor.js +++ b/src/Adaptor.js @@ -1,6 +1,14 @@ -import { execute as commonExecute, expandReferences } from 'language-common'; -import { post } 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,24 +95,49 @@ 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] + } + }) } } +/** + * 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 } + return { + "dataElement": key, + "value": value + } } /** @@ -110,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] + } + }) } } @@ -130,34 +187,55 @@ 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 { + 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 }) - .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] + } + }) } } -// 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,30 +257,57 @@ 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 { + 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 }) - .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 6b310b0..c03907a 100644 --- a/src/Client.js +++ b/src/Client.js @@ -1,19 +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 +}) { + 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) + }) }) }