diff --git a/go-app-ussd_tb_check.js b/go-app-ussd_tb_check.js index ad5fcc6d..8e06470f 100644 --- a/go-app-ussd_tb_check.js +++ b/go-app-ussd_tb_check.js @@ -205,8 +205,26 @@ go.app = function () { }); self.states.add("state_start", function (name, opts) { - 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"){ + 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"); } @@ -1228,6 +1246,29 @@ go.app = function () { }); }); + self.add("state_reached_capacity", function (name) { + return new MenuState(name, { + question: $(["The SUN Research Study has reached capacity. WCDoH strongly encourages you to screen for your own benefit.", + "", + "Do you want to screen?" + ].join("\n") + ), + error: $("Please use numbers from list. Do you want to screen?"), + accept_labels: true, + choices: [new Choice("state_welcome", $("YES")), + new Choice("state_reached_capacity_no_option", $("NO"))], + }); + }); + + self.states.add("state_reached_capacity_no_option", function (name) { + var text = $("Come back and use this service any time. Remember, if you think you have TB, avoid contact with other people and get tested at your nearest clinic."); + + return new EndState(name, { + text: text, + next: "state_start", + }); + }); + self.states.add("state_show_results", function (name) { var answers = self.im.user.answers; var risk = self.calculate_risk(); @@ -1271,6 +1312,46 @@ go.app = function () { }); }); + self.states.add("state_is_activation_active", function (name, opts) { + var activation = self.get_activation(); + + var payload = { + data: {activation: activation}, + headers: { + Authorization: ["Token " + self.im.config.healthcheck.token], + "User-Agent": ["Jsbox/TB-Check-USSD"], + }, + }; + + return new JsonApi(self.im) + .post(self.im.config.healthcheck.url + "/v1/tbactivationstatus/", payload) + .then( + function (response) { + // Get activation status + var is_active = response.data.is_activation_active; + + if (is_active){ + self.im.user.answers.state_city = activation; + return self.states.create("state_get_contact"); + } + else{ + // Set activation to Null if activation is not active + self.im.user.answers.state_city = undefined; + return self.states.create("state_reached_capacity"); + } + }, + 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_survey_start", function (name) { var end = "state_survey_double_participation"; var survey_complete = _.toUpper(_.get(self.im.user.get_answer("contact"), "fields.survey_complete", $("None"))); diff --git a/src/ussd_tb_check.js b/src/ussd_tb_check.js index 6f86b7a8..e1069b9b 100644 --- a/src/ussd_tb_check.js +++ b/src/ussd_tb_check.js @@ -88,8 +88,26 @@ go.app = function () { }); self.states.add("state_start", function (name, opts) { - 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"){ + 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"); } @@ -1111,6 +1129,29 @@ go.app = function () { }); }); + self.add("state_reached_capacity", function (name) { + return new MenuState(name, { + question: $(["The SUN Research Study has reached capacity. WCDoH strongly encourages you to screen for your own benefit.", + "", + "Do you want to screen?" + ].join("\n") + ), + error: $("Please use numbers from list. Do you want to screen?"), + accept_labels: true, + choices: [new Choice("state_welcome", $("YES")), + new Choice("state_reached_capacity_no_option", $("NO"))], + }); + }); + + self.states.add("state_reached_capacity_no_option", function (name) { + var text = $("Come back and use this service any time. Remember, if you think you have TB, avoid contact with other people and get tested at your nearest clinic."); + + return new EndState(name, { + text: text, + next: "state_start", + }); + }); + self.states.add("state_show_results", function (name) { var answers = self.im.user.answers; var risk = self.calculate_risk(); @@ -1154,6 +1195,46 @@ go.app = function () { }); }); + self.states.add("state_is_activation_active", function (name, opts) { + var activation = self.get_activation(); + + var payload = { + data: {activation: activation}, + headers: { + Authorization: ["Token " + self.im.config.healthcheck.token], + "User-Agent": ["Jsbox/TB-Check-USSD"], + }, + }; + + return new JsonApi(self.im) + .post(self.im.config.healthcheck.url + "/v1/tbactivationstatus/", payload) + .then( + function (response) { + // Get activation status + var is_active = response.data.is_activation_active; + + if (is_active){ + self.im.user.answers.state_city = activation; + return self.states.create("state_get_contact"); + } + else{ + // Set activation to Null if activation is not active + self.im.user.answers.state_city = undefined; + return self.states.create("state_reached_capacity"); + } + }, + 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_survey_start", function (name) { var end = "state_survey_double_participation"; var survey_complete = _.toUpper(_.get(self.im.user.get_answer("contact"), "fields.survey_complete", $("None"))); diff --git a/test/ussd_tb_check.test.js b/test/ussd_tb_check.test.js index 5ed25880..6394ed52 100644 --- a/test/ussd_tb_check.test.js +++ b/test/ussd_tb_check.test.js @@ -56,6 +56,7 @@ describe("ussd_tb_check app", function () { describe("set_activation", function() { it("should set activation for new user", function() { return tester + .setup.user.state("state_get_contact") .setup(function (api) { api.http.fixtures.add({ request: { @@ -648,6 +649,7 @@ describe("ussd_tb_check app", function () { }); it("should show an error if users already completed a study", function () { return tester + .setup.user.state("state_get_contact") .setup(function (api) { api.http.fixtures.add({ request: { @@ -807,7 +809,7 @@ describe("ussd_tb_check app", function () { }) .run(); }); - it("should show state_research_consent_no on no consent", function () { + it("should show state_research_consent_no on no consent", function () { return tester.setup.user .state("state_research_consent") .input("2") @@ -2239,7 +2241,7 @@ describe("ussd_tb_check app", function () { it("state_start skip to survey", function() { return tester - .setup.user.state("state_start") + .setup.user.state("state_get_contact") .inputs({ session_event: "continue", to_addr: "*123*123*5#" }) .check.user.state("state_survey_start") .run(); @@ -2646,4 +2648,92 @@ describe("ussd_tb_check app", function () { .run(); }); }); + describe("State_start with activations", function(){ + it("should ask if they want to continue with screening", function () { + return tester.setup.user + .state("state_reached_capacity") + .check.interaction({ + state: "state_reached_capacity", + reply: [ + "The SUN Research Study has reached capacity. WCDoH strongly "+ + "encourages you to screen for your own benefit.", + "", + "Do you want to screen?", + "1. YES", + "2. NO", + ].join("\n"), + char_limit: 160, + }) + .run(); + }); + it("should show state_welcome if they want to continue with screening", function () { + return tester.setup.user + .state("state_reached_capacity") + .input("1") + .check.user.state("state_welcome") + .run(); + }); + it("should go to state_reached_capacity_no_option if they don't want to continue", function () { + return tester.setup.user + .state("state_reached_capacity") + .input("2") + .check.interaction({ + state: "state_reached_capacity_no_option", + reply: + "Come back and use this service any time. Remember, if you think you have TB, " + + "avoid contact with other people and get tested at your nearest clinic.", + char_limit: 160, + }) + .run(); + }); + it("should show state_reached_capacity_no_option if they don't want to continue with screening", function () { + return tester.setup.user + .state("state_reached_capacity") + .input("2") + .check.user.state("state_reached_capacity_no_option") + .run(); + }); + it("Should get state_is_activation_active status", function() { + return tester.setup.user + .state("state_is_activation_active") + .setup(function (api) { + api.http.fixtures.add({ + request: { + url: "http://healthcheck/v1/tbactivationstatus/", + method: "POST", + data: { + "activation": "tb_study_a" + }, + }, + response: { + code: 200, + data: {is_activation_active: true}, + }, + }); + + api.http.fixtures.add({ + request: { + url: "http://healthcheck/v2/healthcheckuserprofile/+27123456789/", + method: "GET" + }, + response: { + code: 200, + data: { + activation: null, + state_gender: "MALE", + state_province: "ZA-WC", + state_city: null, + city_location: "+00-025/", + state_age: "18-39", + state_language: "eng", + data: {tb_privacy_policy_accepted: "yes"}, + } + } + }); + }) + .inputs({ session_event: "continue", to_addr: "*123*123*7#" }) + .check.user.state("state_welcome") + .run(); + }); + }); });