def walkin_appointment_nurse():

    if request.method == 'PUT':

        availability_id = int(request.get_json().get('availability_id'))
        patient_id = int(request.get_json().get('patient_id'))

        patient_service.test_and_set_patient_into_cache(patient_id)
        patient = get_from_cache(int(patient_id))

        availability = availability_service.validate_availability_and_reserve(
            availability_id)
        if availability is not None:
            result = booking_service.write_booking(
                Appointment(patient_id, availability))
            if result == BookingStatus.SUCCESS:
                return js.create_json(
                    data=None,
                    message="Successfully created annual booking",
                    return_code=js.ResponseReturnCode.CODE_200)
            else:
                return js.create_json(
                    data=None,
                    message="Unable to create annual booking",
                    return_code=js.ResponseReturnCode.CODE_400)
        else:
            return js.create_json(data=None,
                                  message="Unable to create annual booking",
                                  return_code=js.ResponseReturnCode.CODE_400)
def cart():
    """ view cart use case """
    if request.method == 'GET':

        # ensure user is logged-in to proceed
        if not cookie_helper.user_is_logged(request):
            return js.create_json(data=None, message="User is not logged", return_code=js.ResponseReturnCode.CODE_400)

        # getting patient_id from cookie
        patient_id = int(request.cookies.get(CookieKeys.ID.value))
        # get patient from cache
        patient = get_from_cache(patient_id)

        cart = patient.get_cart()

        # list of Appointment objects
        appointment_list = cart.get_appointments()

        new_appointment_list = []

        # object parsing going on here to be able to send it with json format
        for appointment in appointment_list:
            new_availability = appointment.availability.__dict__()
            new_appointment = Appointment(patient_id, new_availability).__dict__
            new_appointment_list.append(new_appointment)

        data_to_send = {'appointment_list': new_appointment_list, 'patient_id': patient_id}

        return js.create_json(data=data_to_send, message="List of appointments with patient id",
                              return_code=js.ResponseReturnCode.CODE_200)
def appointment():

    if request.method == 'DELETE':
        # example use case: remove appointment
        # params: patient_id (int, required), availability_id (int, required)
        # return: success/failure

        if not cookie_helper.user_is_logged(request):
            return js.create_json(data=None, message="User is not logged", return_code=js.ResponseReturnCode.CODE_400)

        patient_id = int(request.args.get('patient_id'))
        availability_id = int(request.args.get('availability_id'))

        if availability_id is None:
            return js.create_json(data=None, message="No appointment specified", return_code=js.ResponseReturnCode.CODE_400)
        if patient_id is None:
            return js.create_json(data=None, message="No patient specified", return_code=js.ResponseReturnCode.CODE_400)

        patient = get_from_cache(patient_id)
        result = patient.remove_from_cart(availability_id)

        if result is None:
            return js.create_json(data=None, message="Appointment not found/removed", return_code=js.ResponseReturnCode.CODE_400)

        return js.create_json(data=None, message="Appointment removed", return_code=js.ResponseReturnCode.CODE_200)
def schedule():

    if request.method == 'POST':

        request_type = request.get_json().get('request_type')
        appointment_request_type = request.get_json().get('appointment_request_type')
        patient_id = int(request.get_json().get('patient_id'))

        patient_service.test_and_set_patient_into_cache(patient_id)
        patient = get_from_cache(patient_id)

        clinic_id = patient.clinic_id

        date = datetime.strptime(request.get_json().get('date'), '%Y-%m-%d').date()
        year = int(date.year)
        month = int(date.month)
        day = int(date.day)

        proto_str = request_type + "-" + appointment_request_type

        if request_type == RequestEnum.MONTHLY_REQUEST.value:
            sr_monthly = sched_register.get_request(proto_str).set_date(Date(year, month))
            monthly_schedule = Scheduler.get_instance().get_schedule(sr_monthly, clinic_id)

            return js.create_json(data=monthly_schedule, message=None, return_code=js.ResponseReturnCode.CODE_200)

        if request_type == RequestEnum.DAILY_REQUEST.value:
            sr_daily = sched_register.get_request(proto_str).set_date(Date(year, month, day))
            daily_schedule = Scheduler.get_instance().get_schedule(sr_daily, clinic_id)

            return js.create_json(data=daily_schedule, message=None, return_code=js.ResponseReturnCode.CODE_200)
def login_admin():
    """
    The endpoint for logging in as an administrator;
    This is necessary in order to register a doctor or a nurse -
    only an admin can do this
    """

    if request.method == 'POST':

        # User is already logged in (regardless of login-type {patient, admin, nurse, doctor})
        if cookie_helper.user_is_logged(request):
            return js.create_json(data=None,
                                  message="Already logged in",
                                  return_code=js.ResponseReturnCode.CODE_400)

        email = request.get_json().get('email')
        password = request.get_json().get('password')

        admin_id = admin_service.validate_login_info(email, password)

        if admin_id == -1:
            return js.create_json(data=None,
                                  message="Incorrect Admin Login information",
                                  return_code=js.ResponseReturnCode.CODE_400)

        resp = js.create_json(data=None,
                              message="Logged in successfully",
                              return_code=js.ResponseReturnCode.CODE_200,
                              as_tuple=False)
        resp = cookie_helper.set_user_logged(
            resp, admin_id, cookie_helper.UserTypes.ADMIN.value)

        return resp, js.ResponseReturnCode.CODE_200.value
