Skip to content

Commit

Permalink
Merge pull request #605 from praekeltfoundation/popi-change-msisdn-sm…
Browse files Browse the repository at this point in the history
…s-global-control

Msisdn change globals check
  • Loading branch information
DevChima authored Oct 11, 2023
2 parents c19d86b + f253900 commit 60d3b50
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 47 deletions.
43 changes: 0 additions & 43 deletions go-app-ussd_chw_rapidpro.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,6 @@
var go = {};
go;

go.Hub = function() {
var vumigo = require('vumigo_v02');
var events = vumigo.events;
var Eventable = events.Eventable;
var url = require("url");

var Hub = 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.send_whatsapp_template_message = function(msisdn, template_name, media) {
var api_url = url.resolve(self.base_url, "/api/v1/sendwhatsapptemplate");
var data = {
"msisdn": msisdn,
"template_name": template_name
};
if(media) {
data.media = media;
}
return self.json_api.post(api_url, {data: data})
.then(function(response){
return response.data.preferred_channel;

});
};

self.get_whatsapp_failure_count = function(msisdn) {
var api_url = url.resolve(self.base_url, "/api/v2/deliveryfailure/" + msisdn + "/");

return self.json_api.get(api_url)
.then(
function(response){
return response.data.number_of_failures;
}
);
};

});
return Hub;
}();

go.RapidPro = function() {
var vumigo = require('vumigo_v02');
var url_utils = require('url');
Expand Down
52 changes: 51 additions & 1 deletion go-app-ussd_popi_rapidpro.js
Original file line number Diff line number Diff line change
Expand Up @@ -1314,7 +1314,7 @@ go.app = function() {
"You've entered {{msisdn}} as your new MomConnect number. Is this correct?"
).context({msisdn: msisdn}),
choices: [
new Choice("state_msisdn_change", $("Yes")),
new Choice("state_send_popi_template_message", $("Yes")),
new Choice("state_msisdn_change_enter", $("No, I want to try again"))
],
error: $(
Expand All @@ -1324,6 +1324,56 @@ go.app = function() {
});
});

self.add("state_send_popi_template_message", function(name, opts) {
var msisdn = utils.normalize_msisdn(
self.im.user.answers.state_msisdn_change_enter, "ZA"
);
var template_name = self.im.config.popi_template;
var media = {
"filename": self.im.config.popi_filename,
"id": self.im.config.popi_media_uuid
};

return self.hub
.send_whatsapp_template_message(msisdn, template_name, media)
.then(function(preferred_channel) {
self.im.user.set_answer("preferred_channel", preferred_channel);
if (preferred_channel == "SMS") {
return self.rapidpro.get_global_flag("sms_registrations_enabled")
.then(function(sms_registration_enabled) {
if (sms_registration_enabled) {
return self.states.create("state_msisdn_change");
}
else{
return self.states.create("state_sms_registration_not_available");
}
});
}
return self.states.create("state_msisdn_change");
}).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_sms_registration_not_available", function(name) {
return new EndState(name, {
next: "state_start",
text: $([
"It seems this number is not on WhatsApp and we don't offer SMS at this moment.",
"",
"Please register the new number on WhatsApp for the MomConnect Service."
].join("\n"))
});
});

self.add("state_msisdn_change", function(name, opts) {
var contact = self.im.user.answers.contact;
var channel = _.get(contact, "fields.preferred_channel");
Expand Down
52 changes: 51 additions & 1 deletion src/ussd_popi_rapidpro.js
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,7 @@ go.app = function() {
"You've entered {{msisdn}} as your new MomConnect number. Is this correct?"
).context({msisdn: msisdn}),
choices: [
new Choice("state_msisdn_change", $("Yes")),
new Choice("state_send_popi_template_message", $("Yes")),
new Choice("state_msisdn_change_enter", $("No, I want to try again"))
],
error: $(
Expand All @@ -1164,6 +1164,56 @@ go.app = function() {
});
});

self.add("state_send_popi_template_message", function(name, opts) {
var msisdn = utils.normalize_msisdn(
self.im.user.answers.state_msisdn_change_enter, "ZA"
);
var template_name = self.im.config.popi_template;
var media = {
"filename": self.im.config.popi_filename,
"id": self.im.config.popi_media_uuid
};

return self.hub
.send_whatsapp_template_message(msisdn, template_name, media)
.then(function(preferred_channel) {
self.im.user.set_answer("preferred_channel", preferred_channel);
if (preferred_channel == "SMS") {
return self.rapidpro.get_global_flag("sms_registrations_enabled")
.then(function(sms_registration_enabled) {
if (sms_registration_enabled) {
return self.states.create("state_msisdn_change");
}
else{
return self.states.create("state_sms_registration_not_available");
}
});
}
return self.states.create("state_msisdn_change");
}).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_sms_registration_not_available", function(name) {
return new EndState(name, {
next: "state_start",
text: $([
"It seems this number is not on WhatsApp and we don't offer SMS at this moment.",
"",
"Please register the new number on WhatsApp for the MomConnect Service."
].join("\n"))
});
});

