From 88dbb15bbfc87377584982f18d5876c29a3e94a5 Mon Sep 17 00:00:00 2001 From: LJBabbage Date: Fri, 13 Sep 2024 11:20:33 +0100 Subject: [PATCH 1/7] RAS-1289 Update the way claims is determined in the party service --- .../controllers/respondent_controller.py | 34 +++++++++++-------- ras_party/views/respondent_view.py | 14 ++++---- test/test_respondent_controller.py | 10 +++++- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/ras_party/controllers/respondent_controller.py b/ras_party/controllers/respondent_controller.py index 2459dfc4..6c9aa7a3 100644 --- a/ras_party/controllers/respondent_controller.py +++ b/ras_party/controllers/respondent_controller.py @@ -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, @@ -25,6 +26,7 @@ Enrolment, PendingEnrolment, Respondent, + RespondentStatus, ) from ras_party.support.session_decorator import ( with_db_session, @@ -69,15 +71,25 @@ def get_respondents_by_name_and_email(first_name, last_name, email, page, limit, } +def get_respondent_by_party_uuid(party_uuid: UUID, session: session) -> Respondent: + try: + uuid.UUID(party_uuid) + except ValueError: + raise BadRequest(f"'{party_uuid}' is not a valid UUID") + + respondent = query_respondent_by_party_uuid(party_uuid, session) + return respondent + + @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) @@ -246,18 +258,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 = get_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 diff --git a/ras_party/views/respondent_view.py b/ras_party/views/respondent_view.py index 4bdb2121..2fc18d77 100644 --- a/ras_party/views/respondent_view.py +++ b/ras_party/views/respondent_view.py @@ -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") + business_id = request.args.get("business_id") + survey_id = request.args.get("survey_id") - 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 business_id or not party_uuid or not survey_id: + raise BadRequest("party_uuid, business id and survey_id are required") - 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) diff --git a/test/test_respondent_controller.py b/test/test_respondent_controller.py index 7be96719..4f0d474d 100644 --- a/test/test_respondent_controller.py +++ b/test/test_respondent_controller.py @@ -1692,6 +1692,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() @@ -1701,7 +1709,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", From 2b3143d430b62ce6eb975b7bead8bde7f2429d32 Mon Sep 17 00:00:00 2001 From: ras-rm-pr-bot Date: Fri, 13 Sep 2024 11:46:52 +0000 Subject: [PATCH 2/7] auto patch increment --- _infra/helm/party/Chart.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_infra/helm/party/Chart.yaml b/_infra/helm/party/Chart.yaml index a63360e7..4989807b 100644 --- a/_infra/helm/party/Chart.yaml +++ b/_infra/helm/party/Chart.yaml @@ -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.2 +version: 2.5.3 # 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.2 +appVersion: 2.5.3 From 52902e0cf399c1d2ee81670c36ab0bfc70463d2f Mon Sep 17 00:00:00 2001 From: LJBabbage Date: Mon, 16 Sep 2024 10:45:10 +0100 Subject: [PATCH 3/7] Simply the flow --- ras_party/controllers/respondent_controller.py | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/ras_party/controllers/respondent_controller.py b/ras_party/controllers/respondent_controller.py index 6c9aa7a3..5eb503c6 100644 --- a/ras_party/controllers/respondent_controller.py +++ b/ras_party/controllers/respondent_controller.py @@ -71,16 +71,6 @@ def get_respondents_by_name_and_email(first_name, last_name, email, page, limit, } -def get_respondent_by_party_uuid(party_uuid: UUID, session: session) -> Respondent: - try: - uuid.UUID(party_uuid) - except ValueError: - raise BadRequest(f"'{party_uuid}' is not a valid UUID") - - respondent = query_respondent_by_party_uuid(party_uuid, session) - return respondent - - @with_query_only_db_session def get_respondent_by_id(respondent_id, session): """ @@ -260,7 +250,7 @@ def get_respondents_by_survey_and_business_id(survey_id: UUID, business_id: UUID @with_query_only_db_session def is_user_enrolled(party_uuid: UUID, business_id: UUID, survey_id: UUID, session: session) -> bool: - respondent = get_respondent_by_party_uuid(party_uuid, session) + respondent = query_respondent_by_party_uuid(party_uuid, session) if respondent and respondent.status == RespondentStatus.ACTIVE: enrolment = query_enrolment_by_survey_business_respondent(respondent.id, business_id, survey_id, session) From 79c21515f80ef50cb691915f5307de767e6a3b06 Mon Sep 17 00:00:00 2001 From: LJBabbage Date: Mon, 16 Sep 2024 11:36:21 +0100 Subject: [PATCH 4/7] Move validation check up --- ras_party/views/respondent_view.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ras_party/views/respondent_view.py b/ras_party/views/respondent_view.py index 2fc18d77..29d31205 100644 --- a/ras_party/views/respondent_view.py +++ b/ras_party/views/respondent_view.py @@ -117,8 +117,8 @@ def validate_respondent_claim(): business_id = request.args.get("business_id") survey_id = request.args.get("survey_id") - if not business_id or not party_uuid or not survey_id: - raise BadRequest("party_uuid, business id and survey_id are 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.is_user_enrolled(party_uuid, business_id, survey_id): return make_response("Valid", 200) @@ -130,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) From 772ec26907c90ef16e8fe865b59c6472826a1366 Mon Sep 17 00:00:00 2001 From: ras-rm-pr-bot Date: Wed, 18 Sep 2024 07:27:42 +0000 Subject: [PATCH 5/7] auto patch increment --- _infra/helm/party/Chart.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_infra/helm/party/Chart.yaml b/_infra/helm/party/Chart.yaml index 4989807b..319a3c90 100644 --- a/_infra/helm/party/Chart.yaml +++ b/_infra/helm/party/Chart.yaml @@ -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.3 +version: 2.5.4 # 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.3 +appVersion: 2.5.4 From 8f9d619f5607f5d1443ff04037cf26a79403c5ca Mon Sep 17 00:00:00 2001 From: LJBabbage Date: Fri, 20 Sep 2024 10:36:52 +0100 Subject: [PATCH 6/7] Add survey id to openapi --- openapi.yaml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/openapi.yaml b/openapi.yaml index 5f87ba56..2555892f 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -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 From e7360f56d75162b0e18ff8bbfc04a14bb5516f09 Mon Sep 17 00:00:00 2001 From: ras-rm-pr-bot Date: Fri, 20 Sep 2024 09:39:56 +0000 Subject: [PATCH 7/7] auto patch increment --- _infra/helm/party/Chart.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_infra/helm/party/Chart.yaml b/_infra/helm/party/Chart.yaml index 319a3c90..781b23f3 100644 --- a/_infra/helm/party/Chart.yaml +++ b/_infra/helm/party/Chart.yaml @@ -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