def server_hub_map_endpoint(restaurant_id='', attendant_id=''): """ Handles updates to waitstaff/table mappings, which is used for mobile notifications. :return: JSON response body and status code. """ if request.method == 'POST': # POST Request. request_body = flask.request.get_json() restaurant_id = request_body.get('restaurant_id', '') elif request.method == 'GET': # GET Request. restaurant_id = request.args.get('restaurant_id', restaurant_id) request_body = {} else: # DELETE Request. restaurant_id = request.args.get('restaurant_id', restaurant_id) attendant_id = request.args.get('attendant_id', attendant_id) request_body = {} # Restaurant ID must be specified. if not restaurant_id: request_body.update({'error': 'Restaurant ID not specified.'}) return jsonify(request_body), BAD_REQUEST restaurant = Restaurant(restaurant_id=restaurant_id) if not restaurant.exists(): request_body.update({'error': 'Specified restaurant does not exist.'}) return jsonify(request_body), BAD_REQUEST if request.method == 'POST': # update keys. if 'mappings' not in request_body: request_body.update({'error': 'Mappings not specified.'}) return jsonify(request_body), BAD_REQUEST elif len(request_body['mappings']) < 1: request_body.update({'error': 'Mappings list is empty.'}) return jsonify(request_body), BAD_REQUEST mappings = request_body['mappings'] for mapping in mappings: user = User('') user_id = mapping.get('attendant_id', '') user.get_email(user_id=user_id) hub = Hub(restaurant_id=restaurant_id) hub_id = mapping.get('table_id', '') # User must exist, and be associated with the specified restaurant. if not user.exists(): request_body.update({ 'error': 'Specified user {} does not exist.'.format(user_id) }) return jsonify(request_body), BAD_REQUEST elif user.get_my_restaurant(user_id=user_id) != restaurant_id: request_body.update({ 'error': 'Specified user {} is not affiliated with specified restaurant.' .format(user_id) }) return jsonify(request_body), UNAUTHORIZED # Specified hub must be associated with the specified restaurant. if not hub.is_registered(hub_id=hub_id): request_body.update({ 'error': 'Specified table ID {} is not affiliated with specified restaurant.' .format(hub_id) }) return jsonify(request_body), UNAUTHORIZED mapping_info = restaurant.update_staff_hub_mappings( mapping_info=request_body) return jsonify(mapping_info), CREATED elif request.method == 'GET': # GET all mappings in restaurant (with emails). mappings = restaurant.get_staff_hub_mappings() request_body.update(mappings) if 'error' in mappings: return jsonify(request_body), SERVER_ERROR else: return jsonify(request_body), OK elif request.method == 'DELETE': # Attendant ID must be specified. if not attendant_id: request_body.update({'error': 'User ID not specified.'}) return jsonify(request_body), BAD_REQUEST user = User('') user.get_email(user_id=attendant_id) # Specified user must exist and be associated with the specified restaurant. if not user.exists(): request_body.update({'error': 'Specified user does not exist.'}) return jsonify(request_body), BAD_REQUEST elif user.get_my_restaurant(user_id=attendant_id) != restaurant_id: request_body.update( {'error': 'User restaurant combination does not match.'}) return jsonify(request_body), UNAUTHORIZED response = restaurant.remove_staff_from_mappings(staff_id=attendant_id) request_body.update(response) if 'error' in response: return jsonify(response), SERVER_ERROR else: return jsonify(response), OK
def order_endpoint(order_id='', restaurant_id='', customer_id='', table_id=''): """ Handles user- or staff-initiated requests pertaining to individual orders. This endpoint does not handle order status. :param order_id: Unique order ID (only for GET/PUT). :param restaurant_id: Unique restaurant ID. :param customer_id: Unique user ID of customer. :param table_id: Unique hub device ID. :return: JSON response body and status code. """ if request.method in ['POST', 'PUT']: # Request has json body. order_request = flask.request.get_json() restaurant_id = order_request.get('restaurant_id', '') customer_id = order_request.get('customer_id', '') table_id = order_request.get('table_id', '') else: # GET request restaurant_id = request.args.get('restaurant_id', restaurant_id) order_id = request.args.get('order_id', order_id) customer_id = request.args.get('customer_id', customer_id) table_id = request.args.get('table_id', table_id) order_request = {} # Order ID must be non-empty. if not order_id: order_request.update({'error': 'Order ID must be specified.'}) return jsonify(order_request), BAD_REQUEST if not restaurant_id: order_request.update({'error': 'Restaurant ID is not specified.'}) return jsonify(order_request), BAD_REQUEST if not customer_id: order_request.update({'error': 'Customer ID is not specified.'}) return jsonify(order_request), BAD_REQUEST if not table_id: order_request.update({'error': 'Table ID is not specified.'}) return jsonify(order_request), BAD_REQUEST # Restaurant must exist in database. restaurant = Restaurant(restaurant_id=restaurant_id) if not restaurant.exists(): order_request.update({'error': 'Specified restaurant does not exist.'}) return jsonify(order_request), BAD_REQUEST # Customer must exist in database. user = User('') user.get_email(customer_id) if not user.exists(): order_request.update({'error': 'Specified customer does not exist.'}) return jsonify(order_request), BAD_REQUEST # Table must exist and be affiliated with the restaurant. hub = Hub(restaurant_id=restaurant_id) if not hub.is_registered(hub_id=table_id): order_request.update( {'error': 'Specified table is not registered to this restaurant.'}) return jsonify(order_request), UNAUTHORIZED order = Order(restaurant_id=restaurant_id) if request.method == 'GET': order_info = order.get_order(order_id=order_id, restaurant_id=restaurant_id, content_type=request.content_type) if request.content_type == 'text/csv': return order_info elif 'error' in order_info: return jsonify(order_info), SERVER_ERROR else: return jsonify(order_info), OK if request.method == 'POST': # List of items must exist and must be non-empty. if 'items' not in order_request: order_request.update({'error': 'List of items is not specified.'}) return jsonify(order_request), BAD_REQUEST elif len(order_request['items']) < 1: order_request.update({'error': 'List of items is empty.'}) return jsonify(order_request), BAD_REQUEST order_response = order.place_order(order=order_request, customer_id=customer_id, table_id=table_id) if 'error' in order_response: return jsonify(order_response), SERVER_ERROR else: return jsonify(order_response), OK if request.method == 'PUT': # PUT -> update order (insert/update) pass