-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo-app-ussd_tb_check.js
1886 lines (1715 loc) · 62.9 KB
/
go-app-ussd_tb_check.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
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.app = function () {
var vumigo = require("vumigo_v02");
var _ = require("lodash");
var App = vumigo.App;
var Choice = vumigo.states.Choice;
var EndState = vumigo.states.EndState;
var JsonApi = vumigo.http.api.JsonApi;
var MenuState = vumigo.states.MenuState;
var FreeText = vumigo.states.FreeText;
var ChoiceState = vumigo.states.ChoiceState;
var utils = require("seed-jsbox-utils").utils;
var GoNDOH = App.extend(function (self) {
App.call(self, "state_start");
var $ = self.$;
self.init = function() {
self.rapidpro = new go.RapidPro(
new JsonApi(self.im, {headers: {'User-Agent': ["Jsbox/TBCheck"]}}),
self.im.config.rapidpro.base_url,
self.im.config.rapidpro.token
);
};
self.sessionID = function () {
return '_' + Math.random().toString(36).substr(2, 10);
};
self.calculate_risk = function () {
var answers = self.im.user.answers;
if (answers.state_exposure == "yes") {
return "high";
}
var symptom_count = _.filter([
answers.state_fever,
answers.state_sweat,
answers.state_weight,
]).length;
if (answers.state_cough == "yes" || symptom_count >= 1) {
return "moderate";
}
return "low";
};
self.get_activation = function () {
if (!self.im.config.activations) return null;
var to_regex = new RegExp(self.im.config.activations.to_regex);
var to_addr = self.im.msg.to_addr;
if (!to_addr) return null;
var groups = to_addr.match(to_regex);
if (!groups) return null;
var code_map = self.im.config.activations.code_map;
var to_code = groups[1];
if (!(to_code in code_map)) return null;
return code_map[to_code];
};
self.add = function (name, creator) {
self.states.add(name, function (name, opts) {
if (self.im.msg.session_event !== "new") return creator(name, opts);
var timeout_opts = opts || {};
timeout_opts.name = name;
return self.states.create("state_timed_out", timeout_opts);
});
};
self.states.add("state_timed_out", function (name, creator_opts) {
return new MenuState(name, {
question: $(
"Welcome back to the The National Department of Health's TB HealthCheck"
),
accept_labels: true,
choices: [
new Choice(creator_opts.name, $("Continue where I left off")),
new Choice("state_start", $("Start over")),
],
});
});
self.states.add("state_start", function (name, opts) {
var activation = self.get_activation();
if (activation === "tb_study_a" || activation === "tb_study_b" || activation === "tb_study_c"){
return self.states.create("state_is_activation_active");
}
else{
return self.states.create("state_get_contact");
}
});
self.states.add("state_get_contact", function (name, opts) {
var msisdn = utils.normalize_msisdn(self.im.user.addr, "ZA");
var activation;
if (self.im.user.answers.activation !== undefined){
activation = self.im.user.answers.activation;
}
else{
activation = self.get_activation();
}
if (activation === "tb_study_a_survey_group1" || activation === "tb_study_a_survey_group2") {
return self.states.create("state_survey_start");
}
return new JsonApi(self.im)
.get(
self.im.config.healthcheck.url +
"/v2/healthcheckuserprofile/" +
msisdn +
"/",
{
headers: {
Authorization: ["Token " + self.im.config.healthcheck.token],
"User-Agent": ["Jsbox/TBCheck-USSD"],
},
}
)
.then(
function (response) {
self.im.user.answers = {
activation: activation,
returning_user: true,
state_gender: response.data.gender,
state_province: response.data.province,
state_city: response.data.city ? response.data.city : "<not collected>",
city_location: response.data.city_location,
state_age: response.data.age,
state_language: response.data.language,
state_research_consent: response.data.research_consent,
state_privacy_policy_accepted: _.get(response.data, "data.tb_privacy_policy_accepted"),
study_completed: response.data.activation,
};
if ((activation === "tb_study_a" || activation === "tb_study_b" || activation === "tb_study_c") &&
(response.data.activation === "tb_study_a" || response.data.activation === "tb_study_b" ||
response.data.activation === "tb_study_c")) {
return self.states.create("state_study_already_completed");
}
if (response.data.language != "eng"){
self.im.user.set_lang(response.data.language)
.then(function() {
return self.states.create("state_terms");
});
}
return self.states.create("state_welcome");
},
function (e) {
// If it's 404, new user
if (_.get(e, "response.code") === 404) {
self.im.user.answers = { returning_user: false, activation: activation };
return self.states.create("state_terms");
}
// Go to error state after 3 failed HTTP requests
opts.http_error_count = _.get(opts, "http_error_count", 0) + 1;
if (opts.http_error_count === 3) {
self.im.log.error(e.message);
return self.states.create("__error__", { return_state: name });
}
return self.states.create(name, opts);
}
);
});
self.states.add("state_study_already_completed", function(name) {
self.im.user.answers = { activation: null };
return new MenuState(name, {
question: $(
"Unfortunately, you cannot participate in the study more " +
"than once. You can still continue with a TB Check but " +
"you will not be included in the study."
),
accept_labels: true,
choices: [
new Choice("state_welcome", $("Continue"))]
});
});
self.states.add("state_welcome", function (name) {
var error = $(
"This service works best when you choose number options from the list."
);
var question = $(
"The National Department of Health thanks you for helping to protect the health of all SA citizens. Stop the spread of TB."
);
return new MenuState(name, {
question: question,
error: error,
accept_labels: true,
choices: [new Choice("state_age", $("START"))],
});
});
self.states.add("state_terms", function (name) {
var next = "state_send_privacy_policy_sms";
if (self.im.user.answers.returning_user) {
return self.states.create(next);
}
return new MenuState(name, {
question: $(
[
"This NDoH service only provides health info. Please agree that you are " +
"responsible for your own medical care and treatment."
].join("\n")
),
error: $(
[
"This NDoH service only provides health info. Please agree that you are " +
"responsible for your own medical care and treatment."
].join("\n")
),
accept_labels: true,
choices: [
new Choice(next, $("YES")),
new Choice("state_end", $("NO")),
new Choice("state_more_info_pg1", $("MORE INFO")),
],
});
});
self.states.add("state_end", function (name) {
return new EndState(name, {
text: $(
"Return to use this service at any time. Remember, if you think you have TB, " +
"avoid contact with other people and get tested at your nearest clinic."
),
next: "state_start",
});
});
self.add("state_more_info_pg1", function (name) {
return new MenuState(name, {
question: $(
"TB HealthCheck does not replace medical advice, diagnosis or treatment. Get" +
" a qualified health provider's advice on your medical condition and care."
),
accept_labels: true,
choices: [new Choice("state_more_info_pg2", $("Next"))],
});
});
self.add("state_more_info_pg2", function (name) {
return new MenuState(name, {
question: $(
"You use this info at your own risk. This tool cannot replace medical advice. " +
"Agree not to ignore or delay getting medical advice on treatment or care"
),
accept_labels: true,
choices: [new Choice("state_terms", $("Next"))],
});
});
self.states.add("state_send_privacy_policy_sms", function(name, opts) {
var next_state = "state_privacy_policy_accepted";
if (self.im.user.answers.state_privacy_policy_accepted == "yes") {
return self.states.create(next_state);
}
var flow_uuid = self.im.config.rapidpro.privacy_policy_sms_flow;
var msisdn = utils.normalize_msisdn(
_.get(
self.im.user.answers, "state_enter_msisdn", self.im.user.addr), "ZA");
var data = {"hc_type": "tb"};
if (self.im.user.answers.state_language) {
data.language = self.im.user.answers.state_language;
}
return self.rapidpro
.start_flow(flow_uuid, null, "tel:" + msisdn, data)
.then(function() {
return self.states.create("state_privacy_policy_accepted");
}).catch(function(e) {
// Go to error state after 3 failed HTTP requests
opts.http_error_count = _.get(opts, "http_error_count", 0) + 1;
if(opts.http_error_count === 3) {
self.im.log.error(e.message);
return self.states.create("__error__", {return_state: name});
}
return self.states.create(name, opts);
});
});
self.states.add("state_privacy_policy_accepted", function(name) {
var next_state = "state_language";
var activation = self.im.user.answers.activation;
if(!activation){
next_state = "state_core_language";
}
if (self.im.user.answers.state_privacy_policy_accepted == "yes") {
return self.states.create(next_state);
}
return new MenuState(name, {
question: $(
"Your personal information is protected under POPIA and in accordance " +
"with the provisions of the TB HealthCheck Privacy Notice sent to you by SMS."
),
accept_labels: true,
choices: [new Choice(next_state, $("ACCEPT"))],
});
});
self.add("state_core_language", function (name) {
var next_state = "state_welcome";
if (self.im.user.answers.state_language) {
return self.im.user.set_lang(self.im.user.answers.state_language)
.then(function() {
return self.states.create(next_state);
});
}
return new ChoiceState(name, {
question: $("Choose your preferred language"),
error: $("Please reply with numbers. Choose your preferred language"),
accept_labels: true,
choices: [
new Choice("eng", $("English")),
new Choice("zul", $("isiZulu")),
new Choice("afr", $("Afrikaans")),
new Choice("xho", $("isiXhosa")),
new Choice("sot", $("Sesotho")),
new Choice("set", $("Setswana")),
],
next: function(choice) {
self.im.user.answers.state_language = choice.value;
if (choice.value != "eng"){
return self.im.user.set_lang(choice.value)
.then(function() {
return self.states.create(next_state);
});
}
return self.states.create(next_state);
}
});
});
self.add("state_language", function (name) {
var next_state = "state_welcome";
if (self.im.user.answers.state_language) {
return self.im.user.set_lang(self.im.user.answers.state_language)
.then(function() {
return self.states.create(next_state);
});
}
return new ChoiceState(name, {
question: $("Choose your preferred language"),
error: $("Please reply with numbers. Choose your preferred language"),
accept_labels: true,
choices: [
new Choice("eng", $("English")),
new Choice("zul", $("isiZulu")),
new Choice("afr", $("Afrikaans")),
new Choice("xho", $("isiXhosa")),
new Choice("sot", $("Sesotho")),
],
next: function(choice) {
if (choice.value != "eng"){
return self.im.user.set_lang(choice.value)
.then(function() {
return self.states.create(next_state);
});
}
return self.states.create(next_state);
}
});
});
self.add("state_research_consent", function(name) {
if (self.im.user.answers.state_age === "<18"){
return self.states.create("state_gender");
}
return new MenuState(name, {
question: $(
"If you agree, we will use your information to see if this TB Check helps people."+
"\nDo you agree?"
),
error: $(
"Please reply with numbers. Do you agree?"
),
accept_labels: true,
choices: [
new Choice("state_gender", $("Yes")),
new Choice("state_research_consent_no", $("No")),
new Choice("state_faq", $("FAQ for more info on TBCheck and the research.")),
],
});
});
self.add("state_age", function (name) {
var activation = self.im.user.answers.activation;
var next_state = "state_gender";
if (activation === "tb_study_a" || activation === "tb_study_b" || activation === "tb_study_c") {
next_state = "state_research_consent";
}
if (self.im.user.answers.state_age) {
return self.states.create("state_gender");
}
return new ChoiceState(name, {
question: $("How old are you?"),
error: $(["Please use numbers from list.", "", "How old are you?"].join("\n")),
accept_labels: true,
choices: [
new Choice("<18", $("under 18")),
new Choice("18-40", $("18-39")),
new Choice("40-65", $("40-65")),
new Choice(">65", $("over 65")),
],
next: next_state,
});
});
self.add("state_research_consent_no", function(name) {
return new MenuState(name, {
question: $(
"Okay, no problem. You will not be included in the research, "+
"but you can still continue to check if you need to take a TB test."
),
accept_labels: true,
choices: [
new Choice("state_gender", $("Next"))],
});
});
self.add("state_gender", function (name) {
var activation = self.im.user.answers.activation;
var next = "state_province";
if (self.im.user.answers.state_gender && activation != "undefined") {
next = "state_cough";
}
if (activation === "tb_study_a" || activation === "tb_study_b" || activation === "tb_study_c") {
next = "state_cough";
}
return new ChoiceState(name, {
question: $("Which gender do you identify as?"),
error: $(
[
"Please use numbers from list.",
"",
"Which gender do you identify as?",
].join("\n")
),
accept_labels: true,
choices: [
new Choice("male", $("MALE")),
new Choice("female", $("FEMALE")),
new Choice("other", $("OTHER")),
new Choice("not_say", $("RATHER NOT SAY")),
],
next: next,
});
});
self.add("state_province", function (name) {
var next_state = "state_suburb_name";
if (self.im.user.answers.state_age === "<18"){
next_state = "state_cough";
}
else if (self.im.user.answers.state_province) {
return self.states.create("state_suburb_name");
}
return new ChoiceState(name, {
question: $("Choose your province"),
accept_labels: true,
choices: [
new Choice("ZA-EC", $("E. CAPE")),
new Choice("ZA-FS", $("FREE STATE")),
new Choice("ZA-GT", $("GAUTENG")),
new Choice("ZA-NL", $("KWAZULU NATAL")),
new Choice("ZA-LP", $("LIMPOPO")),
new Choice("ZA-MP", $("MPUMALANGA")),
new Choice("ZA-NW", $("NORTH WEST")),
new Choice("ZA-NC", $("N. CAPE")),
new Choice("ZA-WC", $("W. CAPE")),
],
next: next_state,
});
});
self.add("state_street_name", function (name) {
if ((_.toUpper(self.im.user.answers.state_confirm_city)) != "STATE_STREET_NAME")
{
if (self.im.user.answers.state_street_name &&
self.im.user.answers.state_suburb_name) {
return self.states.create("state_city");
}
}
var question = $(
"Please type the name of the street where you live."
);
return new FreeText(name, {
question: question,
check: function (content) {
// Ensure that they're not giving an empty response
if (!content.trim()) {
return question;
}
},
next: "state_suburb_name",
});
});
self.add("state_suburb_name", function (name) {
if ((_.toUpper(self.im.user.answers.state_confirm_city)) != "STATE_STREET_NAME")
{
if (self.im.user.answers.state_suburb_name) {
return self.states.create("state_city");
}
}
var question = $(
"Please type the name of the suburb/township/village where you live."
);
return new FreeText(name, {
question: question,
check: function (content) {
// Ensure that they're not giving an empty response
if (!content.trim()) {
return question;
}
},
next: "state_city",
});
});
self.add("state_city", function (name) {
if (
self.im.user.answers.state_suburb_name &&
self.im.user.answers.state_city &&
self.im.user.answers.city_location
) {
return self.states.create("state_cough");
}
if(self.im.user.answers.state_age === "<18") {
self.im.user.answers.state_city = "<not collected>";
return self.states.create("state_cough");
}
var question = $(
"Please type the name of the city where you live."
);
if (self.im.user.answers.state_city_error === "TRUE"){
question = $([
"Sorry, we don't understand. Please try again.",
"",
"Please type the name of the city where you live"
].join("\n"));
}
return new FreeText(name, {
question: question,
check: function (content) {
// Ensure that they're not giving an empty response
if (!content.trim()) {
return question;
}
},
next: "state_google_places_lookup",
});
});
self.add("state_google_places_lookup", function (name, opts) {
var street_name = self.im.user.answers.state_street_name;
var suburb = self.im.user.answers.state_suburb_name;
var city_trunc = self.im.user.answers.state_city;
var full_address = (suburb + ',' + city_trunc).slice(0, 160 - 101);
var activation = self.im.user.answers.activation;
if (activation === "tb_study_a") {
full_address = (street_name + ',' + suburb + ',' + city_trunc).slice(0, 160 - 101);
}
self.im.user.answers.state_city = full_address;
return new JsonApi(self.im)
.get("https://maps.googleapis.com/maps/api/place/autocomplete/json", {
params: {
input: self.im.user.answers.state_city,
key: self.im.config.google_places.key,
sessiontoken: self.im.user.answers.google_session_token,
language: "en",
components: "country:za",
},
headers: {
"User-Agent": ["Jsbox/TB-Check-USSD"],
},
})
.then(
function (response) {
if (_.get(response.data, "status") !== "OK") {
self.im.user.answers.state_city_error = "TRUE";
return self.states.create("state_city");
}
var first_result = response.data.predictions[0];
self.im.user.answers.place_id = first_result.place_id;
self.im.user.answers.state_city = first_result.description;
return self.states.create("state_confirm_city");
},
function (e) {
// Go to error state after 3 failed HTTP requests
opts.http_error_count = _.get(opts, "http_error_count", 0) + 1;
if (opts.http_error_count === 3) {
self.im.log.error(e.message);
return self.states.create("__error__", { return_state: name });
}
return self.states.create(name, opts);
}
);
});
self.add("state_confirm_city", function (name, opts) {
var state_city = (self.im.user.answers.state_city).slice(0, 36);
var no_next_state = "state_suburb_name";
return new MenuState(name, {
question: $(
[
"Please check that the address below is correct and matches the information you gave us:",
"{{ address }}"
].join("\n")
).context({ address: state_city }),
accept_labels: true,
choices: [
new Choice("state_place_details_lookup", $("Yes")),
new Choice(no_next_state, $("No")),
],
});
});
self.pad_location = function (location, places) {
// Pads the integer part of the number to places
// Ensures that there's always a sign
// Ensures that there's always a decimal part
var sign = "+";
if (location < 0) {
sign = "-";
location = location * -1;
}
location = _.split(location, ".", 2);
var int = location[0];
var dec = location[1] || 0;
return sign + _.padStart(int, places, "0") + "." + dec;
};
self.add("state_place_details_lookup", function (name, opts) {
var answers = self.im.user.answers;
return new JsonApi(self.im)
.get("https://maps.googleapis.com/maps/api/place/details/json", {
params: {
key: self.im.config.google_places.key,
place_id: self.im.user.answers.place_id,
sessiontoken: self.im.user.answers.google_session_token,
language: "en",
fields: "geometry",
},
headers: {
"User-Agent": ["Jsbox/TB-Check-USSD"],
},
})
.then(
function (response) {
if (_.get(response.data, "status") !== "OK") {
return self.states.create("__error__");
}
var location = response.data.result.geometry.location;
self.im.user.answers.city_location =
self.pad_location(location.lat, 2) +
self.pad_location(location.lng, 3) +
"/";
answers.location_lat = location.lat;
answers.location_lng = location.lng;
return self.states.create("state_cough");
},
function (e) {
// Go to error state after 3 failed HTTP requests
opts.http_error_count = _.get(opts, "http_error_count", 0) + 1;
if (opts.http_error_count === 3) {
self.im.log.error(e.message);
return self.states.create("__error__", { return_state: name });
}
return self.states.create(name, opts);
}
);
});
self.add("state_cough", function (name) {
var question = $(
"Let's see how you're feeling today. Do you have a cough?"
);
var error = $(
[
"Please use numbers from list.",
"Do you have a cough?"
].join("\n")
);
return new ChoiceState(name, {
question: question,
error: error,
accept_labels: true,
choices: [new Choice("yes", $("YES")), new Choice("no", $("NO"))],
next: "state_fever",
});
});
self.add("state_fever", function (name) {
return new ChoiceState(name, {
question: $(
"Do you have a fever? (when you touch your forehead, does it feel hot?)"
),
error: $(
[
"Please use numbers from list. Do you have a fever? (when you touch your " +
"forehead, does it feel hot?)",
].join("\n")
),
accept_labels: true,
choices: [new Choice(true, $("YES")), new Choice(false, $("NO"))],
next: "state_sweat",
});
});
self.add("state_sweat", function (name) {
return new ChoiceState(name, {
question: $("Are you sweating more than usual at night?"),
error: $("Please use numbers from list. Are you sweating more than usual at night?"),
accept_labels: true,
choices: [new Choice(true, $("YES")), new Choice(false, $("NO"))],
next: "state_weight",
});
});
self.add("state_weight", function (name) {
return new ChoiceState(name, {
question: $("Have you been losing weight without trying?"),
error: $("Please use numbers from list. Have you been losing weight without trying?"),
accept_labels: true,
choices: [new Choice(true, $("YES")), new Choice(false, $("NO"))],
next: "state_exposure",
});
});
self.add("state_exposure", function (name) {
var next_state = "state_tracing";
var activation = self.im.user.answers.activation;
if (activation === "tb_study_a" || activation === "tb_study_b" || activation === "tb_study_c"){
next_state = "state_study_tracing";
}
return new ChoiceState(name, {
question: $(
[
"Are you at high risk of TB?",
"",
"Risk means you live with someone who has TB OR you had TB in the last 2 years OR you are HIV+"
].join("\n")
),
error: $("Please use numbers from list. Are you at high risk for TB?"),
accept_labels: true,
choices: [
new Choice("yes", $("Yes high risk")),
new Choice("no", $("No")),
new Choice("not_sure", $("Dont know")),
],
next: next_state,
});
});
self.add("state_tracing", function (name) {
var next_state = "state_opt_in";
var question = $(
"Now, please agree that the info you shared is correct and that you give " +
"the NDoH permission to contact you if needed?"
);
var error = $(
[
"Now, please agree that the info you shared is correct and that you give " +
"the NDoH permission to contact you if needed?"
].join("\n")
);
var choices = [
new Choice(true, $("YES")),
new Choice(false, $("NO")),
];
return new ChoiceState(name, {
question: question,
error: error,
accept_labels: true,
choices: choices,
next: function (response) {
return next_state;
},
});
});
self.add("state_study_tracing", function (name) {
var next_state = "state_opt_in";
var activation = self.im.user.answers.activation;
if (activation === "tb_study_a" || activation === "tb_study_b" || activation === "tb_study_c"){
next_state = "state_submit_data";
}
var question = $(
"Finally, please agree that the info you shared is correct."
);
var error = $(
[
"Finally, please agree that the info you shared is correct."
].join("\n")
);
var choices = [
new Choice(true, $("YES")),
new Choice(false, $("NO")),
];
return new ChoiceState(name, {
question: question,
error: error,
accept_labels: true,
choices: choices,
next: function (response) {
return next_state;
},
});
});
self.states.add("state_opt_in", function (name) {
var question = $(
[
"Thanks for your answers. Your result will be sent soon by SMS. Would you like " +
"to receive follow-up messages?"
].join("\n")
);
var error = $(
[
"Thanks for your answers. Your result will be sent soon by SMS. Would you like " +
"to receive follow-up messages?"
].join("\n")
);
var choices = [new Choice(true, $("Yes")), new Choice(false, $("No"))];
return new ChoiceState(name, {
question: question,
error: error,
accept_labels: true,
choices: choices,
next: "state_submit_data",
});
});
self.states.add("state_submit_data", function (name, opts) {
var answers = self.im.user.answers;
var msisdn = utils.normalize_msisdn(self.im.user.addr, "ZA");
var activation = self.get_activation();
if (activation === "tb_study_a" || activation === "tb_study_b" || activation === "tb_study_c"){
self.im.user.answers.state_tracing = answers.state_study_tracing;
}
var payload = {
data: {
msisdn: msisdn,