Skip to content

Commit

Permalink
Add EYB Lead / Company Activity to Opensearch
Browse files Browse the repository at this point in the history
  • Loading branch information
samuele-mattiuzzo committed Nov 27, 2024
1 parent 7731369 commit 03341b7
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 1 deletion.
1 change: 1 addition & 0 deletions datahub/search/company_activity/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class CompanyActivitySearchApp(SearchApp):
'order__created_by',
'great_export_enquiry',
'great_export_enquiry__contact',
'eyb_lead',
).prefetch_related(
'interaction__contacts',
Prefetch(
Expand Down
13 changes: 13 additions & 0 deletions datahub/search/company_activity/dict_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,16 @@ def activity_great_dict(obj):
'meta_subject': obj.meta_subject,
'data_enquiry': obj.data_enquiry,
}


def activity_eyb_lead_dict(obj):
"""Creates a dictionary for an eyb lead."""
if obj is None:
return None

return {
'id': str(obj.id),
'created_on': obj.created_on,
'company_name': obj.company_name,
'duns_number': obj.duns_number,
}
11 changes: 11 additions & 0 deletions datahub/search/company_activity/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,14 @@ def activity_great_field():
'data_enquiry': Text(index=False),
},
)


def activity_eyb_lead_field():
return Object(
properties={
'id': Keyword(),
'created_on': Date(),
'company_name': Text(index=False),
'duns_number': Text(index=False),
},
)
4 changes: 4 additions & 0 deletions datahub/search/company_activity/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

from datahub.search import dict_utils, fields
from datahub.search.company_activity.dict_utils import (
activity_eyb_lead_dict,
activity_great_dict,
activity_interaction_dict,
activity_investment_dict,
activity_order_dict,
activity_referral_dict,
)
from datahub.search.company_activity.fields import (
activity_eyb_lead_field,
activity_great_field,
activity_interaction_field,
activity_investment_field,
Expand All @@ -32,6 +34,7 @@ class CompanyActivity(BaseSearchModel):
investment = activity_investment_field()
order = activity_order_field()
great_export_enquiry = activity_great_field()
eyb_lead = activity_eyb_lead_field()

COMPUTED_MAPPINGS = {}

Expand All @@ -42,6 +45,7 @@ class CompanyActivity(BaseSearchModel):
'investment': activity_investment_dict,
'order': activity_order_dict,
'great_export_enquiry': activity_great_dict,
'eyb_lead': activity_eyb_lead_dict,
}

SEARCH_FIELDS = (
Expand Down
2 changes: 2 additions & 0 deletions datahub/search/company_activity/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Interaction as DBInteraction,
)
from datahub.investment.project.models import InvestmentProject as DBInvestmentProject
from datahub.investment_lead.models import EYBLead as DBEYBLead
from datahub.omis.order.models import Order as DBOrder
from datahub.search.company_activity import CompanyActivitySearchApp
from datahub.search.company_activity.models import (
Expand Down Expand Up @@ -61,5 +62,6 @@ def remove_interaction_from_opensearch(instance):
SignalReceiver(post_save, DBOrder, sync_related_activity_to_opensearch),
SignalReceiver(post_save, DBGreatExportEnquiry, sync_related_activity_to_opensearch),
SignalReceiver(post_save, DBInvestmentProject, sync_related_activity_to_opensearch),
SignalReceiver(post_save, DBEYBLead, sync_related_activity_to_opensearch),
SignalReceiver(post_delete, DBCompanyActivity, remove_interaction_from_opensearch),
)
15 changes: 15 additions & 0 deletions datahub/search/company_activity/test/test_dict_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from datahub.company_referral.test.factories import CompanyReferralFactory
from datahub.interaction.test.factories import CompanyInteractionFactory
from datahub.investment.project.test.factories import InvestmentProjectFactory
from datahub.investment_lead.test.factories import EYBLeadFactory
from datahub.omis.order.test.factories import OrderFactory
from datahub.search.company_activity import dict_utils

Expand Down Expand Up @@ -89,3 +90,17 @@ def test_activity_great_dict():
assert result['contact']['id'] == str(great.contact.id)
assert result['meta_subject'] == great.meta_subject
assert result['data_enquiry'] == great.data_enquiry


def test_activity_eyb_lead_dict():
obj = None
result = dict_utils.activity_eyb_lead_dict(obj)
assert result is None

eyb_lead = EYBLeadFactory()
result = dict_utils.activity_eyb_lead_dict(eyb_lead)

