def check_status_appointment_after_open(appointment_id: int): try: appointment = Appointment.objects.get(pk=appointment_id) except Appointment.DoesNotExist as e: print(e) logger.error(e) return if appointment.status != AppointmentState.Opened.value: return if appointment.location is not None: longitude = appointment.location.get_x() latitude = appointment.location.get_y() else: longitude, latitude = None, None searcher = AppointmentClinicSearcher( appointment.treatments, longitude=longitude, latitude=latitude, appointment=appointment, raise_exception=False, use_additional_filters=True ) AppointmentActions( appointment, SupportAdminTime().is_support_admin_time ).timeout(searcher.get_suggestions_time())
def _prepare_appointments(self): self.appointment = AppointmentRecipe.make( doctor=self.doctor, date_time=tz_now() + timedelta(days=1) ) self.appointment.treatments.add(self.treatment) self.app_event = AppointmentEventRecipe.make( appointment=self.appointment, clinic=self.clinic) AppointmentActions(self.appointment).accept(self.app_event)
def accept_suggestion(self, request, pk=None, suggestion_id=None): """ Endpoint for accept one of admin's suggestions. If user accept one of suggestion, an appointment will be reserved by this clinic """ event = self.get_object() suggestion = get_object_or_404(AppointmentSuggestion.objects.all(), pk=suggestion_id) aa = AppointmentActions(event.appointment, SupportAdminTime().is_support_admin_time) aa.accept_suggestion(event, suggestion) sz = AppointmentClinicEventSerializer(self.get_object()) check_status_appointment_after_reserved.apply_async( args=(event.appointment.id, ), countdown=settings.CHECK_APPOINTMENT_EXPIRED_AFTER_RESERVED) return Response(sz.data)
def reject(self, request, **kwargs): """ Endpoint for reject appointment by clinic admin. """ event = self.get_object() AppointmentActions( event.appointment, SupportAdminTime().is_support_admin_time).reject(event) return Response(AppointmentClinicEventSerializer(event).data)
def test_reopen_appointment_after_timeout_status_for_event_active( self, *args): c, r, appointment = self._create_appointment() AppointmentActions(appointment).timeout() c, r, appointment = self._reopen(appointment.pk) statuses = appointment.events.values_list('status', flat=True) self.assertTrue( all( map(lambda x: x == AppointmentClinicState.Active.value, statuses)))
def perform_create(self, serializer): appointment = serializer.save() clinics = serializer.clinic_searcher.get_clinics() support_admin_time = SupportAdminTime() AppointmentActions( appointment, support_admin_time.is_support_admin_time).open(clinics) check_status_appointment_after_open.apply_async( args=(appointment.id, ), # after timeout starts search for suggestions date and time # and it will last more-less 1 second countdown=support_admin_time.countdown_open() - 1)
def reject_suggestions(self, request, **kwargs): """ Endpoint for reject admin's suggestions by user. If user no want to take any suggestions from clinic admin, he can just reject their. """ event = self.get_object() AppointmentActions( event.appointment, SupportAdminTime().is_support_admin_time).reject_suggestions(event) return Response(AppointmentClinicEventSerializer(event).data)
def cancel(self, request, **kwargs): """ Endpoint for cancel appointment. Check if possible will raised refund half of money, otherwise appointment will cancel """ appointment = self.get_object() AppointmentActions(appointment, SupportAdminTime().is_support_admin_time).cancel( raise_if_no_refund=False) return Response(AppointmentSerializer(appointment).data)
def test_admin_accept_appointment_after_reopening(self, *args): c, r, appointment = self._create_appointment() AppointmentActions(appointment).timeout() c, r, appointment = self._reopen(appointment.pk) code, response_data, event = self._accept(appointment, self.clinic_admin1) self.assertEquals(code, 200, response_data) self.assertTrue(response_data.get('appointment', {}).get('doctor')) self.assertEquals(event.appointment.status, AppointmentState.Reserved.value) self.assertEquals(event.status, AppointmentClinicState.Accepted.value)
def check_status_appointment_after_reserved(appointment_id: int): try: appointment = Appointment.objects.get(pk=appointment_id) except Appointment.DoesNotExist as e: print(e) logger.error(e) return if appointment.status == AppointmentState.Reserved.value: AppointmentActions( appointment, SupportAdminTime().is_support_admin_time ).cancel(notif_user=True)
def test_reopen_appointment_clinic_which_reject_status_still_reject( self, *args): c, r, appointment = self._create_appointment() self._reject(appointment, self.clinic_admin1) AppointmentActions(appointment).timeout() c, r, appointment = self._reopen(appointment.pk) self.assertEquals( appointment.events.get(clinic__admin=self.clinic_admin1).status, AppointmentClinicState.Rejected.value) self.assertEquals( appointment.events.get(clinic__admin=self.clinic_admin2).status, AppointmentClinicState.Active.value)
def test_appointment_confirmed_push_notification_should_sent_to_user( self, *args): client = HttpClient() client.send_and_consume( "websocket.connect", path="/?auth_token=%s" % self._get_token_for(self.simple_user) ) with mock.patch('clinicapp.pkg.notifications.messenger.Messenger.' 'send_push_notifications') as push_call: c, r, appointment = self._create_appointment() c, r, event = self._accept(appointment, self.clinic_admin1) AppointmentActions(event.appointment).confirm(event) self.assertEquals(push_call.call_count, 1) self.assertEquals(push_call.call_args[0][0], self.simple_user)
def test_after_reopen_clinic_admins_receive_notification(self, *args): c, r, appointment = self._create_appointment() AppointmentActions(appointment).timeout() self._reopen(appointment.pk) client = HttpClient() client.send_and_consume( "websocket.connect", path="/?auth_token=%s" % self._get_token_for(self.clinic_admin1) ) msg = client.receive() self.assertEquals(msg.get('action'), 'appointment_created') self.assertTrue(msg.get('message', {}).get('appointment_event'))
def test_reopen_after_creating_clinic_new_event_will_create(self, *args): c, r, appointment = self._create_appointment() AppointmentActions(appointment).timeout() admin = UserRecipe.make(email='*****@*****.**', is_active=True) admin.groups.add(GroupService.get_clinics_admin()) clinic = ClinicRecipe.make(admin=admin, location=self.clinic_point1, status=ClinicState.Approved.value) clinic.treatments.add(self.treatment) c, r, appointment = self._reopen(appointment.pk) events = appointment.events.all() statuses = events.values_list('status', flat=True) self.assertEquals(events.count(), 3) self.assertTrue( all( map(lambda x: x == AppointmentClinicState.Active.value, statuses)))
def accept(self, request, **kwargs): """ Endpoint for accept appointment. If appointment open and event for current clinic is active, appointment will be reserved by this clinic. """ event = self.get_object() sz = self.get_serializer(event, data=request.data) sz.is_valid(raise_exception=True) sz.save() AppointmentActions( event.appointment, SupportAdminTime().is_support_admin_time).accept(event) check_status_appointment_after_reserved.apply_async( args=(event.appointment.id, ), countdown=settings.CHECK_APPOINTMENT_EXPIRED_AFTER_RESERVED) return Response(sz.data)
def test_clinic_admin_reject_and_not_receive_notification_after_reopen( self, *args): client = HttpClient() client.send_and_consume( "websocket.connect", path="/?auth_token=%s" % self._get_token_for(self.clinic_admin1) ) c, r, appointment = self._create_appointment() msg = client.receive() # first message about creating self._reject(appointment, self.clinic_admin1) AppointmentActions(appointment).timeout() self._reopen(appointment.pk) msg = client.receive() self.assertIsNone(msg)
def suggest(self, request, **kwargs): """ Endpoint for add suggestions for appointment. If clinic admin want to change date and/or time for appointment he can add suggestions (minimum 3)\n For example, POST /api/v1/appointments/events/{id}/suggest { "suggestions": [ { "date": "2017-6-7", "time": "10:00", "doctor": 12, "adjust_30_min": true }, { "date": "2017-6-8", "time": "10:00", "doctor": 12 }, { "date": "2017-6-7", "time": "14:00", "doctor": 12 } ] } """ event = self.get_object() sz = self.get_serializer(event, data=request.data) sz.is_valid(raise_exception=True) with atomic(): sz.save() AppointmentActions( event.appointment, SupportAdminTime().is_support_admin_time).suggest(event) check_status_appointment_after_suggest.apply_async( args=(event.appointment.id, ), countdown=settings.CHECK_APPOINTMENT_EXPIRED_AFTER_SUGGEST) return Response(sz.data)
def test_reopen_appointment_new_clinic_receive_notification(self, *args): client1 = HttpClient() client1.send_and_consume( "websocket.connect", path="/?auth_token=%s" % self._get_token_for(self.clinic_admin1) ) client2 = HttpClient() client2.send_and_consume( "websocket.connect", path="/?auth_token=%s" % self._get_token_for(self.clinic_admin2) ) c, r, appointment = self._create_appointment() client1.receive() # first message about creating client2.receive() # first message about creating self._reject(appointment, self.clinic_admin1) self._reject(appointment, self.clinic_admin2) AppointmentActions(appointment).timeout() admin = UserRecipe.make(email='*****@*****.**', is_active=True) admin.groups.add(GroupService.get_clinics_admin()) clinic = ClinicRecipe.make(admin=admin, location=self.clinic_point1, status=ClinicState.Approved.value) clinic.treatments.add(self.treatment) client3 = HttpClient() client3.send_and_consume( "websocket.connect", path="/?auth_token=%s" % self._get_token_for(admin) ) self._reopen(appointment.pk) msg = client3.receive() self.assertIsNone(client1.receive()) self.assertIsNone(client2.receive()) self.assertEquals(msg.get('action'), 'appointment_created') self.assertTrue(msg.get('message', {}).get('appointment_event'))
def reopen(self, request, pk=None): """ Endpoint for reopen appointment. If none clinics accept request for appointment and timeout expired or user rejected appointment, user can reopen appointment. """ appointment = self.get_object() sz = self.get_serializer(appointment, data=request.data, partial=True) sz.is_valid(raise_exception=True) support_admin_time = SupportAdminTime() with atomic(): sz.save() clinics = sz.clinic_searcher.get_clinics() AppointmentActions( appointment, support_admin_time.is_support_admin_time).reopen(clinics) check_status_appointment_after_open.apply_async( args=(appointment.id, ), countdown=support_admin_time.countdown_open()) return Response(sz.data)
def test_appointment_send_push_notification_about_rating( self, *args): client = HttpClient() client.send_and_consume( "websocket.connect", path="/?auth_token=%s" % self._get_token_for(self.simple_user) ) with mock.patch('clinicapp.pkg.notifications.messenger.Messenger.' 'send_push_notifications') as push_call: c, r, appointment = self._create_appointment() c, r, event = self._accept(appointment, self.clinic_admin1) AppointmentActions(event.appointment).confirm(event) appointment.refresh_from_db() appointment.date_time = tz_now() - timedelta(minutes=30) appointment.save() _check_time_for_passed_appointments_for_sending_notification_about_rating() self.assertEquals(push_call.call_count, 2) self.assertEquals(push_call.call_args_list[1][0][0], self.simple_user) self.assertIsInstance(push_call.call_args_list[1][0][1], RatingAppointmentPushMessage)