"""Product Orchard Pricing Tier Model. This model represents an Orchard Product Pricing Tier """ import datetime from oto import response from pricing.connectors import mysql from pricing.constants import error import sqlalchemy class ProductOrchardPricingTier(mysql.BaseModel): """Product Orchard Pricing Tier model.""" __tablename__ = 'product_orchard_pricing_tier' product_orchard_pricing_tier_id = sqlalchemy.Column( sqlalchemy.BIGINT, primary_key=True, autoincrement=True) orchard_pricing_tier_id = sqlalchemy.Column(sqlalchemy.BIGINT) product_id = sqlalchemy.Column(sqlalchemy.Integer) pricing_family_id = sqlalchemy.Column(sqlalchemy.Integer) 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 the product orchard pricing tier to dict.""" return dict( product_orchard_pricing_tier_id=self. product_orchard_pricing_tier_id, orchard_pricing_tier_id=self.orchard_pricing_tier_id, product_id=self.product_id, pricing_family_id=self.pricing_family_id, ) @mysql.autosession() def get_by_product_id_and_pricing_family_id( product_id, pricing_family_id, session): """Get the product orchard pricing tier for a product and a family. Args: product_id (int): the ID of the product. pricing_family_id (int): the ID of the pricing family. session (Session): the mysql session. Returns: response.Response: containing the product orchard pricing tier dict. """ found_product_orchard_pricing_tier = \ _get_by_product_id_and_pricing_family_id( product_id, pricing_family_id, session) if not found_product_orchard_pricing_tier: return response.Response() result = found_product_orchard_pricing_tier.to_dict() return response.Response(result) def _get_by_product_id_and_pricing_family_id( product_id, pricing_family_id, session): """Get a product orchard pricing tier by its product ID and its family. Args: product_id (int): the ID of the product. pricing_family_id (int): the ID of the pricing family. session (Session): the mysql session. Returns: ProductOrchardPricingTier: the found product orchard pricing tier. """ return session.query(ProductOrchardPricingTier).filter_by( product_id=product_id, pricing_family_id=pricing_family_id).first() @mysql.autosession() def update_product_with_orchard_pricing_tier( product_id, pricing_family_id, data, session): """Set the orchard pricing tier on a product. Args: product_id (int): the ID of the product. pricing_family_id (int): the ID of the pricing family. data (dict): the data from which to create or update the product orchard pricing tier. 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_pricing_family_id( product_id, pricing_family_id, session) if instance: instance.orchard_pricing_tier_id = data['orchard_pricing_tier_id'] instance.updated_date = datetime.datetime.now() else: data['product_id'] = product_id data['pricing_family_id'] = pricing_family_id data['created_date'] = datetime.datetime.now() instance = ProductOrchardPricingTier(**data) session.add(instance) session.commit() return response.Response(instance.to_dict()) @mysql.autosession() def delete_by_product_id_and_pricing_family_id( product_id, pricing_family_id, session): """Delete the orchard pricing tier from a product. Args: product_id (int): the ID of the product. pricing_family_id (int): the ID of the pricing family. session (Session): the mysql session. Returns: response.Response: containing the deleted product orchard pricing tier dict. """ instance = _get_by_product_id_and_pricing_family_id( product_id, pricing_family_id, session) if not instance: return response.create_not_found_response() session.delete(instance) return response.Response(instance.to_dict()) @mysql.autosession() def get_all_migrated_product_ids(session): """Return all the migrated product ids. Args: session (Session): the mysql session Returns: all the migrated product ids """ query = session.query(ProductOrchardPricingTier.product_id) result = session.execute(query).scalars().all() return result