"""Task to match contract terms to sales and insert into snowflake staging table.""" from hooks.royalty_snowflake_hook import RoyaltySnowflakeHook from lib import config from tasks.accounting_run_calculate import helpers from templates.accounting_run_calculate.snowflake_match_contract_terms \ import insert_contract_transaction_staging_distro_template def match_contract_terms_to_sales_task(dag_run: dict, **kwargs) -> None: """Match denormalized contract attachments and term conditions to sales. Contract attachments are label_ids, UPCs, ISRCs, etc. Contract term conditions are countries, stores, transaction_types Args: dag_run (dict): config of the DAG this task belongs to kwargs (dict): any other optional arguments """ event = helpers.get_event_from_params(dag_run, **kwargs) accounting_run_id = event.target_id accounting_period, _ = helpers.get_event_records(accounting_run_id) accounting_period_id = accounting_period.get('accounting_period_id') sales_file_ids = helpers.get_sales_file_ids(accounting_period_id) insert_template = insert_contract_transaction_staging_distro_template() hook = RoyaltySnowflakeHook(snowflake_conn_id=config.SNOWFLAKE_CONN_NAME) insert_statement = insert_template.render( accounting_run_id=accounting_run_id, sales_file_ids=sales_file_ids, schema=config.OWS_ENV ) hook.run(insert_statement, autocommit=True)