Skip to content

Commit 275b51d

Browse files
authored
Merge pull request #637 from praekeltfoundation/sigma-1155-CAPI-registration-USSD
Sigma 1155 capi registration ussd
2 parents 1899345 + f26fb80 commit 275b51d

File tree

3 files changed

+83
-65
lines changed

3 files changed

+83
-65
lines changed

go-app-ussd_clinic_rapidpro.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,10 +1261,9 @@ go.app = function() {
12611261
return self.hub
12621262
.send_whatsapp_template_message(msisdn, template_name, media)
12631263
.then(function(data) {
1264-
console.log("Template Data: ", data);
12651264
self.im.user.answers.preferred_channel = data.preferred_channel;
12661265
self.im.user.answers.status_id = data.status_id;
1267-
console.log("ID: ", self.im.user.answers.status_id);
1266+
12681267
if (data.preferred_channel == "SMS") {
12691268
return self.rapidpro.get_global_flag("sms_registrations_enabled")
12701269
.then(function(sms_registration_enabled) {
@@ -1423,12 +1422,15 @@ go.app = function() {
14231422
});
14241423

14251424
self.add("state_get_whatsapp_template_status", function(name, opts) {
1426-
var status_id = self.im.user.answers.status_id;
1427-
console.log(">>>>>>", status_id);
1425+
var answers = self.im.user.answers;
1426+
1427+
if (!answers.status_id || answers.preferred_channel == "SMS"){
1428+
return self.states.create("state_trigger_rapidpro_flow");
1429+
}
1430+
14281431
return self.hub
1429-
.get_whatsapp_template_status(status_id)
1432+
.get_whatsapp_template_status(answers.status_id)
14301433
.then(function(data) {
1431-
console.log("Status Data: ", data);
14321434
self.im.user.answers.preferred_channel = data.preferred_channel;
14331435
return self.states.create("state_trigger_rapidpro_flow");
14341436
}).catch(function(e) {
@@ -1515,6 +1517,7 @@ go.app = function() {
15151517
return self.states.create("state_registration_complete");
15161518
}).catch(function(e) {
15171519
// Go to error state after 3 failed HTTP requests
1520+
console.log("Trigger RP: ", e.message);
15181521
opts.http_error_count = _.get(opts, "http_error_count", 0) + 1;
15191522
if (opts.http_error_count === 3) {
15201523
self.im.log.error(e.message);

src/ussd_clinic_rapidpro.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,10 +1000,9 @@ go.app = function() {
10001000
return self.hub
10011001
.send_whatsapp_template_message(msisdn, template_name, media)
10021002
.then(function(data) {
1003-
console.log("Template Data: ", data);
10041003
self.im.user.answers.preferred_channel = data.preferred_channel;
10051004
self.im.user.answers.status_id = data.status_id;
1006-
console.log("ID: ", self.im.user.answers.status_id);
1005+
10071006
if (data.preferred_channel == "SMS") {
10081007
return self.rapidpro.get_global_flag("sms_registrations_enabled")
10091008
.then(function(sms_registration_enabled) {
@@ -1162,12 +1161,15 @@ go.app = function() {
11621161
});
11631162

11641163
self.add("state_get_whatsapp_template_status", function(name, opts) {
1165-
var status_id = self.im.user.answers.status_id;
1166-
console.log(">>>>>>", status_id);
1164+
var answers = self.im.user.answers;
1165+
1166+
if (!answers.status_id || answers.preferred_channel == "SMS"){
1167+
return self.states.create("state_trigger_rapidpro_flow");
1168+
}
1169+
11671170
return self.hub
1168-
.get_whatsapp_template_status(status_id)
1171+
.get_whatsapp_template_status(answers.status_id)
11691172
.then(function(data) {
1170-
console.log("Status Data: ", data);
11711173
self.im.user.answers.preferred_channel = data.preferred_channel;
11721174
return self.states.create("state_trigger_rapidpro_flow");
11731175
}).catch(function(e) {
@@ -1254,6 +1256,7 @@ go.app = function() {
12541256
return self.states.create("state_registration_complete");
12551257
}).catch(function(e) {
12561258
// Go to error state after 3 failed HTTP requests
1259+
console.log("Trigger RP: ", e.message);
12571260
opts.http_error_count = _.get(opts, "http_error_count", 0) + 1;
12581261
if (opts.http_error_count === 3) {
12591262
self.im.log.error(e.message);

test/ussd_clinic_rapidpro.test.js

Lines changed: 65 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,15 +1678,6 @@ describe("ussd_clinic app", function() {
16781678
state_clinic_code: "123456",
16791679
preferred_channel: "SMS",
16801680
state_language: "eng",
1681-
status_id: "status-id-uuid",
1682-
})
1683-
.setup(function(api) {
1684-
api.http.fixtures.add(
1685-
fixtures_hub.get_whatsapp_template_status(
1686-
"status-id-uuid",
1687-
"SMS"
1688-
)
1689-
);
16901681
})
16911682
.setup(function(api) {
16921683
api.http.fixtures.add(
@@ -1704,7 +1695,6 @@ describe("ussd_clinic app", function() {
17041695
dob: "1990-01-02T00:00:00Z",
17051696
swt: "1",
17061697
preferred_channel: "SMS",
1707-
status_id: "status-id-uuid",
17081698
}
17091699
)
17101700
);
@@ -1721,17 +1711,16 @@ describe("ussd_clinic app", function() {
17211711
})
17221712
.check.reply.ends_session()
17231713
.check(function(api) {
1724-
assert.equal(api.http.requests.length, 2);
1714+
assert.equal(api.http.requests.length, 1);
17251715
var urls = _.map(api.http.requests, "url");
17261716
assert.deepEqual(urls, [
1727-
"http://hub/api/v2/whatsapptemplatesendstatus/status-id-uuid/",
17281717
"https://rapidpro/api/v2/flow_starts.json"
17291718
]);
17301719
assert.equal(api.log.error.length, 0);
17311720
})
17321721
.run();
17331722
});
1734-
it("should make a request to the RapidPro APIs for prebirth if message type is skipped", function() {
1723+
it("should make a request to the RapidPro APIs for prebirth if message type is skipped for WA", function() {
17351724
return tester
17361725
.setup.user.state("state_accept_popi_2")
17371726
.setup.user.answers({
@@ -1742,7 +1731,7 @@ describe("ussd_clinic app", function() {
17421731
state_edd_month: "201502",
17431732
state_edd_day: "13",
17441733
state_clinic_code: "123456",
1745-
preferred_channel: "SMS",
1734+
preferred_channel: "WhatApp",
17461735
state_language: "eng",
17471736
status_id: "status-id-uuid",
17481737

@@ -1751,7 +1740,7 @@ describe("ussd_clinic app", function() {
17511740
api.http.fixtures.add(
17521741
fixtures_hub.get_whatsapp_template_status(
17531742
"status-id-uuid",
1754-
"SMS"
1743+
"WhatsApp"
17551744
)
17561745
);
17571746
})
@@ -1769,8 +1758,8 @@ describe("ussd_clinic app", function() {
17691758
clinic_code: "123456",
17701759
sa_id_number: "9001020005087",
17711760
dob: "1990-01-02T00:00:00Z",
1772-
swt: "1",
1773-
preferred_channel: "SMS",
1761+
swt: "7",
1762+
preferred_channel: "WhatsApp",
17741763
status_id: "status-id-uuid",
17751764
}
17761765
)
@@ -1783,7 +1772,7 @@ describe("ussd_clinic app", function() {
17831772
"You're done!",
17841773
"",
17851774
"This number 0820001001 will start getting messages from " +
1786-
"MomConnect on SMS."
1775+
"MomConnect on WhatsApp."
17871776
].join("\n")
17881777
})
17891778
.check.reply.ends_session()
@@ -1798,31 +1787,78 @@ describe("ussd_clinic app", function() {
17981787
})
17991788
.run();
18001789
});
1801-
it("should make a request to the RapidPro APIs for prebirth underage registree mom with sa_id on accept selection", function() {
1790+
it("should make a request to the RapidPro APIs for prebirth if message type is skipped for USSD", function() {
18021791
return tester
18031792
.setup.user.state("state_accept_popi_2")
18041793
.setup.user.answers({
1805-
state_message_type: "state_edd_month",
18061794
state_research_consent: "no",
18071795
state_enter_msisdn: "0820001001",
18081796
state_id_type: "state_sa_id_no",
1809-
state_sa_id_no: "1301020005087",
1797+
state_sa_id_no: "9001020005087",
18101798
state_edd_month: "201502",
18111799
state_edd_day: "13",
18121800
state_clinic_code: "123456",
1813-
state_underage_registree: "Yes",
18141801
preferred_channel: "SMS",
18151802
state_language: "eng",
1816-
status_id: "status-id-uuid",
1803+
18171804
})
18181805
.setup(function(api) {
18191806
api.http.fixtures.add(
1820-
fixtures_hub.get_whatsapp_template_status(
1821-
"status-id-uuid",
1822-
"SMS"
1807+
fixtures_rapidpro.start_flow(
1808+
"prebirth-flow-uuid", null, "whatsapp:27820001001", {
1809+
research_consent: "TRUE",
1810+
registered_by: "+27123456789",
1811+
language: "eng",
1812+
timestamp: "2014-04-04T07:07:07Z",
1813+
source: "Clinic USSD",
1814+
id_type: "sa_id",
1815+
edd: "2015-02-13T00:00:00Z",
1816+
clinic_code: "123456",
1817+
sa_id_number: "9001020005087",
1818+
dob: "1990-01-02T00:00:00Z",
1819+
swt: "1",
1820+
preferred_channel: "SMS",
1821+
}
18231822
)
18241823
);
18251824
})
1825+
.input("1")
1826+
.check.interaction({
1827+
state: "state_registration_complete",
1828+
reply: [
1829+
"You're done!",
1830+
"",
1831+
"This number 0820001001 will start getting messages from " +
1832+
"MomConnect on SMS."
1833+
].join("\n")
1834+
})
1835+
.check.reply.ends_session()
1836+
.check(function(api) {
1837+
assert.equal(api.http.requests.length, 1);
1838+
var urls = _.map(api.http.requests, "url");
1839+
assert.deepEqual(urls, [
1840+
"https://rapidpro/api/v2/flow_starts.json"
1841+
]);
1842+
assert.equal(api.log.error.length, 0);
1843+
})
1844+
.run();
1845+
});
1846+
it("should make a request to the RapidPro APIs for prebirth underage registree mom with sa_id on accept selection", function() {
1847+
return tester
1848+
.setup.user.state("state_accept_popi_2")
1849+
.setup.user.answers({
1850+
state_message_type: "state_edd_month",
1851+
state_research_consent: "no",
1852+
state_enter_msisdn: "0820001001",
1853+
state_id_type: "state_sa_id_no",
1854+
state_sa_id_no: "1301020005087",
1855+
state_edd_month: "201502",
1856+
state_edd_day: "13",
1857+
state_clinic_code: "123456",
1858+
state_underage_registree: "Yes",
1859+
preferred_channel: "SMS",
1860+
state_language: "eng",
1861+
})
18261862
.setup(function(api) {
18271863
api.http.fixtures.add(
18281864
fixtures_rapidpro.start_flow(
@@ -1840,7 +1876,6 @@ describe("ussd_clinic app", function() {
18401876
dob: "2013-01-02T00:00:00Z",
18411877
swt: "1",
18421878
preferred_channel: "SMS",
1843-
status_id: "status-id-uuid",
18441879
}
18451880
)
18461881
);
@@ -1857,10 +1892,9 @@ describe("ussd_clinic app", function() {
18571892
})
18581893
.check.reply.ends_session()
18591894
.check(function(api) {
1860-
assert.equal(api.http.requests.length, 2);
1895+
assert.equal(api.http.requests.length, 1);
18611896
var urls = _.map(api.http.requests, "url");
18621897
assert.deepEqual(urls, [
1863-
"http://hub/api/v2/whatsapptemplatesendstatus/status-id-uuid/",
18641898
"https://rapidpro/api/v2/flow_starts.json"
18651899
]);
18661900
assert.equal(api.log.error.length, 0);
@@ -1884,15 +1918,6 @@ describe("ussd_clinic app", function() {
18841918
state_underage_registree: "Yes",
18851919
preferred_channel: "SMS",
18861920
state_language: "eng",
1887-
status_id: "status-id-uuid",
1888-
})
1889-
.setup(function(api) {
1890-
api.http.fixtures.add(
1891-
fixtures_hub.get_whatsapp_template_status(
1892-
"status-id-uuid",
1893-
"SMS"
1894-
)
1895-
);
18961921
})
18971922
.setup(function(api) {
18981923
api.http.fixtures.add(
@@ -1912,7 +1937,6 @@ describe("ussd_clinic app", function() {
19121937
swt: "1",
19131938
age: "16",
19141939
preferred_channel: "SMS",
1915-
status_id: "status-id-uuid",
19161940
}
19171941
)
19181942
);
@@ -1929,10 +1953,9 @@ describe("ussd_clinic app", function() {
19291953
})
19301954
.check.reply.ends_session()
19311955
.check(function(api) {
1932-
assert.equal(api.http.requests.length, 2);
1956+
assert.equal(api.http.requests.length, 1);
19331957
var urls = _.map(api.http.requests, "url");
19341958
assert.deepEqual(urls, [
1935-
"http://hub/api/v2/whatsapptemplatesendstatus/status-id-uuid/",
19361959
"https://rapidpro/api/v2/flow_starts.json"
19371960
]);
19381961
assert.equal(api.log.error.length, 0);
@@ -1956,15 +1979,6 @@ describe("ussd_clinic app", function() {
19561979
state_underage_registree: "Yes",
19571980
preferred_channel: "SMS",
19581981
state_language: "eng",
1959-
status_id: "status-id-uuid",
1960-
})
1961-
.setup(function(api) {
1962-
api.http.fixtures.add(
1963-
fixtures_hub.get_whatsapp_template_status(
1964-
"status-id-uuid",
1965-
"SMS"
1966-
)
1967-
);
19681982
})
19691983
.setup(function(api) {
19701984
api.http.fixtures.add(
@@ -1982,7 +1996,6 @@ describe("ussd_clinic app", function() {
19821996
swt: "1",
19831997
dob: "2014-10-25T00:00:00Z",
19841998
preferred_channel: "SMS",
1985-
status_id: "status-id-uuid",
19861999
}
19872000
)
19882001
);
@@ -1999,10 +2012,9 @@ describe("ussd_clinic app", function() {
19992012
})
20002013
.check.reply.ends_session()
20012014
.check(function(api) {
2002-
assert.equal(api.http.requests.length, 2);
2015+
assert.equal(api.http.requests.length, 1);
20032016
var urls = _.map(api.http.requests, "url");
20042017
assert.deepEqual(urls, [
2005-
"http://hub/api/v2/whatsapptemplatesendstatus/status-id-uuid/",
20062018
"https://rapidpro/api/v2/flow_starts.json"
20072019
]);
20082020
assert.equal(api.log.error.length, 0);

0 commit comments

Comments
 (0)