Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/mcmr 152 add channel from rp contact to registration #321

Merged
Merged
38 changes: 38 additions & 0 deletions eventstore/management/commands/migrate_registration_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from django.core.management.base import BaseCommand

from eventstore.models import (
PostbirthRegistration,
PrebirthRegistration,
PublicRegistration,
)
from ndoh_hub.utils import rapidpro


class Command(BaseCommand):
help = (
"Migrates the preferred channel data from all registrations that don't "
"have a channel, sends a request to RapidPro to look up the channel "
"for the contact, and adds it to the registration"
)

def handle_channel_migration(self):
for each in PublicRegistration.objects.filter(channel=""):
contact = rapidpro.get_contacts(uuid=each.contact_id).first()
if contact.fields["channel"] != "":
each.channel = contact.fields["channel"]
each.save(update_fields=["channel"])

for each in PrebirthRegistration.objects.filter(channel=""):
contact = rapidpro.get_contacts(uuid=each.contact_id).first()
if contact.fields["channel"] != "":
each.channel = contact.fields["channel"]
each.save(update_fields=["channel"])

for each in PostbirthRegistration.objects.filter(channel=""):
contact = rapidpro.get_contacts(uuid=each.contact_id).first()
if contact.fields["channel"] != "":
each.channel = contact.fields["channel"]
each.save(update_fields=["channel"])

def handle(self, *args, **options):
self.handle_channel_migration()
135 changes: 135 additions & 0 deletions eventstore/management/commands/tests/test_migrate_registration_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import responses
from django.core.management import call_command
from django.test import TestCase
from temba_client.v2 import TembaClient

from eventstore.management.commands import migrate_registration_info
from eventstore.models import (
PostbirthRegistration,
PrebirthRegistration,
PublicRegistration,
)


class MigrateToEventstoreTests(TestCase):
@responses.activate
def test_add_channel_public_registration(self):
"""
Should successfully migrate a public registration
"""
p = PublicRegistration.objects.create(
id="7fd2b2d4-e5fd-4264-b365-d2eda1764ba4",
contact_id="0bbb7161-ba0a-45e2-9888-d1a29fa01b40",
device_contact_id="0bbb7161-ba0a-45e2-9888-d1a29fa01b40",
language="zul",
channel="",
)
migrate_registration_info.rapidpro = TembaClient("textit.in", "test-token")
responses.add(
responses.GET,
"https://textit.in/api/v2/contacts.json?uuid=0bbb7161-ba0a-45e2-9888-d1a29fa01b40", # noqa
json={
"results": [
{
"uuid": "0bbb7161-ba0a-45e2-9888-d1a29fa01b40",
"name": "",
"language": "zul",
"groups": [],
"fields": {"facility_code": "123456", "channel": "SMS"},
"blocked": False,
"stopped": False,
"created_on": "2015-11-11T08:30:24.922024+00:00",
"modified_on": "2015-11-11T08:30:25.525936+00:00",
"urns": ["tel:+27820001001"],
}
],
"next": None,
},
)

call_command("migrate_registration_info")

p.refresh_from_db()
self.assertEqual(p.channel, "SMS")

@responses.activate
def test_add_channel_prebirth_registration(self):
"""
Should successfully migrate a prebirth registration
"""
p = PrebirthRegistration.objects.create(
id="7fd2b2d4-e5fd-4264-b365-d2eda1764ba4",
contact_id="0bbb7161-ba0a-45e2-9888-d1a29fa01b40",
device_contact_id="0bbb7161-ba0a-45e2-9888-d1a29fa01b40",
edd="2020-12-12",
language="zul",
channel="",
)
migrate_registration_info.rapidpro = TembaClient("textit.in", "test-token")
responses.add(
responses.GET,
"https://textit.in/api/v2/contacts.json?uuid=0bbb7161-ba0a-45e2-9888-d1a29fa01b40", # noqa
json={
"results": [
{
"uuid": "0bbb7161-ba0a-45e2-9888-d1a29fa01b40",
"name": "",
"language": "zul",
"groups": [],
"fields": {"facility_code": "123456", "channel": "SMS"},
"blocked": False,
"stopped": False,
"created_on": "2015-11-11T08:30:24.922024+00:00",
"modified_on": "2015-11-11T08:30:25.525936+00:00",
"urns": ["tel:+27820001001"],
}
],
"next": None,
},
)

call_command("migrate_registration_info")

p.refresh_from_db()
self.assertEqual(p.channel, "SMS")

@responses.activate
def test_add_channel_postbirth_registration(self):
"""
Should successfully migrate a public registration
"""
p = PostbirthRegistration.objects.create(
id="7fd2b2d4-e5fd-4264-b365-d2eda1764ba4",
contact_id="0bbb7161-ba0a-45e2-9888-d1a29fa01b40",
device_contact_id="0bbb7161-ba0a-45e2-9888-d1a29fa01b40",
baby_dob="2020-12-12",
language="zul",
channel="",
)
migrate_registration_info.rapidpro = TembaClient("textit.in", "test-token")
responses.add(
responses.GET,
"https://textit.in/api/v2/contacts.json?uuid=0bbb7161-ba0a-45e2-9888-d1a29fa01b40", # noqa
json={
"results": [
{
"uuid": "0bbb7161-ba0a-45e2-9888-d1a29fa01b40",
"name": "",
"language": "zul",
"groups": [],
"fields": {"facility_code": "123456", "channel": "SMS"},
"blocked": False,
"stopped": False,
"created_on": "2015-11-11T08:30:24.922024+00:00",
"modified_on": "2015-11-11T08:30:25.525936+00:00",
"urns": ["tel:+27820001001"],
}
],
"next": None,
},
)

call_command("migrate_registration_info")

p.refresh_from_db()
self.assertEqual(p.channel, "SMS")
14 changes: 13 additions & 1 deletion scripts/migrate_to_rapidpro/upload_to_rapidpro.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,19 @@ def string_field(field, value):
VALUES %s
ON CONFLICT DO NOTHING
""",
((contact_id, f"whatsapp:{wa}", wa, None, "whatsapp", ORG, 50, None, None),),
(
(
contact_id,
f"whatsapp:{wa}",
wa,
None,
"whatsapp",
ORG,
50,
None,
None,
),
),
)

if time.time() - d_print > 1:
Expand Down