"""Product Store Effective Prce Code. This model represents a Product Store Effective Price Code """ import datetime from oto import response from pricing.connectors import mysql from pricing.constants import error import sqlalchemy class ProductStoreEffectivePriceCode(mysql.BaseModel): """Product Store Effective Price Code model.""" __tablename__ = 'product_store_effective_price_code' product_store_effective_price_code_id = sqlalchemy.Column( sqlalchemy.BIGINT, primary_key=True, autoincrement=True) product_id = sqlalchemy.Column(sqlalchemy.Integer) store_id = sqlalchemy.Column(sqlalchemy.Integer) price_code = sqlalchemy.Column(sqlalchemy.VARCHAR(45)) store_pricing_tier_name = sqlalchemy.Column(sqlalchemy.VARCHAR(45)) created_date = sqlalchemy.Column(sqlalchemy.DateTime) created_by = sqlalchemy.Column(sqlalchemy.VARCHAR(45)) updated_date = sqlalchemy.Column(sqlalchemy.DateTime) updated_by = sqlalchemy.Column(sqlalchemy.VARCHAR(45)) def to_dict(self): """Convert Product Store Effective Price Code to dict.""" row_id = self.product_store_effective_price_code_id return dict( product_store_effective_price_code_id=row_id, product_id=self.product_id, store_id=self.store_id, price_code=self.price_code, store_pricing_tier_name=self.store_pricing_tier_name) @mysql.autosession() def update_product_store_effective_price_code( product_id, store_id, data, session): """Set the effective pricing code for a product on a store. Args: product_id (int): the ID of the product. store_id(int): the ID of the store. data (dict): the data from which to create or update the product store effective price code. session (Session): the mysql session. Returns: response.Response: containing the created or updated product orchard pricing tier dict. """ if not data: return response.create_error_response( 400, error.ERROR_MESSAGE_EMPTY_BODY) instance = _get_by_product_id_and_store_id(product_id, store_id, session) if instance: instance.price_code = data['price_code'] if 'store_pricing_tier_name' in data: instance.store_pricing_tier_name = data['store_pricing_tier_name'] instance.updated_date = datetime.datetime.now() else: data['product_id'] = product_id data['store_id'] = store_id data['created_date'] = datetime.datetime.now() instance = ProductStoreEffectivePriceCode(**data) session.add(instance) session.commit() return response.Response(instance.to_dict()) def _get_by_product_id_and_store_id(product_id, store_id, session): """Get a product store effective price code its product and store IDs. Args: product_id (int): the ID of the product. store_id (int): the ID of the store. session (Session): the mysql session. Returns: ProductStoreEffectivePriceCode: the found product store price code. """ return session.query(ProductStoreEffectivePriceCode).filter_by( product_id=product_id, store_id=store_id).first() @mysql.autosession() def get_by_product_id(product_id, session): """Get the product store effective price codes for a product ID. Args: product_id (int): the ID of the product. session (Session): the mysql session. Returns: Response: the found product store price code. """ items = session.query(ProductStoreEffectivePriceCode).filter_by( product_id=product_id).all() return response.Response( { 'items': [ item.to_dict() for item in items ] })