Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added validation for max dosage and corresponding test cases #2383 #2531

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
38ae0c9
feat: Add validation for max dosage and corresponding test cases
himanshu-sharmav Oct 12, 2024
ed1e461
Merge branch 'develop' into validate_prn
himanshu-sharmav Oct 12, 2024
c4138c1
Merge branch 'develop' of https://github.com/himanshu-sharmav/care in…
himanshu-sharmav Oct 12, 2024
57999f2
removed redundant validations.
himanshu-sharmav Oct 13, 2024
59c9439
Merge branch 'validate_prn' of https://github.com/himanshu-sharmav/ca…
himanshu-sharmav Oct 13, 2024
b7010df
Update Pipfile
himanshu-sharmav Oct 13, 2024
2bdff7f
Update admin.py
himanshu-sharmav Oct 13, 2024
d87342b
Update admin.py
himanshu-sharmav Oct 13, 2024
cab7c8c
Update admin.py
himanshu-sharmav Oct 13, 2024
8537afa
Update admin.py
himanshu-sharmav Oct 13, 2024
4210e98
Update admin.py
himanshu-sharmav Oct 13, 2024
55d23bf
Update prescription.py
himanshu-sharmav Oct 13, 2024
fdf75a0
added return statement
himanshu-sharmav Nov 5, 2024
639aab7
changed indendation for linter test
himanshu-sharmav Nov 5, 2024
2f4769f
removed regex and unrequired comments and functions.
himanshu-sharmav Nov 5, 2024
6291b72
indentation change
himanshu-sharmav Nov 5, 2024
0ae7922
Merge branch 'develop' into validate_prn
himanshu-sharmav Nov 5, 2024
fa839cc
Merge branch 'develop' into validate_prn
himanshu-sharmav Nov 6, 2024
25cf4b4
Update care/facility/api/serializers/prescription.py
himanshu-sharmav Nov 6, 2024
b5f08ed
pre-commit changes
himanshu-sharmav Nov 6, 2024
9a452a1
added multiple validate functions to split the branches.
himanshu-sharmav Nov 6, 2024
8cea1fb
applied changes suggested by coderabbitai
himanshu-sharmav Nov 6, 2024
a090f44
again changed suggested by coderabbitai
himanshu-sharmav Nov 6, 2024
6cba5a5
changes for pr test
himanshu-sharmav Nov 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions care/facility/api/serializers/prescription.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
PrescriptionDosageType,
)
from care.users.api.serializers.user import UserBaseMinimumSerializer
import re
sainak marked this conversation as resolved.
Show resolved Hide resolved


class MedibaseMedicineSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -98,6 +99,13 @@ class Meta:
)

def validate(self, attrs):
def extract_numeric_value(dosage):

match = re.match(r"(\d+(\.\d+)?)", dosage) # Matches digits and optional decimal part
if match:
return float(match.group(1))
raise serializers.ValidationError({"dosage": "Invalid dosage format."})

sainak marked this conversation as resolved.
Show resolved Hide resolved
if "medicine" in attrs:
attrs["medicine"] = get_object_or_404(
MedibaseMedicine, external_id=attrs["medicine"]
Expand Down Expand Up @@ -127,6 +135,26 @@ def validate(self, attrs):
{"base_dosage": "Base dosage is required."}
)

# Validate max_dosage is greater than or equal to base_dosage
sainak marked this conversation as resolved.
Show resolved Hide resolved
base_dosage = attrs.get("base_dosage")
max_dosage = attrs.get("max_dosage")

if base_dosage and max_dosage:
try:
# Extract numeric values from dosage strings
sainak marked this conversation as resolved.
Show resolved Hide resolved
base_dosage_value = extract_numeric_value(base_dosage)
max_dosage_value = extract_numeric_value(max_dosage)

# Raise error if max_dosage is less than base_dosage
if max_dosage_value < base_dosage_value:
raise serializers.ValidationError(
{"max_dosage": "Max dosage in 24 hours should be greater than or equal to base dosage."}
)
sainak marked this conversation as resolved.
Show resolved Hide resolved
except ValueError:
raise serializers.ValidationError(
{"dosage": "Dosages must be numeric values with optional units."}
)

if attrs.get("dosage_type") == PrescriptionDosageType.PRN:
if not attrs.get("indicator"):
raise serializers.ValidationError(
Expand Down
29 changes: 29 additions & 0 deletions care/facility/tests/test_prescriptions_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,32 @@ def test_medicine_filter_for_prescription(self):
self.assertEqual(
prescription["medicine_object"]["name"], self.medicine.name
)

def test_max_dosage_greater_than_base_dosage(self):
data = self.prescription_data(base_dosage="500 mg", max_dosage="1000 mg")
response = self.client.post(
f"/api/v1/consultation/{self.consultation.external_id}/prescriptions/",
data,
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

def test_max_dosage_equal_to_base_dosage(self):
data = self.prescription_data(base_dosage="500 mg", max_dosage="500 mg")
response = self.client.post(
f"/api/v1/consultation/{self.consultation.external_id}/prescriptions/",
data,
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

def test_max_dosage_less_than_base_dosage(self):
data = self.prescription_data(base_dosage="500 mg", max_dosage="400 mg")
response = self.client.post(
f"/api/v1/consultation/{self.consultation.external_id}/prescriptions/",
data,
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn("max_dosage", response.data)
self.assertEqual(
response.data["max_dosage"][0],
"Max dosage in 24 hours should be greater than or equal to base dosage."
)