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

Commit

Permalink
new dataValueSet function
Browse files Browse the repository at this point in the history
  • Loading branch information
taylordowns2000 committed Sep 29, 2017
1 parent 3cd5108 commit 01204a8
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 57 deletions.
26 changes: 11 additions & 15 deletions README.md
Expand Up @@ -71,21 +71,17 @@ fetchEvents({

#### Current `DataValueSets API` expression
```js
dataValueSet(
fields(
field("dataSet", "pBOMPrpg1QX"),
field("orgUnit", "DiszpKrYNg8"),
field("period", "201401"),
field("completeData", dataValue("form.date")),
field("dataValues", function(state) {
return [
dataElement("qrur9Dvnyt5", dataValue("form.prop_a")(state)),
dataElement("oZg33kd9taw", dataValue("form.prop_b")(state)),
dataElement("msodh3rEMJa", dataValue("form.prop_c")(state))
]
})
)
)
dataValueSet({
dataSet: dataValue("set"),
orgUnit: "DiszpKrYNg8",
period: "201402",
completeData: "2014-03-03",
dataValues: [
dataElement("f7n9E0hX8qk", dataValue("data[0].site_school_number")),
dataElement("Ix2HsbDMLea", dataValue("age")),
dataElement("eY5ehpbEsB7", 30)
]
});
```

#### Current `fetchData API` expression (Optional `postUrl` for a complete fetch)
Expand Down
40 changes: 28 additions & 12 deletions lib/Adaptor.js
Expand Up @@ -5,7 +5,9 @@ Object.defineProperty(exports, "__esModule", {
});
exports.alterState = 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; };
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

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; }; /** @module Adaptor */

exports.execute = execute;
exports.fetchData = fetchData;
Expand Down Expand Up @@ -78,9 +80,9 @@ var _Client = require('./Client');

var _url3 = require('url');

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
var _fp = require('lodash/fp');

/** @module Adaptor */
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

