This repository has been archived by the owner on Oct 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Adaptor.js
184 lines (155 loc) · 5.38 KB
/
Adaptor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.lastReferenceValue = exports.dataValue = exports.dataPath = exports.merge = exports.each = 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; };
exports.execute = execute;
exports.submit = submit;
exports.fetchReportData = fetchReportData;
var _languageCommon = require('language-common');
Object.defineProperty(exports, 'field', {
enumerable: true,
get: function get() {
return _languageCommon.field;
}
});
Object.defineProperty(exports, 'fields', {
enumerable: true,
get: function get() {
return _languageCommon.fields;
}
});
Object.defineProperty(exports, 'sourceValue', {
enumerable: true,
get: function get() {
return _languageCommon.sourceValue;
}
});
Object.defineProperty(exports, 'each', {
enumerable: true,
get: function get() {
return _languageCommon.each;
}
});
Object.defineProperty(exports, 'merge', {
enumerable: true,
get: function get() {
return _languageCommon.merge;
}
});
Object.defineProperty(exports, 'dataPath', {
enumerable: true,
get: function get() {
return _languageCommon.dataPath;
}
});
Object.defineProperty(exports, 'dataValue', {
enumerable: true,
get: function get() {
return _languageCommon.dataValue;
}
});
Object.defineProperty(exports, 'lastReferenceValue', {
enumerable: true,
get: function get() {
return _languageCommon.lastReferenceValue;
}
});
var _Client = require('./Client');
var _url = require('url');
var _js2xmlparser = require('js2xmlparser');
var _js2xmlparser2 = _interopRequireDefault(_js2xmlparser);
var _languageHttp = require('language-http');
var _languageHttp2 = _interopRequireDefault(_languageHttp);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 get = _languageHttp2.default.get;
var post = _languageHttp2.default.post;
/** @module Adaptor */
/**
* Execute a sequence of operations.
* Wraps `language-common/execute`, and prepends initial state for commcare.
* @example
* execute(
* create('foo'),
* delete('bar')
* )(state)
* @constructor
* @param {Operations} operations - Operations to be performed.
* @returns {Operation}
*/
function execute() {
for (var _len = arguments.length, operations = Array(_len), _key = 0; _key < _len; _key++) {
operations[_key] = arguments[_key];
}
var initialState = {
references: [],
data: null
};
return function (state) {
state.configuration.authType = 'basic';
state.configuration.baseUrl = "https://www.commcarehq.org/a/".concat(state.configuration.applicationName);
return _languageCommon.execute.apply(undefined, operations)(_extends({}, initialState, state));
};
}
function submit(formData) {
return function (state) {
var jsonBody = (0, _languageCommon.expandReferences)(formData)(state);
var body = (0, _js2xmlparser2.default)("data", jsonBody);
var _state$configuration = state.configuration;
var applicationName = _state$configuration.applicationName;
var username = _state$configuration.username;
var password = _state$configuration.password;
var appId = _state$configuration.appId;
var hostUrl = _state$configuration.hostUrl;
var url = (hostUrl || "https://www.commcarehq.org").concat('/a/', applicationName, '/receiver/', appId, '/');
console.log("Posting to url: ".concat(url));
console.log("Raw JSON body: ".concat(JSON.stringify(jsonBody)));
console.log("X-form submission: ".concat(body));
return (0, _Client.post)({
url: url,
body: body,
username: username,
password: password
}).then(function (result) {
console.log("Success:", result);
return _extends({}, state, {
references: [result].concat(_toConsumableArray(state.references))
});
});
};
}
/**
* Make a GET request to CommCare's Reports Api
* and POST the response it somewhere else
* @example
* execute(
* fetchReportData(reportId, params, postUrl)
* )(state)
* @constructor
* @param {String} reportId - API name of the report.
* @param {Object} params - Query params, incl: limit, offset, and custom report filters.
* @param {String} postUrl - Url to which the response object will be posted.
* @returns {Operation}
*/
function fetchReportData(reportId, params, postUrl) {
return get('api/v0.5/configurablereportdata/' + reportId + '/', {
query: function query(state) {
console.log("getting from url: ".concat(state.configuration.baseUrl, '/api/v0.5/configurablereportdata/' + reportId + '/'));
console.log("with params: ".concat(params));
return params;
},
callback: function callback(state) {
var reportData = state.response.body;
return post(postUrl, {
body: reportData
})(state).then(function (state) {
delete state.response;
console.log("fetchReportData succeeded.");
console.log("Posted to: ".concat(postUrl));
return state;
});
}
});
}