Skip to content

Commit

Permalink
feat: improve validation in client registration
Browse files Browse the repository at this point in the history
  • Loading branch information
ngurenyaga committed Nov 25, 2021
1 parent 7568ec5 commit cdc96bb
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 36 deletions.
22 changes: 22 additions & 0 deletions mycarehub/clients/migrations/0014_auto_20211125_2058.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.2.9 on 2021-11-25 17:58

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('clients', '0013_alter_identifier_identifier_value'),
]

operations = [
migrations.AlterField(
model_name='identifier',
name='identifier_value',
field=models.TextField(),
),
migrations.AlterUniqueTogether(
name='identifier',
unique_together={('identifier_type', 'identifier_value')},
),
]
8 changes: 7 additions & 1 deletion mycarehub/clients/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class IdentifierUse(models.TextChoices):
identifier_type = models.CharField(
choices=IdentifierType.choices, max_length=64, null=False, blank=False
)
identifier_value = models.TextField(unique=True)
identifier_value = models.TextField()
identifier_use = models.CharField(
choices=IdentifierUse.choices, max_length=64, null=False, blank=False
)
Expand All @@ -99,6 +99,12 @@ class IdentifierUse(models.TextChoices):
def __str__(self):
return f"{self.identifier_value} ({self.identifier_type}, {self.identifier_use})"

class Meta(AbstractBase.Meta):
unique_together = (
"identifier_type",
"identifier_value",
)


@register_snippet
class SecurityQuestion(AbstractBase):
Expand Down
2 changes: 2 additions & 0 deletions mycarehub/clients/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ def test_client_registration_view_valid(user_with_all_permissions, client):
content_type="application/json",
accept="application/json",
)

print(response.content)
assert response.status_code == status.HTTP_201_CREATED

response_data = response.json()
Expand Down
72 changes: 37 additions & 35 deletions mycarehub/clients/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,44 +70,44 @@ def post(self, request, format=None):
flavour = "CONSUMER"

new_user, _ = User.objects.get_or_create(
name=data["name"],
gender=data["gender"],
date_of_birth=data["date_of_birth"],
user_type="CLIENT",
phone=data["phone_number"],
flavour=flavour,
organisation=org,
username=data["name"],
defaults={
"username": data["name"],
"name": data["name"],
"gender": data["gender"],
"date_of_birth": data["date_of_birth"],
"user_type": "CLIENT",
"phone": data["phone_number"],
"flavour": flavour,
"organisation": org,
},
)

# create a contact, already opted in
contact, _ = Contact.objects.get_or_create(
contact_type="PHONE",
contact_value=data["phone_number"],
opted_in=True,
flavour=flavour,
user=new_user,
organisation=org,
created_by=request_user.pk,
updated_by=request_user.pk,
defaults={
"contact_type": "PHONE",
"contact_value": data["phone_number"],
"opted_in": True,
"flavour": flavour,
"user": new_user,
"organisation": org,
"created_by": request_user.pk,
"updated_by": request_user.pk,
},
)

# create an identifier (CCC)
identifier, _ = Identifier.objects.get_or_create(
identifier_value=data["ccc_number"],
identifier_type="CCC",
identifier_use="OFFICIAL",
description="CCC Number, Primary Identifier",
is_primary_identifier=True,
organisation=org,
created_by=request_user.pk,
updated_by=request_user.pk,
defaults={
"identifier_value": data["ccc_number"],
"identifier_use": "OFFICIAL",
"description": "CCC Number, Primary Identifier",
"is_primary_identifier": True,
"organisation": org,
"created_by": request_user.pk,
"updated_by": request_user.pk,
},
)

Expand All @@ -117,16 +117,16 @@ def post(self, request, format=None):

# create a client
client, _ = Client.objects.get_or_create(
client_type=data["client_type"],
user=new_user,
enrollment_date=data["enrollment_date"],
current_facility=facility,
counselled=data["counselled"],
organisation=org,
created_by=request_user.pk,
updated_by=request_user.pk,
defaults={
"client_type": data["client_type"],
"user": new_user,
"enrollment_date": data["enrollment_date"],
"current_facility": facility,
"counselled": data["counselled"],
"organisation": org,
"created_by": request_user.pk,
"updated_by": request_user.pk,
},
)

Expand All @@ -137,18 +137,20 @@ def post(self, request, format=None):
client.identifiers.add(identifier)

ClientFacility.objects.get_or_create(
organisation=org,
created_by=request_user.pk,
updated_by=request_user.pk,
client=client,
facility=facility,
defaults={
"client": client,
"facility": facility,
"organisation": org,
"created_by": request_user.pk,
"updated_by": request_user.pk,
},
)

# return the newly created client
serialized_client = ClientSerializer(client)
return Response(serialized_client.data, status=status.HTTP_201_CREATED)
except Exception as e: # noqa # pragma: nocover
return Response({"exception": str(e)}) # pragma: nocover
return Response(
{"exception": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR
) # pragma: nocover
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

0 comments on commit cdc96bb

Please sign in to comment.