Skip to content

Commit

Permalink
chore: staff registration and removal, client removal
Browse files Browse the repository at this point in the history
  • Loading branch information
Muchogoc committed Sep 12, 2022
1 parent 17b30f3 commit 997115e
Show file tree
Hide file tree
Showing 15 changed files with 312 additions and 187 deletions.
10 changes: 10 additions & 0 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,21 @@
csrf_exempt(DRFAuthenticatedGraphQLView.as_view(graphiql=True)),
name="graphql",
),
path(
"client_remove/<pk>",
ClientRegistrationView.as_view(),
name="client_removal",
),
path(
"client_registration",
ClientRegistrationView.as_view(),
name="client_registration",
),
path(
"staff_remove/<pk>",
StaffRegistrationView.as_view(),
name="staff_removal",
),
path(
"staff_registration",
StaffRegistrationView.as_view(),
Expand Down
21 changes: 21 additions & 0 deletions mycarehub/clients/migrations/0035_auto_20220909_1417.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 3.2.14 on 2022-09-09 11:17

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('clients', '0034_auto_20220906_1131'),
]

operations = [
migrations.RemoveField(
model_name='client',
name='addresses',
),
migrations.RemoveField(
model_name='client',
name='contacts',
),
]
13 changes: 1 addition & 12 deletions mycarehub/clients/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from wagtail.snippets.models import register_snippet

from mycarehub.common.models import AbstractBase
from mycarehub.common.models.common_models import Address, Contact, Facility
from mycarehub.common.models.common_models import Facility


class FlavourChoices(TextChoices):
Expand Down Expand Up @@ -151,17 +151,6 @@ class Client(AbstractBase):
# counselled
counselled = models.BooleanField(default=False)

addresses = models.ManyToManyField(
Address,
related_name="client_addresses",
blank=True,
)
contacts = models.ManyToManyField(
Contact,
related_name="client_contacts",
blank=True,
)

languages = ArrayField(
models.CharField(
max_length=150,
Expand Down
53 changes: 53 additions & 0 deletions mycarehub/clients/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,56 @@ def test_client_registration_view_invalid(user_with_all_permissions, client):
accept="application/json",
)
assert response.status_code == 400


def test_client_deletion_view_valid(user_with_all_permissions, client):
client.force_login(user_with_all_permissions)
org = user_with_all_permissions.organisation

facility = baker.make(Facility, organisation=org)
user = get_user_model().objects.create_superuser(
email=fake.email(),
password="pass123",
username=str(uuid.uuid4()),
)

user_client = baker.make(
Client,
client_types=["PMTCT"],
user=user,
current_facility=facility,
fhir_patient_id=str(uuid.uuid4()),
organisation=org,
)

baker.make(ClientFacility, client=user_client, facility=facility)

url = reverse("client_removal", kwargs={"pk": user.id})

response = client.delete(
url,
content_type="application/json",
accept="application/json",
)

assert response.status_code == 200


def test_client_deletion_view_invalid(user_with_all_permissions, client):
client.force_login(user_with_all_permissions)

user = get_user_model().objects.create_superuser(
email=fake.email(),
password="pass123",
username=str(uuid.uuid4()),
)

url = reverse("client_removal", kwargs={"pk": user.id})

response = client.delete(
url,
content_type="application/json",
accept="application/json",
)

assert response.status_code == 400
19 changes: 19 additions & 0 deletions mycarehub/clients/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,22 @@ def post(self, request, format=None):
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
) # pragma: nocover
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

def delete(self, request, pk, format=None):
try:
user = User.objects.get(pk=pk)
client = Client.objects.get(user=user)
allocation = ClientFacility.objects.get(client=client)

with transaction.atomic():
allocation.delete()
client.delete()
user.delete()

except Exception as e:
return Response(
{"exception": str(e)},
status=status.HTTP_400_BAD_REQUEST,
)

return Response(status=status.HTTP_200_OK)
12 changes: 1 addition & 11 deletions mycarehub/common/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.contrib import admin

from mycarehub.common.models.common_models import Address, AuditLog, Contact
from mycarehub.common.models.common_models import AuditLog

from .models import Facility, Organisation

Expand Down Expand Up @@ -45,16 +45,6 @@ class OrganisationAdmin(BaseAdmin):
pass


@admin.register(Address)
class AddressAdmin(BaseAdmin):
pass


@admin.register(Contact)
class ContactAdmin(BaseAdmin):
pass


@admin.register(AuditLog)
class AuditLogAdmin(BaseAdmin):
pass
33 changes: 33 additions & 0 deletions mycarehub/common/migrations/0025_auto_20220909_1417.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 3.2.14 on 2022-09-09 11:17

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('staff', '0007_auto_20220909_1417'),
('clients', '0035_auto_20220909_1417'),
('common', '0024_auto_20220906_1131'),
]

operations = [
migrations.AlterUniqueTogether(
name='contact',
unique_together=None,
),
migrations.RemoveField(
model_name='contact',
name='organisation',
),
migrations.RemoveField(
model_name='contact',
name='user',
),
migrations.DeleteModel(
name='Address',
),
migrations.DeleteModel(
name='Contact',
),
]
63 changes: 1 addition & 62 deletions mycarehub/common/models/common_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
from django.db.models.fields.json import JSONField
from django.urls import reverse
from django.utils import timezone
from django.utils.translation import gettext_lazy as _

