From f253900a8a0d16ea87dbae0d6f308966215705e8 Mon Sep 17 00:00:00 2001 From: devchima Date: Wed, 11 Oct 2023 02:36:16 -0400 Subject: [PATCH] Msisdn change globals check --- go-app-ussd_chw_rapidpro.js | 43 ------------- go-app-ussd_popi_rapidpro.js | 52 ++++++++++++++- src/ussd_popi_rapidpro.js | 52 ++++++++++++++- test/ussd_popi_rapidpro.test.js | 108 +++++++++++++++++++++++++++++++- 4 files changed, 208 insertions(+), 47 deletions(-) diff --git a/go-app-ussd_chw_rapidpro.js b/go-app-ussd_chw_rapidpro.js index 09076211..822dda30 100644 --- a/go-app-ussd_chw_rapidpro.js +++ b/go-app-ussd_chw_rapidpro.js @@ -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'); diff --git a/go-app-ussd_popi_rapidpro.js b/go-app-ussd_popi_rapidpro.js index 88498306..2c831554 100644 --- a/go-app-ussd_popi_rapidpro.js +++ b/go-app-ussd_popi_rapidpro.js @@ -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: $( @@ -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"); diff --git a/src/ussd_popi_rapidpro.js b/src/ussd_popi_rapidpro.js index 634e80df..289eed4a 100644 --- a/src/ussd_popi_rapidpro.js +++ b/src/ussd_popi_rapidpro.js @@ -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: $( @@ -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"); diff --git a/test/ussd_popi_rapidpro.test.js b/test/ussd_popi_rapidpro.test.js index 438f0b77..b9f4be85 100644 --- a/test/ussd_popi_rapidpro.test.js +++ b/test/ussd_popi_rapidpro.test.js @@ -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", }); }); @@ -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({ @@ -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", { @@ -1289,6 +1392,7 @@ describe("ussd_popi_rapidpro app", function() { } ) ); + }) .input("1") .check.user.state("state_msisdn_change_success")