-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo-app-ussd_nurse_rapidpro.js
258 lines (228 loc) · 9.11 KB
/
go-app-ussd_nurse_rapidpro.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
var go = {};
go;
go.RapidPro = function() {
var vumigo = require('vumigo_v02');
var url_utils = require('url');
var events = vumigo.events;
var Eventable = events.Eventable;
var RapidPro = Eventable.extend(function(self, json_api, base_url, auth_token) {
self.json_api = json_api;
self.base_url = base_url;
self.auth_token = auth_token;
self.json_api.defaults.headers.Authorization = ['Token ' + self.auth_token];
self.json_api.defaults.headers['User-Agent'] = ['NDoH-JSBox/RapidPro'];
self.get_contact = function(filters) {
filters = filters || {};
var url = self.base_url + "/api/v2/contacts.json";
return self.json_api.get(url, {params: filters})
.then(function(response){
var contacts = response.data.results;
if(contacts.length > 0){
return contacts[0];
}
else {
return null;
}
});
};
self.update_contact = function(filter, details) {
var url = self.base_url + "/api/v2/contacts.json";
return self.json_api.post(url, {params: filter, data: details})
.then(function(response) {
return response.data;
});
};
self.create_contact = function(details) {
var url = self.base_url + "/api/v2/contacts.json";
return self.json_api.post(url, {data: details})
.then(function(response) {
return response.data;
});
};
self._get_paginated_response = function(url, params) {
/* Gets all the pages of a paginated response */
return self.json_api.get(url, {params: params})
.then(function(response){
var results = response.data.results;
if(response.data.next === null) {
return results;
}
var query = url_utils.parse(response.data.next).query;
return self._get_paginated_response(url, query)
.then(function(response) {
return results.concat(response);
});
});
};
self.get_flows = function(filter) {
var url = self.base_url + "/api/v2/flows.json";
return self._get_paginated_response(url, filter);
};
self.get_flow_by_name = function(name) {
name = name.toLowerCase().trim();
return self.get_flows().then(function(flows){
flows = flows.filter(function(flow) {
return flow.name.toLowerCase().trim() === name;
});
if(flows.length > 0) {
return flows[0];
} else {
return null;
}
});
};
self.start_flow = function(flow_uuid, contact_uuid, contact_urn, extra) {
var url = self.base_url + "/api/v2/flow_starts.json";
var data = {flow: flow_uuid};
if(contact_uuid) {
data.contacts = [contact_uuid];
}
if(contact_urn) {
data.urns = [contact_urn];
}
if(extra) {
data.extra = extra;
}
return self.json_api.post(url, {data: data});
};
self.get_global_flag = function(global_name) {
var url = self.base_url + "/api/v2/globals.json";
return self.json_api.get(url, {params: {key: global_name}})
.then(function(response){
var results = response.data.results;
if(results.length > 0){
return results[0].value.toLowerCase() === "true";
}
else {
return false;
}
});
};
});
return RapidPro;
}();
go.OpenHIM = function() {
var vumigo = require('vumigo_v02');
var events = vumigo.events;
var Eventable = events.Eventable;
var Q = require('q');
var SeedJsboxUtils = require('seed-jsbox-utils');
var utils = SeedJsboxUtils.utils;
var moment = require('moment');
var url = require("url");
var _ = require("lodash");
var OpenHIM = Eventable.extend(function(self, http_api, base_url, username, password) {
self.http_api = http_api;
self.base_url = base_url;
self.http_api.defaults.auth = {username: username, password: password};
self.http_api.defaults.headers['Content-Type'] = ['application/json; charset=utf-8'];
self.validate_clinic_code = function(clinic_code, endpoint) {
/* Returns the clinic name if clinic code is valid, otherwise returns false */
if (!utils.check_valid_number(clinic_code) || clinic_code.length !== 6) {
return Q(false);
}
else {
var api_url = url.resolve(self.base_url, endpoint);
var params = {
criteria: "value:" + clinic_code
};
return self.http_api.get(api_url, {params: params})
.then(function(result) {
result = JSON.parse(result.body);
var rows = result.rows;
if (rows.length === 0) {
return false;
} else {
return rows[0][_.findIndex(result.headers, ["name", "name"])];
}
});
}
};
self.validate_nc_clinic_code = function(clinic_code) {
return self.validate_clinic_code(clinic_code, "NCfacilityCheck");
};
self.validate_mc_clinic_code = function(clinic_code) {
return self.validate_clinic_code(clinic_code, "facilityCheck");
};
self.uuidv4 = function(mock) {
if(mock !== undefined) {
return mock;
}
// From https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
};
self.submit_nc_registration = function(contact, mock_eid) {
var api_url = url.resolve(self.base_url, "nc/subscription");
var msisdn = contact.urns.filter(function(urn) {
// Starts with tel:
return urn.search('tel:') == 0;
})[0].replace("tel:", "");
// We don't retry this HTTP request, so we can generate a random event ID
var eid = self.uuidv4(mock_eid);
return self.http_api.post(api_url, {data: JSON.stringify({
mha: 1,
swt: contact.fields.preferred_channel === "whatsapp" ? 7 : 1,
type: 7,
sid: contact.uuid,
eid: eid,
dmsisdn: contact.fields.registered_by,
cmsisdn: msisdn,
rmsisdn: null,
faccode: contact.fields.facility_code,
id: msisdn.replace("+", "") + "^^^ZAF^TEL",
dob: null,
persal: contact.fields.persal || null,
sanc: contact.fields.sanc || null,
encdate: moment(contact.fields.registration_date).utc().format('YYYYMMDDHHmmss')
})});
};
});
return OpenHIM;
}();
go.app = function() {
var vumigo = require('vumigo_v02');
var Choice = vumigo.states.Choice;
var MenuState = vumigo.states.MenuState;
var EndState = vumigo.states.EndState;
var App = vumigo.App;
var GoNDOH = App.extend(function(self) {
App.call(self, 'state_start');
var $ = self.$;
self.states.add("state_start", function(name) {
var text = $(
"NurseConnect is now part of HealthWorkerConnect - a service for " +
"all health workers in SA. To find out more and how to join reply:"
);
return new MenuState(name, {
question: text,
accept_labels: true,
choices: [new Choice("state_join", $("Next"))],
});
});
self.states.add("state_join", function(name) {
return new EndState(name, {
text: $(
"HealthWorkerConnect is a trusted information and support service " +
"from the National Department of Health. To join, send hi on " +
"WhatsApp to 060 060 1111."
),
next: "state_start",
});
});
});
return {
GoNDOH: GoNDOH
};
}();
/* globals api */
go.init = function() {
var vumigo = require('vumigo_v02');
var InteractionMachine = vumigo.InteractionMachine;
var GoNDOH = go.app.GoNDOH;
return {
im: new InteractionMachine(api, new GoNDOH())
};
}();