Skip to content

Commit

Permalink
Added research capacity restriction
Browse files Browse the repository at this point in the history
  • Loading branch information
Buhle79 committed Nov 14, 2023
1 parent f1d86d7 commit de4867b
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 1 deletion.
73 changes: 73 additions & 0 deletions go-app-ussd_tb_check.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,20 @@ go.app = function () {
});

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 = self.get_activation();

if (activation === "tb_study_a_survey_group1" || activation === "tb_study_a_survey_group2") {
return self.states.create("state_survey_start");
}
Expand Down Expand Up @@ -1228,6 +1240,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();
Expand Down Expand Up @@ -1271,6 +1306,44 @@ 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){
return self.states.create("state_get_contact");
}
else{
// Set activation to Null if activation is not active
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")));
Expand Down
73 changes: 73 additions & 0 deletions src/ussd_tb_check.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,20 @@ go.app = function () {
});

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 = self.get_activation();

if (activation === "tb_study_a_survey_group1" || activation === "tb_study_a_survey_group2") {
return self.states.create("state_survey_start");
}
Expand Down Expand Up @@ -1111,6 +1123,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();
Expand Down Expand Up @@ -1154,6 +1189,44 @@ 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){
return self.states.create("state_get_contact");
}
else{
// Set activation to Null if activation is not active
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")));
Expand Down
91 changes: 90 additions & 1 deletion test/ussd_tb_check.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,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: {
Expand Down Expand Up @@ -807,7 +808,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")
Expand Down Expand Up @@ -2646,4 +2647,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();
});
});
});

0 comments on commit de4867b

Please sign in to comment.