def process_read_query(query, res_code=200): if res_code != 200: return query result, res_code = readQ.select_connection(query) if res_code == 200: return JSONResponse(content=jsonable_encoder(result)) else: raise HTTPException(status_code=res_code, detail=result)
def get_user_preferences(user_email: str): """gets preferences for a specific user based on user_email required parameters: user_email""" connection = Connection() reader = readQ(connection) user_id = reader.get_userid_by_email(user_email) query = readQ.get_user_preferences_query(user_id) return connection.get_result(query)
def get_current_weight(business_id: int, item_ids: List[int] = None): """gets all current weight for all items of a specific business provided optional params: can get specific items by item id required parameters: business_id optional parameters: item_ids""" query = readQ.get_current_weight_query(business_id=business_id, item_ids=item_ids) connection = Connection() return connection.get_result(query)
async def get_suppliers(business_id: int, item_id: Optional[int] = -1, item_ids: Optional[List] = None): """gets all suppliers based on business_id, provided optional params: can get only specific suppliers by item_ids""" ids = None if item_id > 0: ids = [item_id] elif item_ids: ids = item_ids query = readQ.get_suppliers_query(business_id, items_ids=ids) connection = Connection() return connection.get_result(query)
def get_notifications(business_id: int, active: bool = True, notification_id: int = None): """gets all notifications based on business_id, provided optional params: can get only active notifications and a specific notification required parameters: business_id optional parameters: active ,notification_id """ query, res_code = readQ.get_notifications_with_info( business_id=business_id, active=active, notification_id=notification_id) connection = Connection() return connection.get_result(query)
def get_containers(business_id: int = None, container_id: Optional[int] = None, item_id: Optional[int] = None): """gets all current weight for all items of a specific business provided optional params: can get specific containers, all containers per item required parameters: business_id optional parameters: container_id, item_id """ if item_id: item_id = [item_id] query = readQ.get_current_weight_query(business_id=business_id, container_ids=container_id, item_ids=item_id, get_by_container=True) connection = Connection() return connection.get_result(query)
def get_users(business_id: int = None, get_supplier: bool = False, get_businesses: bool = False): """gets all users in the DB """ res = {} connection = Connection() users_query = readQ.get_users_query(business_id=business_id) res["users"] = connection.get_result(users_query) # if get_supplier: # users_query = readQ.get_users_query(business_id=business_id) # res["suppliers"] = connection.get_result(users_query) # if get_businesses: # business_query = readQ.get_users_query(business_id=business_id) # res["businesses"] = connection.get_result(business_query) for user in res["users"]: user["email"] = user["email_user_name"] + "@" + user[ "email_domain_name"] return res
async def get_current_view(business_id: int, user_id: int = 1): """gets all the info a user needs based on business_id output: "preferences": pre, "notifications": notifications, "weights": weights, "suppliers": suppliers, "orders": orders,""" message = "unable to load preference" user_preferences_query = readQ.get_user_preferences_query(user_id) message = "unable to load items" items_query = readQ.get_items(business_id=business_id) message = "unable to load weight" weight_query = readQ.get_current_weight_query(business_id) message = "unable to load notifications" notifications_query, notifications_code = readQ.get_notifications_with_info( business_id, active=True) message = "unable to load suppliers" suppliers_query = readQ.get_suppliers_query(business_id) message = "unable to load orders" orders_query, orders_code = readQ.get_open_orders_query(business_id) try: if notifications_code != 200: message = notifications_query raise Exception if orders_code != 200: message = orders_query raise Exception connection = Connection() # reader = readQ(connection) message = "unable to load preferences" preferences = connection.get_result(user_preferences_query) message = "unable to load items" items = connection.get_result(items_query) message = "unable to load notifications" notifications = connection.get_result(notifications_query) message = "unable to load weights" weights = connection.get_result(weight_query) message = "unable to load suppliers" suppliers = connection.get_result(suppliers_query) message = "unable to load orders" orders = connection.get_result(orders_query) res = { "preferences": preferences, "items": items, "notifications": notifications, "weights": weights, "suppliers": suppliers, "orders": orders, } del connection return res except HTTPException as error: raise error except (Exception, psycopg2.DatabaseError) as error: print(error) print(message) settings.email_client.email_admin(message, "error details:" + str(error)) raise HTTPException(status_code=400, detail=message)