|
3 | 3 |
|
4 | 4 | from django.conf import settings
|
5 | 5 | from django.db import transaction
|
| 6 | +from django.utils import timezone |
6 | 7 | from django.utils.timezone import localtime, make_aware, now
|
7 | 8 | from rest_framework import serializers
|
8 | 9 | from rest_framework.exceptions import ValidationError
|
|
41 | 42 | EncounterSymptom,
|
42 | 43 | Symptom,
|
43 | 44 | )
|
| 45 | +from care.facility.models.file_upload import FileUpload |
44 | 46 | from care.facility.models.icd11_diagnosis import (
|
45 | 47 | ConditionVerificationStatus,
|
46 | 48 | ConsultationDiagnosis,
|
|
51 | 53 | RouteToFacility,
|
52 | 54 | SuggestionChoices,
|
53 | 55 | )
|
54 |
| -from care.facility.models.patient_consultation import PatientConsultation |
| 56 | +from care.facility.models.patient_consultation import ( |
| 57 | + ConsentType, |
| 58 | + PatientConsent, |
| 59 | + PatientConsultation, |
| 60 | +) |
55 | 61 | from care.users.api.serializers.user import (
|
56 | 62 | UserAssignedSerializer,
|
57 | 63 | UserBaseMinimumSerializer,
|
@@ -848,3 +854,108 @@ def validate(self, attrs):
|
848 | 854 | class Meta:
|
849 | 855 | model = PatientConsultation
|
850 | 856 | fields = ("email",)
|
| 857 | + |
| 858 | + |
| 859 | +class PatientConsentSerializer(serializers.ModelSerializer): |
| 860 | + id = serializers.CharField(source="external_id", read_only=True) |
| 861 | + created_by = UserBaseMinimumSerializer(read_only=True) |
| 862 | + archived_by = UserBaseMinimumSerializer(read_only=True) |
| 863 | + |
| 864 | + class Meta: |
| 865 | + model = PatientConsent |
| 866 | + |
| 867 | + fields = ( |
| 868 | + "id", |
| 869 | + "type", |
| 870 | + "patient_code_status", |
| 871 | + "archived", |
| 872 | + "archived_by", |
| 873 | + "archived_date", |
| 874 | + "created_by", |
| 875 | + "created_date", |
| 876 | + ) |
| 877 | + |
| 878 | + read_only_fields = ( |
| 879 | + "id", |
| 880 | + "created_by", |
| 881 | + "created_date", |
| 882 | + "archived", |
| 883 | + "archived_by", |
| 884 | + "archived_date", |
| 885 | + ) |
| 886 | + |
| 887 | + def validate(self, attrs): |
| 888 | + user = self.context["request"].user |
| 889 | + if ( |
| 890 | + user.user_type < User.TYPE_VALUE_MAP["DistrictAdmin"] |
| 891 | + and self.context["consultation"].facility_id != user.home_facility_id |
| 892 | + ): |
| 893 | + raise ValidationError( |
| 894 | + "Only Home Facility Staff can create consent for a Consultation" |
| 895 | + ) |
| 896 | + |
| 897 | + if attrs.get("type") == ConsentType.PATIENT_CODE_STATUS and not attrs.get( |
| 898 | + "patient_code_status" |
| 899 | + ): |
| 900 | + raise ValidationError( |
| 901 | + { |
| 902 | + "patient_code_status": [ |
| 903 | + "This field is required for Patient Code Status Consent" |
| 904 | + ] |
| 905 | + } |
| 906 | + ) |
| 907 | + |
| 908 | + if attrs.get("type") != ConsentType.PATIENT_CODE_STATUS and attrs.get( |
| 909 | + "patient_code_status" |
| 910 | + ): |
| 911 | + raise ValidationError( |
| 912 | + { |
| 913 | + "patient_code_status": [ |
| 914 | + "This field is not required for this type of Consent" |
| 915 | + ] |
| 916 | + } |
| 917 | + ) |
| 918 | + return attrs |
| 919 | + |
| 920 | + def clear_existing_records(self, consultation, type, user, self_id=None): |
| 921 | + consents = PatientConsent.objects.filter( |
| 922 | + consultation=consultation, type=type |
| 923 | + ).exclude(id=self_id) |
| 924 | + |
| 925 | + archived_date = timezone.now() |
| 926 | + consents.update( |
| 927 | + archived=True, |
| 928 | + archived_by=user, |
| 929 | + archived_date=archived_date, |
| 930 | + ) |
| 931 | + FileUpload.objects.filter( |
| 932 | + associating_id__in=list(consents.values_list("external_id", flat=True)), |
| 933 | + file_type=FileUpload.FileType.CONSENT_RECORD, |
| 934 | + is_archived=False, |
| 935 | + ).update( |
| 936 | + is_archived=True, |
| 937 | + archived_datetime=archived_date, |
| 938 | + archive_reason="Consent Archived", |
| 939 | + archived_by=user, |
| 940 | + ) |
| 941 | + |
| 942 | + def create(self, validated_data): |
| 943 | + with transaction.atomic(): |
| 944 | + self.clear_existing_records( |
| 945 | + consultation=self.context["consultation"], |
| 946 | + type=validated_data["type"], |
| 947 | + user=self.context["request"].user, |
| 948 | + ) |
| 949 | + validated_data["consultation"] = self.context["consultation"] |
| 950 | + validated_data["created_by"] = self.context["request"].user |
| 951 | + return super().create(validated_data) |
| 952 | + |
| 953 | + def update(self, instance, validated_data): |
| 954 | + with transaction.atomic(): |
| 955 | + self.clear_existing_records( |
| 956 | + consultation=instance.consultation, |
| 957 | + type=instance.type, |
| 958 | + user=self.context["request"].user, |
| 959 | + self_id=instance.id, |
| 960 | + ) |
| 961 | + return super().update(instance, validated_data) |
0 commit comments