Coverage for polar/models/held_balance.py: 95%
41 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 12:42 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 12:42 +0000
1from typing import TYPE_CHECKING
2from uuid import UUID
4from sqlalchemy import ForeignKey, Integer, Uuid
5from sqlalchemy.orm import Mapped, declared_attr, mapped_column, relationship
7from polar.kit.db.models import RecordModel
9if TYPE_CHECKING: 9 ↛ 10line 9 didn't jump to line 10 because the condition on line 9 was never true
10 from polar.models import (
11 Account,
12 IssueReward,
13 Order,
14 Organization,
15 Pledge,
16 Transaction,
17 )
20class HeldBalance(RecordModel):
21 """
22 Represent an on hold balance.
24 It may happen because the destination account is not yet created.
26 When the account is successfully created, those balances should be executed.
27 """
29 __tablename__ = "held_balances"
31 organization_id: Mapped[UUID | None] = mapped_column(
32 Uuid,
33 ForeignKey("organizations.id", ondelete="cascade"),
34 nullable=True,
35 index=True,
36 )
37 """
38 ID of the `Organization` concerned by this balance.
39 Set only if the account is not yet created.
40 """
42 account_id: Mapped[UUID | None] = mapped_column(
43 Uuid,
44 ForeignKey("accounts.id", ondelete="cascade"),
45 nullable=True,
46 index=True,
47 )
48 """
49 ID of the `Account` concerned by this balance.
50 Will be `None` if the account is not yet created.
51 """
53 @declared_attr
54 def organization(cls) -> Mapped["Organization | None"]:
55 return relationship("Organization", lazy="raise")
57 @declared_attr
58 def account(cls) -> Mapped["Account | None"]:
59 return relationship("Account", lazy="raise")
61 payment_transaction_id: Mapped[UUID] = mapped_column(
62 Uuid,
63 ForeignKey("transactions.id", ondelete="cascade"),
64 nullable=False,
65 index=True,
66 )
67 """ID of the transaction that pays for this balance."""
69 @declared_attr
70 def payment_transaction(cls) -> Mapped["Transaction"]:
71 """Transaction that pays for this balance."""
72 return relationship("Transaction", lazy="raise")
74 amount: Mapped[int] = mapped_column(Integer, nullable=False)
75 """Amount in cents to balance."""
77 pledge_id: Mapped[UUID | None] = mapped_column(
78 Uuid,
79 ForeignKey("pledges.id", ondelete="set null"),
80 nullable=True,
81 index=True,
82 )
83 """ID of the `Pledge` related to this balance."""
85 @declared_attr
86 def pledge(cls) -> Mapped["Pledge | None"]:
87 return relationship("Pledge", lazy="raise")
89 order_id: Mapped[UUID | None] = mapped_column(
90 Uuid,
91 ForeignKey("orders.id", ondelete="set null"),
92 nullable=True,
93 index=True,
94 )
95 """ID of the `Order` related to this balance."""
97 @declared_attr
98 def order(cls) -> Mapped["Order | None"]:
99 return relationship("Order", lazy="raise")
101 issue_reward_id: Mapped[UUID | None] = mapped_column(
102 Uuid,
103 ForeignKey("issue_rewards.id", ondelete="set null"),
104 nullable=True,
105 index=True,
106 )
107 """ID of the `IssueReward` related to this balance."""
109 @declared_attr
110 def issue_reward(cls) -> Mapped["IssueReward | None"]:
111 return relationship("IssueReward", lazy="raise")