"""Model for target_market_goal table in sales_goals database.""" from oto import response from sqlalchemy import Column, ForeignKey, Integer, String from sales_goals.connectors import mysql from sales_goals.constants import error from sales_goals.models import error_handlers from sales_goals.models.store import Store class TargetMarketGoal(mysql.BaseModel): """Class representing the sales goal's target market goal.""" __tablename__ = 'target_market_goal' target_market_goal_id = Column( 'id', Integer, primary_key=True, autoincrement=True) sales_goal_id = Column( Integer, ForeignKey('sales_goal.id'), nullable=False) store_id = Column( Integer, ForeignKey(Store.store_id), nullable=False) label_goal_value = Column(Integer) distribution_goal_value = Column(Integer) country_id = Column(Integer, nullable=False) country_name = Column(String, nullable=False) def to_dict(self): """Return object as dict. Returns: dict: Dictionary representation of the object. """ target_market_goal_dict = { 'target_market_goal_id': self.target_market_goal_id, 'sales_goal_id': self.sales_goal_id, 'store_id': self.store_id, 'label_goal_value': self.label_goal_value, 'distribution_goal_value': self.distribution_goal_value, 'country_id': self.country_id, 'country_name': self.country_name } return target_market_goal_dict @error_handlers.sqlalchemy_error_handler @error_handlers.integrity_error_handler( error_code=error.ERROR_CODE_MODEL_VALIDATION) def create( *, sales_goal_id, store_id, label_goal_value, distribution_goal_value, country_id, country_name): """Create a new record in the target_market_goal table. Args: sales_goal_id (int): SalesGoal FK. store_id (int): Store FK. label_goal_value (int): target market goal value entered by label user. distribution_goal_value: target market goal value entered by distribution user. country_id (int): country id from ows-territories. country_name (str): country name from ows-territories. Returns: response.Response: .message with TargetMarketGoal.to_dict() on success, .errors on failure. """ target_market_goal_obj = TargetMarketGoal( sales_goal_id=sales_goal_id, store_id=store_id, label_goal_value=label_goal_value, distribution_goal_value=distribution_goal_value, country_id=country_id, country_name=country_name) with mysql.sales_goals_session_scope() as session: session.add(target_market_goal_obj) return response.Response(target_market_goal_obj.to_dict()) @error_handlers.sqlalchemy_error_handler @error_handlers.integrity_error_handler( error_code=error.ERROR_CODE_MODEL_VALIDATION) def fetch_by_sales_goal_id(sales_goal_id): """Get target market goals data from db by sales goal id. Args: sales_goal_id (int): id (pk) of sales goal. Returns: response.Response: .message with TargetMarketGoal.to_dict() on success, .errors on failure. """ with mysql.sales_goals_session_scope() as session: goals = session.query(TargetMarketGoal).filter_by( sales_goal_id=sales_goal_id).all() if not goals: return response.create_not_found_response( error.ERROR_MESSAGE_TARGET_MARKET_GOALS_DO_NOT_EXIST.format( sales_goal_id=sales_goal_id)) return response.Response([goal.to_dict() for goal in goals]) @error_handlers.sqlalchemy_error_handler @error_handlers.integrity_error_handler( error_code=error.ERROR_CODE_MODEL_VALIDATION) def update( *, target_market_goal_id, label_goal_value=None, distribution_goal_value=None): """Update target market goals data. Args: target_market_goal_id (int): id (pk) of target market goal. label_goal_value (int): new target market goal value entered by label user. distribution_goal_value: new target market goal value entered by distribution user. Returns: response.Response: .message with TargetMarketGoal.to_dict() on success, .errors on failure. """ with mysql.sales_goals_session_scope() as session: target_market_goal = ( session.get(TargetMarketGoal, target_market_goal_id, with_for_update=True)) if not target_market_goal: return response.create_not_found_response( error.ERROR_MESSAGE_TARGET_MARKET_GOAL_WITH_ID_DOES_NOT_EXIST .format(target_market_goal_id=target_market_goal_id)) if label_goal_value is not None: target_market_goal.label_goal_value = label_goal_value if distribution_goal_value is not None: target_market_goal.distribution_goal_value = ( distribution_goal_value) session.add(target_market_goal) return response.Response(target_market_goal.to_dict()) @error_handlers.sqlalchemy_error_handler @error_handlers.integrity_error_handler( error_code=error.ERROR_CODE_MODEL_VALIDATION) def delete_by_sales_goal_id_and_country_id(*, sales_goal_id, country_id): """Delete target market goals data from db by sales goal id and country_id. Args: sales_goal_id (int): id (pk) of sales goal. country_id (int): country id from ows-territories. Returns: response.Response: empty .message on success, .errors on failure. """ with mysql.sales_goals_session_scope() as session: existing_goals = session.query(TargetMarketGoal).filter_by( sales_goal_id=sales_goal_id, country_id=country_id).all() if not existing_goals: return response.create_not_found_response( error.GOAL_FOR_SALES_GOAL_AND_COUNTRY_DOES_NOT_EXIST.format( sales_goal_id=sales_goal_id, country_id=country_id)) for existing_goal in existing_goals: session.delete(existing_goal) session.flush() return response.Response()