assert result['id'] == str(eyb_lead.id)
assert result['created_on'] == eyb_lead.created_on
assert result['duns_number'] == eyb_lead.duns_number
assert result['company_name'] == eyb_lead.company_name
41 changes: 41 additions & 0 deletions datahub/search/company_activity/test/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from datahub.company_activity.models import CompanyActivity as DBCompanyActivity
from datahub.company_activity.tests.factories import (
CompanyActivityEYBLeadFactory,
CompanyActivityGreatExportEnquiryFactory,
CompanyActivityInteractionFactory,
CompanyActivityInvestmentProjectFactory,
Expand All @@ -27,6 +28,7 @@ def test_company_activity_referral_to_dict():
'investment': company_activity.investment,
'order': company_activity.order,
'great_export_enquiry': company_activity.great_export_enquiry,
'eyb_lead': company_activity.eyb_lead,
'referral': {
'id': str(company_activity.referral_id),
'completed_on': company_activity.referral.completed_on,
Expand Down Expand Up @@ -123,6 +125,7 @@ def test_company_activity_interaction_to_dict():
'referral': company_activity.referral,
'order': company_activity.order,
'great_export_enquiry': company_activity.great_export_enquiry,
'eyb_lead': company_activity.eyb_lead,
'company': (
{
'id': str(company_activity.company_id),
Expand Down Expand Up @@ -179,6 +182,7 @@ def test_company_activity_investment_to_dict():
},
'client_contacts': client_contacts,
},
'eyb_lead': company_activity.eyb_lead,
'referral': company_activity.referral,
'company': (
{
Expand Down Expand Up @@ -207,6 +211,7 @@ def test_company_activity_order_to_dict():
'investment': company_activity.investment,
'referral': company_activity.referral,
'great_export_enquiry': company_activity.great_export_enquiry,
'eyb_lead': company_activity.eyb_lead,
'company': (
{
'id': str(company_activity.company_id),
Expand Down Expand Up @@ -285,13 +290,49 @@ def test_company_activity_great_to_dict():
'meta_subject': great.meta_subject,
'data_enquiry': great.data_enquiry,
},
'eyb_lead': company_activity.eyb_lead,
'activity_source': DBCompanyActivity.ActivitySource.great_export_enquiry,
'id': company_activity.pk,
'_document_type': CompanyActivitySearchApp.name,
'date': company_activity.date,
}


def test_company_activity_eyb_lead_to_dict():
"""Test converting a CompanyActivity with an eyb lead to a dict."""
company_activity = CompanyActivityEYBLeadFactory.build()

result = CompanyActivity.db_object_to_dict(company_activity)
eyb_lead = company_activity.eyb_lead

assert result == {
'interaction': company_activity.interaction,
'investment': company_activity.investment,
'referral': company_activity.referral,
'company': (
{
'id': str(company_activity.company_id),
'name': company_activity.company.name,
'trading_names': company_activity.company.trading_names,
}
if company_activity.company
else None
),
'order': company_activity.order,
'great_export_enquiry': company_activity.great_export_enquiry,
'eyb_lead': {
'id': str(eyb_lead.id),
'created_on': eyb_lead.created_on,
'duns_number': eyb_lead.duns_number,
'company_name': eyb_lead.company_name,
},
'activity_source': DBCompanyActivity.ActivitySource.eyb_lead,
'id': company_activity.pk,
'_document_type': CompanyActivitySearchApp.name,
'date': company_activity.date,
}


def test_interactions_to_documents():
"""Test converting 2 CompanyActivity's to OpenSearch documents."""
company_activities = CompanyActivityReferralFactory.build_batch(2)
Expand Down
30 changes: 30 additions & 0 deletions datahub/search/company_activity/test/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
InteractionDITParticipantFactory,
)
from datahub.investment.project.test.factories import InvestmentProjectFactory
from datahub.investment_lead.test.factories import EYBLeadFactory
from datahub.omis.order.test.factories import OrderFactory
from datahub.search.company_activity.apps import CompanyActivitySearchApp

Expand Down Expand Up @@ -287,3 +288,32 @@ def test_company_activity_syncs_great_fields_when_changed(opensearch_with_signal
assert actual_great_export_enquiry['contact']['id'] == str(new_contact.id)
assert actual_great_export_enquiry['meta_email_address'] == new_email_address
assert actual_great_export_enquiry['meta_subject'] == new_subject


def test_company_activity_syncs_eyb_lead_fields_when_changed(opensearch_with_signals):
"""Test that company_activities are synced to OpenSearch if their eyb lead updates."""
eyb_lead = EYBLeadFactory()
company_activity = DBCompanyActivity.objects.get(eyb_lead=eyb_lead)
opensearch_with_signals.indices.refresh()

doc = opensearch_with_signals.get(
index=CompanyActivitySearchApp.search_model.get_read_alias(),
id=company_activity.pk,
)

assert doc['_source']['eyb_lead']['duns_number'] == str(eyb_lead.duns_number)
assert doc['_source']['eyb_lead']['company_name'] == str(eyb_lead.company_name)
new_company_name = str(eyb_lead.company_name) + ' new'
eyb_lead.company_name = new_company_name

eyb_lead.save()

opensearch_with_signals.indices.refresh()

updated_doc = opensearch_with_signals.get(
index=CompanyActivitySearchApp.search_model.get_read_alias(),
id=company_activity.pk,
)
actual_eyb_lead = updated_doc['_source']['eyb_lead']
assert actual_eyb_lead['duns_number'] == str(eyb_lead.duns_number)
assert actual_eyb_lead['company_name'] == str(new_company_name)
6 changes: 5 additions & 1 deletion datahub/search/company_activity/test/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)
from datahub.company_activity.models import CompanyActivity
from datahub.company_activity.tests.factories import (
CompanyActivityEYBLeadFactory,
CompanyActivityInteractionFactory,
CompanyActivityInvestmentProjectFactory,
CompanyActivityOmisOrderFactory,
Expand Down Expand Up @@ -62,6 +63,9 @@ def company_activities(opensearch_with_collector):
CompanyActivityOmisOrderFactory(company=company_1),
CompanyActivityOmisOrderFactory(company=company_1),
CompanyActivityOmisOrderFactory(company=company_2),
CompanyActivityEYBLeadFactory(company=company_1),
CompanyActivityEYBLeadFactory(company=company_1),
CompanyActivityEYBLeadFactory(company=company_2),
],
)

Expand Down Expand Up @@ -123,7 +127,7 @@ def test_offset(self, company_activities):

assert response.status_code == status.HTTP_200_OK
response_data = response.json()
assert len(response_data['results']) == 12
assert len(response_data['results']) == 15

def test_default_sort_by_date(self, opensearch_with_collector):
"""Tests default sorting of results by date (descending)."""
Expand Down

0 comments on commit 03341b7

Please sign in to comment.