-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo-app-ussdapp.js
1107 lines (937 loc) · 38.2 KB
/
go-app-ussdapp.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
// WARNING: This is a generated file.
// If you edit it you will be sad.
// Edit src/app.js instead.
var go = {};
go;
/*jshint -W083 */
var vumigo = require('vumigo_v02');
var moment = require('moment');
var assert = require('assert');
var _ = require('lodash');
var JsonApi = vumigo.http.api.JsonApi;
var Choice = vumigo.states.Choice;
var ChoiceState = vumigo.states.ChoiceState;
go.states = {
MessengerChoiceState: ChoiceState.extend(function(self, name, opts) {
/*
Automatically add the necessary helper metadata for
ChoiceStates when using the Messenger transport
*/
opts = _.defaults(opts || {}, {
helper_metadata: function () {
// disable for now
if(true) {
return {};
}
var i18n = self.im.user.i18n;
return {
messenger: {
template_type: 'generic',
title: i18n(opts.title),
subtitle: i18n(opts.question),
image_url: opts.image_url || '',
buttons: opts.choices.map(function(choice, index) {
return {
title: i18n(choice.label),
payload: {
content: (index + 1) + '',
in_reply_to: self.im.msg.message_id || null,
}
};
})
}
};
}
});
ChoiceState.call(self, name, opts);
}),
"trailing": "comma"
};
// GENERIC UTILS
go.utils = {
// FB HELPERS
get_user_profile: function(msg) {
return msg.helper_metadata.messenger || {};
},
// FIXTURES HELPERS
// function checks fixtures used against fixture expected
// if multiple_possibilities is true, expected_used can be an array of arrays
// representing possible valid combinations of fixtures
check_fixtures_used: function(api, expected_used, multiple_possibilities) {
var fixts = api.http.fixtures.fixtures;
var fixts_used = [];
fixts.forEach(function(f, i) {
f.uses > 0 ? fixts_used.push(i) : null;
});
if (multiple_possibilities) {
for(var i = 0; i < expected_used.length; i++) {
try {
assert.deepEqual(fixts_used, expected_used[i]);
break; // break if used fixtures match any of the valid_fixture_possibilities
}
catch(AssertionError) {
//console.log(AssertionError.message);
}
}
}
else {
assert.deepEqual(fixts_used, expected_used);
}
},
// TIMEOUT HELPERS
timed_out: function(im) {
return im.msg.session_event === 'new'
&& im.user.state.name
&& im.config.no_timeout_redirects.indexOf(im.user.state.name) === -1;
},
timeout_redirect: function(im) {
return im.config.timeout_redirects.indexOf(im.user.state.name) !== -1;
},
// SERVICE API CALL HELPERS
service_api_call: function (service, method, params, payload, endpoint, im) {
var http = new JsonApi(im, {
headers: {
'Authorization': ['Token ' + im.config.services[service].api_token]
}
});
switch (method) {
case "post":
return http.post(im.config.services[service].url + endpoint, {
data: payload
})
.then(go.utils.log_service_api_call(service, method, params, payload, endpoint, im));
case "get":
return http.get(im.config.services[service].url + endpoint, {
params: params
})
.then(go.utils.log_service_api_call(service, method, params, payload, endpoint, im));
case "patch":
return http.patch(im.config.services[service].url + endpoint, {
data: payload
})
.then(go.utils.log_service_api_call(service, method, params, payload, endpoint, im));
case "put":
return http.put(im.config.services[service].url + endpoint, {
params: params,
data: payload
})
.then(go.utils.log_service_api_call(service, method, params, payload, endpoint, im));
case "delete":
return http
.delete(im.config.services[service].url + endpoint)
.then(go.utils.log_service_api_call(service, method, params, payload, endpoint, im));
}
},
log_service_api_call: function(service, method, params, payload, endpoint, im) {
return function (response) {
return im
.log([
'Request: ' + method + ' ' + im.config.services[service].url + endpoint,
'Payload: ' + JSON.stringify(payload),
'Params: ' + JSON.stringify(params),
'Response: ' + JSON.stringify(response),
].join('\n'))
.then(function () {
return response;
});
};
},
// MSISDN HELPERS
// Check that it's a number and starts with 0 and approximate length
// TODO: refactor to take length, explicitly deal with '+'
is_valid_msisdn: function(content) {
return go.utils.check_valid_number(content)
&& content[0] === '0'
&& content.length >= 10
&& content.length <= 13;
},
normalize_msisdn: function(raw, country_code) {
// don't touch shortcodes
if (raw.length <= 5) {
return raw;
}
// remove chars that are not numbers or +
raw = raw.replace(/[^0-9+]/g);
if (raw.substr(0,2) === '00') {
return '+' + raw.substr(2);
}
if (raw.substr(0,1) === '0') {
return '+' + country_code + raw.substr(1);
}
if (raw.substr(0,1) === '+') {
return raw;
}
if (raw.substr(0, country_code.length) === country_code) {
return '+' + raw;
}
return raw;
},
// NUMBER HELPERS
// An attempt to solve the insanity of JavaScript numbers
check_valid_number: function(content) {
var numbers_only = new RegExp('^\\d+$');
return content !== ''
&& numbers_only.test(content)
&& !Number.isNaN(Number(content));
},
double_digit_number: function(input) {
input_numeric = parseInt(input, 10);
if (parseInt(input, 10) < 10) {
return "0" + input_numeric.toString();
} else {
return input_numeric.toString();
}
},
// DATE HELPERS
get_now: function(config) {
if (config.testing_today) {
return new moment(config.testing_today).format('YYYY-MM-DD HH:mm:ss');
} else {
return new moment().format('YYYY-MM-DD HH:mm:ss');
}
},
get_today: function(config) {
if (config.testing_today) {
return new moment(config.testing_today, 'YYYY-MM-DD');
} else {
return new moment();
}
},
get_january: function(config) {
// returns current year january 1st moment date
return go.utils.get_today(config).startOf('year');
},
is_valid_date: function(date, format) {
// implements strict validation with 'true' below
return moment(date, format, true).isValid();
},
is_valid_year: function(year, minYear, maxYear) {
// expects string parameters
// checks that the number is within the range determined by the
// minYear & maxYear parameters
return go.utils.check_valid_number(year)
&& parseInt(year, 10) >= parseInt(minYear, 10)
&& parseInt(year, 10) <= parseInt(maxYear, 10);
},
is_valid_day_of_month: function(input) {
// check that it is a number and between 1 and 31
return go.utils.check_valid_number(input)
&& parseInt(input, 10) >= 1
&& parseInt(input, 10) <= 31;
},
// TEXT HELPERS
check_valid_alpha: function(input) {
// check that all chars are in standard alphabet
var alpha_only = new RegExp('^[A-Za-z]+$');
return input !== '' && alpha_only.test(input);
},
is_valid_name: function(input, min, max) {
// check that the string does not include the characters listed in the
// regex, and min <= input string length <= max
var name_check = new RegExp(
'(^[^±!@£$%^&*_+§¡€#¢§¶•ªº«\\/<>?:;|=.,0123456789]{min,max}$)'
.replace('min', min.toString())
.replace('max', max.toString())
);
return input !== '' && name_check.test(input);
},
get_clean_first_word: function(user_message) {
return user_message
.split(" ")[0] // split off first word
.replace(/\W/g, '') // remove non letters
.toUpperCase(); // capitalise
},
// CHOICE HELPERS
make_month_choices: function($, startDate, limit, increment, valueFormat, labelFormat) {
var choices = [];
var monthIterator = startDate;
for (var i=0; i<limit; i++) {
choices.push(new Choice(monthIterator.format(valueFormat),
monthIterator.format(labelFormat)));
monthIterator.add(increment, 'months');
}
return choices;
},
// REGISTRATION HELPERS
create_registration: function(im, reg_info) {
return go.utils
.service_api_call("registrations", "post", null, reg_info, "registration/", im)
.then(function(result) {
return result.id;
});
},
// IDENTITY HELPERS
get_identity_by_address: function(address, im) {
// Searches the Identity Store for all identities with the provided address.
// Returns the first identity object found
// Address should be an object {address_type: address}, eg.
// {'msisdn': '0821234444'}, {'email': 'me@example.com'}
var address_type = Object.keys(address)[0];
var address_val = address[address_type];
var params = {};
var search_string = 'details__addresses__' + address_type;
params[search_string] = address_val;
return im
.log('Getting identity for: ' + JSON.stringify(params))
.then(function() {
return go.utils
.service_api_call('identities', 'get', params, null, 'identities/search/', im)
.then(function(json_get_response) {
var identities_found = json_get_response.data.results;
// Return the first identity in the list of identities
return (identities_found.length > 0)
? identities_found[0]
: null;
});
});
},
get_identity: function(identity_id, im) {
// Gets the identity from the Identity Store
// Returns the identity object
var endpoint = 'identities/' + identity_id + '/';
return go.utils
.service_api_call('identities', 'get', {}, null, endpoint, im)
.then(function(json_get_response) {
return json_get_response.data;
});
},
create_identity: function(im, address, communicate_through_id, operator_id) {
// Create a new identity
// Returns the identity object
var payload = {
"details": {
"default_addr_type": null,
"addresses": {}
}
};
// compile base payload
if (address) {
var address_type = Object.keys(address)[0];
var addresses = {};
addresses[address_type] = {};
addresses[address_type][address[address_type]] = {};
payload.details = {
"default_addr_type": address_type,
"addresses": addresses
};
}
if (communicate_through_id) {
payload.communicate_through = communicate_through_id;
}
// add operator_id if available
if (operator_id) {
payload.operator = operator_id;
}
return go.utils
.service_api_call("identities", "post", null, payload, 'identities/', im)
.then(function(json_post_response) {
return json_post_response.data;
});
},
get_or_create_identity: function(address, im, operator_id) {
// Gets a identity if it exists, otherwise creates a new one
if (address.msisdn) {
address.msisdn = go.utils
.normalize_msisdn(address.msisdn, im.config.country_code);
}
return go.utils
// Get identity id using address
.get_identity_by_address(address, im)
.then(function(identity) {
if (identity !== null) {
// If identity exists, return the id
return identity;
} else {
// If identity doesn't exist, create it
return go.utils
.create_identity(im, address, null, operator_id)
.then(function(identity) {
return identity;
});
}
});
},
update_identity: function(im, identity) {
// Update an identity by passing in the full updated identity object
// Removes potentially added fields that auto-complete and should not
// be submitted
// Returns the id (which should be the same as the identity's id)
auto_fields = ["url", "created_at", "updated_at", "created_by", "updated_by", "user"];
for (var i in auto_fields) {
field = auto_fields[i];
if (field in identity) {
delete identity[field];
}
}
var endpoint = 'identities/' + identity.id + '/';
return go.utils
.service_api_call('identities', 'patch', {}, identity, endpoint, im)
.then(function(response) {
return response.data.id;
});
},
// SUBSCRIPTION HELPERS
get_subscription: function(im, subscription_id) {
// Gets the subscription from the Stage-base Store
// Returns the subscription object
var endpoint = 'subscriptions/' + subscription_id + '/';
return go.utils
.service_api_call('subscriptions', 'get', {}, null, endpoint, im)
.then(function(response) {
return response.data;
});
},
get_active_subscriptions_by_identity: function(im, identity_id) {
// Searches the Stage-base Store for all active subscriptions with the provided identity_id
// Returns the first subscription object found or null if none are found
var params = {
identity: identity_id,
active: true
};
var endpoint = 'subscriptions/';
return go.utils
.service_api_call('subscriptions', 'get', params, null, endpoint, im)
.then(function(response) {
return response.data.results;
});
},
get_active_subscription_by_identity: function(im, identity_id) {
// Searches the Stage-base Store for all active subscriptions with the provided identity_id
// Returns the first subscription object found or null if none are found
return go.utils
.get_active_subscriptions_by_identity(im, identity_id)
.then(function(subscriptions_found) {
return (subscriptions_found.length > 0)
? subscriptions_found[0]
: null;
});
},
has_active_subscription: function(identity_id, im) {
// Returns whether an identity has an active subscription
// Returns true / false
return go.utils
.get_active_subscriptions_by_identity(im, identity_id)
.then(function(subscriptions) {
return subscriptions.length > 0;
});
},
update_subscription: function(im, subscription) {
// Update a subscription by passing in the full updated subscription object
// Returns the id (which should be the same as the subscription's id)
var endpoint = 'subscriptions/' + subscription.id + '/';
return go.utils
.service_api_call('subscriptions', 'patch', {}, subscription, endpoint, im)
.then(function(response) {
return response.data.id;
});
},
// MESSAGESET HELPERS
get_messageset: function(im, messageset_id) {
// Gets the messageset from the Stage-base Store
// Returns the messageset object
var endpoint = 'messageset/' + messageset_id + '/';
return go.utils
.service_api_call('subscriptions', 'get', {}, null, endpoint, im)
.then(function(response) {
return response.data;
});
},
// MESSAGE_SENDER HELPERS
save_inbound_message: function(im, from_addr, content) {
// Saves the inbound messages to seed-message-sender
var payload = {
"message_id": im.config.testing_message_id || im.msg.message_id,
"in_reply_to": null,
"to_addr": im.config.channel,
"from_addr": from_addr,
"content": content,
"transport_name": im.config.transport_name,
"transport_type": im.config.transport_type,
"helper_metadata": {}
};
return go.utils
.service_api_call("message_sender", "post", null, payload, 'inbound/', im)
.then(function(json_post_response) {
var inbound_response = json_post_response.data;
// Return the inbound id
return inbound_response.id;
});
},
// OPTOUT & OPTIN HELPERS
optout: function(im, identity_id, optout_reason, address_type, address,
request_source, requestor_source_id, optout_type, config) {
// Posts an optout to the identity store optout endpoint
var optout_info = {
optout_type: optout_type || 'stop', // default to 'stop'
identity: identity_id,
reason: optout_reason || 'unknown', // default to 'unknown'
address_type: address_type || 'msisdn', // default to 'msisdn'
address: address,
request_source: request_source,
requestor_source_id: requestor_source_id
};
return go.utils
.service_api_call("identities", "post", null, optout_info, "optout/", im)
.then(function(response) {
return response;
});
},
"commas": "commas"
};
/*jshint -W083 */
var vumigo = require("vumigo_v02");
var Choice = vumigo.states.Choice;
// Project utils library
go.utils_project = {
// REGISTRATION HELPERS
finish_registration: function(im) {
return go.utils
.get_identity(im.user.answers.user_id, im)
.then(function(identity) {
identity.details.registered = true;
identity.details.facility_code = im.user.answers.state_facility_code;
identity.details.gender = im.user.answers.state_gender;
identity.details.cadre = im.user.answers.state_cadre;
identity.details.department = im.user.answers.state_department;
return go.utils.update_identity(im, identity);
});
},
// QUIZ HELPERS
// returns an array of untaken quizzes
get_untaken_quizzes: function(im, user_id) {
var endpoint = "quiz/untaken";
var params = {
"identity": user_id
};
return go.utils
.service_api_call("continuous-learning", "get", params, null, endpoint, im)
.then(function(json_get_response) {
return json_get_response.data.results;
});
},
get_quiz: function(im, quiz_id) {
var endpoint = "quiz/"+quiz_id+"/";
return go.utils
.service_api_call("continuous-learning", "get", {}, null, endpoint, im)
.then(function(json_get_response) {
return json_get_response.data;
});
},
get_quiz_question: function(im, question_id) {
var endpoint = "question/"+question_id+"/";
return go.utils
.service_api_call("continuous-learning", "get", {}, null, endpoint, im)
.then(function(json_get_response) {
return json_get_response.data;
});
},
get_facility_codes: function(im) {
var endpoint = "facilitycode/";
return go.utils
.service_api_call("hub", "get", {}, null, endpoint, im)
.then(function(json_get_response) {
return json_get_response.data.results;
});
},
construct_faccode_choices: function(facility_codes) {
var choices = [];
for (var i = 0; i < facility_codes.length; i++) {
choices.push(new Choice(facility_codes[i].code, facility_codes[i].code));
}
return choices;
},
/* parameter to construct_choices function is an array of objects
e.g. [
{
"value": "mike",
"text": "Mike",
"correct": false
},
{
"value": "nicki",
"text": "Nicki",
"correct": true
}
],
where value/text to be used accordingly in ChoiceState and 'correct'
indicates correct quiz answer
returns an array of Choice objects representing answers for ChoiceState*/
construct_choices: function(possible_answers) {
var choices = [];
for (var i = 0; i < possible_answers.length; i++) {
choices.push(new Choice(possible_answers[i].value, possible_answers[i].text));
}
return choices;
},
// takes an array of answer-objects with properties 'value', 'text', 'correct';
// returns the value of the correct answer
get_correct_answer: function(possible_answers) {
for (var i = 0; i < possible_answers.length; i++) {
if (possible_answers[i].correct) {
return possible_answers[i].value;
}
}
},
// returns object to keep track of user's quiz status
init_quiz_status: function(quiz, questions_array) {
// to set quiz_status with quiz uuid, an array of outstanding questions
// to be answered, an array of questions answered, and a flag to indicate
// whether quiz is completed or not
return {"quiz": quiz, "questions_remaining": questions_array, "questions_answered": [], "completed": false};
},
// update the questions and answer part of user's quiz status
save_quiz_status: function(im) {
return go.utils
.get_identity(im.user.answers.user_id, im)
.then(function(identity) {
return go.utils.update_identity(im, identity);
});
},
// returns an object; first property represents the number of correct
// answers, and second the total number of questions asked, and the
// third the subsequent percentage of correct_answers out of questions asked
get_quiz_summary: function(questions_answered) {
var total_questions = questions_answered.length;
var correct_answers = 0;
var obj = questions_answered;
for (var x in obj) {
if (obj[x].correct) correct_answers++;
}
return {
"correct_answers": correct_answers,
"total_questions": total_questions,
"percentage": (correct_answers/total_questions).toFixed(2)*100
};
},
// taking identity_uuid and quiz_uuid, returns tracker_uuid
init_tracker: function(im, identity_id, quiz_id) {
var payload = {
"identity": identity_id,
"quiz": quiz_id
};
return go.utils
.service_api_call("continuous-learning", "post", null, payload, 'tracker/', im)
.then(function(json_post_response) {
return json_post_response.data.id;
});
},
log_quiz_answer: function(im, quiz_question, answer_value, answer_text, answer_correct, response, tracker_id) {
var payload = {
"question": quiz_question.id,
"question_text": quiz_question.question,
"answer_value": answer_value,
"answer_text": answer_text,
"answer_correct": answer_correct,
"response_sent": response,
"tracker": tracker_id
};
return go.utils
.service_api_call("continuous-learning", "post", null, payload, 'answer/', im)
.then(function(json_post_response) {
return json_post_response.data;
});
},
close_tracker: function(im, tracker_id) {
var endpoint = "tracker/"+tracker_id+"/";
var payload = {
"complete": true,
"completed_at": go.utils.get_now(im.config)
};
return go.utils
.service_api_call("continuous-learning", "patch", null, payload, endpoint, im)
.then(function(json_post_response) {
return json_post_response.data;
});
},
// SMS HELPERS
send_completion_text: function(im, to_addr, text_to_add) {
var sms_content = "Your results from today's quiz:"+text_to_add;
var payload = {
"to": to_addr,
"content": sms_content
};
return go.utils
.service_api_call("junebug", "post", null, payload, 'messages/', im)
.then(function(json_post_response) {
var outbound_response = json_post_response.data;
// Return the outbound id
return outbound_response.result.message_id;
});
},
"commas": "commas"
};
go.app = function() {
var vumigo = require("vumigo_v02");
var App = vumigo.App;
var Choice = vumigo.states.Choice;
var ChoiceState = vumigo.states.ChoiceState;
var PaginatedChoiceState = vumigo.states.PaginatedChoiceState;
var EndState = vumigo.states.EndState;
var FreeText = vumigo.states.FreeText;
var _ = require('lodash');
var GoUOPBMOH = App.extend(function(self) {
App.call(self, "state_start");
var $ = self.$;
self.init = function() {};
// TEXT CONTENT
var questions = {
"state_already_registered":
$("You are already registered for this service. Contact your administrator if you have any queries"),
"state_facility_code":
$("Please choose your facility code:"),
"state_gender":
$("Please enter your gender:"),
"state_cadre":
$("Please enter your cadre"),
"state_department":
$("Please enter your department name"),
"state_end_registration":
$("Thank you for registering. You'll soon be receiving quizzes."),
"state_end_quiz":
$("Thank you for completing your quiz. You correctly answered {{correct_answers}} of {{total_questions}} questions. Your score is {{score_percentage}}%"),
"state_end_quiz_status":
$("Currently you've got no untaken quizzes."),
"state_end_thank_you":
$("Thank you for using our service."),
};
var errors = {
"state_facility_code":
$("Invalid facility code. Please choose your facility code:"),
};
// override normal state adding
self.add = function(name, creator) {
self.states.add(name, function(name, opts) {
return creator(name, opts);
});
};
// START STATE
self.add("state_start", function(name) {
return self.states.create("state_check_registered");
});
// interstitial to check registration status
self.add("state_check_registered", function(name) {
return go.utils
.get_or_create_identity({"msisdn": self.im.user.addr}, self.im, null)
.then(function(identity) {
self.im.user.set_answer("user_id", identity.id);
if(identity.details && !identity.details.registered) {
return self.states.create("state_facility_code");
} else {
return self.states.create("state_check_quiz_status");
}
});
});
// EndState
self.add("state_already_registered", function(name) {
return new EndState(name, {
text: questions[name],
next: "state_start"
});
});
// REGISTRATION STATES
// FreeText st-01
self.add("state_facility_code", function(name) {
return go.utils_project
// get first question in the now random line-up
.get_facility_codes(self.im)
.then(function(facility_codes) {
var choices = go.utils_project.construct_faccode_choices(facility_codes);
return new PaginatedChoiceState(name, {
question: questions[name],
error: errors[name],
characters_per_page: 160,
options_per_page: null,
more: $('More'),
back: $('Back'),
choices: choices,
next: "state_gender"
});
});
return new FreeText(name, {
question: questions[name],
next: "state_gender"
});
});
// ChoiceState st-02
self.add("state_gender", function(name) {
return new ChoiceState(name, {
question: questions[name],
choices: [
new Choice("male", $("Male")),
new Choice("female", $("Female"))
],
next: "state_cadre"
});
});
// FreeText st-03
self.add("state_cadre", function(name) {
return new FreeText(name, {
question: questions[name],
next: "state_department"
});
});
// FreeText st-04
self.add("state_department", function(name) {
return new FreeText(name, {
question: questions[name],
next: function() {
return go.utils_project
.finish_registration(self.im)
.then(function() {
return "state_end_registration";
});
}
});
});
// EndState st-05
self.add("state_end_registration", function(name) {
return new EndState(name, {
text: questions[name],
next: "state_start"
});
});
// QUIZ STATES
// interstitial
self.add("state_check_quiz_status", function(name) {
return go.utils_project
.get_untaken_quizzes(self.im, self.im.user.answers.user_id)
.then(function(untaken_quizzes) {
if (untaken_quizzes.length > 0) {
// get random quiz to take
var quiz_to_take = self.im.config.randomize_quizzes
? untaken_quizzes[Math.floor(Math.random() * untaken_quizzes.length)]
: untaken_quizzes[0];
return go.utils_project
.init_tracker(self.im, self.im.user.answers.user_id, quiz_to_take.id)
.then(function(tracker_id) {
self.im.user.set_answer("tracker", tracker_id);
return self.states.create("state_get_quiz_questions", quiz_to_take.id);
});
} else {
return self.states.create("state_end_quiz_status");
}
});
});
self.add("state_get_quiz_questions", function(name, quiz_id) {
return go.utils_project
.get_quiz(self.im, quiz_id)
.then(function(quiz) {
// creates a random line-up of questions
var random_questions = self.im.config.randomize_questions
? _.shuffle(quiz.questions)
: quiz.questions;
var quiz_status = go.utils_project.init_quiz_status(quiz_id, random_questions);
self.im.user.set_answer("quiz_status", quiz_status);
self.im.user.set_answer("sms_results_text", "");
return self.states.create("state_quiz");
});
});
// ChoiceState
self.add("state_quiz", function(name) {
return go.utils_project
// get first question in the now random line-up
.get_quiz_question(self.im, self.im.user.answers.quiz_status.questions_remaining[0])
.then(function(quiz_question) {
var correct_answer = go.utils_project.get_correct_answer(quiz_question.answers);
return new ChoiceState(name, {
question: quiz_question.question,
choices: go.utils_project.construct_choices(quiz_question.answers),
next: function(choice) {
var response_text = "";
var correct = choice.value === correct_answer;
if (correct) {