"""Supply Chain Metadata Logic.""" from oto import response from ows_product_physical.constant import error from ows_product_physical.constant import header from ows_product_physical.models import ows_product from ows_product_physical.models import persister from ows_product_physical.models import product_physical_supply_chain_metadata def get_product_supply_chain_metadata( product_id=None, account_type=None, account_id=None): """Get mapped list of supply chain product metadata. Returns: response.Response: list of supply chain product metadata objects. """ product_response = persister.get_product_by_id(product_id=product_id) if not product_response: return product_response if account_type and account_id: ownership_response = ows_product.check_product_ownership( product_id, account_type, account_id) if not ownership_response.message: return response.create_error_response( message=error.OWNERSHIP_ERROR_MESSAGE.format(account_type), status=error.FORBIDDEN_CODE, code=error.OWNERSHIP_ERROR) product_supply_chain_metadata_response = \ product_physical_supply_chain_metadata \ .get_product_supply_chain_metadata(product_id=product_id) return product_supply_chain_metadata_response def set_physical_supply_chain_metadata( product_id, data, account_type=None, account_id=None): """Set supply chain product metadata. Returns: response.Response: list of supply chain product metadata objects. """ product_response = persister.get_product_by_id(product_id=product_id) if not product_response: return product_response if not data: return response.Response( status=400, message=error.SUPPLY_CHAIN_EMPTY) if account_type and account_id: ownership_response = ows_product.check_product_ownership( product_id, account_type, account_id) if not ownership_response.message: return response.create_error_response( message=error.OWNERSHIP_ERROR_MESSAGE.format(account_type), status=error.FORBIDDEN_CODE, code=error.OWNERSHIP_ERROR) result = product_physical_supply_chain_metadata. \ set_physical_supply_chain_metadata(product_id, data) return result def delete_supply_chain_metadata( product_id, metadata_id, account_type=None, account_id=None): """Soft delete an existing record by setting is_deleted flag as 1. Args: supplychain_metadata_id (int): supply_chain_metadata id Returns: Response: Response containing the result of deleting record. """ product_response = persister.get_product_by_id(product_id=product_id) if not product_response: return product_response if account_type and account_id: ownership_response = ows_product.check_product_ownership( product_id, account_type, account_id) if not ownership_response.message: return response.create_error_response( message=error.OWNERSHIP_ERROR_MESSAGE.format(account_type), status=error.FORBIDDEN_CODE, code=error.OWNERSHIP_ERROR) deletion_result = \ product_physical_supply_chain_metadata.delete_supply_chain_metadata( metadata_id) return deletion_result def delete_supply_chain_metadata_by_product_and_store( product_id, store_id, orchard_user_id, account_type=None, account_id=None): """Soft delete an existing record by setting is_deleted flag as 1. Args: product_id (int): Product key. store_id (int): customer_master_master_id. orchard_user_id (str): Orchard user Id account_type (string): either vendor or subaccount account_id (int): vendor_id or subaccount_id Returns: Response: Response containing the result of deleting record. """ product_response = persister.get_product_by_id(product_id=product_id) if not product_response: return product_response if orchard_user_id and \ not orchard_user_id.startswith(header.OA_USER_PREFIX): ownership_response = ows_product.check_product_ownership( product_id, account_type, account_id) if not ownership_response.message: return response.create_error_response( message=error.OWNERSHIP_ERROR_MESSAGE.format(account_type), status=error.FORBIDDEN_CODE, code=error.OWNERSHIP_ERROR) deletion_result = \ product_physical_supply_chain_metadata\ .delete_supply_chain_metadata_by_product_and_store( product_id, store_id) return deletion_result