from ..constants import COUNTRY_CODES, WHITELIST_COUNTIES
from ..constants import WHITELIST_COUNTIES
from ..utils import get_constituencies, get_counties, get_sub_counties, get_wards
from .base_models import AbstractBase, AbstractBaseManager, AbstractBaseQuerySet

Expand Down Expand Up @@ -287,66 +286,6 @@ class Meta(AbstractBase.Meta):
ordering = ("-updated", "-created")


class Address(AbstractBase):
class AddressType(models.TextChoices):
POSTAL = "POSTAL", _("Postal Address")
PHYSICAL = "PHYSICAL", _("Physical Address")
BOTH = "BOTH", _("Both physical and postal")

address_type = models.CharField(choices=AddressType.choices, max_length=16)
text = models.TextField()
postal_code = models.TextField()
country = models.CharField(max_length=255, choices=COUNTRY_CODES, default="KEN")

def __str__(self):
return f"{self.text} ({self.address_type})"


class Contact(AbstractBase):
class ContactType(models.TextChoices):
PHONE = "PHONE", _("PHONE")
EMAIL = "EMAIL", _("EMAIL")

class FlavourChoices(models.TextChoices):
PRO = "PRO", _("PRO")
CONSUMER = "CONSUMER", _("CONSUMER")

contact_type = models.CharField(choices=ContactType.choices, max_length=16)
contact_value = models.TextField()
opted_in = models.BooleanField(default=False)
flavour = models.CharField(
choices=FlavourChoices.choices, max_length=32, null=True, blank=True
)
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)

model_validators = ["validate_if_contact_exists"]

def __str__(self):
return f"{self.contact_value} ({self.contact_type})"

class Meta:
unique_together = ["contact_value", "flavour"]

def validate_if_contact_exists(self):
if Contact.objects.filter(
contact_value=self.contact_value,
contact_type=self.contact_type,
flavour=self.flavour,
).exists():
raise ValidationError(
_(
"Contact value %(contact_value)s of "
"type %(contact_type)s and flavour "
"%(flavour)s already exists"
),
params={
"contact_value": self.contact_value,
"contact_type": self.contact_type,
"flavour": self.flavour,
},
)


class AuditLog(AbstractBase):
"""
AuditLog is used to record all senstive changes
Expand Down
32 changes: 0 additions & 32 deletions mycarehub/common/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
is_image_type,
unique_list,
)
from mycarehub.common.models.common_models import Address, Contact

fake = Faker()

Expand Down Expand Up @@ -554,34 +553,3 @@ def test_user_facility_allotment_by_both_facility_and_region(self):
allotment.save()

assert UserFacilityAllotment.get_facilities_for_allotment(allotment).count() == 5


def test_address_str():
addr = baker.make(Address, text="Wapi", address_type="BOTH")
assert str(addr) == "Wapi (BOTH)"


def test_contact_str():
contact = baker.make(Contact, contact_value="0722000000", contact_type="PHONE")
assert str(contact) == "0722000000 (PHONE)"


def test_validate_if_contact_exists():
contact_value = "+1111111111"
contact_type = "PHONE"
flavour = "CONSUMER"
baker.make(
Contact,
contact_value=contact_value,
contact_type=contact_type,
flavour=flavour,
)

duplicate_contact = baker.prepare(
Contact,
contact_value=contact_value,
contact_type=contact_type,
flavour=flavour,
)
with pytest.raises(Exception):
duplicate_contact.save()
37 changes: 29 additions & 8 deletions mycarehub/staff/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ class StaffRegistrationForm(forms.Form):
to register new Staffs.
"""

facility = forms.ChoiceField(
required=True,
choices=get_facility_choices,
label="Staff's Facility/Clinic",
help_text="The Staff's currently assigned facility/clinic",
user_id = forms.UUIDField(
required=True, label="User ID", help_text="The staff's unique user identifier"
)

name = forms.CharField(
Expand All @@ -25,6 +22,13 @@ class StaffRegistrationForm(forms.Form):
help_text="The Staff's full name i.e family, given and other names, on one row",
)

handle = forms.CharField(
required=True,
max_length=255,
label="Username",
help_text="The staff's unique username",
)

gender = forms.ChoiceField(
required=True,
choices=GenderChoices.choices,
Expand All @@ -47,10 +51,27 @@ class StaffRegistrationForm(forms.Form):
help_text="The Staff's phone number",
)

id_number = forms.IntegerField(
organisation_id = forms.UUIDField(
required=True,
label="ID Number",
help_text="ID, to be used as the primary identifier",
label="Organisation ID",
help_text="The staff's currently assigned facility/clinic",
)

facility_id = forms.UUIDField(
required=True,
label="Staff's Facility/Clinic ID",
help_text="The staff's currently assigned facility/clinic",
)

facility_name = forms.ChoiceField(
required=True,
choices=get_facility_choices,
label="Staff's Facility/Clinic",
help_text="The staff's currently assigned facility/clinic",
)

staff_id = forms.UUIDField(
required=True, label="Staff ID", help_text="The staff's unique user identifier"
)

staff_number = forms.CharField(
Expand Down
Loading

0 comments on commit 997115e

Please sign in to comment.