Skip to content

Commit

Permalink
Block multiple entries after invalid province
Browse files Browse the repository at this point in the history
  • Loading branch information
erikh360 committed Aug 21, 2023
1 parent d5cdfa7 commit 932a362
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
34 changes: 33 additions & 1 deletion yal/surveys/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ async def state_location_introduction(self):

if survey_status in ("completed", "airtime_sent"):
return await self.go_to_state("state_location_already_completed")
elif survey_status == "invalid_province":
return await self.go_to_state("state_location_invalid_province")
elif (
group_count >= config.LOCATION_STUDY_GROUP_LIMIT
or survey_status != "pending"
Expand Down Expand Up @@ -105,6 +107,22 @@ async def state_location_not_invited(self):
next=self.START_STATE,
)

async def state_location_invalid_province(self):
return EndState(
self,
self._(
"\n".join(
[
"Unfortunately, this number is not eligible for this survey "
"at this moment.",
"",
"Reply with “menu” to return to the main menu”.",
]
)
),
next=self.START_STATE,
)

async def state_location_already_completed(self):
return EndState(
self,
Expand Down Expand Up @@ -162,7 +180,7 @@ async def _next(choice: Choice):
async def state_location_province(self):
async def _next(choice: Choice):
if choice.value == "other":
return "state_location_not_recruiting"
return "state_location_update_invalid_province"
return "state_location_name_city"

question = "*What province do you live in?*"
Expand All @@ -181,6 +199,20 @@ async def _next(choice: Choice):
error=self._(get_generic_error()),
)

async def state_location_update_invalid_province(self):
msisdn = normalise_phonenumber(self.inbound.from_addr)
whatsapp_id = msisdn.lstrip(" + ")

error = await rapidpro.update_profile(
whatsapp_id,
{"ejaf_location_survey_status": "invalid_province"},
self.user.metadata,
)
if error:
return await self.go_to_state("state_error")

return await self.go_to_state("state_location_not_recruiting")

async def state_location_not_recruiting(self):
return EndState(
self,
Expand Down
2 changes: 2 additions & 0 deletions yal/tests/states_dictionary.md
Original file line number Diff line number Diff line change
Expand Up @@ -772,3 +772,5 @@
| state_location_question | TRUE | text | Asks user if they'd like to continue after seeing consent pdf
| state_send_consent_pdf | FALSE | text | Send the consent pdf to the user
| state_location_decline | FALSE | text | Tells users there are no consequences for not joining study
| state_location_update_invalid_province | FALSE | text | Updates the ejaf_location_survey_status contact field on rapidpro
| state_location_invalid_province | FALSE | text | Tells them they are not eligible for the survey
31 changes: 30 additions & 1 deletion yal/tests/surveys/test_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def get_rapidpro_contact(urn):
status = None
if "27820001004" in urn:
survey_group = "2"
if "27820001005" in urn:
status = "invalid_province"
return {
"fields": {
"ejaf_location_survey_status": status,
Expand Down Expand Up @@ -100,6 +102,27 @@ async def test_state_location_introduction_already_completed(tester: AppTester):
tester.assert_message("This number has already completed the location survey.")


@pytest.mark.asyncio
async def test_state_location_introduction_invalid_province(tester: AppTester):
tester.setup_user_address("27820001005")
tester.setup_state("state_location_introduction")

await tester.user_input(session=Message.SESSION_EVENT.NEW)

tester.assert_state("state_start")

tester.assert_message(
"\n".join(
[
"Unfortunately, this number is not eligible for this survey at this "
"moment.",
"",
"Reply with “menu” to return to the main menu”.",
]
)
)


@pytest.mark.asyncio
async def test_state_location_introduction_not_invited(tester: AppTester):
tester.setup_user_address("27820001003")
Expand Down Expand Up @@ -244,7 +267,7 @@ async def test_state_location_province(tester: AppTester):


@pytest.mark.asyncio
async def test_state_location_province_excluded(tester: AppTester):
async def test_state_location_province_excluded(tester: AppTester, rapidpro_mock):
tester.setup_state("state_location_province")
await tester.user_input("4")

Expand All @@ -260,6 +283,12 @@ async def test_state_location_province_excluded(tester: AppTester):
)
)

assert len(rapidpro_mock.tstate.requests) == 2
request = rapidpro_mock.tstate.requests[1]
assert json.loads(request.body.decode("utf-8")) == {
"fields": {"ejaf_location_survey_status": "invalid_province"},
}


@pytest.mark.asyncio
async def test_state_location_name_city(tester: AppTester):
Expand Down

0 comments on commit 932a362

Please sign in to comment.