Skip to content

Commit

Permalink
fix: improve error handling in client creation view
Browse files Browse the repository at this point in the history
  • Loading branch information
ngurenyaga committed Nov 24, 2021
1 parent 3593ff2 commit fc062c0
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 86 deletions.
14 changes: 13 additions & 1 deletion config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@
"wagtail.contrib.settings",
"wagtail.contrib.frontend_cache",
"wagtail.contrib.search_promotions",
"wagtailreadinglevel",
"wagtailfontawesome",
"wagtailquickcreate",
"django_extensions",
]

Expand Down Expand Up @@ -182,7 +185,9 @@
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [str(APPS_DIR / "templates")],
"DIRS": [
str(APPS_DIR / "templates"),
],
"OPTIONS": {
"loaders": [
"django.template.loaders.filesystem.Loader",
Expand Down Expand Up @@ -394,6 +399,13 @@
WAGTAILDOCS_EXTENSIONS = [
"pdf",
]
MULTI_IMAGE_EDIT_FIELDS = [
"title",
"tags",
]
WAGTAIL_QUICK_CREATE_PAGE_TYPES = ["content.ContentItem"]
WAGTAIL_QUICK_CREATE_DOCUMENTS = True
WAGTAIL_QUICK_CREATE_IMAGES = True

# phone numbers
PHONENUMBER_DB_FORMAT = "E164"
Expand Down
173 changes: 88 additions & 85 deletions mycarehub/clients/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,89 +63,92 @@ class ClientRegistrationView(APIView):
def post(self, request, format=None):
serializer = self.serializer_class(data=request.data)
if serializer.is_valid():
data = serializer.validated_data
request_user = request.user
org = request_user.organisation
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,
defaults={
"username": data["name"],
},
)

# 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_value": data["phone_number"],
},
)

# create an identifier (CCC)
identifier, _ = Identifier.objects.get_or_create(
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"],
},
)

# retrieve the facility by the unique name
facility_name = data["facility"]
facility = Facility.objects.get(name=facility_name)

# 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={
"user": new_user,
},
)

# add the contact to the client
client.contacts.add(contact)

# add the identifier to the client
client.identifiers.add(identifier)

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

# return the newly created client
serialized_client = ClientSerializer(client)
return Response(serialized_client.data, status=status.HTTP_201_CREATED)
try:
data = serializer.validated_data
request_user = request.user
org = request_user.organisation
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,
defaults={
"username": data["name"],
},
)

# 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_value": data["phone_number"],
},
)

# create an identifier (CCC)
identifier, _ = Identifier.objects.get_or_create(
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"],
},
)

# retrieve the facility by the unique name
facility_name = data["facility"]
facility = Facility.objects.get(name=facility_name)

# 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={
"user": new_user,
},
)

# add the contact to the client
client.contacts.add(contact)

# add the identifier to the client
client.identifiers.add(identifier)

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

# 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(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
1 change: 1 addition & 0 deletions mycarehub/templates/fragments/head.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% load static i18n compress%}
{% load wagtailfontawesome %}
<!DOCTYPE html>
<html lang="en">
<head>
Expand Down
3 changes: 3 additions & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ opencv_python~=4.5.4.58
rustface~=0.1.0
wagtailvideos~=2.10.9
wagtailmedia~=0.8.0
wagtail-readinglevel~=3.5.0
wagtailfontawesome~=1.2.1
wagtail-quick-create~=1.0.7

0 comments on commit fc062c0

Please sign in to comment.