Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RAS-1289 Update the way claims is determined in the party service #435

Merged
merged 9 commits into from
Sep 25, 2024
Merged
4 changes: 2 additions & 2 deletions _infra/helm/party/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
version: 2.5.4
version: 2.5.5

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application.
appVersion: 2.5.4
appVersion: 2.5.5
7 changes: 7 additions & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,13 @@ paths:
schema:
type: string
format: uuid
- name: survey_id
in: query
required: true
description: The UUID of the survey
schema:
type: string
format: uuid
responses:
200:
description: The information has been retrieved
Expand Down
24 changes: 10 additions & 14 deletions ras_party/controllers/respondent_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
)
from ras_party.controllers.notify_gateway import NotifyGateway
from ras_party.controllers.queries import (
query_enrolment_by_survey_business_respondent,
query_respondent_by_email,
query_respondent_by_names_and_emails,
query_respondent_by_party_uuid,
Expand All @@ -25,6 +26,7 @@
Enrolment,
PendingEnrolment,
Respondent,
RespondentStatus,
)
from ras_party.support.session_decorator import (
with_db_session,
Expand Down Expand Up @@ -72,12 +74,12 @@ def get_respondents_by_name_and_email(first_name, last_name, email, page, limit,
@with_query_only_db_session
def get_respondent_by_id(respondent_id, session):
"""
Get a Respondent by its Party ID. Returns a single Party
Get a Respondent by its Party ID.

:param respondent_id: ID of Respondent to return
:type respondent_id: str
:return: An object representing a respondent, if it exists.
:rtype: Respondent
:rtype: respondent dict with business associations
"""
try:
uuid.UUID(respondent_id)
Expand Down Expand Up @@ -246,18 +248,12 @@ def get_respondents_by_survey_and_business_id(survey_id: UUID, business_id: UUID
return respondents_enrolled


def does_user_have_claim(user_id, business_id):
# with_db_session function wrapper automatically injects the session parameter
# pylint: disable=no-value-for-parameter
user_details = get_respondent_by_id(user_id)
associations = user_details["associations"]
is_associated_to_business = _is_user_associated_to_the_business(associations, business_id)
return user_details["status"] == "ACTIVE" and is_associated_to_business

@with_query_only_db_session
def is_user_enrolled(party_uuid: UUID, business_id: UUID, survey_id: UUID, session: session) -> bool:
respondent = query_respondent_by_party_uuid(party_uuid, session)

def _is_user_associated_to_the_business(associations, business_id):
for association in associations:
if str(association["partyId"]) == business_id:
if respondent and respondent.status == RespondentStatus.ACTIVE:
enrolment = query_enrolment_by_survey_business_respondent(respondent.id, business_id, survey_id, session)
if enrolment:
return True

return False
22 changes: 10 additions & 12 deletions ras_party/views/respondent_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,14 @@ def validate_respondent_claim():
in client services. There is an argument to use a 403 on invalid claims , but that should be a status on the
resource not the state of the returned data and so that's stretching the use of http status codes somewhat
"""
respondent_id = request.args.get("respondent_id", default="").strip()
business_id = request.args.get("business_id", default="").strip()
party_uuid = request.args.get("respondent_id")
LJBabbage marked this conversation as resolved.
Show resolved Hide resolved
business_id = request.args.get("business_id")
survey_id = request.args.get("survey_id")
LJBabbage marked this conversation as resolved.
Show resolved Hide resolved

if not business_id or not respondent_id:
logger.info(
"either respondent id or business id is missing", respondent_id=respondent_id, business_id=business_id
)
raise BadRequest("respondent id and business id is required")
if not (is_valid_uuid4(party_uuid) and is_valid_uuid4(business_id) and is_valid_uuid4(survey_id)):
return make_response("Bad request, party_uuid, business or survey id not UUID", 400)

if respondent_controller.does_user_have_claim(respondent_id, business_id):
if respondent_controller.is_user_enrolled(party_uuid, business_id, survey_id):
return make_response("Valid", 200)

return make_response("Invalid", 200)
Expand All @@ -132,8 +130,8 @@ def validate_respondent_claim():
def get_respondents_by_business_and_survey_id(business_id: UUID, survey_id: UUID) -> Response:
"""Gets a list of Respondents enrolled in a survey for a specified business"""

if is_valid_uuid4(survey_id) and is_valid_uuid4(business_id):
respondents = respondent_controller.get_respondents_by_survey_and_business_id(survey_id, business_id)
return make_response(respondents, 200)
else:
if not (is_valid_uuid4(survey_id) and is_valid_uuid4(business_id)):
return make_response("Bad request, business or survey id not UUID", 400)

respondents = respondent_controller.get_respondents_by_survey_and_business_id(survey_id, business_id)
return make_response(respondents, 200)
10 changes: 9 additions & 1 deletion test/test_respondent_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -1694,6 +1694,14 @@ def test_validate_claim_returns_200_if_respondent_has_a_claim(self):
expected_result="Valid",
)

def test_validate_claim_invalid_respondent_id(self):
self.validate_respondent_claim(
respondent_id="invalid",
business_id=DEFAULT_BUSINESS_UUID,
survey_id=DEFAULT_SURVEY_UUID,
expected_status=400,
)

def test_validate_claim_returns_invalid_if_respondent_does_not_have_a_claim_on_specific_business(self):
self.populate_with_respondent(respondent=self.mock_respondent_with_id_active)
self.populate_with_business()
Expand All @@ -1703,7 +1711,7 @@ def test_validate_claim_returns_invalid_if_respondent_does_not_have_a_claim_on_s

self.validate_respondent_claim(
respondent_id=DEFAULT_RESPONDENT_UUID,
business_id="ADifferentBusiness",
business_id="feb28732-fa48-4f6d-802b-65e1215837b0",
survey_id=DEFAULT_SURVEY_UUID,
expected_status=200,
expected_result="Invalid",
Expand Down
Loading