"""Logic for Sales Goals. Perform CRUD operations against the sales goals schema. """ from itertools import groupby from oto import response from sales_goals.constants import error from sales_goals.constants import header from sales_goals.constants import models from sales_goals.logic import countries from sales_goals.models import ows_product from sales_goals.models import sales_goal from sales_goals.models import sales_goals_for_country from sales_goals.models import store from sales_goals.models import target_market_goal from sales_goals.utils import logic_utils def _format_market_goal(goal, store_id_to_name_map): """Function that formats target market goal data to dict. Args: goal (dict): target market goal data. store_id_to_name_map (dict): store_id to store_name mapping. Returns: dict: formatted market goal. """ result = { 'target_market_goal_id': goal['target_market_goal_id'], 'label_goal_value': goal['label_goal_value'], 'distribution_goal_value': goal['distribution_goal_value'], 'store': { 'store_id': goal['store_id'], 'name': store_id_to_name_map[goal['store_id']] } } return result def group_target_market_goals( market_goals, country_id_to_name_map, store_id_to_name_map, total_data=None, sales_goals_for_country=None, ): """Combine and format sales goal fetched data into one dict. Args: market_goals (list): marketing target goals for sales goal. country_id_to_name_map (dict): {country_id: country_name} mapping. store_id_to_name_map (dict): {store_id: store_name} mapping. total_data (list): list of dicts with total sum of label goals and distr goals data for a country in format: [ {'country_id': 1, 'distr_total': 31, 'label_total': 15}, {'country_id': 2, 'distr_total': 32, 'label_total': 16}]. ({'items': [**goals_data], 'total': {**total_data}} instead of list). Return: Response: list with target market goals. """ if sales_goals_for_country is None: sales_goals_for_country = [] result = [] sorted_data = sorted(market_goals, key=lambda s: s['country_id']) if not sorted_data: return result for country_id, items in groupby(sorted_data, lambda s: s['country_id']): formatted_goal = { 'country': { 'country_id': country_id, 'name': country_id_to_name_map[country_id] } } sales_goal_for_country = [ sales_goal_for_country for sales_goal_for_country in sales_goals_for_country if sales_goal_for_country.country_id == country_id ] if len(sales_goal_for_country) > 0: formatted_goal['first_week_estimate'] = sales_goal_for_country[0].\ first_week_sales else: formatted_goal['first_week_estimate'] = None goals = [] total_map = { country_total['country_id']: { 'distribution_total': country_total['distribution_total'], 'label_total': country_total['label_total']} for country_total in total_data} country_total = total_map.get(country_id, {}) if market_goals: goals = [ item for item in items if models.TARGET_MARKET_GOAL_ID in item] formatted_goal.update(_format_create_target_market_goal_response( goals, store_id_to_name_map, total_data=country_total)) result.append(formatted_goal) return result def format_fetched_goal_data( goal, market_goals, stores=None, total_data=None, sales_goals_for_country=None, ): """Combine and format sales goal fetched data into one dict. Args: goal (dict): sales goal dict representation. market_goals (list): marketing target goals for sales goal. stores (list): stores used in marketing target goals. total_data (list): list of dicts with total sum of label goals and distr goals data for a country in format: [ {'country_id': 1, 'distr_total': 31, 'label_total': 15}, {'country_id': 2, 'distr_total': 32, 'label_total': 16}]. ({'items': [**goals_data], 'total': {**total_data}} instead of list). Return: Response: dict representing sales goal. """ if sales_goals_for_country is None: sales_goals_for_country = [] countries_map = { arr['country_id']: arr['country_name'] for arr in market_goals} stores_map = {st['store_id']: st['name'] for st in stores} target_market_goals = group_target_market_goals( market_goals, countries_map, stores_map, total_data, sales_goals_for_country) result = { 'sales_goals': goal, 'target_market_goals': [], } if not target_market_goals: return result for target_mkt_goal in target_market_goals: if target_mkt_goal.get('goals'): target_mkt_goal['goals']['items'] = _sort_target_mkt_goals( target_mkt_goal['goals']['items']) result['target_market_goals'] = target_market_goals return result def _sort_target_mkt_goals(target_mkt_goals): """Sort target mkt goals by store name. Goals are sorted in alphabetical order, the goal related to the store Other is moved to the end of list. Args: target_mkt_goals (list): list of target market goals to sort. Return: List: sorted target market goals. """ return sorted( target_mkt_goals, key=lambda k: (k['store']['name'] == 'Other', (k['store']['name']).lower())) def fetch_by_product_id( product_id, account_type=None, account_id=None, user_id=None): """Logic for fetching a sales goal by product id. Args: product_id (int): product_id for the goal we want to fetch. account_type (str): a grass header parameter (subaccount|vendor). account_id (str): a grass header parameter. user_id (str): user id from request header. Return: Response: dict object representing sales goal in response body. """ if account_type and account_id: validated_product = ows_product.get_product_ownership_by_account( account_type=account_type, account_id=account_id, product_id=str(product_id)) if not validated_product: return validated_product goal_response = sales_goal.fetch_by_product_id(product_id) if not goal_response: return goal_response goal = goal_response.message sales_goal_id = goal['sales_goal_id'] market_goals_response = target_market_goal.fetch_by_sales_goal_id( sales_goal_id) if market_goals_response.status not in [200, 404]: return market_goals_response market_goals = market_goals_response.message or [] stores = [] total_data = [] countries_hide_retailers = ( countries.get_countries_with_hidden_retailers()) if market_goals: store_ids = list({m_goal['store_id'] for m_goal in market_goals}) stores_response = store.fetch_by_store_ids(store_ids) if not stores_response: return stores_response stores = stores_response.message total_data = _compose_total_data(market_goals) market_goals = logic_utils.compose_market_goals_result( market_goals, countries_hide_retailers, user_id) sales_goals_for_countries = sales_goals_for_country.\ fetch_sales_goals_for_country(sales_goal_id) result = format_fetched_goal_data( goal, market_goals, stores, total_data, sales_goals_for_countries, ) # don't return 'notes' field if request was performed by workstation user if user_id and logic_utils.is_workstation_user(user_id): del result['sales_goals']['notes'] return response.Response(result) def _compose_total_data(market_goals): """Logic for composing total_data of correct format. Args: market_goals (list): list of dicts representing target arket goals. Return: list: list of dicts, every dict has the following structure: { 'country_id':, 'distribution_total':, 'label_total':}. """ total_data = [] for key, grouped_goal in groupby(market_goals, lambda s: s['country_id']): total_for_country = {'country_id': key} total_for_country.update(logic_utils.compose_totals(grouped_goal)) total_data.append(total_for_country) return total_data def create_for_product_id( product_id, sales_goal_data, account_type=None, account_id=None, user_id=None): """Logic for creating a sales goal for product given product_id. Args: product_id (int): product_id for the goal we want to fetch. sales_goal_data (dict): request body data. account_type (str): a grass header parameter (subaccount|vendor). account_id (str): a grass header parameter. user_id (str): user id from request header. Return: Response: JSON object representing sales goal in response body. """ if account_type and account_id: validated_product = ows_product.get_product_ownership_by_account( account_type=account_type, account_id=account_id, product_id=str(product_id)) if not validated_product: return validated_product notes = sales_goal_data[models.SALES_GOALS].get(models.NOTES) hmv_notes = sales_goal_data[models.SALES_GOALS].get(models.HMV_NOTES) created_sales_goals = sales_goal.create( product_id=product_id, notes=notes, updated_by=user_id, hmv_notes=hmv_notes) if not created_sales_goals: return created_sales_goals return response.Response(created_sales_goals.message) def _create_target_market_goals_for_sales_goal( sales_goal_id, country_id, country_name, user_id, market_goals_data): """Function that creates target market goals by list with goals data. Args: sales_goal_id (int): id of sales goal which get have market goals. country_id (int): id of country for which market goals will be created. country_name (str): name of country. user_id (str): user id from request header. market_goals_data (list): list of market goals data that should be created. Returns: Response: .message contains list of new TargetMarketGoal.to_dict() objects and list of stores used in created market goals on success, .errors on fail. """ store_ids = [] created_goals = [] for market_goal in market_goals_data: store_id = market_goal[models.STORE][models.STORE_ID] label_goal_value = market_goal.get(models.LABEL_GOAL_VALUE) distribution_goal_value = market_goal.get( models.DISTRIBUTION_GOAL_VALUE, 0) if user_id is not None: editable_for_label_user_response = ( check_access_for_user_to_edit_target_market_goal( distribution_goal_value, user_id)) if not editable_for_label_user_response: return editable_for_label_user_response target_market_goal_response = target_market_goal.create( 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) if not target_market_goal_response: return target_market_goal_response store_ids.append(store_id) created_goals.append(target_market_goal_response.message) result = {'created_goals': created_goals, 'store_ids': store_ids} return response.Response(result) def _get_country_name_by_id(valid_countries, country_id): """Helper function returns country_name by provided country_id. Args: valid_countries (list): the list of valid countries. country_id (int): id of country for which name will be returned. Returns: Response: .message country name string on success, .errors on fail. """ country_name = next(( country[models.NAME] for country in valid_countries if country[models.COUNTRY_ID] == country_id), None) if not country_name: return response.create_not_found_response( message=error.ERROR_MESSAGE_COUNTRY_DO_NOT_EXIST.format( country_id=country_id)) return response.Response(country_name) def _format_create_target_market_goal_response( goals, stores_map, update_data=None, total_data=None, first_week_estimate=None): """Format created target market goals into dict object. Args: goals (list): created target market goals data. stores_map (dict): store_id to store_name mapping. total_data (dict): total sum of label goals and distr goals data, e.g {'label_total': sum_of_label_goals,'distr_total': sum_of_distr_goals}. Returns: dict: Formatted dict with target market goals data. """ result = {} if update_data: result = { models.UPDATED_AT: update_data[models.UPDATED_AT], models.UPDATED_BY: update_data[models.UPDATED_BY] } if goals: result[models.GOALS] = [ { models.TARGET_MARKET_GOAL_ID: goal[ models.TARGET_MARKET_GOAL_ID], models.STORE: { models.STORE_ID: goal[models.STORE_ID], models.NAME: stores_map[goal[models.STORE_ID]], }, models.LABEL_GOAL_VALUE: goal[models.LABEL_GOAL_VALUE], models.DISTRIBUTION_GOAL_VALUE: goal.get( models.DISTRIBUTION_GOAL_VALUE), } for goal in goals] result[models.GOALS] = { models.ITEMS: [{ models.TARGET_MARKET_GOAL_ID: goal[ models.TARGET_MARKET_GOAL_ID], models.STORE: { models.STORE_ID: goal[models.STORE_ID], models.NAME: stores_map[goal[models.STORE_ID]], }, models.LABEL_GOAL_VALUE: goal[models.LABEL_GOAL_VALUE], models.DISTRIBUTION_GOAL_VALUE: goal.get( models.DISTRIBUTION_GOAL_VALUE, '') } for goal in goals], models.TOTAL: total_data, models.FIRST_WEEK_ESTIMATE: first_week_estimate, } return result def _add_missing_market_goals_data(new_market_goals_data, country_id): """Add empty goals for each store of the country to provided goals list. Args: new_market_goals_data (list): list of goals that should be created. country_id (int): id of country which stores should be filled. Returns: list: updated list of new_market_goals_data. """ available_stores = countries.get_stores_available_in_country( country_id).message unfilled_stores = [ available_store for available_store in available_stores.get('items', []) if available_store[models.STORE_ID] != models.OTHER_STORE_ID] goals_to_create = [ { 'distribution_goal_value': 0, 'store': { 'name': unfilled_store['name'], 'store_id': unfilled_store['store_id'] }, 'label_goal_value': 0 } for unfilled_store in unfilled_stores] goals_to_create.extend(new_market_goals_data) return goals_to_create def create_target_market_goal( product_id, country_id, market_goal_data, account_type=None, account_id=None, user_id=None): """Logic for creating a target market goal for given product and country. Args: product_id (int): product_id for target market goal we want to create. country_id (int): country_id for target market goal we want to create. market_goal_data (dict): request body data. account_type (str): a grass header parameter (subaccount|vendor). account_id (str): a grass header parameter. user_id (str): user id from request header. Return: Response: dict object representing target market goal in response body. """ if account_type and account_id: validated_product = ows_product.get_product_ownership_by_account( account_type=account_type, account_id=account_id, product_id=str(product_id)) if not validated_product: return validated_product goal_response = sales_goal.fetch_by_product_id(product_id) if not goal_response: return goal_response goal = goal_response.message sales_goal_id = goal[models.SALES_GOAL_ID] countries_response = countries.get_countries(user_id) if not countries_response: return countries_response valid_countries = countries_response.message[models.ITEMS] country_response = _get_country_name_by_id(valid_countries, country_id) if not country_response: return country_response country_name = country_response.message market_goal_data_for_update = market_goal_data.get(models.GOALS, []) store_in_country_goal_data_is_valid = _validate_store_in_country_goal_data( valid_countries=valid_countries, country_id=country_id, market_goal_data=market_goal_data_for_update) if not store_in_country_goal_data_is_valid: return store_in_country_goal_data_is_valid hidden_retailers_countries = ( countries.get_countries_with_hidden_retailers()) retailers_are_hidden = ( country_id in hidden_retailers_countries and logic_utils.is_workstation_user(user_id)) if retailers_are_hidden: market_goal_data_for_update = _add_missing_market_goals_data( country_id=country_id, new_market_goals_data=market_goal_data_for_update) create_market_goals_response = _create_or_update_target_market_goal( sales_goal_id=sales_goal_id, country_id=country_id, country_name=country_name, user_id=user_id, market_goal_data=market_goal_data_for_update) if not create_market_goals_response: return create_market_goals_response created_goals = create_market_goals_response.message['created_goals'] total_data = _compose_total_data(created_goals)[0] if retailers_are_hidden: created_goals = logic_utils.compose_market_goals_result( created_goals, hidden_retailers_countries, user_id) stores_map_response = _compose_stores_map( create_market_goals_response.message) if not stores_map_response: return stores_map_response stores_map = stores_map_response.message sales_goal_update_response = sales_goal.update( sales_goal_id=sales_goal_id, values={models.UPDATED_BY: user_id}) if not sales_goal_update_response: return sales_goal_update_response update_data = _compose_update_data(user_id, product_id) if not update_data: return update_data first_week_estimate = None if market_goal_data.get(models.FIRST_WEEK_ESTIMATE): first_week_estimate = market_goal_data.get( models.FIRST_WEEK_ESTIMATE) sales_goals_for_country.upsert_sales_goals_for_country( sales_goal_id, country_id, first_week_estimate ) result = _format_create_target_market_goal_response( created_goals, stores_map, update_data.message, total_data=total_data, first_week_estimate=first_week_estimate ) return response.Response(result) def _compose_stores_map(create_mkt_goals_response_message): """Compose stores mapping. Args: create_mkt_goals_response_message (dict): created target market goals data. Return: Response: dict object representing the stores mapping. """ store_ids = create_mkt_goals_response_message.get('store_ids') stores_map = {} if store_ids: stores = store.fetch_by_store_ids(store_ids) if not stores: return stores stores_map = { st[models.STORE_ID]: st[models.NAME] for st in stores.message} return response.Response(stores_map) def _create_or_update_target_market_goal( sales_goal_id, country_id, country_name, user_id, market_goal_data): """Create or update target market goals in country. Args: sales_goal_id (int): id of sales goal for which target market goals should be created. country_id (int): id of country for which target market goals should be created. country_name(str): name of country. user_id (str): user id. market_goal_data (list): list of market goals data that should be created. Returns: Response: dict object representing target market goal in response body in case of successful creation. Error response in case of failure. Update response if target market goals are updated. """ market_goals_response = target_market_goal.fetch_by_sales_goal_id( sales_goal_id) if market_goals_response.status not in [200, 404]: return market_goals_response market_goals = market_goals_response.message or [] if market_goals and any( item['country_id'] == country_id for item in market_goals): goals_in_country = _prepare_target_mkt_goals_for_update( market_goal_data, country_id, market_goals) goal_data_for_country = {'goals': goals_in_country} update_target_mkt_goals_response = ( _update_target_market_goals_in_country( user_id=user_id, goals_in_country=goals_in_country, goal_data_for_country=goal_data_for_country, existing_target_market_goals=market_goals)) return update_target_mkt_goals_response create_market_goals_response = _create_target_market_goals_for_sales_goal( sales_goal_id=sales_goal_id, country_id=country_id, country_name=country_name, user_id=user_id, market_goals_data=market_goal_data) return create_market_goals_response def _prepare_target_mkt_goals_for_update( market_goal_data, country_id, existing_target_market_goals): """Compose data for update given data designed to create target goals. Args: market_goal_data(list): data for target market goals creation. country_id(int): id of country. existing_target_market_goals(list): target market goals retrieved from database. Returns: List of data composed for update target market goals. """ target_mkt_goals_for_update = [] for target_mkt_goal in market_goal_data: for existing_goal in existing_target_market_goals: target_mkt_goal_store = target_mkt_goal['store'] if (target_mkt_goal_store['store_id'] == existing_goal['store_id'] and existing_goal['country_id'] == country_id): target_mkt_goal['target_market_goal_id'] = existing_goal[ 'target_market_goal_id'] target_mkt_goals_for_update.append(target_mkt_goal) break return target_mkt_goals_for_update def _update_target_market_goals(goal_data_for_country): """Helper function that updates target market goals for sales goal. Args: goal_data_for_country (dict): goal data containing country id, target marketing goals for country, e.g. { "country_id": 1, "goals": [ { "store": { "name": "Target", "store_id": 7 }, "target_market_goal_id": 123, "label_goal_value": 20, "distribution_goal_value": 30 } ], } Returns: Response: successful Response with empty message on success, error response on failure. """ store_ids = [] created_goals = [] for target_marketing_goal_data in ( goal_data_for_country.get(models.GOALS)): valid_target_mkt_goal_keys = [ models.TARGET_MARKET_GOAL_ID, models.LABEL_GOAL_VALUE, models.DISTRIBUTION_GOAL_VALUE] valid_target_marketing_goal_data = { key: target_marketing_goal_data.get(key) for key in valid_target_mkt_goal_keys} target_market_goal_updated = target_market_goal.update( **valid_target_marketing_goal_data) if not target_market_goal_updated: return target_market_goal_updated store_ids.append(target_market_goal_updated.message['store_id']) created_goals.append(target_market_goal_updated.message) result = {'created_goals': created_goals, 'store_ids': store_ids} return response.Response(result) def update_for_product_id( product_id, sales_goal_data, account_type=None, account_id=None, user_id=None): """Logic for partial update of a sales goal for product by product_id. Args: product_id (int): product_id for the goal we want to fetch. sales_goal_data (dict): request body data. account_type (str): a grass header parameter (subaccount|vendor). account_id (str): a grass header parameter. user_id (str): user id from request header. Return: Response: JSON object representing sales goal in response body. """ if account_type and account_id: validated_product = ows_product.get_product_ownership_by_account( account_type=account_type, account_id=account_id, product_id=str(product_id)) if not validated_product: return validated_product sales_goal_to_update = sales_goal.fetch_by_product_id(product_id) if not sales_goal_to_update: return sales_goal_to_update sales_goal_id = sales_goal_to_update.message[models.SALES_GOAL_ID] if sales_goal_data.get(models.TARGET_MARKET_GOALS): existing_target_market_goals = ( target_market_goal.fetch_by_sales_goal_id( sales_goal_id)) if existing_target_market_goals.status not in (200, 404): return existing_target_market_goals for goal_data_for_country in ( sales_goal_data[models.TARGET_MARKET_GOALS]): goals_in_country = goal_data_for_country.get(models.GOALS) if goals_in_country: target_market_goals_updated = ( _update_target_market_goals_in_country( user_id, goals_in_country, goal_data_for_country, existing_target_market_goals.message)) if not target_market_goals_updated: return target_market_goals_updated first_week_estimate = goal_data_for_country.get( models.FIRST_WEEK_ESTIMATE) sales_goals_for_country.upsert_sales_goals_for_country( sales_goal_id, goal_data_for_country.get(models.COUNTRY_ID), first_week_estimate ) new_values = {models.UPDATED_BY: user_id} for field_name in ( models.FIRST_WEEK_ESTIMATE, models.NOTES, models.HMV_NOTES): new_value = sales_goal_data.get(field_name) if new_value is not None: new_values.update({field_name: new_value}) sales_goal_updated = sales_goal.update( sales_goal_id=sales_goal_to_update.message.get('sales_goal_id'), values=new_values) if not sales_goal_updated: return sales_goal_updated sales_goal_data = fetch_by_product_id(product_id, user_id=user_id) return sales_goal_data def _update_target_market_goals_in_country( user_id, goals_in_country, goal_data_for_country, existing_target_market_goals): """Update target market goals in country. Args: user_id (str): user id from request header. goals_in_country(list): list of target market goals for country. goal_data_for_country(dict): target market goals for country. existing_target_market_goals(list): existing target market goals data retrieved from database. Return: Response: contains empty .message on success or .errors on fail. """ changed_values_to_update = _filter_changed_target_market_goals( goals_in_country, existing_target_market_goals) goal_data_for_country['goals'] = changed_values_to_update if user_id is not None: for goal_in_store in changed_values_to_update: editable_for_label_user_response = ( check_access_for_user_to_edit_target_market_goal( goal_in_store.get(models.DISTRIBUTION_GOAL_VALUE), user_id)) if not editable_for_label_user_response: return editable_for_label_user_response target_market_goal_is_updated = _update_target_market_goals( goal_data_for_country) return target_market_goal_is_updated def _filter_changed_target_market_goals( goals_in_country, existing_target_market_goals): """Filter target market goals which were changed. Args: goals_in_country(list): goals data for update. existing_target_market_goals (list): existing target market goals data retrieved from database. Return: list: only changed target market goals in country for update. """ keys_to_check = [models.LABEL_GOAL_VALUE, models.DISTRIBUTION_GOAL_VALUE] for target_mkt_goal in goals_in_country: for existing_goal in existing_target_market_goals: if (existing_goal[models.TARGET_MARKET_GOAL_ID] == target_mkt_goal[models.TARGET_MARKET_GOAL_ID]): goal_to_compare = existing_goal for key in keys_to_check: if target_mkt_goal.get(key) == goal_to_compare.get(key): del target_mkt_goal[key] break goals_to_update = [] for target_mkt_goal in goals_in_country: if any(key in target_mkt_goal for key in keys_to_check): goals_to_update.append(target_mkt_goal) return goals_to_update def check_access_for_user_to_edit_target_market_goal( distribution_goal_value, user_id): """Check if there is no restriction for label to edit distr goal value. Args: distribution_goal_value: new distribution target market goal value. user_id (str): user id from request header. Return: Response: contains empty .message on success or .errors on fail. """ if distribution_goal_value and user_id.startswith( header.WORKSTATION_USER_PREFIX): return response.create_error_response( code=error.ERROR_CODE_PERMISSION_DENIED, message=( error.ACTION_ON_DISTR_VALUE_IS_NOT_ALLOWED_FOR_LABEL .format( user_id=user_id, value=models.DISTRIBUTION_GOAL_VALUE))) return response.Response() def delete_target_market_goal( product_id, country_id, account_type=None, account_id=None, user_id=None): """Logic for deleting a target market goal for given product and country. Args: product_id (int): product_id for target market goal we want to delete. country_id (int): country_id for target market goal we want to delete. account_type (str): a grass header parameter (subaccount|vendor). account_id (str): a grass header parameter. user_id (str): user id from request header. Return: Response: contains empty .message on success or .errors on fail. """ if account_type and account_id: validated_product = ows_product.get_product_ownership_by_account( account_type=account_type, account_id=account_id, product_id=str(product_id)) if not validated_product: return validated_product sales_goal_fetch_response = sales_goal.fetch_by_product_id(product_id) if not sales_goal_fetch_response: return sales_goal_fetch_response sales_goal_id = sales_goal_fetch_response.message['sales_goal_id'] updated_sales_goal = sales_goal.update( sales_goal_id=sales_goal_id, values={models.UPDATED_BY: user_id}) if not updated_sales_goal: return updated_sales_goal update_data = _compose_update_data(user_id, product_id) if not update_data: return update_data deleted_goal = target_market_goal.delete_by_sales_goal_id_and_country_id( sales_goal_id=sales_goal_id, country_id=country_id) if deleted_goal.status not in [200, 404]: return deleted_goal return update_data def _compose_update_data(user_id, product_id): """Compose update data response. Args: user_id (str): the Orchard User Id. product_id (int): product_id for target market goal we want to delete. Return: Response: contains .message with update_data on success or .errors on fail. """ sales_goal_fetch_response = sales_goal.fetch_by_product_id(product_id) if not sales_goal_fetch_response: return sales_goal_fetch_response update_data = { models.UPDATED_BY: user_id, models.UPDATED_AT: sales_goal_fetch_response.message[models.UPDATED_AT] } return response.Response(message=update_data) def _validate_store_in_country_goal_data( valid_countries, country_id, market_goal_data): """Check that store_id in goal data relates to given country. Args: valid_countries (list): list of valid countries with stores related to them. country_id (int): id of country what target market goals should be saved for. market_goal_data (dict): target market goal data for saving. Return: Response: dict object representing the stores mapping. """ stores_in_country = next( country[models.STORES] for country in valid_countries if country[models.COUNTRY_ID] == country_id) for store_data in market_goal_data: store_is_valid_for_country = any( store_data[models.STORE][models.STORE_ID] == valid_store[models.STORE_ID] for valid_store in stores_in_country) if not store_is_valid_for_country: return response.create_not_found_response( message=error.ERROR_MESSAGE_WRONG_STORE_ID_WAS_PROVIDED.format( country_id=country_id)) return response.Response()