Skip to content

Commit

Permalink
Merge pull request #5523 from uktrade/feature/legacy-export-win-notif…
Browse files Browse the repository at this point in the history
…ications-migration

Import legacy win notifications.
  • Loading branch information
elcct authored Jul 9, 2024
2 parents e384e06 + 70508fe commit 4eac9a4
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
36 changes: 36 additions & 0 deletions datahub/export_win/legacy_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
BreakdownType,
BusinessPotential,
CustomerResponse,
CustomerResponseToken,
ExpectedValueRelation,
Experience,
HQTeamRegionOrPost,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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'),
),
]
7 changes: 7 additions & 0 deletions datahub/export_win/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})'

Expand Down
78 changes: 78 additions & 0 deletions datahub/export_win/test/test_legacy_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
_email_mapping,
migrate_all_legacy_wins,
migrate_edit_history,
migrate_notifications,
)
from datahub.export_win.models import (
Win,
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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',
},
],
},
}


Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 4eac9a4

Please sign in to comment.