From 70508fe0cb515111704d7d501e57e9deba012d3e Mon Sep 17 00:00:00 2001 From: Pawel Szymanski Date: Mon, 8 Jul 2024 08:50:33 +0100 Subject: [PATCH] Import legacy win notifications. --- datahub/export_win/legacy_migration.py | 36 +++++++++ ...ustomerresponsetoken_legacy_id_and_more.py | 23 ++++++ datahub/export_win/models.py | 7 ++ .../export_win/test/test_legacy_migration.py | 78 +++++++++++++++++++ 4 files changed, 144 insertions(+) create mode 100644 datahub/export_win/migrations/0051_customerresponsetoken_legacy_id_and_more.py diff --git a/datahub/export_win/legacy_migration.py b/datahub/export_win/legacy_migration.py index 9b63bf951d..bb62c18735 100644 --- a/datahub/export_win/legacy_migration.py +++ b/datahub/export_win/legacy_migration.py @@ -27,6 +27,7 @@ BreakdownType, BusinessPotential, CustomerResponse, + CustomerResponseToken, ExpectedValueRelation, Experience, HQTeamRegionOrPost, @@ -619,6 +620,41 @@ def migrate_edit_history(soft_deleted=False): migrate_legacy_edit_history(content_type_id, legacy_edit_history, soft_deleted) +def migrate_legacy_notifications(legacy_notification): + try: + win = Win.objects.all_wins().select_related( + 'customer_response', + ).get(pk=legacy_notification['win_id']) + customer_response = win.customer_response + + if legacy_notification['type'] == 'c': + # customer notification + token, _ = CustomerResponseToken.objects.update_or_create( + customer_response_id=customer_response.id, + legacy_id=legacy_notification['id'], + defaults={ + 'legacy_recipient': legacy_notification['recipient'], + 'expires_on': legacy_notification['created'], + }, + ) + token.created_on = legacy_notification['created'] + token.save() + elif legacy_notification['type'] == 'o': + customer_response.lead_officer_email_sent_on = legacy_notification['created'] + customer_response.save() + except Win.DoesNotExist: + logger.warning(f'Legacy Win {legacy_notification["win_id"]} does not exist.') + pass + + return None + + +def migrate_notifications(soft_deleted=False): + for page in get_legacy_export_wins_dataset('/datasets/data-hub-notifications'): + for legacy_notification in page: + migrate_legacy_notifications(legacy_notification) + + def _email_mapping(email): return { email, diff --git a/datahub/export_win/migrations/0051_customerresponsetoken_legacy_id_and_more.py b/datahub/export_win/migrations/0051_customerresponsetoken_legacy_id_and_more.py new file mode 100644 index 0000000000..a993a934c5 --- /dev/null +++ b/datahub/export_win/migrations/0051_customerresponsetoken_legacy_id_and_more.py @@ -0,0 +1,23 @@ +# Generated by Django 4.2.11 on 2024-07-08 07:51 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('export_win', '0050_legacyexportwinstodatahubadminuser'), + ] + + operations = [ + migrations.AddField( + model_name='customerresponsetoken', + name='legacy_id', + field=models.IntegerField(blank=True, null=True, unique=True), + ), + migrations.AddField( + model_name='customerresponsetoken', + name='legacy_recipient', + field=models.CharField(blank=True, max_length=256, verbose_name='Legacy recipient'), + ), + ] diff --git a/datahub/export_win/models.py b/datahub/export_win/models.py index a275bad701..5a9523962f 100644 --- a/datahub/export_win/models.py +++ b/datahub/export_win/models.py @@ -741,6 +741,13 @@ class CustomerResponseToken(models.Model): times_used = models.PositiveIntegerField(default=0) created_on = models.DateTimeField(db_index=True, null=True, blank=True, auto_now_add=True) + legacy_id = models.IntegerField(blank=True, null=True, unique=True) + legacy_recipient = models.CharField( + blank=True, + max_length=256, + verbose_name='Legacy recipient', + ) + def __str__(self): return f'Token: {self.id} ({self.expires_on})' diff --git a/datahub/export_win/test/test_legacy_migration.py b/datahub/export_win/test/test_legacy_migration.py index 89844cfc58..2101ae6677 100644 --- a/datahub/export_win/test/test_legacy_migration.py +++ b/datahub/export_win/test/test_legacy_migration.py @@ -21,6 +21,7 @@ _email_mapping, migrate_all_legacy_wins, migrate_edit_history, + migrate_notifications, ) from datahub.export_win.models import ( Win, @@ -55,6 +56,11 @@ f'{settings.EXPORT_WINS_SERVICE_BASE_URL}/datasets/data-hub-edit-history' '?cursor=1&source=L', ], + 'notifications': [ + f'{settings.EXPORT_WINS_SERVICE_BASE_URL}/datasets/data-hub-notifications', + f'{settings.EXPORT_WINS_SERVICE_BASE_URL}/datasets/data-hub-notifications' + '?cursor=1&source=L', + ], } legacy_wins = { @@ -628,6 +634,52 @@ }, ], }, + mock_legacy_wins_page_urls['notifications'][0]: { + 'next': mock_legacy_wins_page_urls['notifications'][1], + 'results': [ + { + 'id': 204, + 'win_id': '4c90a214-035f-4445-b6a1-ca7af3486f8c', + 'created': '2020-02-29T12:47:50.062940Z', + 'type': 'c', + 'user__name': 'John Doe', + 'user__email': 'john.doe@test', + 'recipient': 'test@test', + }, + ], + }, + mock_legacy_wins_page_urls['notifications'][1]: { + 'next': None, + 'results': [ + { + 'id': 205, + 'win_id': '4c90a214-035f-4445-b6a1-ca7af3486f8c', + 'created': '2020-02-29T12:47:51.012230Z', + 'type': 'c', + 'user__name': 'John Doe', + 'user__email': 'john.doe@test', + 'recipient': 'test@test', + }, + { + 'id': 206, + 'win_id': '4c90a214-035f-4445-b6a1-ca7af3486f8c', + 'created': '2020-02-29T15:47:50.062940Z', + 'type': 'o', + 'user__name': 'John Doe', + 'user__email': 'john.doe@test', + 'recipient': 'john.doe@test', + }, + { + 'id': 207, + 'win_id': '11111111-035f-4445-b6a1-ca7af3486f8c', + 'created': '2020-02-29T12:47:50.062940Z', + 'type': 'c', + 'user__name': 'John Doe', + 'user__email': 'john.doe@test', + 'recipient': 'test@test', + }, + ], + }, } @@ -890,6 +942,32 @@ def test_legacy_migration(mock_legacy_wins_pages): } assert LogEntry.objects.count() == 0 + migrate_notifications() + + win_1_tokens = win_1.customer_response.tokens + win_1.customer_response.refresh_from_db() + assert win_1_tokens.count() == 2 + tokens = win_1_tokens.filter(customer_response=win_1.customer_response) + token_ids = {token.legacy_id for token in tokens} + assert token_ids == {204, 205} + token_created = {token.created_on for token in tokens} + assert token_created == { + datetime(2020, 2, 29, 12, 47, 50, 62940, tzinfo=timezone.utc), + datetime(2020, 2, 29, 12, 47, 51, 12230, tzinfo=timezone.utc), + } + token_expires = {token.expires_on for token in tokens} + assert token_created == token_expires + assert win_1.customer_response.lead_officer_email_sent_on == datetime( + 2020, + 2, + 29, + 15, + 47, + 50, + 62940, + tzinfo=timezone.utc, + ) + @pytest.mark.parametrize( 'email,expected',