/**
* Execute a sequence of operations.
Expand Down Expand Up @@ -128,6 +130,7 @@ function execute() {
function fetchData(params) {

return function (state) {

var data = (0, _languageCommon.expandReferences)(params)(state);

var _state$configuration = state.configuration,
Expand Down Expand Up @@ -261,6 +264,20 @@ function event(eventData) {
};
}

function expandDataValues(obj) {
return function (state) {
return (0, _fp.mapValues)(function (value) {
if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) == 'object') {
return value.map(function (item) {
return expandDataValues(item)(state);
});
} else {
return typeof value == 'function' ? value(state) : value;
}
})(obj);
};
}

/**
* Send data values using the dataValueSets resource
* @public
Expand All @@ -273,7 +290,8 @@ function event(eventData) {
function dataValueSet(data) {

return function (state) {
var body = (0, _languageCommon.expandReferences)(data)(state);

var body = expandDataValues(data)(state);

var _state$configuration4 = state.configuration,
username = _state$configuration4.username,
Expand All @@ -292,7 +310,7 @@ function dataValueSet(data) {
body: body,
url: url
}).then(function (result) {
console.log("Success:", result);
console.log("Success:", result.body);
return _extends({}, state, {
references: [result].concat(_toConsumableArray(state.references))
});
Expand All @@ -309,15 +327,13 @@ function dataValueSet(data) {
* @example
* dataElement(key, value)
* @constructor
* @param {object} key - Payload data for the Data Element key
* @param {object} value - Payload data for the Data Element value
* @param {string} key - Payload data for the Data Element key
* @param {variable} value - Payload data for the Data Element value
* @param {string} comment - comment for the Data Element
* @returns {Operation}
*/
function dataElement(key, value) {
return {
"dataElement": key,
"value": value
};
function dataElement(dataElement, value, comment) {
return { dataElement: dataElement, value: value, comment: comment };
}

/**
Expand Down
7 changes: 3 additions & 4 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "language-dhis2",
"version": "0.1.0",
"version": "0.2.0",
"description": "DHIS2 Language Pack for OpenFn",
"main": "lib/index.js",
"scripts": {
Expand All @@ -14,9 +14,8 @@
"lib/"
],
"dependencies": {
"JSONPath": "^0.10.0",
"language-common": "github:openfn/language-common",
"lodash-fp": "^0.10.2",
"language-common": "github:openfn/language-common#v0.2.0",
"lodash": "^4.17.4",
"superagent": "^1.7.2"
},
"devDependencies": {
Expand Down
62 changes: 39 additions & 23 deletions src/Adaptor.js
@@ -1,18 +1,10 @@
import {
execute as commonExecute,
expandReferences
} from 'language-common';
import {
get,
post,
put
} from './Client';
import {
resolve as resolveUrl
} from 'url';

/** @module Adaptor */

import { execute as commonExecute, expandReferences } from 'language-common';
import { get, post, put } from './Client';
import { resolve as resolveUrl } from 'url';
import { mapValues } from 'lodash/fp';

/**
* Execute a sequence of operations.
* Wraps `language-common/execute`, and prepends initial state for DHIS2.
Expand Down Expand Up @@ -58,6 +50,7 @@ export function execute(...operations) {
export function fetchData(params) {

return state => {

const data = expandReferences(params)(state);

const {username, password, apiUrl} = state.configuration;
Expand Down Expand Up @@ -204,19 +197,44 @@ export function event(eventData) {
}
}

function expandDataValues(obj) {
return state => {
return mapValues(function(value) {
if (typeof value == 'object') {
return value.map((item) => {
return expandDataValues(item)(state)
})
} else {
return typeof value == 'function' ? value(state) : value;
}
})(obj);
}
}

/**
* Send data values using the dataValueSets resource
* @public
* @example
* dataValueSet(data)
* dataValueSet({
* dataSet: dataValue("set"),
* orgUnit: "DiszpKrYNg8",
* period: "201402",
* completeData: "2014-03-03",
* dataValues: [
* dataElement("f7n9E0hX8qk", dataValue("name")),
* dataElement("Ix2HsbDMLea", dataValue("age")),
* dataElement("eY5ehpbEsB7", 30)
* ]
* });
* @constructor
* @param {object} data - Payload data for the data value set
* @returns {Operation}
*/
export function dataValueSet(data) {

return state => {
const body = expandReferences(data)(state);

const body = expandDataValues(data)(state);

const {
username,
Expand All @@ -236,7 +254,7 @@ export function dataValueSet(data) {
url
})
.then((result) => {
console.log("Success:", result);
console.log("Success:", result.body);
return {...state,
references: [result, ...state.references]
}
Expand All @@ -254,15 +272,13 @@ export function dataValueSet(data) {
* @example
* dataElement(key, value)
* @constructor
* @param {object} key - Payload data for the Data Element key
* @param {object} value - Payload data for the Data Element value
* @param {string} key - Payload data for the Data Element key
* @param {variable} value - Payload data for the Data Element value
* @param {string} comment - comment for the Data Element
* @returns {Operation}
*/
export function dataElement(key, value) {
return {
"dataElement": key,
"value": value
}
export function dataElement(dataElement, value, comment) {
return { dataElement, value, comment }
}

/**
Expand Down
8 changes: 5 additions & 3 deletions test/index.js
Expand Up @@ -79,10 +79,12 @@ describe("event", () => {
})

describe("dataElement", function() {
it("creates a on dataElement object object", function() {
let result = dataElement("key", "foo")
it("creates a dataElement from key, value, comment args", function() {
let element = dataElement("key", "foo")
expect(element).to.eql({ dataElement: "key", value: "foo", comment: undefined })

expect(result).to.eql({ dataElement: "key", value: "foo" })
let commentedElement = dataElement("key", "foo", "bar")
expect(commentedElement).to.eql({ dataElement: "key", value: "foo", comment: "bar" })
})
})

Expand Down

0 comments on commit 01204a8

Please sign in to comment.