self.add("state_msisdn_change", function(name, opts) {
var contact = self.im.user.answers.contact;
var channel = _.get(contact, "fields.preferred_channel");
Expand Down
108 changes: 106 additions & 2 deletions test/ussd_popi_rapidpro.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ describe("ussd_popi_rapidpro app", function() {
optout_flow_id: "optout-flow",
research_optout_flow: "research-optout-flow",
edd_dob_change_flow_uuid: "edd-dob-change-flow-uuid",
sms_engagement_flow_id: "sms-engagement-flow"
sms_engagement_flow_id: "sms-engagement-flow",
popi_template: "popi_template",
popi_filename: "privacy_policy.pdf",
popi_media_uuid: "media-uuid",
});
});

Expand Down Expand Up @@ -1266,7 +1269,7 @@ describe("ussd_popi_rapidpro app", function() {
})
.run();
});
it("should trigger the number change if they select yes", function() {
it("should trigger the number change if SMS is enabled", function() {
return tester
.setup.user.state("state_msisdn_change_confirm")
.setup.user.answers({
Expand All @@ -1277,6 +1280,106 @@ describe("ussd_popi_rapidpro app", function() {
}},
})
.setup(function(api) {
api.http.fixtures.add(
fixtures_hub.send_whatsapp_template_message(
"+27820001001",
"popi_template",
{
"filename": "privacy_policy.pdf",
"id": "media-uuid"
},
"SMS"
)
);
api.http.fixtures.add(
fixtures_rapidpro.get_global_flag("sms_registrations_enabled", "TRUE")
);
api.http.fixtures.add(
fixtures_rapidpro.start_flow(
"msisdn-change-flow", null, "whatsapp:27820001001", {
new_msisdn: "+27820001001",
old_msisdn: "+27123456789",
contact_uuid: "contact-uuid",
source: "POPI USSD",
old_channel: "WhatsApp",
new_wa_id: "whatsapp:27820001001",
}
)
);

})
.input("1")
.check.user.state("state_msisdn_change_success")
.run();
});
it("should NOT trigger the number change if SMS is disabled", function() {
return tester
.setup.user.state("state_msisdn_change_confirm")
.setup.user.answers({
state_msisdn_change_enter: "0820001001",
contact: {uuid: "contact-uuid",
fields: {
preferred_channel: "WhatsApp"
}},
})
.setup(function(api) {
api.http.fixtures.add(
fixtures_hub.send_whatsapp_template_message(
"+27820001001",
"popi_template",
{
"filename": "privacy_policy.pdf",
"id": "media-uuid"
},
"SMS"
)
);
api.http.fixtures.add(
fixtures_rapidpro.get_global_flag("sms_registrations_enabled", "FALSE")
);
api.http.fixtures.add(
fixtures_rapidpro.start_flow(
"msisdn-change-flow", null, "whatsapp:27820001001", {
new_msisdn: "+27820001001",
old_msisdn: "+27123456789",
contact_uuid: "contact-uuid",
source: "POPI USSD",
old_channel: "WhatsApp",
new_wa_id: "whatsapp:27820001001",
}
)
);

})
.input("1")
.check.user.state("state_sms_registration_not_available")
.run();
});
it("should trigger the number change if SMS is disabled and preferred channel is WhatsApp", function() {
return tester
.setup.user.state("state_msisdn_change_confirm")
.setup.user.answers({
state_msisdn_change_enter: "0820001001",
contact: {uuid: "contact-uuid",
fields: {
preferred_channel: "WhatsApp"
}},
})
.setup(function(api) {
api.http.fixtures.add(
fixtures_hub.send_whatsapp_template_message(
"+27820001001",
"popi_template",
{
"filename": "privacy_policy.pdf",
"id": "media-uuid"
},
"WhatsApp"
)
);
api.http.fixtures.add(
fixtures_rapidpro.get_global_flag("sms_registrations_enabled", "FALSE")
);
api.http.fixtures.add(
fixtures_rapidpro.start_flow(
"msisdn-change-flow", null, "whatsapp:27820001001", {
Expand All @@ -1289,6 +1392,7 @@ describe("ussd_popi_rapidpro app", function() {
}
)
);

})
.input("1")
.check.user.state("state_msisdn_change_success")
Expand Down

0 comments on commit 60d3b50

Please sign in to comment.