"""Lambda fan-preference-sforce-updates function module.""" import logging import sentry_sdk import config from lambdacommon.common_config import logger from sentry_sdk import capture_exception from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration from sentry_sdk.integrations.logging import LoggingIntegration from src.connectors.kafka_producer import produce_for_id from src.logic.msg_handlers import read_source_event from src.logic.msg_handlers import get_operation_type from src.logic.msg_handlers import prepare_message IGNORED_MESSAGE = 'Message ignored for lack of `crmId` value.' SUCCESS_MESSAGE = 'Successfully sent {operation} message for crmId:{crm_id}' logging_integration = LoggingIntegration( level=logging.INFO, event_level=logging.CRITICAL ) sentry_sdk.init( config.SENTRY_DSN, integrations=[AwsLambdaIntegration(), logging_integration] ) def handler(event, context): """Lambda entry point.""" try: msk_event = read_source_event(event) # Grab crmId crm_id = msk_event.get('crmId') # Ignore messages without crmID if not crm_id: logger.info(IGNORED_MESSAGE) return {'status': 'IGNORED', 'message': IGNORED_MESSAGE} operation = get_operation_type(msk_event) message = prepare_message(msk_event, operation).to_dict() # Send Update to Kafka topic produce_for_id(message, crm_id) logger.info(SUCCESS_MESSAGE.format(operation=operation, crm_id=crm_id)) return { 'status': 'OK', 'message': SUCCESS_MESSAGE.format(operation=operation, crm_id=crm_id) } except Exception as e: logger.exception(str(e)) capture_exception(e) raise e