Example #6
0
def view_availability():
    if request.method == 'PUT':

        doctor_id = request.get_json().get('doctor_id')

        if doctor_id is None:
            return js.create_json(data=None,
                                  message="No parameters provided",
                                  return_code=js.ResponseReturnCode.CODE_400)

        result = None

        result = availability_service.get_availability_by_doctor_id(doctor_id)

        if result is None:
            return js.create_json(data=None,
                                  message="Could not retrieve availabilities",
                                  return_code=js.ResponseReturnCode.CODE_400)
        if result == AvailabilityStatus.NO_AVAILABILITIES:
            return js.create_json(
                data=None,
                message="Doctor does not have any availabilites",
                return_code=js.ResponseReturnCode.CODE_200)

        return js.create_json(data=result,
                              message=None,
                              return_code=js.ResponseReturnCode.CODE_200)
def current_clinic(user_type, id):
    if request.method == 'GET':

        result = clinic_service.get_current_clinic(user_type, id)

        if result is not None:
            return js.create_json(data=result,
                                  message=None,
                                  return_code=js.ResponseReturnCode.CODE_200)

        return js.create_json(data=None,
                              message="Could not fetch current clinic.",
                              return_code=js.ResponseReturnCode.CODE_400)
def doctor():

    if request.method == 'GET':
        # params: id (int, semi-required), doctor_name (text, semi-required)
        # return: doctor object
        doctor_id = request.args.get('id')
        doctor_last_name = request.args.get('last_name')
        clinic_id = request.args.get('clinic_id')
        clinic_id_from_nurse = None

        if cookie_helper.user_is_logged(request, UserTypes.NURSE):
            clinic_list = clinic_service.get_current_clinic(
                "nurse", int(request.cookies.get(CookieKeys.ID.value)))
            clinic_id_from_nurse = int(clinic_list[0]["id"])

        if doctor_id is None and doctor_last_name is None and clinic_id is None:
            return js.create_json(data=None,
                                  message='No doctor params specified',
                                  return_code=js.ResponseReturnCode.CODE_400)

        result = None

        if doctor_last_name is not None:
            result = doctor_service.get_doctor_by_last_name(
                doctor_last_name, clinic_id_from_nurse)
        elif clinic_id is not None:
            result = doctor_service.get_doctor_by_clinic(clinic_id)
        else:
            result = doctor_service.get_doctor(doctor_id)

        if result is None:
            return js.create_json(data=None,
                                  message='Could not retrieve doctor',
                                  return_code=js.ResponseReturnCode.CODE_500)
        if result == 3:
            return js.create_json(data=None,
                                  message='Doctor Id does not exist',
                                  return_code=js.ResponseReturnCode.CODE_400)
        if result == 4:
            return js.create_json(
                data=None,
                message=
                f"Doctor with last name '{doctor_last_name}' does not exist",
                return_code=js.ResponseReturnCode.CODE_400)

        return js.create_json(data=result,
                              message=None,
                              return_code=js.ResponseReturnCode.CODE_200)
def update_patient(patient_id):

    if request.method == "PUT":
        # params: patient_id (string, required)
        # return: sucess/failure
        # note: the email and health card number of the user cannot be changed
        
        date_of_birth = request.args.get('date_of_birth')
        gender = request.args.get('gender')
        phone_nb = request.args.get('phone_nb')
        home_address = request.args.get('home_address')
        first_name = request.args.get('first_name')
        last_name = request.args.get('last_name')
        password = request.args.get('password')

        if patient_id is None:
            return js.create_json(data=None, message="No patient id provided", return_code=js.ResponseReturnCode.CODE_400)
        if date_of_birth is None:
            return js.create_json(data=None, message="No data of birth provided", return_code=js.ResponseReturnCode.CODE_400)
        if gender is None:
            return js.create_json(data=None, message="No gender provided", return_code=js.ResponseReturnCode.CODE_400)
        if phone_nb is None:
            return js.create_json(data=None, message="No phone number provided", return_code=js.ResponseReturnCode.CODE_400)
        if home_address is None:
            return js.create_json(data=None, message="No home address provided", return_code=js.ResponseReturnCode.CODE_400)
        if first_name is None:
            return js.create_json(data=None, message="No first name provided", return_code=js.ResponseReturnCode.CODE_400)
        if last_name is None:
            return js.create_json(data=None, message="No last name provided", return_code=js.ResponseReturnCode.CODE_400)
        if password is None:
            return js.create_json(data=None, message="No password provided", return_code=js.ResponseReturnCode.CODE_400)
         
        result = patient_service.update_patient(
            patient_id,
            date_of_birth,
            gender,
            phone_nb,
            home_address,
            first_name,
            last_name,
            password
        )

        if result == CreatePatientStatus.EMAIL_ALREADY_EXISTS:
            return js.create_json(data=None, message="Email address already registered", return_code=js.ResponseReturnCode.CODE_500)

        return js.create_json(data=None, message="Patient record updated", return_code=js.ResponseReturnCode.CODE_201)
Example #10
0
def get_patient_bookings(patient_id):

    if request.method == 'GET':
        # params: patient_id (int, required)
        # return: list of bookings belonging to a patient

        if patient_id is None:
            return js.create_json(data=None,
                                  message="No patient id provided",
                                  return_code=js.ResponseReturnCode.CODE_400)

        result = booking_service.get_bookings_for_patient(patient_id)

        return js.create_json(
            data=result,
            message=f"Found {len(result)} bookings for patient {patient_id}",
            return_code=js.ResponseReturnCode.CODE_200)
Example #11
0
def get_doctor_bookings(doctor_id):

    if request.method == 'GET':
        # params: doctor_id (int, required)
        # return: list of bookings belonging to a doctor

        if doctor_id is None:
            return js.create_json(data=None,
                                  message="No doctor_id id provided",
                                  return_code=js.ResponseReturnCode.CODE_400)

        result = booking_service.get_bookings_for_doctor(doctor_id)
        print(result)

        return js.create_json(
            data=result,
            message=f"Found {len(result)} bookings for doctor {doctor_id}",
            return_code=js.ResponseReturnCode.CODE_200)
def login_patient():

    # Grab the data from the post request
    if request.method == 'POST':

        # Check that the user is not already logged (users can login accross multiple PCs, however)
        if cookie_helper.user_is_logged(request):
            return js.create_json(data=None,
                                  message="Already logged in!",
                                  return_code=js.ResponseReturnCode.CODE_400)

        if request.get_json() is None:
            return js.create_json(None, "No login information provided",
                                  js.ResponseReturnCode.CODE_400)

        health_card_nb = request.get_json().get('health_card_nb')
        password = request.get_json().get('password')

        # Validate the login information
        patient_id = patient_service.validate_login_info(
            health_card_nb, password)

        # There was no patient linked with the health card number and password
        if patient_id == -1:
            return js.create_json(data=None,
                                  message="Invalid login information",
                                  return_code=js.ResponseReturnCode.CODE_400)

        # Set patient in cache
        patient_service.test_and_set_patient_into_cache(patient_id)

        # observer pattern hook
        notifier.notify()

        # set the cookie in the response object
        resp = js.create_json(data=None,
                              message="Logged in successfully",
                              return_code=js.ResponseReturnCode.CODE_200,
                              as_tuple=False)
        resp = cookie_helper.set_user_logged(
            resp, patient_id, cookie_helper.UserTypes.PATIENT.value)

        return resp, js.ResponseReturnCode.CODE_200.value
def my_update():
    """ endpoint that returns the info concerning the canceled booking(s)
    of the patient to the front end"""
    # getting patient_id from cookie
    patient_id = request.cookies.get(CookieKeys.ID.value)

    patient_id = int(patient_id)
    # get patient from cache
    patient = get_from_cache(patient_id)

    messages = deepcopy(patient.get_login_messages())

    if len(messages) > 0:
        patient.login_messages.clear()
        return js.create_json(data=messages, message="canceled bookings notification",
                              return_code=js.ResponseReturnCode.CODE_200, as_tuple=False)
    else:
        return js.create_json(data=None, message="nothing to notify", return_code=js.ResponseReturnCode.CODE_200,
                              as_tuple=False)
Example #14
0
def register_medical_personel():

    if request.method == 'PUT':

        # Check if logged in as admin
        if cookie_helper.user_is_logged(
                request, as_user_type=cookie_helper.UserTypes.ADMIN):

            request_object = request.get_json()

            medical_personel_runner = MedicalPersonelRunner(request_object)
            result = medical_personel_runner.create_medical_personel()

            if result == MedicalPersonelStatus.MISSING_PARAMETERS:
                return js.create_json(
                    data=None,
                    message="Missing Parameters",
                    return_code=js.ResponseReturnCode.CODE_400)

            if result == CreateDoctorStatus.PHYSICIAN_NUMBER_ALREADY_EXISTS:
                return js.create_json(
                    data=None,
                    message="Physician Number Already Exists",
                    return_code=js.ResponseReturnCode.CODE_400)

            if result == CreateNurseStatus.ACCESS_ID_ALREADY_EXISTS:
                return js.create_json(
                    data=None,
                    message="Access ID Already Exists",
                    return_code=js.ResponseReturnCode.CODE_400)

            return js.create_json(
                data=None,
                message="Successfully created Medical Personel",
                return_code=js.ResponseReturnCode.CODE_201)

        else:
            return js.create_json(
                data=None,
                message=
                "Not logged in as admin, cannot register medical personel",
                return_code=js.ResponseReturnCode.CODE_400)
def walkin_appointment():

    if request.method == 'PUT':

        availability_id = int(request.get_json().get('availability_id'))
        patient_id = int(request.get_json().get('patient_id'))

        patient_service.test_and_set_patient_into_cache(patient_id)
        patient = get_from_cache(patient_id)
        availability = availability_service.get_availability(availability_id)

        result = patient.make_walkin_appointment(availability)


        if result == MakeAnnualStatus.SUCCESS:
            return js.create_json(data=None, message="Successfully added walkin appointment", return_code=js.ResponseReturnCode.CODE_200)

        elif result == MakeAnnualStatus.HAS_THIS_APPOINTMENT_IN_CART:
            return js.create_json(data=None, message="This appointment is already in your cart!",
                              return_code=js.ResponseReturnCode.CODE_400)
def annual_appointment():

    if request.method == 'PUT':
        availability_id = int(request.get_json().get('availability_id'))
        patient_id = int(request.get_json().get('patient_id'))

        patient_service.test_and_set_patient_into_cache(patient_id)
        patient = get_from_cache(patient_id)
        availability = availability_service.get_availability(availability_id)


        if patient_service.has_annual_booking(patient_id):
            return js.create_json(data=None, message="You have an annual booking!", return_code=js.ResponseReturnCode.CODE_400)

        result = patient.make_annual_appointment(availability)

        if result == MakeAnnualStatus.SUCCESS:
            return js.create_json(data=None, message="Successfully added annual appointment", return_code=js.ResponseReturnCode.CODE_200)

        if result == MakeAnnualStatus.HAS_ANNUAL_APPOINTMENT:
            return js.create_json(data=None, message="You already have an annual appointment in your cart!", return_code=js.ResponseReturnCode.CODE_400)
def nurse():
    # params: clinic_id (required)
    # return: nurse object
    if request.method == 'GET':
        clinic_id = request.args.get('clinic_id')

        if clinic_id is not None:
            result = nurse_service.get_nurse_by_clinic(clinic_id)
        else:
            return js.create_json(data=None,
                                  message='No clinic param specified',
                                  return_code=js.ResponseReturnCode.CODE_400)

        if result is None:
            return js.create_json(data=None,
                                  message='Could not retrieve nurse',
                                  return_code=js.ResponseReturnCode.CODE_500)

        return js.create_json(data=result,
                              message=None,
                              return_code=js.ResponseReturnCode.CODE_200)
def login_doctor():

    # Grab the data from the post request
    if request.method == 'POST':

        # Check that the user is not already logged (users can login accross multiple PCs, however)
        if cookie_helper.user_is_logged(request):
            return js.create_json(data=None,
                                  message="Already logged in!",
                                  return_code=js.ResponseReturnCode.CODE_400)

        if request.get_json() is None:
            return js.create_json(None, "No login information provided",
                                  js.ResponseReturnCode.CODE_400)

        physician_permit = request.get_json().get('physician_permit')
        password = request.get_json().get('password')

        # Validate the login information
        doctor_id = doctor_service.validate_login_info(physician_permit,
                                                       password)

        # There was no doctor linked with the physician number and password
        if doctor_id == -1:
            return js.create_json(data=None,
                                  message="Invalid login information",
                                  return_code=js.ResponseReturnCode.CODE_400)

        # Set patient in cache
        doctor_service.test_and_set_doctor_into_cache(doctor_id)

        # set the cookie in the response object
        resp = js.create_json(data=None,
                              message="Logged in successfully",
                              return_code=js.ResponseReturnCode.CODE_200,
                              as_tuple=False)
        resp = cookie_helper.set_user_logged(
            resp, doctor_id, cookie_helper.UserTypes.DOCTOR.value)

        return resp, js.ResponseReturnCode.CODE_200.value
def logout():
    """
    Logout of ANY user type
    :return:
    """
    if request.method == 'GET':

        resp = js.create_json(data=None,
                              message="Successfully logged out!",
                              return_code=js.ResponseReturnCode.CODE_200,
                              as_tuple=False)
        resp = cookie_helper.logout_user_cookie(resp)
        return resp, js.ResponseReturnCode.CODE_200.value
Example #20
0
def get_available_rooms():

    if request.method == 'GET':

        start = request.args.get('start')
        year = request.args.get('year')
        month = request.args.get('month').lstrip("0")
        day = request.args.get('day').lstrip("0")

        if start is None:
            return js.create_json(data=None,
                                  message="No start time parameter provided",
                                  return_code=js.ResponseReturnCode.CODE_400)
        if year is None:
            return js.create_json(data=None,
                                  message="No year parameter provided",
                                  return_code=js.ResponseReturnCode.CODE_400)
        if month is None:
            return js.create_json(data=None,
                                  message="No month parameter provided",
                                  return_code=js.ResponseReturnCode.CODE_400)
        if day is None:
            return js.create_json(data=None,
                                  message="No day parameter provided",
                                  return_code=js.ResponseReturnCode.CODE_400)

        result = availability_service.get_available_rooms(
            start, year, month, day)

        if result is None:
            return js.create_json(data=None,
                                  message="Could not retrieve availabilities",
                                  return_code=js.ResponseReturnCode.CODE_500)
        if result == AvailabilityStatus.ALL_ROOMS_OPEN:
            return js.create_json(data=format_room.all_rooms,
                                  message=None,
                                  return_code=js.ResponseReturnCode.CODE_200)
        if result == AvailabilityStatus.NO_ROOMS_AT_THIS_TIME:
            return js.create_json(data={},
                                  message="No rooms available at this time",
                                  return_code=js.ResponseReturnCode.CODE_200)

        return js.create_json(data=format_room.negate_rooms(result),
                              message=None,
                              return_code=js.ResponseReturnCode.CODE_200)
Example #21
0
def book():

    if request.method == 'GET':
        # example use case: get_bookings
        # params: booking_id (int, required)
        # return: booking(s) belonging to the patient or doctor

        booking_id = request.args.get('booking_id')

        results = None

        if booking_id is None:
            results = booking_service.get_all_bookings()
        else:
            results = booking_service.get_booking(booking_id)

        if results == BookingStatus.NO_BOOKINGS:
            return js.create_json(data=None,
                                  message="Booking id does not exist",
                                  return_code=js.ResponseReturnCode.CODE_400)

        return js.create_json(data=results,
                              message=None,
                              return_code=js.ResponseReturnCode.CODE_200)

    if request.method == 'PUT':
        # example use case: checkout_appointment
        # params: appointment_id (int, required, from cookie), patient_id(int, required)
        # return: success/failure

        if not cookie_helper.user_is_logged(request):
            return js.create_json(data=None,
                                  message="User is not logged",
                                  return_code=js.ResponseReturnCode.CODE_400)

        availability_id = request.get_json().get('availability_id')
        patient_id = int(request.get_json().get('patient_id'))
        booking_id = request.get_json().get('booking_id')

        if availability_id is None:
            return js.create_json(data=None,
                                  message="No appointment specified",
                                  return_code=js.ResponseReturnCode.CODE_400)
        if patient_id is None:
            return js.create_json(data=None,
                                  message="No patient specified",
                                  return_code=js.ResponseReturnCode.CODE_400)

        # update booking
        if booking_id is not None:
            availability_obj = availability_service.get_availability(
                availability_id)
            appointment = (Appointment(patient_id, availability_obj)
                           )  # creating appointment without using cart

            if availability_obj is not None:
                f_key = booking_service.cancel_booking_return_key(booking_id)

                if f_key:
                    Scheduler.get_instance().free_availability(f_key)
                    booking_service.write_booking(appointment)
                    Scheduler.get_instance().reserve_appointment(appointment)
                    return js.create_json(
                        data={appointment},
                        message="Appointment successfully updated",
                        return_code=js.ResponseReturnCode.CODE_200)
                else:
                    return js.create_json(
                        data=None,
                        message="Appointment not updated",
                        return_code=js.ResponseReturnCode.CODE_400)
            else:
                return js.create_json(
                    data=None,
                    message="Unable to update booking",
                    return_code=js.ResponseReturnCode.CODE_400)

        patient = get_from_cache(patient_id)  # get the patient from cache
        appointment = patient.cart.get_appointment(availability_id)

        process_payment = payment_processing.checkout(patient_id)
        result = None

        if process_payment == True:
            result = Scheduler.get_instance().reserve_appointment(appointment)
        else:
            return js.create_json(
                data=None,
                message=
                "Could not process payment with your credit card information",
                return_code=js.ResponseReturnCode.CODE_500)

        if result:
            removed = patient.cart.remove_appointment(availability_id)
            booking_service.write_booking(appointment)

            if removed is None:
                return js.create_json(
                    data=None,
                    message="Appointment not found/removed",
                    return_code=js.ResponseReturnCode.CODE_400)

            return js.create_json(data={appointment},
                                  message="Appointment successfully booked",
                                  return_code=js.ResponseReturnCode.CODE_200)
        else:
            return js.create_json(data=None,
                                  message="Appointment slot already booked",
                                  return_code=js.ResponseReturnCode.CODE_400)

    if request.method == 'DELETE':
        # example use case: cancel_booking
        # params: booking_id (int, required)
        # return: success/failure

        booking_id = request.args.get('booking_id')

        if booking_id is None:
            return js.create_json(data=None,
                                  message="No booking specified",
                                  return_code=js.ResponseReturnCode.CODE_400)

        f_key = booking_service.cancel_booking_return_key(
            booking_id
        )  # returns primary key of booking's corresponding availability
        if f_key:
            Scheduler.get_instance().free_availability(f_key)
        else:
            return js.create_json(data=None,
                                  message="Unable to delete booking",
                                  return_code=js.ResponseReturnCode.CODE_400)

        return js.create_json(data=None,
                              message="Booking successfully deleted",
                              return_code=js.ResponseReturnCode.CODE_200)
def view_cookie():

    return js.create_json(data=request.cookies, message="Here is your cookie", return_code=js.ResponseReturnCode.CODE_200)
def patient():

    if request.method == 'GET':
        # params: patient_id (int, semi-required), last_name (text, semi-required)
        # return: patient object
        patient_id = (request.args.get('patient_id')) # Get patient by id only
        patient_last_name = request.args.get('last_name') # Get patient by last name only
        patient_info = request.args.get('patient_info') # Get patient by either last name or health card NB

        clinic_id = None

        if cookie_helper.user_is_logged(request, UserTypes.NURSE):
            clinic_list = clinic_service.get_current_clinic("nurse", int(request.cookies.get(CookieKeys.ID.value)))
            clinic_id = int(clinic_list[0]["id"])


        if patient_id is None and patient_last_name is None and patient_info is None:
            return js.create_json(data=None, message="No patient params specified", return_code=js.ResponseReturnCode.CODE_400)

        result = None

        if patient_last_name is not None:
            result = patient_service.get_patient_by_last_name(patient_last_name, clinic_id)
        elif patient_info is not None:
            # Returns a list of patient objects
            result = patient_service.get_patient_by_last_name(patient_info, clinic_id)
            if result is -3:
                # Returns a single patient object
                result = patient_service.get_patient_by_health_card_nb(patient_info, clinic_id)
        else:
            result = patient_service.get_patient(int(patient_id))

        if result is None:
            return js.create_json(data=None, message="Could not retrieve patient", return_code=js.ResponseReturnCode.CODE_500)
        if result == -3:
            return js.create_json(data=None, message="Patient does not exist", return_code=js.ResponseReturnCode.CODE_400)

        return js.create_json(data=result, message=None, return_code=js.ResponseReturnCode.CODE_200)


    if request.method == 'PUT':
        # params: [various, look below] (int or string, required)
        # return: sucess/failure

        if request.get_json() is None:
            return js.create_json(None, "No patient information provided", js.ResponseReturnCode.CODE_400)

        health_card_nb = request.get_json().get('health_card_nb')
        date_of_birth = request.get_json().get('date_of_birth')
        gender = request.get_json().get('gender')
        phone_nb = request.get_json().get('phone_nb')
        home_address = request.get_json().get('home_address')
        email = request.get_json().get('email')
        first_name = request.get_json().get('first_name')
        last_name = request.get_json().get('last_name')
        password = request.get_json().get('password')
        clinic_id = request.get_json().get('clinic_id')

        if health_card_nb is None:
            return js.create_json(data=None, message="No health card number provided", return_code=js.ResponseReturnCode.CODE_400)
        if date_of_birth is None:
            return js.create_json(data=None, message="No data of birth provided", return_code=js.ResponseReturnCode.CODE_400)
        if gender is None:
            return js.create_json(data=None, message="No gender provided", return_code=js.ResponseReturnCode.CODE_400)
        if phone_nb is None:
            return js.create_json(data=None, message="No phone number provided", return_code=js.ResponseReturnCode.CODE_400)
        if home_address is None:
            return js.create_json(data=None, message="No home address provided", return_code=js.ResponseReturnCode.CODE_400)
        if email is None:
            return js.create_json(data=None, message="No email provided", return_code=js.ResponseReturnCode.CODE_400)
        if first_name is None:
            return js.create_json(data=None, message="No first name provided", return_code=js.ResponseReturnCode.CODE_400)
        if last_name is None:
            return js.create_json(data=None, message="No last name provided", return_code=js.ResponseReturnCode.CODE_400)
        if password is None:
            return js.create_json(data=None, message="No password provided", return_code=js.ResponseReturnCode.CODE_400)
        if clinic_id is None:
            return js.create_json(data=None, message="No clinic id provided", return_code=js.ResponseReturnCode.CODE_400)

        result = patient_service.create_patient(
            health_card_nb,
            date_of_birth,
            gender,
            phone_nb,
            home_address,
            email,
            first_name,
            last_name,
            password,
            clinic_id
        )

        if result == CreatePatientStatus.HEALTH_CARD_ALREADY_EXISTS:
            return js.create_json(data=None, message="Health card number already registered", return_code=js.ResponseReturnCode.CODE_500)

        if result == CreatePatientStatus.EMAIL_ALREADY_EXISTS:
            return js.create_json(data=None, message="Email address already registered", return_code=js.ResponseReturnCode.CODE_500)

        return js.create_json(data=None, message="Patient record created", return_code=js.ResponseReturnCode.CODE_201)
Example #24
0
def respond_to_options_request():

    if request.method == "OPTIONS":
        return js.create_json(data=None,
                              message=None,
                              return_code=js.ResponseReturnCode.CODE_200)
def clinic():
    if request.method == 'GET':
        result = clinic_service.get_clinics()

        return js.create_json(data=result,
                              message=None,
                              return_code=js.ResponseReturnCode.CODE_200)

    if request.method == 'PUT':

        if request.get_json() is None:
            return js.create_json(None, "No clinic information provided",
                                  js.ResponseReturnCode.CODE_400)

        name = request.get_json().get('name')
        location = request.get_json().get('location')
        nb_rooms = request.get_json().get('nb_rooms')
        nb_doctors = request.get_json().get('nb_doctors')
        nb_nurses = request.get_json().get('nb_nurses')
        open_time = convert_time.get_time_to_second(
            request.get_json().get('open_time'))
        close_time = convert_time.get_time_to_second(
            request.get_json().get('close_time'))
        phone = request.get_json().get('phone')
        clinic_id = request.get_json().get(
            'clinic_id')  #semi-required - indicated UPDATE

        if location is None:
            return js.create_json(data=None,
                                  message="No clinic location provided",
                                  return_code=js.ResponseReturnCode.CODE_400)
        if open_time is None:
            return js.create_json(data=None,
                                  message="No open time for clinic provided",
                                  return_code=js.ResponseReturnCode.CODE_400)
        if close_time is None:
            return js.create_json(data=None,
                                  message="No close time for clinic provided",
                                  return_code=js.ResponseReturnCode.CODE_400)
        if phone is None:
            return js.create_json(
                data=None,
                message="No phone number for clinic provided",
                return_code=js.ResponseReturnCode.CODE_400)

        #update clinic use case
        if clinic_id is not None:
            result = clinic_service.modify_clinic_limited(
                clinic_id, location, open_time, close_time, phone)

            if result is None:
                return js.create_json(
                    data=None,
                    message="Could not update clinic",
                    return_code=js.ResponseReturnCode.CODE_500)

            return js.create_json(data=None,
                                  message="Successfully updated clinic",
                                  return_code=js.ResponseReturnCode.CODE_201)

        if name is None:
            return js.create_json(data=None,
                                  message="No clinic name provided",
                                  return_code=js.ResponseReturnCode.CODE_400)
        if nb_rooms is None:
            return js.create_json(
                data=None,
                message="Number of rooms for clinic not provided",
                return_code=js.ResponseReturnCode.CODE_400)
        if nb_doctors is None:
            return js.create_json(
                data=None,
                message="Number of doctors for clinic not provided",
                return_code=js.ResponseReturnCode.CODE_400)
        if nb_nurses is None:
            return js.create_json(
                data=None,
                message="Number of nurses for clininc not provided",
                return_code=js.ResponseReturnCode.CODE_400)

        result = clinic_service.register_clinic(name, location, nb_rooms,
                                                nb_doctors, nb_nurses,
                                                open_time, close_time, phone)

        if result is None:
            return js.create_json(data=None,
                                  message="Could not register clinic",
                                  return_code=js.ResponseReturnCode.CODE_500)

        return js.create_json(data=None,
                              message="Successfully created clinic",
                              return_code=js.ResponseReturnCode.CODE_201)
Example #26
0
def availability():
    if request.method == 'GET':
        # example use case: Make doctor availability
        # params: doctor_id (int, required, from cookie)
        # return: availability object(s)

        availability_id = request.args.get('availability_id')
        doctor_id = request.args.get('doctor_id')
        clinic_id = request.args.get('clinic_id')

        if doctor_id is None and availability_id is None and clinic_id is None:
            result = availability_service.get_all_availabilities()

            if result == AvailabilityStatus.NO_AVAILABILITIES:
                return js.create_json(
                    data=None,
                    message="No availabilites",
                    return_code=js.ResponseReturnCode.CODE_200)

            return js.create_json(data=result,
                                  message=None,
                                  return_code=js.ResponseReturnCode.CODE_200)

        result = None

        if doctor_id is not None:
            result = availability_service.get_availability_by_doctor_id(
                doctor_id)
        elif availability_id is not None:
            result = availability_service.get_availability(availability_id)
        else:
            result = availability_service.get_availability_by_clinic_id(
                clinic_id)
        if result is None:
            return js.create_json(data=None,
                                  message="Could not retrieve availabilities",
                                  return_code=js.ResponseReturnCode.CODE_500)
        if result == AvailabilityStatus.NO_AVAILABILITIES:
            return js.create_json(
                data=None,
                message="Doctor does not have any availabilites",
                return_code=js.ResponseReturnCode.CODE_200)

        return js.create_json(data=result,
                              message=None,
                              return_code=js.ResponseReturnCode.CODE_200)

    if request.method == 'PUT':
        # example use case: Make doctor availability
        # params: appointment_id (int, required, from cookie), patient_id(int, required)
        # return: success/failure

        doctor_id = request.get_json().get('doctor_id')
        start = request.get_json().get('start')
        room = request.get_json().get('room')
        year = request.get_json().get('year')
        month = request.get_json().get('month').lstrip("0")
        day = request.get_json().get('day').lstrip("0")

        booking_type = request.get_json().get('booking_type')

        if doctor_id is None:
            return js.create_json(data=None,
                                  message="No doctor id provided",
                                  return_code=js.ResponseReturnCode.CODE_400)
        if start is None:
            return js.create_json(data=None,
                                  message="No start time provided",
                                  return_code=js.ResponseReturnCode.CODE_400)
        if room is None:
            return js.create_json(data=None,
                                  message="No room number provided",
                                  return_code=js.ResponseReturnCode.CODE_400)
        if year is None:
            return js.create_json(data=None,
                                  message="No year provided",
                                  return_code=js.ResponseReturnCode.CODE_400)
        if month is None:
            return js.create_json(data=None,
                                  message="No month provided",
                                  return_code=js.ResponseReturnCode.CODE_400)
        if day is None:
            return js.create_json(data=None,
                                  message="No day provided",
                                  return_code=js.ResponseReturnCode.CODE_400)
        if booking_type is None:
            return js.create_json(data=None,
                                  message="No booking type provided",
                                  return_code=js.ResponseReturnCode.CODE_400)

        result = None

        doctor_service.test_and_set_doctor_into_cache(doctor_id)
        clinic_id = get_from_cache("doctor" + doctor_id).clinic_id

        if booking_type == AppointmentRequestType.WALKIN:
            result = availability_service.check_and_create_availability_walkin(
                doctor_id, start, room, '1', year, month, day, booking_type,
                clinic_id)

        if booking_type == AppointmentRequestType.ANNUAL:
            result = availability_service.check_and_create_availability_annual(
                doctor_id, start, room, '1', year, month, day, booking_type,
                clinic_id)

        if result == AvailabilityStatus.NO_ROOMS_AT_THIS_TIME:
            return js.create_json(
                data=None,
                message="No rooms available at this time slot",
                return_code=js.ResponseReturnCode.CODE_400)
        if result == AvailabilityStatus.AVAILABILITY_ALREADY_BOOKED_AT_THIS_TIME:
            return js.create_json(
                data=None,
                message="Doctor already has a room booked at this time slot",
                return_code=js.ResponseReturnCode.CODE_400)

        return js.create_json(data=[result],
                              message="Availability record created",
                              return_code=js.ResponseReturnCode.CODE_201)

    if request.method == 'DELETE':
        # example use case: delete doctor availability
        # params: appointment_id (int, required)
        # return: success/failure message

        availability_id = request.get_json().get('id')

        if availability_id is None:
            return js.create_json(data=None,
                                  message="No availability id provided",
                                  return_code=js.ResponseReturnCode.CODE_400)

        availability_result = availability_service.get_availability(
            availability_id)

        if availability_result == False:
            return js.create_json(data=None,
                                  message="Doctor availability does not exist",
                                  return_code=js.ResponseReturnCode.CODE_400)

        is_free = availability_result.free
        result = None

        patient_id = booking_service.get_patient_id_from_availability_id(
            availability_id)

        if is_free:
            result = availability_service.cancel_availability(availability_id)
        else:
            bookingResult = BookingService().cancel_booking_with_availability(
                availability_id)

            if bookingResult:
                res = availability_service.cancel_availability(availability_id)
                if res and patient_id != None:
                    #observer pattern hook
                    patient_id = int(patient_id)
                    notifier.attach({patient_id: availability_result})

            else:
                return js.create_json(
                    data=None,
                    message="Could not delete doctor availability",
                    return_code=js.ResponseReturnCode.CODE_500)

        return js.create_json(
            data=None,
            message="Successfully deleted doctor availability",
            return_code=js.ResponseReturnCode.CODE_200)
Example #27
0
def modify_availability(availability_id):
    """ Makes a new availability and deletes the old one, which is used to generate a new availability_id
    This is so that a patient who tries to book the previous availability_id won't be able to """
    if request.method == 'PUT':
        doctor_id = request.get_json().get('doctor_id')
        start = request.get_json().get('start')
        room = request.get_json().get('room')
        year = request.get_json().get('year')
        month = request.get_json().get('month').lstrip("0")
        day = request.get_json().get('day').lstrip("0")

        current_availability = availability_service.get_availability(
            availability_id)
        booking_type = current_availability.booking_type

        if doctor_id is None:
            doctor_id = current_availability.doctor_id
        if start is None or not start:
            start = str(current_availability.start)
        if room is None or not room:
            room = current_availability.room
        if year is None or not year:
            year = str(current_availability.year)
        if month is None or not month:
            month = str(current_availability.month)
        if day is None or not day:
            day = str(current_availability.day)

        doctor_service.test_and_set_doctor_into_cache(doctor_id)
        clinic_id = get_from_cache("doctor" + doctor_id).clinic_id

        if availability_service.room_is_available_at_this_time(
                start, room, year, month, day,
                doctor_id) == AvailabilityStatus.NO_ROOMS_AT_THIS_TIME:
            return js.create_json(data=None,
                                  message="Room not available at this time.",
                                  return_code=js.ResponseReturnCode.CODE_400)

        else:
            # Make a new availability
            if booking_type == AppointmentRequestType.WALKIN:
                result = availability_service.check_and_create_availability_walkin(
                    doctor_id, start, room, '1', year, month, day,
                    booking_type, clinic_id, availability_id)
            if booking_type == AppointmentRequestType.ANNUAL:
                result = availability_service.check_and_create_availability_annual(
                    doctor_id, start, room, '1', year, month, day,
                    booking_type, clinic_id, availability_id)

            if result == AvailabilityStatus.NO_ROOMS_AT_THIS_TIME:
                return js.create_json(
                    data=None,
                    message="No rooms available at this time slot",
                    return_code=js.ResponseReturnCode.CODE_400)

            # Delete the old availability and booking at this availability if there is one
            booking_service.cancel_booking_with_availability(availability_id)
            availability_service.cancel_availability(availability_id)

            return js.create_json(data=None,
                                  message="Availability successfully modified",
                                  return_code=js.ResponseReturnCode.CODE_201)