Coverage for polar/transaction/tasks.py: 42%

30 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 12:42 +0000

1import uuid 

2 

3from dramatiq import Retry 

4 

5from polar.exceptions import PolarTaskError 

6from polar.worker import AsyncSessionMaker, CronTrigger, TaskPriority, actor, can_retry 

7 

8from .repository import PaymentTransactionRepository 

9from .service.processor_fee import ( 

10 BalanceTransactionNotFound, 

11) 

12from .service.processor_fee import ( 

13 processor_fee_transaction as processor_fee_transaction_service, 

14) 

15 

16 

17class TransactionTaskError(PolarTaskError): ... 17 ↛ 20line 17 didn't jump to line 20 because

18 

19 

20class PaymentTransactionDoesNotExist(TransactionTaskError): 

21 def __init__(self, payment_transaction_id: uuid.UUID) -> None: 

22 self.payment_transaction_id = payment_transaction_id 

23 message = ( 

24 f"Payment transaction with id {payment_transaction_id} does not exist." 

25 ) 

26 super().__init__(message) 

27 

28 

29@actor( 

30 actor_name="processor_fee.sync_stripe_fees", 

31 cron_trigger=CronTrigger(hour=0, minute=0), 

32 priority=TaskPriority.LOW, 

33) 

34async def sync_stripe_fees() -> None: 

35 async with AsyncSessionMaker() as session: 

36 await processor_fee_transaction_service.sync_stripe_fees(session) 

37 

38 

39@actor(actor_name="processor_fee.create_payment_fees", priority=TaskPriority.LOW) 

40async def create_payment_fees(payment_transaction_id: uuid.UUID) -> None: 

41 async with AsyncSessionMaker() as session: 

42 repository = PaymentTransactionRepository.from_session(session) 

43 payment_transaction = await repository.get_by_id(payment_transaction_id) 

44 if payment_transaction is None: 

45 raise PaymentTransactionDoesNotExist(payment_transaction_id) 

46 

47 try: 

48 await processor_fee_transaction_service.create_payment_fees( 

49 session, payment_transaction=payment_transaction 

50 ) 

51 except BalanceTransactionNotFound as e: 

52 # Retry because Stripe may have not created the balance transaction yet 

53 if can_retry(): 

54 raise Retry() from e 

55 # Raise the exception to be notified about it 

56 else: 

57 raise