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

1from typing import TYPE_CHECKING 

2from uuid import UUID 

3 

4from sqlalchemy import ForeignKey, Integer, Uuid 

5from sqlalchemy.orm import Mapped, declared_attr, mapped_column, relationship 

6 

7from polar.kit.db.models import RecordModel 

8 

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 ) 

18 

19 

20class HeldBalance(RecordModel): 

21 """ 

22 Represent an on hold balance. 

23 

24 It may happen because the destination account is not yet created. 

25 

26 When the account is successfully created, those balances should be executed. 

27 """ 

28 

29 __tablename__ = "held_balances" 

30 

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 """ 

41 

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 """ 

52 

53 @declared_attr 

54 def organization(cls) -> Mapped["Organization | None"]: 

55 return relationship("Organization", lazy="raise") 

56 

57 @declared_attr 

58 def account(cls) -> Mapped["Account | None"]: 

59 return relationship("Account", lazy="raise") 

60 

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.""" 

68 

69 @declared_attr 

70 def payment_transaction(cls) -> Mapped["Transaction"]: 

71 """Transaction that pays for this balance.""" 

72 return relationship("Transaction", lazy="raise") 

73 

74 amount: Mapped[int] = mapped_column(Integer, nullable=False) 

75 """Amount in cents to balance.""" 

76 

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.""" 

84 

85 @declared_attr 

86 def pledge(cls) -> Mapped["Pledge | None"]: 

87 return relationship("Pledge", lazy="raise") 

88 

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.""" 

96 

97 @declared_attr 

98 def order(cls) -> Mapped["Order | None"]: 

99 return relationship("Order", lazy="raise") 

100 

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.""" 

108 

109 @declared_attr 

110 def issue_reward(cls) -> Mapped["IssueReward | None"]: 

111 return relationship("IssueReward", lazy="raise")