Coverage for polar/models/held_balance.py: 95%

41 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-12-05 17:15 +0000

1from typing import TYPE_CHECKING 1ab

2from uuid import UUID 1ab

3 

4from sqlalchemy import ForeignKey, Integer, Uuid 1ab

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

6 

7from polar.kit.db.models import RecordModel 1ab

8 

9if TYPE_CHECKING: 9 ↛ 10line 9 didn't jump to line 10 because the condition on line 9 was never true1ab

10 from polar.models import ( 

11 Account, 

12 IssueReward, 

13 Order, 

14 Organization, 

15 Pledge, 

16 Transaction, 

17 ) 

18 

19 

20class HeldBalance(RecordModel): 1ab

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

30 

31 organization_id: Mapped[UUID | None] = mapped_column( 1ab

32 Uuid, 

33 ForeignKey("organizations.id", ondelete="cascade"), 

34 nullable=True, 

35 index=True, 

36 ) 

37 """ 1ab

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( 1ab

43 Uuid, 

44 ForeignKey("accounts.id", ondelete="cascade"), 

45 nullable=True, 

46 index=True, 

47 ) 

48 """ 1ab

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 1ab

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

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

56 

57 @declared_attr 1ab

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

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

60 

61 payment_transaction_id: Mapped[UUID] = mapped_column( 1ab

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

68 

69 @declared_attr 1ab

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

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

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

73 

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

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

76 

77 pledge_id: Mapped[UUID | None] = mapped_column( 1ab

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

84 

85 @declared_attr 1ab

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

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

88 

89 order_id: Mapped[UUID | None] = mapped_column( 1ab

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

96 

97 @declared_attr 1ab

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

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

100 

101 issue_reward_id: Mapped[UUID | None] = mapped_column( 1ab

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

108 

109 @declared_attr 1ab

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

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