diff --git a/go-app-ussd_popi_rapidpro.js b/go-app-ussd_popi_rapidpro.js index ee0c75e1..a3dbc73a 100644 --- a/go-app-ussd_popi_rapidpro.js +++ b/go-app-ussd_popi_rapidpro.js @@ -466,6 +466,7 @@ go.app = function() { self.add("state_change_info", function(name) { var contact = self.im.user.answers.contact; + var answers = self.im.user.answers; var dates_count, postbirth; var dates_list = []; var channel = _.get(contact, "fields.preferred_channel"); @@ -564,6 +565,7 @@ go.app = function() { more: $("Next"), back: $("Previous"), next: function(choice) { + answers.update_value = choice.label; return choice.value; } }); @@ -872,21 +874,35 @@ go.app = function() { var year = answers.state_edd_baby_unborn_year || answers.state_baby_born_year; var month = answers.state_edd_baby_unborn_month || answers.state_baby_born_month; var day = answers.state_edd_baby_unborn_day || answers.state_baby_born_day; + var baby_date = new moment.utc( + year + month + day, + "YYYYMMDD" + ).format(); if ((typeof answers.state_edd_baby_unborn_year != "undefined")){ data.change_type = "edd_baby_expected"; - data.baby_edd = new moment.utc( - year + month + day, - "YYYYMMDD" - ).format(), + data.baby_edd = baby_date, end_flow = "state_edd_baby_unborn_complete"; } + else if ((answers.update_value) && (answers.update_value.startsWith("1st") || + answers.update_value.startsWith("2nd") || + answers.update_value.startsWith("3rd"))) { + if (answers.update_value.startsWith("1st")){ + data.change_baby = "baby_dob1"; + } + else if (answers.update_value.startsWith("2nd")){ + data.change_baby = "baby_dob2"; + } + else if (answers.update_value.startsWith("3rd")){ + data.change_baby = "baby_dob3"; + } + data.change_type = "change_baby_dob"; + data.baby_dob = baby_date, + end_flow = "state_baby_born_complete"; + } else { data.change_type = "baby_born"; - data.baby_dob = new moment.utc( - year + month + day, - "YYYYMMDD" - ).format(), + data.baby_dob = baby_date, end_flow = "state_baby_born_complete"; } return self.rapidpro diff --git a/src/ussd_popi_rapidpro.js b/src/ussd_popi_rapidpro.js index aa5ed2c9..82903dc9 100644 --- a/src/ussd_popi_rapidpro.js +++ b/src/ussd_popi_rapidpro.js @@ -294,6 +294,7 @@ go.app = function() { self.add("state_change_info", function(name) { var contact = self.im.user.answers.contact; + var answers = self.im.user.answers; var dates_count, postbirth; var dates_list = []; var channel = _.get(contact, "fields.preferred_channel"); @@ -392,6 +393,7 @@ go.app = function() { more: $("Next"), back: $("Previous"), next: function(choice) { + answers.update_value = choice.label; return choice.value; } }); @@ -700,21 +702,35 @@ go.app = function() { var year = answers.state_edd_baby_unborn_year || answers.state_baby_born_year; var month = answers.state_edd_baby_unborn_month || answers.state_baby_born_month; var day = answers.state_edd_baby_unborn_day || answers.state_baby_born_day; + var baby_date = new moment.utc( + year + month + day, + "YYYYMMDD" + ).format(); if ((typeof answers.state_edd_baby_unborn_year != "undefined")){ data.change_type = "edd_baby_expected"; - data.baby_edd = new moment.utc( - year + month + day, - "YYYYMMDD" - ).format(), + data.baby_edd = baby_date, end_flow = "state_edd_baby_unborn_complete"; } + else if ((answers.update_value) && (answers.update_value.startsWith("1st") || + answers.update_value.startsWith("2nd") || + answers.update_value.startsWith("3rd"))) { + if (answers.update_value.startsWith("1st")){ + data.change_baby = "baby_dob1"; + } + else if (answers.update_value.startsWith("2nd")){ + data.change_baby = "baby_dob2"; + } + else if (answers.update_value.startsWith("3rd")){ + data.change_baby = "baby_dob3"; + } + data.change_type = "change_baby_dob"; + data.baby_dob = baby_date, + end_flow = "state_baby_born_complete"; + } else { data.change_type = "baby_born"; - data.baby_dob = new moment.utc( - year + month + day, - "YYYYMMDD" - ).format(), + data.baby_dob = baby_date, end_flow = "state_baby_born_complete"; } return self.rapidpro diff --git a/test/ussd_popi_rapidpro.test.js b/test/ussd_popi_rapidpro.test.js index f2a52e6c..42bbd372 100644 --- a/test/ussd_popi_rapidpro.test.js +++ b/test/ussd_popi_rapidpro.test.js @@ -414,6 +414,32 @@ describe("ussd_popi_rapidpro app", function() { }) .run(); }); + it("should show option to update baby DOB 1", function() { + return tester + .setup.user.state("state_change_info") + .setup.user.answer("contact", {fields: {preferred_channel: "WhatsApp", + baby_dob1: "2021-03-10", + baby_dob2: "2021-11-11", + baby_dob3: "2022-07-07", + edd: "2020-06-04T00:00:00Z", + postbirth_messaging: "True", + }, + }) + .input("4") + .check.user.answer("update_value", "1st Baby's DoB: 10-03-2021") + .check.interaction({ + reply: [ + "Which year was your baby born? " + + "Please reply with a number that matches your answer, " + + "not the year e.g. 1.", + "1. 2022", + "2. 2021", + "3. 2020", + "4. Other" + ].join("\n") + }) + .run(); + }); it("should display the list of options to the user SMS page 2", function() { return tester .setup.user.state("state_change_info") @@ -760,6 +786,105 @@ describe("ussd_popi_rapidpro app", function() { }) .run(); }); + it("should trigger the baby dob change on confirm for 1st baby update DOB", function() { + return tester + .setup.user.state("state_baby_born_confirm_date") + .setup.user.answer("state_baby_born_year", "2022") + .setup.user.answer("state_baby_born_month", "04") + .setup.user.answer("state_baby_born_day", "4") + .setup.user.answer("update_value", "1st Baby's DOB") + + .setup(function(api) { + api.http.fixtures.add( + fixtures_rapidpro.start_flow( + "edd-dob-change-flow-uuid", null, "whatsapp:27123456789", { + timestamp: "2022-04-04T07:07:07Z", + change_type: "change_baby_dob", + baby_dob: '2022-04-04T00:00:00Z', + change_baby: "baby_dob1" + } + ) + ); + }) + .input("1") + .check.user.state("state_baby_born_complete") + .check.interaction({ + reply: [ + "Your baby's date of birth has been updated " + + "to 2022-04-04 and you will start receiving " + + "messages based on this schedule.", + "1. Back", + "2. Exit" + ].join("\n") + }) + .run(); + }); + it("should trigger the baby dob change on confirm for 2nd baby update DOB", function() { + return tester + .setup.user.state("state_baby_born_confirm_date") + .setup.user.answer("state_baby_born_year", "2022") + .setup.user.answer("state_baby_born_month", "04") + .setup.user.answer("state_baby_born_day", "4") + .setup.user.answer("update_value", "2nd Baby's DOB") + + .setup(function(api) { + api.http.fixtures.add( + fixtures_rapidpro.start_flow( + "edd-dob-change-flow-uuid", null, "whatsapp:27123456789", { + timestamp: "2022-04-04T07:07:07Z", + change_type: "change_baby_dob", + baby_dob: '2022-04-04T00:00:00Z', + change_baby: "baby_dob2" + } + ) + ); + }) + .input("1") + .check.user.state("state_baby_born_complete") + .check.interaction({ + reply: [ + "Your baby's date of birth has been updated " + + "to 2022-04-04 and you will start receiving " + + "messages based on this schedule.", + "1. Back", + "2. Exit" + ].join("\n") + }) + .run(); + }); + it("should trigger the baby dob change on confirm for 3rd baby update DOB", function() { + return tester + .setup.user.state("state_baby_born_confirm_date") + .setup.user.answer("state_baby_born_year", "2022") + .setup.user.answer("state_baby_born_month", "04") + .setup.user.answer("state_baby_born_day", "4") + .setup.user.answer("update_value", "3rd Baby's DOB") + + .setup(function(api) { + api.http.fixtures.add( + fixtures_rapidpro.start_flow( + "edd-dob-change-flow-uuid", null, "whatsapp:27123456789", { + timestamp: "2022-04-04T07:07:07Z", + change_type: "change_baby_dob", + baby_dob: '2022-04-04T00:00:00Z', + change_baby: "baby_dob3" + } + ) + ); + }) + .input("1") + .check.user.state("state_baby_born_complete") + .check.interaction({ + reply: [ + "Your baby's date of birth has been updated " + + "to 2022-04-04 and you will start receiving " + + "messages based on this schedule.", + "1. Back", + "2. Exit" + ].join("\n") + }) + .run(); + }); /*************************** ***Edd Baby Unborn Tests***