Skip to content

Policies

components.payment_gateway.subcomponents.transfers.business_logic.policies.helpers

record_ledger_entry_for_transfer_event

record_ledger_entry_for_transfer_event(
    session, /, ledger_actions, ledger_id, transfer_event
)

Record a new ledger entry matching the balance change of the provided transfer event.

Does nothing if the balance change is zero.

Note

The function makes no assumption about multiple ledger entries sharing the same external_id, deduplication is the caller's responsibility to ensure there is no double accounting of the same event.

Parameters:

Name Type Description Default
ledger_actions LedgerActions

The ledger actions instance to use.

required
ledger_id LedgerId

The ID of the ledger to record the entry in.

required
transfer_event TransferEvent

The transfer event to record the ledger entry for.

required
Source code in components/payment_gateway/subcomponents/transfers/business_logic/policies/helpers.py
def record_ledger_entry_for_transfer_event(
    session: Session,
    /,
    ledger_actions: LedgerActions,
    ledger_id: LedgerId,
    transfer_event: TransferEvent,
) -> None:
    """Record a new ledger entry matching the balance change of the provided
    transfer event.

    Does nothing if the balance change is zero.

    Note:
        The function makes no assumption about multiple ledger entries sharing
        the same external_id, deduplication is the caller's responsibility to
        ensure there is no double accounting of the same event.

    Args:
        ledger_actions: The ledger actions instance to use.
        ledger_id: The ID of the ledger to record the entry in.
        transfer_event: The transfer event to record the ledger entry for.
    """
    balance_change = calculate_amount_from_events(
        [transfer_event_model_to_dataclass(transfer_event)],
        including_received=True,
    )
    if balance_change == 0:
        # Don't record a ledger entry if there is no balance change
        return

    ledger_actions.record_entry(
        session,
        id=ledger_id,
        amount=balance_change,
        occurred_at=transfer_event.effective_date,
        external_transaction_id=transfer_event.external_id,
    )