"""Model for work with salessheets daemon.""" import json from botocore.exceptions import ClientError from oto import response from sentry_sdk import capture_exception from salessheets import config from salessheets.connectors import sqs from salessheets.connectors.sentry import sentry_client from salessheets.constants import daemon from salessheets.constants import error from salessheets.constants import salessheets def put_job_to_sqs( job_id, product_ids, generation_method, correlation_id, user_id, template_type=None, localized_template_type_id=None): """Put message to SQS queue. Args: job_id (str): id of job from db. product_ids (list): ids for which salesshets should be generated. generation_method (str): output format for daemon sales sheets. correlation_id (str): correlation_id from request header. user_id (str): the user id. template_type (str): template_type from request body. localized_template_type_id (int): id of localized template type, corresponds to the id in salessheets.template_detail table. Returns: oto.Response: response with error or job id. """ sqs_queue = sqs.get_queue(config.SALESSHEETS_SQS_QUEUE) conn = sqs.get_connection() if not sqs_queue: return response.create_not_found_response( error.ERROR_MESSAGE_WRONG_SQS_QUEUE) payload = { daemon.PRODUCT_ID: product_ids, daemon.OUTPUT_TYPE: generation_method, daemon.JOB_ID: job_id } if template_type: payload[salessheets.TEMPLATE_TYPE] = template_type if localized_template_type_id: try: payload[salessheets.LOCALIZED_TEMPLATE_TYPE_ID] = ( int(localized_template_type_id)) except ValueError as e: capture_exception(e) return response.create_error_response( code=error.ERROR_CODE_WRONG_LOCALIZED_TEMPLATE_TYPE_ID, message=error.ERROR_MSG_WRONG_LOCALIZED_TEMPLATE_TYPE_ID ) message = json.dumps(payload) message_attributes = {} # generate correlation_id for message correlation_id_attr = { daemon.CORRELATION_ID: { daemon.DATA_TYPE: daemon.STRING_DATA_TYPE, daemon.STRING_VALUE: correlation_id } } message_attributes.update(correlation_id_attr) if user_id: feature_flag_user_context_attr = { salessheets.FEATURE_FLAG_USER_CONTEXT: { daemon.DATA_TYPE: daemon.STRING_DATA_TYPE, daemon.STRING_VALUE: user_id } } message_attributes.update(feature_flag_user_context_attr) try: conn.send_message( QueueUrl=sqs_queue, MessageBody=message, MessageAttributes=message_attributes) except ClientError as e: capture_exception(e) return response.create_fatal_response(e.args) return response.Response(job_id)