Example #1
0
    def post(self, args):
        ####################
        # Check Restrictions
        ####################
        patient = getitem_or_404(Patient, Patient.id, args['patient_id'],
                                 error_text="Patient not found")
        provider = getitem_or_404(Provider, Provider.id, args['provider_id'],
                                  error_text="Provider not found")

        # is timeslot in the future (given a delay)
        appt_start_time = args['start'].replace(tzinfo=None)
        booking_start = datetime.now() + timedelta(hours=BOOKING_DELAY_IN_HOURS)
        _appointment_starts_before_booking_delay(appt_start_time, booking_start)

        # is appointment duration longer than allowed
        duration = args['duration']
        _appointment_longer_than_max_length(duration)

        # check if double booked
        appt_end_time = appt_start_time + timedelta(minutes=duration)
        # TODO should check if patient double book? need clarification
        _start_time_overlaps(Appointment.provider_id, provider.id, appt_start_time)
        _end_time_overlaps(Appointment.provider_id, provider.id, appt_end_time)

        # TODO is it during "Office Hours?" for this doctor (another lookup)
        is_office_hours = True
        if not is_office_hours:
            response = create_response(status_code=409, error='Office closed')
            return response

        ###################
        # Store in Database
        ###################
        appointment = Appointment(start=appt_start_time,
                                  end=appt_end_time,
                                  department=args['department'],
                                  patient=patient,
                                  provider=provider)
        db.session.add(appointment)
        db.session.commit()

        #########
        # Webhook
        #########
        result = appointment_schema.dump(appointment)
        appointment_notification_webhook(notification_type='created', data=result.data)

        ##########
        # Response
        ##########
        HEADERS = {
            'Location': f'{BASE_URL}/appointments/{appointment.id}',
        }
        return create_response(status_code=201, headers=HEADERS, data={})
Example #2
0
    def patch(self, args, appointment_id):
        """
        Change the appointment start time, duration, and department.

        If you want to change provider and patient, delete and create new.
        """
        appointment = getitem_or_404(Appointment, Appointment.id, appointment_id)

        ##################
        # Handle Arguments
        ##################
        if not args:
            # TODO what to do if nothing is passed it? clarify requirements
            pass

        if 'duration' in args:
            duration = args['duration']
            appt_end_time = appt_start_time + timedelta(minutes=duration)
        else:
            appt_end_time = appointment.end
            duration = (appt_end_time - appointment.start).seconds // 60

        if 'start' in args:
            appt_start_time = args['start'].replace(tzinfo=None)
            appt_end_time = appt_start_time + timedelta(minutes=duration)
        else:
            appt_start_time = appointment.start

        if 'department' in args:
            department = args['department']
        else:
            department = appointment.department

        provider = appointment.provider
        patient = appointment.patient

        ####################
        # Check Restrictions
        ####################
        if appointment.start < datetime.utcnow():
            error_text = 'Cannot modify appointments in the past'
            response = create_response(status_code=400, error=error_text)
            return response

        booking_start = datetime.now() + timedelta(hours=BOOKING_DELAY_IN_HOURS)
        _appointment_starts_before_booking_delay(appt_start_time, booking_start)

        _appointment_longer_than_max_length(duration)

        _start_time_overlaps(Appointment.provider_id, provider.id,
                             appt_start_time, allowed_overlap=[appointment.id])
        _end_time_overlaps(Appointment.provider_id, provider.id, appt_end_time,
                           allowed_overlap=[appointment.id])

        ###############
        # Update record
        ###############
        appointment.start = appt_start_time
        appointment.end = appt_end_time
        appointment.department = department
        db.session.add(appointment)
        db.session.commit()

        #########
        # Webhook
        #########
        result = appointment_schema.dump(appointment)
        appointment_notification_webhook(notification_type='updated', data=result.data)

        ##########
        # Response
        ##########
        return create_response(status_code=200, data=result.data)
Example #3
0
 def delete(self, appointment_id):
     appointment = getitem_or_404(Appointment, Appointment.id, appointment_id)
     db.session.delete(appointment)
     db.session.commit()
     return create_response(status_code=204, data={})
Example #4
0
 def get(self, appointment_id):
     appointment = getitem_or_404(Appointment, Appointment.id, appointment_id)
     result = appointment_schema.dump(appointment)
     return create_response(status_code=200, data=result.data)
Example #5
0
 def delete(self, provider_id):
     provider = getitem_or_404(Provider, Provider.id, provider_id)
     db.session.delete(provider)
     db.session.commit()
     return create_response(status_code=204, data={})
Example #6
0
 def get(self, provider_id):
     provider = getitem_or_404(Provider, Provider.id, provider_id)
     result = provider_schema.dump(provider)
     return create_response(status_code=200, data=result.data)
Example #7
0
 def delete(self, patient_id):
     patient = getitem_or_404(Patient, Patient.id, patient_id)
     db.session.delete(patient)
     db.session.commit()
     return create_response(status_code=204, data={})
Example #8
0
 def get(self, patient_id):
     patient = getitem_or_404(Patient, Patient.id, patient_id)
     result = patient_schema.dump(patient)
     return create_response(status_code=200, data=result.data)