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

Add openhim queue model and signal #590

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions eventstore/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@

class EventstoreConfig(AppConfig):
name = "eventstore"

def ready(self):
import eventstore.signals # noqa
54 changes: 54 additions & 0 deletions eventstore/migrations/0062_openhimqueue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Generated by Django 4.1.7 on 2023-11-23 09:40

import uuid

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("eventstore", "0061_alter_optout_reason"),
]

operations = [
migrations.CreateModel(
name="OpenHIMQueue",
fields=[
(
"id",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
),
),
("object_id", models.UUIDField()),
(
"object_type",
models.CharField(
choices=[
("PrebirthRegistration", "PrebirthRegistration"),
("CHWRegistration", "CHWRegistration"),
("PublicRegistration", "PublicRegistration"),
("ChannelSwitch", "ChannelSwitch"),
("OptOut", "OptOut"),
],
max_length=30,
),
),
(
"status",
models.PositiveSmallIntegerField(
choices=[
(0, "Pending"),
(1, "Processing"),
(2, "Complete"),
(3, "Error"),
],
default=0,
),
),
],
),
]
26 changes: 26 additions & 0 deletions eventstore/migrations/0063_alter_openhimqueue_object_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.1.7 on 2023-11-23 11:40

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("eventstore", "0062_openhimqueue"),
]

operations = [
migrations.AlterField(
model_name="openhimqueue",
name="object_type",
field=models.CharField(
choices=[
("prebirth_registration", "PrebirthRegistration"),
("chw_registration", "CHWRegistration"),
("public_registration", "PublicRegistration"),
("channel_switch", "ChannelSwitch"),
("optout", "OptOut"),
],
max_length=30,
),
),
]
29 changes: 29 additions & 0 deletions eventstore/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1094,3 +1094,32 @@ def clean(self):
date(self.dob_year, self.dob_month, self.dob_day)
except ValueError as e:
raise ValidationError(f"Invalid date of birth date, {str(e)}")


class OpenHIMQueue(models.Model):
class ObjectType:
PREBIRTH_REGISTRATION = "prebirth_registration"
CHW_REGISTRATION = "chw_registration"
PUBLIC_REGISTRATION = "public_registration"
CHANNEL_SWITCH = "channel_switch"
OPTOUT = "optout"
choices = (
(PREBIRTH_REGISTRATION, "PrebirthRegistration"),
(CHW_REGISTRATION, "CHWRegistration"),
(PUBLIC_REGISTRATION, "PublicRegistration"),
(CHANNEL_SWITCH, "ChannelSwitch"),
(OPTOUT, "OptOut"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big thing, but I think we usually use the snake case for these values, eg. prebirth_registration, for the value that gets stored in the database, and then title case for the values that get shown in the UI, eg. Prebirth Registration

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review, yes that makes sense, I initially wanted to get the models dymanically so tried to keep them the same as the class name

)

class Status(models.IntegerChoices):
PENDING = 0
PROCESSING = 1
COMPLETE = 2
ERROR = 3

id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
object_id = models.UUIDField()
object_type = models.CharField(max_length=30, choices=ObjectType.choices)
status = models.PositiveSmallIntegerField(
choices=Status.choices, default=Status.PENDING
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we also need to store a copy of the details from the object? In case they do a forget delete before this gets forwarded? Or in that case do we not want to forward the object to them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I didn't think about that use case, I would assume we don't send it then, since they requested we delete all their data. We can't leave it in a queue then

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've noted the assumption on the ticket

13 changes: 13 additions & 0 deletions eventstore/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.db.models.signals import post_save
from django.dispatch import receiver

from eventstore.models import OpenHIMQueue, PrebirthRegistration


@receiver(post_save, sender=PrebirthRegistration)
def queue_prebirth_registration(sender, instance, created, **kwargs):
if created:
OpenHIMQueue.objects.create(
object_id=instance.id,
object_type=OpenHIMQueue.ObjectType.PREBIRTH_REGISTRATION,
)
Empty file added eventstore/tests/__init__.py
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions eventstore/test_models.py → eventstore/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
HCSStudyBRandomization,
HealthCheckUserProfile,
Message,
OpenHIMQueue,
PrebirthRegistration,
)


Expand Down Expand Up @@ -569,3 +571,19 @@ def test_get_random_study_b_arm(self, mock_get_study_totals_per_province):

mock_get_study_totals_per_province.return_value = (2000, 10)
self.assertIsNone(rand.get_random_study_b_arm())


class PrebirthRegistrationTests(TestCase):
def test_create_signal(self):
prebirthregistration = PrebirthRegistration.objects.create(
contact_id="9e12d04c-af25-40b6-aa4f-57c72e8e3f91",
device_contact_id="9e12d04c-af25-40b6-aa4f-57c72e8e3f91",
edd="2020-12-01",
)

queue_record = OpenHIMQueue.objects.first()

self.assertEqual(queue_record.object_id, prebirthregistration.id)
self.assertEqual(
queue_record.object_type, OpenHIMQueue.ObjectType.PREBIRTH_REGISTRATION
)
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading