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 setUp(self): """ Must call setUp() on BaseTestClass to setup the test application :return: N/A """ super(BookingTest, self).setUp() self.booking_url = "http://localhost:5000/booking" self.patient_id = "16" self.availability_id = "20" cache.reset_cache() """ Log in user """ self.login_url = "http://localhost:5000/login" self.valid_health_card_nb = "DRSJ 9971 0157" self.password = "******" valid_health_card_and_pw = { "health_card_nb": self.valid_health_card_nb, "password": self.password } response = self.send_post(self.login_url, valid_health_card_and_pw) self.assert_status_code(response, 200) """ Create and store patient in cache""" PatientService().test_and_set_patient_into_cache(self.patient_id) patient = cache.get_from_cache(self.patient_id) availability = Availability(self.availability_id, "20", "32400", "881", "1", "2019", "4", "8", AppointmentRequestType.WALKIN) appointment = Appointment(self.patient_id, availability) patient.add_walkin_to_cart(appointment)
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 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 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 test_patient_in_cache_after_login(self): """ Tests that the id returned from validate_login_info, when used to set the patient in cache, yields the correct patient object when retrieved from the cache :return: N/A """ patient_id = PatientService().validate_login_info( self.valid_health_card_nb, self.password) assert (cache.get_from_cache(patient_id) == None) PatientService().test_and_set_patient_into_cache(patient_id) patient_obj = cache.get_from_cache(patient_id) assert (not (patient_obj == None)) assert (patient_id == patient_obj.get_id())
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)
def test_and_set_nurse_into_cache(self, nurse_id): """ If the doctor object is stored in cache already, don't do anything. Otherwise create a new doctor object and store it in cache. """ nurse_key = "nurse" + str(nurse_id) if get_from_cache(nurse_key) is not None: return else: select_stmt = '''SELECT * FROM Nurse WHERE id = ?''' params = (nurse_id, ) result = self.db.read_one(select_stmt, params) nurse = Nurse(result['id'], result['access_id'], result['first_name'], result['last_name'], result['clinic_id']) set_to_cache(nurse_key, nurse)
def test_and_set_doctor_into_cache(self, doctor_id): """ If the doctor object is stored in cache already, don't do anything. Otherwise create a new doctor object and store it in cache. """ doctor_key = "doctor" + str(doctor_id) if get_from_cache(doctor_key) is not None: return else: select_stmt = "SELECT * FROM Doctor WHERE id = ?" params = (doctor_id, ) result = self.db.read_one(select_stmt, params) doctor = Doctor(result['id'], result['first_name'], result['last_name'], result['physician_permit_nb'], result['specialty'], result['city'], result['password'], result['clinic_id']) set_to_cache(doctor_key, doctor)
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 get_patient_by_health_card_nb(self, health_card_nb, clinic_id=None): """Query the db for a patient by the patient's health card nb and return the created patient object""" select_stmt = '''SELECT * FROM Patient WHERE health_card_nb = ?''' params = (health_card_nb, ) result = self.db.read_one(select_stmt, params) if len(result) == 0: return -3 if clinic_id is not None and not (int(result['clinic_id']) == clinic_id): return -3 self.test_and_set_patient_into_cache(result['id']) patient = get_from_cache(result['id']) return patient
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 test_and_set_patient_into_cache(self, patient_id): """ If the patient object is stored in cache already, don't do anything. Otherwise create a new patient object and store it in cache. """ if patient_id == None: return if get_from_cache(patient_id) is not None: return else: select_stmt = '''SELECT * FROM Patient WHERE id = ?''' params = (patient_id, ) result = self.db.read_one(select_stmt, params) patient = Patient(result['id'], result['first_name'], result['last_name'], result['health_card_nb'], result['date_of_birth'], result['gender'], result['phone_nb'], result['home_address'], result['email'], result['clinic_id']) set_to_cache(patient_id, patient)
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)