"""Task to snapshot Snowflake for transactions eligible for mechanical deductions.""" from hooks.royalty_snowflake_hook import RoyaltySnowflakeHook from lib import config from lib.utils import helpers as shared_helpers from lib.utils.ows import get_accounting_period_details from tasks.accounting_period_mechanicals import helpers from templates.accounting_period_mechanicals.snowflake_snapshot_mechanical_transactions\ import insert_snapshot_mechanical_transactions def snowflake_snapshot_mechanical_transactions_task(dag_run: dict, **kwargs) -> None: """Query and snapshot Snowflake for transactions eligible for mechanical deductions. - Finds any transactions in the accounting period's sales files with a 'contract_mechanical_admin' transaction_type (from reference_transaction_type_group_transaction_type) - Sets the 'mechanical_type' ('digital' or 'physical') using the transaction_type - Inserts into SNAPSHOT_MECHANICAL_TRANSACTION without a royalty_amount (which will be calculated downstream by the snowflake_snapshot_mechanical_transaction_details task and inserted by the snowflake_calculate_mech_royalty_amounts task) 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_period_id = event.target_id accounting_period = get_accounting_period_details(accounting_period_id) sales_file_ids = shared_helpers.get_sales_file_ids(accounting_period_id) hook = RoyaltySnowflakeHook(snowflake_conn_id=config.SNOWFLAKE_CONN_NAME) insert_statement = insert_snapshot_mechanical_transactions().render( accounting_period_id=accounting_period_id, sales_file_ids=sales_file_ids, schema=config.OWS_ENV, statement_period_id=accounting_period.get('statement_period_id') ) hook.run(insert_statement, autocommit=True)