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

resolved issue #2383 #2452

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
102 changes: 57 additions & 45 deletions care/facility/api/serializers/prescription.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,59 +94,71 @@ class Meta:
"is_migrated",
)

def validate(self, attrs):
if "medicine" in attrs:
attrs["medicine"] = get_object_or_404(
MedibaseMedicine, external_id=attrs["medicine"]
def validate(self, attrs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add valid test cases.


if "medicine" in attrs:
attrs["medicine"] = get_object_or_404(
MedibaseMedicine, external_id=attrs["medicine"]
)


if not self.instance:
if Prescription.objects.filter(
consultation__external_id=self.context["request"].parser_context[
"kwargs"
]["consultation_external_id"],
medicine=attrs["medicine"],
discontinued=False,
).exists():
raise serializers.ValidationError(
{
"medicine": (
"This medicine is already prescribed to this patient. "
"Please discontinue the existing prescription to prescribe again."
)
}
)

if not self.instance:
if Prescription.objects.filter(
consultation__external_id=self.context["request"].parser_context[
"kwargs"
]["consultation_external_id"],
medicine=attrs["medicine"],
discontinued=False,
).exists():
raise serializers.ValidationError(
{
"medicine": (
"This medicine is already prescribed to this patient. "
"Please discontinue the existing prescription to prescribe again."
)
}
)

if not attrs.get("base_dosage"):
raise serializers.ValidationError({"base_dosage": "Base dosage is required."})

if not attrs.get("base_dosage"):

if attrs.get("dosage_type") == PrescriptionDosageType.PRN:
if not attrs.get("indicator"):
raise serializers.ValidationError(
{"indicator": "Indicator should be set for PRN prescriptions."}
)
attrs.pop("frequency", None)
attrs.pop("days", None)
else:
if not attrs.get("frequency"):
raise serializers.ValidationError(
{"base_dosage": "Base dosage is required."}
{"frequency": "Frequency should be set for prescriptions."}
)
attrs.pop("indicator", None)
attrs.pop("max_dosage", None)
attrs.pop("min_hours_between_doses", None)

if attrs.get("dosage_type") == PrescriptionDosageType.PRN:
if not attrs.get("indicator"):

if attrs.get("dosage_type") == PrescriptionDosageType.TITRATED:
if not attrs.get("target_dosage"):
raise serializers.ValidationError(
{"indicator": "Indicator should be set for PRN prescriptions."}
{"target_dosage": "Target dosage should be set for titrated prescriptions."}
)
attrs.pop("frequency", None)
attrs.pop("days", None)
else:
if not attrs.get("frequency"):
raise serializers.ValidationError(
{"frequency": "Frequency should be set for prescriptions."}
)
attrs.pop("indicator", None)
attrs.pop("max_dosage", None)
attrs.pop("min_hours_between_doses", None)

if attrs.get("dosage_type") == PrescriptionDosageType.TITRATED:
if not attrs.get("target_dosage"):
raise serializers.ValidationError(
{
"target_dosage": "Target dosage should be set for titrated prescriptions."
}
)
else:
attrs.pop("target_dosage", None)
attrs.pop("target_dosage", None)


base_dosage = attrs.get("base_dosage")
max_dosage = attrs.get("max_dosage")

if max_dosage is not None and base_dosage is not None:
if max_dosage < base_dosage:
raise serializers.ValidationError(
{"max_dosage": "Max dosage in 24 hours must be greater than or equal to the base dosage."}
)

return super().validate(attrs)

return super().validate(attrs)
# TODO: Ensure that this medicine is not already prescribed to the same patient and is currently active.
Loading