Skip to content

Commit

Permalink
Merge pull request #5328 from uktrade/fix/win-dataset-endpoint
Browse files Browse the repository at this point in the history
Fix company contact and cdms reference in Win dataset endpoint.
  • Loading branch information
elcct authored Apr 4, 2024
2 parents 210ca4c + e9d99e6 commit e7b3721
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
14 changes: 10 additions & 4 deletions datahub/dataset/export_wins/test/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from freezegun import freeze_time

from datahub.company.test.factories import CompanyFactory, ContactFactory
from datahub.core.test_utils import (
format_date_or_datetime,
)
Expand Down Expand Up @@ -160,12 +161,13 @@ def _assert_win_matches_result(
tokens,
result,
):
contact = win.company_contacts.first()
expected = {
'created_on': format_date_or_datetime(win.created_on),
'id': str(win.id),
'audit': None,
'business_type': win.business_type,
'cdms_reference': win.cdms_reference,
'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,
Expand Down Expand Up @@ -203,9 +205,9 @@ def _assert_win_matches_result(
'confirmation__support_improved_speed': win.customer_response.support_improved_speed,
'country': win.country.iso_alpha2_code,
'created': format_date_or_datetime(win.created_on),
'customer_email_address': win.customer_email_address,
'customer_job_title': win.customer_job_title,
'customer_name': win.customer_name,
'customer_email_address': contact.email,
'customer_job_title': contact.job_title,
'customer_name': contact.name,
'date': format_date_or_datetime(win.date),
'description': win.description,
'has_hvo_specialist_involvement': win.has_hvo_specialist_involvement,
Expand Down Expand Up @@ -252,10 +254,14 @@ def _assert_win_matches_result(
def test_success(self, data_flow_api_client):
associated_programmes = AssociatedProgrammeFactory.create_batch(3)
types_of_support = SupportTypeFactory.create_batch(2)
company = CompanyFactory(company_number='012345')
contact = ContactFactory(company=company)
win = self.factory(
associated_programme=associated_programmes,
type_of_support=types_of_support,
hvo_programme=HVOProgrammesFactory(),
company=company,
company_contacts=[contact],
)
customer_response = CustomerResponseFactory(win=win)
tokens = CustomerResponseTokenFactory.create_batch(3, customer_response=customer_response)
Expand Down
41 changes: 35 additions & 6 deletions datahub/dataset/export_wins/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from django.contrib.postgres.expressions import ArraySubquery
from django.db.models import Case, Count, ExpressionWrapper, F, Max, When
from django.db.models import IntegerField, OuterRef, Value
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.functions import (
Cast,
Concat,
ExtractMonth,
ExtractYear,
)

from datahub.company.models import Contact
from datahub.dataset.core.views import BaseDatasetView, BaseFilterDatasetView
from datahub.dataset.export_wins.pagination import HVCDatasetViewCursorPagination
from datahub.dataset.export_wins.utils import (
Expand Down Expand Up @@ -128,17 +130,33 @@ def get_dataset(self):
'hvo_programme',
'sector',
)
.annotate(
# Subquery returns a JSONB document to avoid creating multiple subqueries
# to retrieve different properties of the same contact
customer_details=Subquery(
Contact.objects.filter(
wins=OuterRef('pk'),
).annotate(
details=Func(
Value('name'), Concat('first_name', Value(' '), 'last_name'),
Value('email'), Cast('email', CharField()),
Value('job_title'), 'job_title',
function='jsonb_build_object',
),
).order_by(
'pk',
).values('details')[:1],
output_field=JSONField(),
),
temp_cdms_reference=F('cdms_reference'),
)
.prefetch_related('associated_programme', 'type_of_support')
.values(
'created_on',
'id',
'audit',
'business_type',
'cdms_reference',
'complete',
'customer_email_address',
'customer_job_title',
'customer_name',
'date',
'description',
'has_hvo_specialist_involvement',
Expand Down Expand Up @@ -256,6 +274,17 @@ def get_dataset(self):
export_wins_export_experience_display=F(
'export_experience__export_wins_export_experience__name',
),
customer_email_address=F('customer_details__email'),
customer_job_title=F('customer_details__job_title'),
customer_name=F('customer_details__name'),
cdms_reference=Case(
When(
temp_cdms_reference='',
then=F('company__company_number'),
),
default=F('temp_cdms_reference'),
output_field=CharField(),
),
)
)

Expand Down

0 comments on commit e7b3721

Please sign in to comment.