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

Fix wins dataset endpoint for responded_on and sector. #5331

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
68 changes: 46 additions & 22 deletions datahub/dataset/export_wins/test/test_views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import date, timedelta
from datetime import date, datetime, timedelta

import pytest
from dateutil.relativedelta import relativedelta
Expand All @@ -8,6 +8,7 @@
from freezegun import freeze_time

from datahub.company.test.factories import CompanyFactory, ContactFactory
from datahub.core.constants import Sector
from datahub.core.test_utils import (
format_date_or_datetime,
)
Expand Down Expand Up @@ -162,6 +163,7 @@ def _assert_win_matches_result(
result,
):
contact = win.company_contacts.first()
has_responded = win.customer_response.responded_on is not None
expected = {
'created_on': format_date_or_datetime(win.created_on),
'id': str(win.id),
Expand All @@ -170,39 +172,52 @@ def _assert_win_matches_result(
'cdms_reference': win.company.company_number,
'company_name': win.company.name,
'complete': win.complete,
'confirmation__access_to_contacts': win.customer_response.our_support.export_win_id,
'confirmation__access_to_contacts':
win.customer_response.our_support.export_win_id if has_responded else None,
'confirmation__access_to_information':
win.customer_response.access_to_information.export_win_id,
win.customer_response.access_to_information.export_win_id
if has_responded else None,
'confirmation__agree_with_win': win.customer_response.agree_with_win,
'confirmation__case_study_willing': win.customer_response.case_study_willing,
'confirmation__case_study_willing':
win.customer_response.case_study_willing if has_responded else None,
'confirmation__comments': win.customer_response.comments,
'confirmation__company_was_at_risk_of_not_exporting':
win.customer_response.company_was_at_risk_of_not_exporting,
win.customer_response.company_was_at_risk_of_not_exporting
if has_responded else None,
'confirmation__created': format_date_or_datetime(
win.customer_response.created_on,
win.customer_response.responded_on,
),
'confirmation__developed_relationships':
win.customer_response.developed_relationships.export_win_id,
win.customer_response.developed_relationships.export_win_id
if has_responded else None,
'confirmation__gained_confidence':
win.customer_response.gained_confidence.export_win_id,
win.customer_response.gained_confidence.export_win_id if has_responded else None,
'confirmation__has_enabled_expansion_into_existing_market':
win.customer_response.has_enabled_expansion_into_existing_market,
win.customer_response.has_enabled_expansion_into_existing_market
if has_responded else None,
'confirmation__has_enabled_expansion_into_new_market':
win.customer_response.has_enabled_expansion_into_new_market,
win.customer_response.has_enabled_expansion_into_new_market
if has_responded else None,
'confirmation__has_explicit_export_plans':
win.customer_response.has_explicit_export_plans,
win.customer_response.has_explicit_export_plans if has_responded else None,
'confirmation__has_increased_exports_as_percent_of_turnover':
win.customer_response.has_increased_exports_as_percent_of_turnover,
'confirmation__improved_profile': win.customer_response.improved_profile.export_win_id,
win.customer_response.has_increased_exports_as_percent_of_turnover
if has_responded else None,
'confirmation__improved_profile':
win.customer_response.improved_profile.export_win_id if has_responded else None,
'confirmation__interventions_were_prerequisite':
win.customer_response.interventions_were_prerequisite,
win.customer_response.interventions_were_prerequisite if has_responded else None,
'confirmation__involved_state_enterprise':
win.customer_response.involved_state_enterprise,
win.customer_response.involved_state_enterprise if has_responded else None,
'confirmation__name': win.customer_response.name,
'confirmation__other_marketing_source': win.customer_response.other_marketing_source,
'confirmation__our_support': win.customer_response.our_support.export_win_id,
'confirmation__overcame_problem': win.customer_response.overcame_problem.export_win_id,
'confirmation__support_improved_speed': win.customer_response.support_improved_speed,
'confirmation__other_marketing_source':
win.customer_response.other_marketing_source if has_responded else None,
'confirmation__our_support':
win.customer_response.our_support.export_win_id if has_responded else None,
'confirmation__overcame_problem':
win.customer_response.overcame_problem.export_win_id if has_responded else None,
'confirmation__support_improved_speed':
win.customer_response.support_improved_speed if has_responded else None,
'country': win.country.iso_alpha2_code,
'created': format_date_or_datetime(win.created_on),
'customer_email_address': contact.email,
Expand Down Expand Up @@ -238,7 +253,7 @@ def _assert_win_matches_result(
'goods_vs_services_display': win.goods_vs_services.name,
'hq_team_display': win.hq_team.name,
'hvo_programme_display': win.hvo_programme.name,
'sector_display': win.sector.segment,
'sector_display': win.sector.name,
'team_type_display': win.team_type.name,
'num_notifications': len(tokens),
'customer_email_date':
Expand All @@ -251,7 +266,11 @@ def _assert_win_matches_result(

assert result == expected

def test_success(self, data_flow_api_client):
@pytest.mark.parametrize(
'responded',
(True, False),
)
def test_success(self, data_flow_api_client, responded):
associated_programmes = AssociatedProgrammeFactory.create_batch(3)
types_of_support = SupportTypeFactory.create_batch(2)
company = CompanyFactory(company_number='012345')
Expand All @@ -262,8 +281,13 @@ def test_success(self, data_flow_api_client):
hvo_programme=HVOProgrammesFactory(),
company=company,
company_contacts=[contact],
sector_id=Sector.aerospace_assembly_aircraft.value.id,
)
customer_response = CustomerResponseFactory(
win=win,
other_marketing_source='marketing' if responded else '',
responded_on=datetime.now() if responded else None,
)
customer_response = CustomerResponseFactory(win=win)
tokens = CustomerResponseTokenFactory.create_batch(3, customer_response=customer_response)

response = data_flow_api_client.get(self.view_url).json()
Expand Down
2 changes: 1 addition & 1 deletion datahub/dataset/export_wins/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
def use_nulls_on_empty_string_fields(data):
columns = ['audit']
columns = ['audit', 'confirmation__other_marketing_source']
for column in columns:
column_value = data.get(column)
if column_value == '':
Expand Down
122 changes: 83 additions & 39 deletions datahub/dataset/export_wins/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
from django.contrib.postgres.expressions import ArraySubquery
from django.contrib.postgres.fields import JSONField
from django.db.models import Case, Count, ExpressionWrapper, F, Func, Max, When
from django.db.models import CharField, IntegerField, OuterRef, Subquery, Value
from django.db.models import (
BooleanField,
Case,
CharField,
Count,
ExpressionWrapper,
F,
Func,
IntegerField,
Max,
OuterRef,
Subquery,
Value,
When,
)
from django.db.models.functions import (
Cast,
Concat,
Expand All @@ -27,6 +40,7 @@
Win,
WinAdviser,
)
from datahub.metadata.query_utils import get_sector_name_subquery


class ExportWinsAdvisersDatasetView(BaseDatasetView):
Expand Down Expand Up @@ -131,6 +145,7 @@ def get_dataset(self):
'sector',
)
.annotate(
sector_name=get_sector_name_subquery('sector'),
# Subquery returns a JSONB document to avoid creating multiple subqueries
# to retrieve different properties of the same contact
customer_details=Subquery(
Expand Down Expand Up @@ -188,70 +203,99 @@ def get_dataset(self):
goods_vs_services_display=F('goods_vs_services__name'),
hq_team_display=F('hq_team__name'),
hvo_programme_display=F('hvo_programme__name'),
sector_display=F('sector__segment'),
sector_display=F('sector_name'),
team_type_display=F('team_type__name'),
num_notifications=Count('customer_response__tokens'),
customer_email_date=Max('customer_response__tokens__created_on'),
)
.annotate(
company_name=F('company__name'),
confirmation__access_to_contacts=Cast(
'customer_response__our_support__export_win_id', IntegerField(),
confirmation__access_to_contacts=Case(
When(customer_response__responded_on__isnull=True, then=None),
default=F('customer_response__our_support__export_win_id'),
output_field=IntegerField(),
),
confirmation__access_to_information=Cast(
'customer_response__access_to_information__export_win_id',
IntegerField(),
confirmation__access_to_information=Case(
When(customer_response__responded_on__isnull=True, then=None),
default=F('customer_response__access_to_information__export_win_id'),
output_field=IntegerField(),
),
confirmation__agree_with_win=F('customer_response__agree_with_win'),
confirmation__case_study_willing=F(
'customer_response__case_study_willing',
confirmation__case_study_willing=Case(
When(customer_response__responded_on__isnull=True, then=None),
default=F('customer_response__case_study_willing'),
output_field=BooleanField(),
),
confirmation__comments=F('customer_response__comments'),
confirmation__company_was_at_risk_of_not_exporting=F(
'customer_response__company_was_at_risk_of_not_exporting',
confirmation__company_was_at_risk_of_not_exporting=Case(
When(customer_response__responded_on__isnull=True, then=None),
default=F('customer_response__company_was_at_risk_of_not_exporting'),
output_field=BooleanField(),
),
confirmation__created=F('customer_response__created_on'),
confirmation__developed_relationships=Cast(
'customer_response__developed_relationships__export_win_id',
IntegerField(),
confirmation__created=F('customer_response__responded_on'),
confirmation__developed_relationships=Case(
When(customer_response__responded_on__isnull=True, then=None),
default=F('customer_response__developed_relationships__export_win_id'),
output_field=IntegerField(),
),
confirmation__gained_confidence=Cast(
'customer_response__gained_confidence__export_win_id',
IntegerField(),
confirmation__gained_confidence=Case(
When(customer_response__responded_on__isnull=True, then=None),
default=F('customer_response__gained_confidence__export_win_id'),
output_field=IntegerField(),
),
confirmation__has_enabled_expansion_into_existing_market=F(
'customer_response__has_enabled_expansion_into_existing_market',
confirmation__has_enabled_expansion_into_existing_market=Case(
When(customer_response__responded_on__isnull=True, then=None),
default=F('customer_response__has_enabled_expansion_into_existing_market'),
output_field=BooleanField(),
),
confirmation__has_enabled_expansion_into_new_market=F(
'customer_response__has_enabled_expansion_into_new_market',
confirmation__has_enabled_expansion_into_new_market=Case(
When(customer_response__responded_on__isnull=True, then=None),
default=F('customer_response__has_enabled_expansion_into_new_market'),
output_field=BooleanField(),
),
confirmation__has_explicit_export_plans=F(
'customer_response__has_explicit_export_plans',
confirmation__has_explicit_export_plans=Case(
When(customer_response__responded_on__isnull=True, then=None),
default=F('customer_response__has_explicit_export_plans'),
output_field=BooleanField(),
),
confirmation__has_increased_exports_as_percent_of_turnover=F(
'customer_response__has_increased_exports_as_percent_of_turnover',
confirmation__has_increased_exports_as_percent_of_turnover=Case(
When(customer_response__responded_on__isnull=True, then=None),
default=F('customer_response__has_increased_exports_as_percent_of_turnover'),
output_field=BooleanField(),
),
confirmation__improved_profile=Cast(
'customer_response__improved_profile__export_win_id', IntegerField(),
confirmation__improved_profile=Case(
When(customer_response__responded_on__isnull=True, then=None),
default=F('customer_response__improved_profile__export_win_id'),
output_field=IntegerField(),
),
confirmation__interventions_were_prerequisite=F(
'customer_response__interventions_were_prerequisite',
confirmation__interventions_were_prerequisite=Case(
When(customer_response__responded_on__isnull=True, then=None),
default=F('customer_response__interventions_were_prerequisite'),
output_field=BooleanField(),
),
confirmation__involved_state_enterprise=F(
'customer_response__involved_state_enterprise',
confirmation__involved_state_enterprise=Case(
When(customer_response__responded_on__isnull=True, then=None),
default=F('customer_response__involved_state_enterprise'),
output_field=BooleanField(),
),
confirmation__name=F('customer_response__name'),
confirmation__other_marketing_source=F(
'customer_response__other_marketing_source',
),
confirmation__our_support=Cast(
'customer_response__our_support__export_win_id', IntegerField(),
confirmation__our_support=Case(
When(customer_response__responded_on__isnull=True, then=None),
default=F('customer_response__our_support__export_win_id'),
output_field=IntegerField(),
),
confirmation__overcame_problem=Cast(
'customer_response__overcame_problem__export_win_id', IntegerField(),
confirmation__overcame_problem=Case(
When(customer_response__responded_on__isnull=True, then=None),
default=F('customer_response__overcame_problem__export_win_id'),
output_field=IntegerField(),
),
confirmation__support_improved_speed=F(
'customer_response__support_improved_speed',
confirmation__support_improved_speed=Case(
When(customer_response__responded_on__isnull=True, then=None),
default=F('customer_response__support_improved_speed'),
output_field=BooleanField(),
),
country=F('country__iso_alpha2_code'),
hvc=F('hvc__export_win_id'),
Expand Down