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-1400 use native sql #450

Merged
merged 4 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.14
version: 2.5.15

# 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.14
appVersion: 2.5.15
14 changes: 7 additions & 7 deletions ras_party/controllers/enrolments_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,25 @@ def respondent_enrolments(

enrolments = query_respondent_enrolments(session, respondent.id, business_id, survey_id, status)

if not enrolments:
if enrolments.rowcount == 0:
return []

surveys_details = get_surveys_details()
respondents_enrolled = []
for enrolment, business_ref, business_attributes in enrolments:
for enrolment in enrolments:
survey_id = enrolment.survey_id
respondents_enrolled.append(
(
{
"enrolment_status": enrolment.status.name,
"enrolment_status": enrolment.status,
"business_details": {
"id": enrolment.business_id,
"name": business_attributes["name"],
"trading_as": business_attributes["trading_as"],
"ref": business_ref,
"name": enrolment.attributes["name"],
"trading_as": enrolment.attributes["trading_as"],
"ref": enrolment.business_ref,
},
"survey_details": {
"id": survey_id,
"id": enrolment.survey_id,
"long_name": surveys_details.get(survey_id)["long_name"],
"short_name": surveys_details.get(survey_id)["short_name"],
"ref": surveys_details.get(survey_id)["ref"],
Expand Down
28 changes: 16 additions & 12 deletions ras_party/controllers/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import structlog
from flask import session
from sqlalchemy import and_, distinct, func, or_
from sqlalchemy.sql import text
from sqlalchemy.sql.functions import count

from ras_party.models.models import (
Expand Down Expand Up @@ -649,21 +650,24 @@ def query_respondent_enrolments(
session: session, respondent_id: int, business_id: UUID = None, survey_id: UUID = None, status: int = None
) -> list[Enrolment]:
"""
Query to return a list of respondent Enrolments. Business_id, survey_id and status can also be added as conditions
Query to return a list of respondent Enrolments and business attributes.
Business_id, survey_id and status can also be added as conditions
"""
additional_conditions = []
where_clause = f"WHERE respondent_id = {respondent_id}"

if business_id:
additional_conditions.append(Enrolment.business_id == business_id)
where_clause += f" and partysvc.enrolment.business_id='{business_id}'"
if survey_id:
additional_conditions.append(Enrolment.survey_id == survey_id)
where_clause += f" and partysvc.enrolment.survey_id='{survey_id}'"
if status:
additional_conditions.append(Enrolment.status == status)

return (
session.query(Enrolment, Business.business_ref, BusinessAttributes.attributes)
.join(Business, Business.party_uuid == Enrolment.business_id)
.join(BusinessAttributes, BusinessAttributes.business_id == Enrolment.business_id)
.filter(and_(Enrolment.respondent_id == respondent_id, *additional_conditions))
.all()
where_clause += f" and partysvc.enrolment.status='{status}'"

return session.execute(
text(
f"SELECT * FROM partysvc.enrolment "
pricem14pc marked this conversation as resolved.
Show resolved Hide resolved
f"inner join partysvc.business on partysvc.business.party_uuid = partysvc.enrolment.business_id, "
f"LATERAL (SELECT * FROM partysvc.business_attributes "
f"WHERE partysvc.enrolment.business_id = partysvc.business_attributes.business_id "
f"ORDER BY partysvc.business_attributes.created_on DESC limit 1) ba {where_clause}"
)
)
47 changes: 33 additions & 14 deletions test/test_enrolments_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,23 @@
{
"business_id": "75d9af56-1225-4d43-b41d-1199f5f89daa",
"business_ref": "001",
"business_attributes": {"name": "Business 1", "trading_as": "1 Business"},
"business_attributes": {
"id": "75d9af56-1225-4d43-b41d-1199f5f89daa",
"name": "Business 1",
"trading_as": "1 Business",
"created_on": "2024-01-01 12:00:00",
},
"survey_id": "9200d295-9d6e-41fe-b541-747ae67a279f",
"status": EnrolmentStatus.ENABLED,
},
{
"business_id": "98e2c9dd-a760-47dd-ba18-439fd5fb93a3",
"business_ref": "002",
"business_attributes": {"name": "Business 2", "trading_as": "2 Business"},
"business_attributes": {
"name": "Business 2",
"trading_as": "2 Business",
"created_on": "2025-01-01 12:00:00",
},
"survey_id": "c641f6ad-a5eb-4d82-a647-7cd586549bbc",
"status": EnrolmentStatus.ENABLED,
},
Expand All @@ -42,14 +51,23 @@
{
"business_id": "af25c9d5-6893-4342-9d24-4b88509e965f",
"business_ref": "003",
"business_attributes": {"name": "Business 3", "trading_as": "3 Business"},
"business_attributes": {
"name": "Business 3",
"trading_as": "3 Business",
"created_on": "2025-01-01 12:00:00",
},
"survey_id": "9200d295-9d6e-41fe-b541-747ae67a279f",
"status": EnrolmentStatus.ENABLED,
},
{
"business_id": "75d9af56-1225-4d43-b41d-1199f5f89daa",
"business_ref": "004",
"business_attributes": {"name": "Business 4", "trading_as": "4 Business"},
"business_ref": "001",
"business_attributes": {
"id": "75d9af56-1225-4d43-b41d-1199f5f89daa",
"name": "Business 4",
"trading_as": "4 Business",
"created_on": "2025-01-01 12:00:00",
},
"survey_id": "9200d295-9d6e-41fe-b541-747ae67a279f",
"status": EnrolmentStatus.DISABLED,
},
Expand Down Expand Up @@ -80,8 +98,8 @@ def test_get_enrolments_party_id(self, get_surveys_details):
"enrolment_status": "ENABLED",
"business_details": {
"id": UUID("75d9af56-1225-4d43-b41d-1199f5f89daa"),
"name": "Business 1",
"trading_as": "1 Business",
"name": "Business 4", # Business 4 is the latest business_attributes for 75d9af56
"trading_as": "4 Business",
"ref": "001",
},
"survey_details": {
Expand Down Expand Up @@ -126,7 +144,7 @@ def test_get_enrolments_party_id_and_business_id_and_survey_id(self, get_surveys
def test_get_enrolments_party_id_enabled(self, get_surveys_details):
get_surveys_details.return_value = SURVEYS_DETAILS
enrolments = respondent_enrolments(
party_uuid="5718649e-30bf-4c25-a2c0-aaa733e54ed6", status=EnrolmentStatus.ENABLED
party_uuid="5718649e-30bf-4c25-a2c0-aaa733e54ed6", status=EnrolmentStatus.ENABLED.name
)

self.assertEqual(len(enrolments), 1)
Expand All @@ -137,7 +155,7 @@ def test_get_enrolments_party_id_enabled(self, get_surveys_details):
def test_get_enrolments_party_id_disabled(self, get_surveys_details):
get_surveys_details.return_value = SURVEYS_DETAILS
enrolments = respondent_enrolments(
party_uuid="5718649e-30bf-4c25-a2c0-aaa733e54ed6", status=EnrolmentStatus.DISABLED
party_uuid="5718649e-30bf-4c25-a2c0-aaa733e54ed6", status=EnrolmentStatus.DISABLED.name
)

self.assertEqual(len(enrolments), 1)
Expand Down Expand Up @@ -171,13 +189,14 @@ def _add_enrolments(self, session):
for enrolment in respondent_enrolment["enrolment_details"]:
if not (business := businesses.get(enrolment["business_id"])):
business = Business(party_uuid=enrolment["business_id"], business_ref=enrolment["business_ref"])
business_attributes = BusinessAttributes(
business_id=business.party_uuid, attributes=enrolment["business_attributes"]
)
session.add(business)
session.add(business_attributes)
businesses[enrolment["business_id"]] = business

business_attributes = BusinessAttributes(
business_id=business.party_uuid,
attributes=enrolment["business_attributes"],
created_on=enrolment["business_attributes"]["created_on"],
)
session.add(business_attributes)
business_respondent = BusinessRespondent(business=business, respondent=respondent)
session.add(business_respondent)
session.flush()
Expand Down
Loading