"""Logic for Orders.""" from ows_product_physical.connector import mysql from ows_product_physical.constant.delivery_order import ORDER_STATUS_COMPLETE from ows_product_physical.models import \ delivery_history as delivery_history_model from ows_product_physical.models import order as order_model from ows_product_physical.models import order_product as order_product_model def get_orders(**kwargs): """Return orders. Keyword Args: ids (array (int)): array of order ids limit (int | None): a limit integer (or None) offset (int | None): an offset integer (or None) order_by (str | None): name to order by order_dir (str | None): order direction delivery_store_ids (array (int)): delivery_store_ids to include delivery_types (array (str)): delivery_type to include delivery_statuses (array (str)): delivery_status to include Returns: response.response: Response containing the orders. """ return order_model.get_orders(**kwargs) def create_order(product_ids, delivery_store_id, identity_id, delivery_type): """Create order. Args: product_ids (list int): product ids to associate with the order delivery_store_id (string): delivery store id to associate with the order identity_id (str): id of the identity creating the order delivery_type (str): Type of delivery Returns: response.Response: response containing the details of the created order """ return order_model.create_order( product_ids, delivery_store_id, delivery_type, identity_id) def update_orders(identity_id, orders): """Update orders. Update orders, and persist delivery history for the associated products as a side effect. Args: identity_id (str): id of the identity updating the orders orders: (list dict): orders Returns: response.Response: response containing the details of the updated orders """ with mysql.delivery_db_session() as delivery_session: with mysql.db_session() as ar_session: # orders from the delivery database result = order_model.update_orders( identity_id, orders, delivery_session) for order in orders: if order.get('status') == ORDER_STATUS_COMPLETE: order_id = order['order_id'] # order products from the delivery database products = \ order_product_model.get_order_products_by_order_id( order_id, delivery_session) # order from the delivery database db_order = order_model.get_order_by_order_id( order_id, delivery_session) delivery_store_id = db_order.cmm_id # delivery history from art_relations delivery_history_model.save_delivery_history( products, delivery_store_id, ar_session) ar_session.flush() delivery_session.flush() return result