Skip to content

Commit

Permalink
feat: add staff role view
Browse files Browse the repository at this point in the history
Signed-off-by: maxwellgithinji <maxwellgithinji@gmail.com>
  • Loading branch information
maxwellgithinji committed Feb 18, 2022
1 parent ebc20c1 commit 3e0b977
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions mycarehub/authority/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from mycarehub.authority.models import AuthorityRole


def get_role_choices():
choices = []
for role in AuthorityRole.objects.all():
choices.append((role.name, role.name))

return choices
8 changes: 8 additions & 0 deletions mycarehub/staff/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django import forms
from phonenumber_field.formfields import PhoneNumberField

from mycarehub.authority.forms import get_role_choices
from mycarehub.clients.forms import get_facility_choices, validate_date_past
from mycarehub.users.models import GenderChoices

Expand Down Expand Up @@ -58,3 +59,10 @@ class StaffRegistrationForm(forms.Form):
label="Staff Number",
help_text="The Staff's currently assigned staff number",
)

role = forms.ChoiceField(
required=True,
choices=get_role_choices,
label="Staff's Roles",
help_text="The Staff's currently assigned roles",
)
3 changes: 3 additions & 0 deletions mycarehub/staff/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from model_bakery import baker
from rest_framework import status

from mycarehub.authority.models import AuthorityRole
from mycarehub.common.models.common_models import Facility

fake = Faker()
Expand All @@ -15,6 +16,7 @@ def test_staff_registration_view_valid(user_with_all_permissions, client):
url = reverse("staff_registration")
org = user_with_all_permissions.organisation
facility = baker.make(Facility, organisation=org)
role = baker.make(AuthorityRole, organisation=org)
response = client.post(
url,
data={
Expand All @@ -25,6 +27,7 @@ def test_staff_registration_view_valid(user_with_all_permissions, client):
"phone_number": "+254722000000",
"id_number": fake.random_int(),
"staff_number": fake.random_int(),
"role": role.name,
},
content_type="application/json",
accept="application/json",
Expand Down
4 changes: 4 additions & 0 deletions mycarehub/staff/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from rest_framework.response import Response
from rest_framework.views import APIView

from mycarehub.authority.models import AuthorityRole
from mycarehub.clients.models import Identifier
from mycarehub.common.models.common_models import Contact, Facility
from mycarehub.users.models import User
Expand Down Expand Up @@ -94,6 +95,9 @@ def post(self, request, format=None):
# add the identifier to the staff
staff.identifiers.add(identifier)

# add user to roles
for role in AuthorityRole.objects.filter(name=data["role"]):
role.users.add(new_user)
# return the newly created staff
serialized_staff = StaffSerializer(staff)
return Response(serialized_staff.data, status=status.HTTP_201_CREATED)
Expand Down

0 comments on commit 3e0b977

Please sign in to comment.