From 91c9bf7be6d3fb32d85695a343a25e6f144c6b67 Mon Sep 17 00:00:00 2001 From: Samuele Mattiuzzo <1381563+samuele-mattiuzzo@users.noreply.github.com> Date: Wed, 27 Nov 2024 16:44:18 +0000 Subject: [PATCH] CLS2-1031 Add EYB Lead to Company Activity and Opensearch (#5828) * Add EYB Lead to Company Activity * Add EYB Lead / Company Activity to Opensearch --- .../commands/test_delete_orphaned_versions.py | 1 + .../0018_companyactivity_eyb_lead_and_more.py | 25 ++++++ .../models/company_activity.py | 14 ++++ datahub/company_activity/tasks/sync.py | 41 ++++++++-- datahub/company_activity/tests/factories.py | 23 ++++++ .../tests/test_tasks/test_eyb_lead_tasks.py | 76 +++++++++++++++++++ .../tests/test_tasks/test_order_task.py | 4 +- datahub/investment_lead/models.py | 17 ++++- datahub/investment_lead/test/test_models.py | 20 +++++ datahub/search/company_activity/apps.py | 1 + datahub/search/company_activity/dict_utils.py | 13 ++++ datahub/search/company_activity/fields.py | 11 +++ datahub/search/company_activity/models.py | 4 + datahub/search/company_activity/signals.py | 2 + .../company_activity/test/test_dict_utils.py | 15 ++++ .../company_activity/test/test_models.py | 41 ++++++++++ .../company_activity/test/test_signals.py | 30 ++++++++ .../company_activity/test/test_views.py | 6 +- 18 files changed, 335 insertions(+), 9 deletions(-) create mode 100644 datahub/company_activity/migrations/0018_companyactivity_eyb_lead_and_more.py create mode 100644 datahub/company_activity/tests/test_tasks/test_eyb_lead_tasks.py diff --git a/datahub/cleanup/test/commands/test_delete_orphaned_versions.py b/datahub/cleanup/test/commands/test_delete_orphaned_versions.py index 109c06210..c4ce46937 100644 --- a/datahub/cleanup/test/commands/test_delete_orphaned_versions.py +++ b/datahub/cleanup/test/commands/test_delete_orphaned_versions.py @@ -112,6 +112,7 @@ GreatExportEnquiryFactory, InvestmentProjectFactory, OrderFactory, + EYBLeadFactory, ] diff --git a/datahub/company_activity/migrations/0018_companyactivity_eyb_lead_and_more.py b/datahub/company_activity/migrations/0018_companyactivity_eyb_lead_and_more.py new file mode 100644 index 000000000..404a8c9df --- /dev/null +++ b/datahub/company_activity/migrations/0018_companyactivity_eyb_lead_and_more.py @@ -0,0 +1,25 @@ +# Generated by Django 4.2.16 on 2024-11-27 15:47 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('investment_lead', '0009_alter_eyblead_marketing_hashed_uuid'), + ('company_activity', '0017_ingestedfile_file_created_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='companyactivity', + name='eyb_lead', + field=models.ForeignKey(blank=True, help_text='If related to an EYB lead, must not have relations to any other activity (referral, event etc)', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='activity', to='investment_lead.eyblead', unique=True), + ), + migrations.AlterField( + model_name='companyactivity', + name='activity_source', + field=models.CharField(choices=[('interaction', 'interaction'), ('referral', 'referral'), ('event', 'event'), ('investment', 'investment'), ('order', 'order'), ('great_export_enquiry', 'great_export_enquiry'), ('eyb_lead', 'eyb_lead')], help_text='The type of company activity, such as an interaction, event, referral etc.', max_length=255), + ), + ] diff --git a/datahub/company_activity/models/company_activity.py b/datahub/company_activity/models/company_activity.py index 9c6f5739c..0e49ab1ff 100644 --- a/datahub/company_activity/models/company_activity.py +++ b/datahub/company_activity/models/company_activity.py @@ -30,6 +30,7 @@ class ActivitySource(models.TextChoices): investment = ('investment', 'investment') order = ('order', 'order') great_export_enquiry = ('great_export_enquiry', 'great_export_enquiry') + eyb_lead = ('eyb_lead', 'eyb_lead') id = models.UUIDField(primary_key=True, default=uuid.uuid4) company = models.ForeignKey( @@ -114,6 +115,19 @@ class ActivitySource(models.TextChoices): ), ) + eyb_lead = models.ForeignKey( + 'investment_lead.EYBLead', + unique=True, + null=True, + blank=True, + related_name='activity', + on_delete=models.CASCADE, + help_text=( + 'If related to an EYB lead, must not have relations to any other activity ' + '(referral, event etc)' + ), + ) + def __str__(self): """Readable name for CompanyActivity""" return f'Activity from "{self.activity_source}" for company: {self.company.name}' diff --git a/datahub/company_activity/tasks/sync.py b/datahub/company_activity/tasks/sync.py index 7f0de514c..52a18d3eb 100644 --- a/datahub/company_activity/tasks/sync.py +++ b/datahub/company_activity/tasks/sync.py @@ -7,6 +7,7 @@ from datahub.core.queues.scheduler import LONG_RUNNING_QUEUE from datahub.interaction.models import Interaction from datahub.investment.project.models import InvestmentProject +from datahub.investment_lead.models import EYBLead from datahub.omis.order.models import Order @@ -15,7 +16,7 @@ def relate_company_activity_to_interactions(batch_size=500): """ - Grabs all interactions so they can be related to in the + Grabs all interactions so they can be related to the `CompanyActivity` model with bulk_create. Excludes any interactions already associated in the CompanyActivity model. @@ -46,7 +47,7 @@ def relate_company_activity_to_interactions(batch_size=500): def relate_company_activity_to_referrals(batch_size=500): """ - Grabs all referrals so they can be related to in the + Grabs all referrals so they can be related to the `CompanyActivity` model with a bulk_create. Excludes any referrals already associated in the CompanyActivity model. @@ -76,7 +77,7 @@ def relate_company_activity_to_referrals(batch_size=500): def relate_company_activity_to_investment_projects(batch_size=500): """ - Grabs all investment projects so they can be related to in the + Grabs all investment projects so they can be related to the `CompanyActivity` model with a bulk_create. Excludes any investment projects already associated in the CompanyActivity model. """ @@ -106,7 +107,7 @@ def relate_company_activity_to_investment_projects(batch_size=500): def relate_company_activity_to_orders(batch_size=500): """ - Grabs all omis orders so they can be related to in the + Grabs all omis orders so they can be related to the `CompanyActivity` model with a bulk_create. Excludes any order projects already associated in the CompanyActivity model. """ @@ -136,7 +137,7 @@ def relate_company_activity_to_orders(batch_size=500): def relate_company_activity_to_great(batch_size=500): """ - Grabs all great export enquiry so they can be related to in the + Grabs all great export enquiry so they can be related to the `CompanyActivity` model with a bulk_create. Excludes any great export enquiry already associated in the CompanyActivity model. """ @@ -164,6 +165,36 @@ def relate_company_activity_to_great(batch_size=500): bulk_create_activity(objs, batch_size) +def relate_company_activity_to_eyb_lead(batch_size=500): + """ + Grabs all EYB leads so they can be related to the + `CompanyActivity` model with a bulk_create. Excludes any + EYB leads already associated in the CompanyActivity model. + """ + activity = set( + CompanyActivity.objects.filter( + eyb_lead__isnull=False, + ).values_list('eyb_lead', flat=True), + ) + + eyb_leads = EYBLead.objects.filter( + company__isnull=False, + ).values('id', 'created_on', 'company_id') + + objs = [ + CompanyActivity( + eyb_lead_id=eyb_lead['id'], + date=eyb_lead['created_on'], + company_id=eyb_lead['company_id'], + activity_source=CompanyActivity.ActivitySource.eyb_lead, + ) + for eyb_lead in eyb_leads + if eyb_lead['id'] not in activity + ] + + bulk_create_activity(objs, batch_size) + + def schedule_sync_data_to_company_activity(relate_function): """ Schedules a task for the given function. diff --git a/datahub/company_activity/tests/factories.py b/datahub/company_activity/tests/factories.py index 7a52ac4bb..d0b744a52 100644 --- a/datahub/company_activity/tests/factories.py +++ b/datahub/company_activity/tests/factories.py @@ -6,6 +6,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.metadata.test.factories import CountryFactory, SectorFactory from datahub.omis.order.test.factories import OrderFactory @@ -205,3 +206,25 @@ def _create(cls, model_class, *args, **kwargs): """ obj = model_class(*args, **kwargs) return CompanyActivity.objects.get(great_export_enquiry_id=obj.great_export_enquiry_id) + + +class CompanyActivityEYBLeadFactory(CompanyActivityBaseFactory): + """ + CompanyActivity factory with an EYB lead. + """ + + activity_source = CompanyActivity.ActivitySource.eyb_lead + eyb_lead = factory.SubFactory(EYBLeadFactory) + + class Meta: + model = 'company_activity.CompanyActivity' + + @classmethod + def _create(cls, model_class, *args, **kwargs): + """ + Overwrite the _create to prevent the CompanyActivity from saving to the database. + This is due to the EYB lead already creating the CompanyActivity inside its + model save. + """ + obj = model_class(*args, **kwargs) + return CompanyActivity.objects.get(eyb_lead_id=obj.eyb_lead_id) diff --git a/datahub/company_activity/tests/test_tasks/test_eyb_lead_tasks.py b/datahub/company_activity/tests/test_tasks/test_eyb_lead_tasks.py new file mode 100644 index 000000000..4af3977de --- /dev/null +++ b/datahub/company_activity/tests/test_tasks/test_eyb_lead_tasks.py @@ -0,0 +1,76 @@ +from unittest import mock + +import pytest + +from datahub.company_activity.models import CompanyActivity +from datahub.company_activity.tasks.sync import ( + relate_company_activity_to_eyb_lead, + schedule_sync_data_to_company_activity, +) +from datahub.investment_lead.test.factories import EYBLeadFactory + + +@pytest.mark.django_db +class TestCompanyActivityEYBLeadTasks: + """ + Tests for the schedule_sync_data_to_company_activity task. + """ + + def test_eyb_leads_are_copied_to_company_activity(self): + """ + Test that eyb leads are added to the CompanyActivity model. + """ + eyb_leads = EYBLeadFactory.create_batch(5) + + # Remove the created CompanyActivities added by the eyb lead `save` method + # to mimic already existing data in staging and prod database. + CompanyActivity.objects.all().delete() + assert CompanyActivity.objects.count() == 0 + + # Check the "existing" eyb leads are added to the company activity model + schedule_sync_data_to_company_activity(relate_company_activity_to_eyb_lead) + assert CompanyActivity.objects.count() == len(eyb_leads) + + company_activity = CompanyActivity.objects.get(eyb_lead=eyb_leads[0]) + assert company_activity.date == eyb_leads[0].created_on + assert company_activity.activity_source == CompanyActivity.ActivitySource.eyb_lead + assert company_activity.eyb_lead_id == eyb_leads[0].id + + @mock.patch('datahub.company_activity.models.CompanyActivity.objects.bulk_create') + def test_eyb_leads_are_bulk_created_in_batches(self, mocked_bulk_create, caplog): + """ + Test that eyb leads are bulk created in batches. + """ + caplog.set_level('INFO') + batch_size = 5 + + EYBLeadFactory.create_batch(10) + + # Delete any activity created through the investments save method. + CompanyActivity.objects.all().delete() + assert CompanyActivity.objects.count() == 0 + + # Ensure eyb leads are bulk_created + relate_company_activity_to_eyb_lead(batch_size) + assert mocked_bulk_create.call_count == 2 + + assert ( + f'Creating in batches of: {batch_size} CompanyActivities. 10 remaining.' in caplog.text + ) + assert ( + f'Creating in batches of: {batch_size} CompanyActivities. 5 remaining.' in caplog.text + ) + assert 'Finished bulk creating CompanyActivities.' in caplog.text + + def test_eyb_leads_with_a_company_activity_are_not_added_again(self): + """ + Test that eyb leads which are already part of the `CompanyActivity` model + are not added again. + """ + EYBLeadFactory.create_batch(4) + + assert CompanyActivity.objects.count() == 4 + + # Check count remains unchanged. + schedule_sync_data_to_company_activity(relate_company_activity_to_eyb_lead) + assert CompanyActivity.objects.count() == 4 diff --git a/datahub/company_activity/tests/test_tasks/test_order_task.py b/datahub/company_activity/tests/test_tasks/test_order_task.py index c51b5e3d4..4822f332c 100644 --- a/datahub/company_activity/tests/test_tasks/test_order_task.py +++ b/datahub/company_activity/tests/test_tasks/test_order_task.py @@ -13,12 +13,12 @@ @pytest.mark.django_db class TestCompanyActivityOrderTasks: """ - Tests for the schedule_sync_investments_to_company_activity task. + Tests for the schedule_sync_data_to_company_activity task. """ def test_orders_are_copied_to_company_activity(self): """ - Test that investments are added to the CompanyActivity model. + Test that omis orders are added to the CompanyActivity model. """ orders = OrderFactory.create_batch(5) diff --git a/datahub/investment_lead/models.py b/datahub/investment_lead/models.py index f565b2cd0..b049210f4 100644 --- a/datahub/investment_lead/models.py +++ b/datahub/investment_lead/models.py @@ -6,9 +6,10 @@ MaxLengthValidator, MinLengthValidator, ) -from django.db import models +from django.db import models, transaction from mptt.fields import TreeForeignKey +from datahub.company_activity.models import CompanyActivity from datahub.core import reversion from datahub.core.models import ArchivableModel @@ -221,3 +222,17 @@ def name(self): if self.company: return f'EYB Lead ({shortened_pk}...) for {self.company.name}' return f'EYB Lead ({shortened_pk}...)' + + def save(self, *args, **kwargs): + """ + Creates a CompanyActivity when a EYB Lead is saved + """ + with transaction.atomic(): + super().save(*args, **kwargs) + if not self.company: + return + CompanyActivity.objects.update_or_create( + eyb_lead_id=self.id, + activity_source=CompanyActivity.ActivitySource.eyb_lead, + defaults={'date': self.created_on, 'company_id': self.company_id}, + ) diff --git a/datahub/investment_lead/test/test_models.py b/datahub/investment_lead/test/test_models.py index 490e19434..f8edddc23 100644 --- a/datahub/investment_lead/test/test_models.py +++ b/datahub/investment_lead/test/test_models.py @@ -1,10 +1,30 @@ import pytest +from datahub.company_activity.models import CompanyActivity +from datahub.investment_lead.test.factories import EYBLeadFactory + @pytest.mark.django_db class TestEYBLead: """Tests EYB Lead model""" + def test_save_without_company_no_company_activity(self): + assert not CompanyActivity.objects.all().exists() + EYBLeadFactory(company=None) + assert not CompanyActivity.objects.all().exists() + + def test_save_with_company_creates_company_activity(self): + assert not CompanyActivity.objects.all().exists() + + eyb_lead = EYBLeadFactory() + + assert CompanyActivity.objects.all().count() == 1 + + company_activity = CompanyActivity.objects.get(eyb_lead=eyb_lead.id) + assert company_activity.company_id == eyb_lead.company.id + assert company_activity.date == eyb_lead.created_on + assert company_activity.activity_source == CompanyActivity.ActivitySource.eyb_lead + def test_str(self, eyb_lead_instance_from_db): """Test the human friendly string representation of the object""" assert str(eyb_lead_instance_from_db) == eyb_lead_instance_from_db.name diff --git a/datahub/search/company_activity/apps.py b/datahub/search/company_activity/apps.py index d7f3b3397..37dd37612 100644 --- a/datahub/search/company_activity/apps.py +++ b/datahub/search/company_activity/apps.py @@ -31,6 +31,7 @@ class CompanyActivitySearchApp(SearchApp): 'order__created_by', 'great_export_enquiry', 'great_export_enquiry__contact', + 'eyb_lead', ).prefetch_related( 'interaction__contacts', Prefetch( diff --git a/datahub/search/company_activity/dict_utils.py b/datahub/search/company_activity/dict_utils.py index 34cd751f4..99dd95187 100644 --- a/datahub/search/company_activity/dict_utils.py +++ b/datahub/search/company_activity/dict_utils.py @@ -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, + } diff --git a/datahub/search/company_activity/fields.py b/datahub/search/company_activity/fields.py index 393bd8a38..98083fb06 100644 --- a/datahub/search/company_activity/fields.py +++ b/datahub/search/company_activity/fields.py @@ -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), + }, + ) diff --git a/datahub/search/company_activity/models.py b/datahub/search/company_activity/models.py index 454a3409e..6d310a53c 100644 --- a/datahub/search/company_activity/models.py +++ b/datahub/search/company_activity/models.py @@ -2,6 +2,7 @@ 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, @@ -9,6 +10,7 @@ activity_referral_dict, ) from datahub.search.company_activity.fields import ( + activity_eyb_lead_field, activity_great_field, activity_interaction_field, activity_investment_field, @@ -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 = {} @@ -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 = ( diff --git a/datahub/search/company_activity/signals.py b/datahub/search/company_activity/signals.py index cd75c6d84..974de3ad7 100644 --- a/datahub/search/company_activity/signals.py +++ b/datahub/search/company_activity/signals.py @@ -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 ( @@ -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), ) diff --git a/datahub/search/company_activity/test/test_dict_utils.py b/datahub/search/company_activity/test/test_dict_utils.py index f8ddeb13c..f268882bb 100644 --- a/datahub/search/company_activity/test/test_dict_utils.py +++ b/datahub/search/company_activity/test/test_dict_utils.py @@ -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 @@ -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 diff --git a/datahub/search/company_activity/test/test_models.py b/datahub/search/company_activity/test/test_models.py index 6472fe70d..926af855e 100644 --- a/datahub/search/company_activity/test/test_models.py +++ b/datahub/search/company_activity/test/test_models.py @@ -4,6 +4,7 @@ from datahub.company_activity.models import CompanyActivity as DBCompanyActivity from datahub.company_activity.tests.factories import ( + CompanyActivityEYBLeadFactory, CompanyActivityGreatExportEnquiryFactory, CompanyActivityInteractionFactory, CompanyActivityInvestmentProjectFactory, @@ -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, @@ -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), @@ -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': ( { @@ -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), @@ -285,6 +290,7 @@ 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, @@ -292,6 +298,41 @@ def test_company_activity_great_to_dict(): } +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) diff --git a/datahub/search/company_activity/test/test_signals.py b/datahub/search/company_activity/test/test_signals.py index 054d1082c..8b7680e52 100644 --- a/datahub/search/company_activity/test/test_signals.py +++ b/datahub/search/company_activity/test/test_signals.py @@ -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 @@ -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) diff --git a/datahub/search/company_activity/test/test_views.py b/datahub/search/company_activity/test/test_views.py index 2952be41a..7ea8e16e9 100644 --- a/datahub/search/company_activity/test/test_views.py +++ b/datahub/search/company_activity/test/test_views.py @@ -16,6 +16,7 @@ ) from datahub.company_activity.models import CompanyActivity from datahub.company_activity.tests.factories import ( + CompanyActivityEYBLeadFactory, CompanyActivityInteractionFactory, CompanyActivityInvestmentProjectFactory, CompanyActivityOmisOrderFactory, @@ -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), ], ) @@ -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)."""