"""Database Persister for Inventory. This module runs the low level database operations needed by the API at the HTTP handler level. """ from oto import response import sentry_sdk from ows_product_physical.connector import mysql from ows_product_physical.constant import error from ows_product_physical.constant import field from ows_product_physical.constant import success from ows_product_physical.models.sql.product_physical_inventory import \ SELECT_PRODUCT_PHYSICAL_INVENTORY_BY_PRODUCT_ID def get_product_inventory_by_product_id(product_id): """Fetch the product inventory by product id. Args: product_id (string|int): the id of the product to fetch if exists. Returns: Response: Response containing the result of fetching. """ if not product_id: return response.Response(message={}) with mysql.db_session() as session: try: rows = session.execute( SELECT_PRODUCT_PHYSICAL_INVENTORY_BY_PRODUCT_ID, {field.PRODUCT_ID: product_id}, ) result = rows.fetchone() if not result: return response.Response(status=success.NO_CONTENT_CODE) return response.Response(message=dict(result)) except Exception as exception: sentry_sdk.capture_exception(exception) return response.create_error_response( code=error.INTERNAL_ERROR, message='mysql error', status=500 )