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

Add Reschedule API and serialize Facility in TokenBooking #2738

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
43 changes: 41 additions & 2 deletions care/emr/api/viewsets/scheduling/booking.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.db import transaction
from django_filters import CharFilter, DateFromToRangeFilter, FilterSet, UUIDFilter
from django_filters.rest_framework import DjangoFilterBackend
from pydantic import BaseModel
from pydantic import UUID4, BaseModel
from rest_framework.decorators import action
from rest_framework.exceptions import PermissionDenied
from rest_framework.generics import get_object_or_404
Expand All @@ -15,6 +15,8 @@
EMRRetrieveMixin,
EMRUpdateMixin,
)
from care.emr.api.viewsets.scheduling import lock_create_appointment
from care.emr.models import TokenSlot
from care.emr.models.scheduling import SchedulableUserResource, TokenBooking
from care.emr.resources.scheduling.slot.spec import (
CANCELLED_STATUS_CHOICES,
Expand All @@ -29,10 +31,16 @@

class CancelBookingSpec(BaseModel):
reason: Literal[
BookingStatusChoices.cancelled, BookingStatusChoices.entered_in_error
BookingStatusChoices.cancelled,
BookingStatusChoices.entered_in_error,
BookingStatusChoices.rescheduled,
]


class RescheduleBookingSpec(BaseModel):
new_slot: UUID4


class TokenBookingFilters(FilterSet):
status = CharFilter(field_name="status")
date = DateFromToRangeFilter(field_name="token_slot__start_datetime__date")
Expand Down Expand Up @@ -117,6 +125,37 @@ def cancel(self, request, *args, **kwargs):
self.authorize_update({}, instance)
return self.cancel_appointment_handler(instance, request.data, request.user)

@action(detail=True, methods=["POST"])
def reschedule(self, request, *args, **kwargs):
request_data = RescheduleBookingSpec(**request.data)
existing_booking = self.get_object()
facility = self.get_facility_obj()
self.authorize_update({}, existing_booking)
if not AuthorizationController.call(
"can_create_appointment", self.request.user, facility
):
raise PermissionDenied("You do not have permission to create appointments")
new_slot = get_object_or_404(
TokenSlot,
external_id=request_data.new_slot,
resource=existing_booking.token_slot.resource,
)
with transaction.atomic():
self.cancel_appointment_handler(
existing_booking,
{"reason": BookingStatusChoices.rescheduled},
request.user,
)
appointment = lock_create_appointment(
new_slot,
existing_booking.patient,
request.user,
existing_booking.reason_for_visit,
)
return Response(
TokenBookingReadSpec.serialize(appointment).model_dump(exclude=["meta"])
)

@action(detail=False, methods=["GET"])
def available_users(self, request, *args, **kwargs):
facility = Facility.objects.get(external_id=self.kwargs["facility_external_id"])
Expand Down
8 changes: 8 additions & 0 deletions care/emr/resources/scheduling/slot/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
from care.emr.models.scheduling.booking import TokenSlot
from care.emr.models.scheduling.schedule import Availability
from care.emr.resources.base import EMRResource
from care.emr.resources.facility.spec import FacilityBareMinimumSpec
from care.emr.resources.patient.otp_based_flow import PatientOTPReadSpec
from care.emr.resources.user.spec import UserSpec
from care.facility.models import Facility
from care.users.models import User


Expand Down Expand Up @@ -52,11 +54,13 @@ class BookingStatusChoices(str, Enum):
checked_in = "checked_in"
waitlist = "waitlist"
in_consultation = "in_consultation"
rescheduled = "rescheduled"


CANCELLED_STATUS_CHOICES = [
BookingStatusChoices.entered_in_error.value,
BookingStatusChoices.cancelled.value,
BookingStatusChoices.rescheduled.value,
]


Expand All @@ -83,6 +87,7 @@ class TokenBookingReadSpec(TokenBookingBaseSpec):
status: str
reason_for_visit: str
user: dict = {}
facility: dict = {}

@classmethod
def perform_extra_serialization(cls, mapping, obj):
Expand All @@ -96,3 +101,6 @@ def perform_extra_serialization(cls, mapping, obj):
mapping["user"] = UserSpec.serialize(
User.objects.get(id=obj.token_slot.resource.user_id)
).model_dump(exclude=["meta"])
mapping["facility"] = FacilityBareMinimumSpec.serialize(
Facility.objects.get(id=obj.token_slot.resource.facility_id)
).model_dump(exclude=["meta"])
rithviknishad marked this conversation as resolved.
Show resolved Hide resolved
Loading