def businessRegister(json): validParams = Utilities.verify_parameters(json, [ 'business_name', 'address', 'city', 'zip_code', 'business_email', 'business_phone', 'max_capacity', 'owner_id', 'service_id' ]) if validParams: try: if not re.match(r'[A-Za-z0-9]+', validParams['business_name']): return jsonify( reason= "Business name must contain only characters and numbers" ) if not re.match(r'[^@]+@[^@]+\.[^@]+', validParams['business_email']): return jsonify(reason="Invalid email address") elif not re.search(r'\w{3}-\w{3}-\w{4}', validParams['business_phone']): return jsonify( reason="The format for business phone is: ###-###-####" ) elif not re.match(r'[0-9]{5}', validParams['zip_code']): return jsonify(reason="The format for zip code is: #####") elif not re.match(r'[0-9]', validParams['max_capacity']): return jsonify(reason="Max capacity must be a number") Users.updateRole(validParams['owner_id']) newBusiness = Businesses(**validParams).create() result = { "message": "Success!", "request": Utilities.to_dict(newBusiness) } return jsonify(result), 200 except Exception as e: return jsonify(reason="Server error", error=e.__str__()), 500 else: return jsonify(reason="Invalid parameters"), 400
def updateRecord(json): valid_parameters = Utilities.verify_parameters(json, CovidCases.REQUIRED_PARAMETERS) if valid_parameters: try: updatedInfo = CovidCases.updateCovidStatus(json) result = { "message": "Success!", "case": Utilities.to_dict(updatedInfo) } user = Utilities.to_dict(User.getUserById(json['patient_id'])) office = Utilities.to_dict(MedicalOffice.getMedicalOfficeById(json['office_id'])) statuses = {2: 'negative', 3: 'positive'} msg = Message('COVID-19 Test Result', sender='*****@*****.**', recipients=[user['email']]) msg.body = f'''Hi {user['full_name']}, Your tested {statuses[json['test_status']]} to the COVID-19. If you want to know more info about your COVID-19 test, please call {office['office_phone_number']}. ''' mail.send(msg) CovidCasesHandler.notifyNearbyUsers(json, user) return jsonify(result), 200 except Exception as e: return jsonify(reason="Server error", error=e.__str__()), 500
def register(json): validParams = Utilities.verify_parameters(json, [ 'username', 'email', 'password', 'first_name', 'last_name', 'role' ]) validParams['role'] = 'customer' checkUsername = Users.getUserByUsername(validParams['username']) checkUserEmail = Users.getUserByEmail(validParams['email']) if validParams: try: if checkUsername != None: return jsonify(reason="Username already exists!") elif checkUserEmail != None: return jsonify(reason="Email already exists!") elif not re.match(r'[a-zA-Z0-9]+', validParams['username']): return jsonify( reason= "Username must contain only characters and numbers") elif not re.match(r'[^@]+@[^@]+\.[^@]+', validParams['email']): return jsonify(reason="Invalid email address") newUser = Users(**validParams).create() result = { "message": "Success!", "request": Utilities.to_dict(newUser) } return jsonify(result), 200 except Exception as e: return jsonify(reason="Server error", error=e.__str__()), 500 else: return jsonify(reason="Invalid parameters"), 400
def userRegisterAppt(aid, uid): validParams = Utilities.verify_parameters(json, ['number_of_customers']) try: appt = Appointment.updateAppt(aid, uid) result = { "message": "Success!", "request": Utilities.to_dict(appt) } return jsonify(result), 200 except Exception as e: return jsonify(reason="Server error", error=e.__str__()), 500
def createPatient(json): valid_params = Utilities.verify_parameters(json, Patient.REQUIRED_PARAMETERS) if valid_params: try: created_patient = Patient(**valid_params).create() patient_dict = Utilities.to_dict(created_patient) result = { "message": "Success!", "patient": patient_dict, } return jsonify(result), 201 except Exception as err: return jsonify(message="Server error!", error=err.__str__()), 500 else: return jsonify(message="Bad Request!"), 40
def createVisitedLocation(json): valid_params = Utilities.verify_parameters( json, VisitedLocation.REQUIRED_PARAMETERS) if valid_params: try: visited_location = VisitedLocation(**valid_params).create() location_dict = Utilities.to_dict(visited_location) result = { "message": "Success!", "visited_location": location_dict, } return jsonify(result), 201 except Exception as err: return jsonify(message="Server error!", error=err.__str__()), 500 else: return jsonify(message="Bad Request!"), 40
def createAppt(json): validParams = Utilities.verify_parameters( json, ['business_id', 'business_name', 'start_time', 'end_time']) validParams['business_name'] = Businesses().getBusinessById( validParams['business_id']).business_name if validParams: try: newAppt = Appointment(**validParams).create() newAppt.business_name = Businesses().getBusinessById( validParams['business_id']).business_name result = { "message": "Success!", "request": Utilities.to_dict(newAppt) } return jsonify(result), 200 except Exception as e: return jsonify(reason="Server error", error=e.__str__()), 500 else: return jsonify(reason="Invalid parameters"), 400
def updateUserInfo(json): valid_parameters = Utilities.verify_parameters( json, ['user_id', 'email', 'phone_number', 'password', 'address_id']) if valid_parameters: try: email_exists = User.getUserByEmail( json['email']) and User.user_id == json['user_id'] if email_exists: return jsonify(message="Email already in use."), 400 updatedInfo = User.updateUserInfo(**valid_parameters) result = { "message": "Success!", "user": Utilities.to_dict(updatedInfo) } return jsonify(result), 200 except Exception as e: return jsonify(reason="Server error", error=e.__str__()), 500
def createRecord(json): valid_params = Utilities.verify_parameters(json, CovidCases.REQUIRED_PARAMETERS) if valid_params: try: patient_exists = Patient.getPatientByOfficeAndUserId(json['office_id'], json['patient_id']) if patient_exists: try: covid_case = CovidCases(**valid_params).create() case_dict = Utilities.to_dict(covid_case) result = { "message": "Success!", "case": case_dict, } return jsonify(result), 201 except: return jsonify(reason="Patient was already tested today."), 401 else: return jsonify(reason="User is not in our office record."), 401 except Exception as err: return jsonify(message="Server error!", error=err.__str__()), 500 else: return jsonify(message="Bad Request!"), 40
def createLocation(json): valid_params = Utilities.verify_parameters( json, Location.REQUIRED_PARAMETERS) if valid_params: try: location = Location.getLocationByLattitudeAndLongitude(json) if location == None: location = Location(**valid_params).create() location_dict = Utilities.to_dict(location) result = { "message": "Success!", "location": location_dict, } return jsonify(result), 201 except Exception as err: return jsonify(message="Server error!", error=err.__str__()), 500 else: return jsonify(message="Bad Request!"), 40
def createUser(json): valid_params = Utilities.verify_parameters(json, User.REQUIRED_PARAMETERS) if valid_params: try: email_exists = User.getUserByEmail(json['email']) if email_exists: return jsonify( message="Email already taken. Please use another one." ), 400 created_user = User(**valid_params).create() user_dict = Utilities.to_dict(created_user) result = { "message": "Success!", "user": user_dict, } return jsonify(result), 201 except Exception as err: return jsonify(message="Server error!", error=err.__str__()), 500 else: return jsonify(message="Bad Request!"), 40
def createDoctor(json): valid_params = Utilities.verify_parameters(json, Doctor.REQUIRED_PARAMETERS) if valid_params: try: user_exists = User.getUserById(json['user_id']) if not user_exists: return jsonify( message= "The doctor you are trying to register doesn't have an account." ), 400 created_doctor = Doctor(**valid_params).create() doctor_dict = Utilities.to_dict(created_doctor) result = { "message": "Success!", "doctor": doctor_dict, } return jsonify(result), 201 except Exception as err: return jsonify(message="Server error!", error=err.__str__()), 500 else: return jsonify(message="Bad Request!"), 40