Coverage for polar/checkout/tasks.py: 50%

24 statements  

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

1import uuid 1a

2 

3from polar.exceptions import PolarTaskError 1a

4from polar.worker import AsyncSessionMaker, CronTrigger, TaskPriority, actor 1a

5 

6from .repository import CheckoutRepository 1a

7from .service import checkout as checkout_service 1a

8 

9 

10class CheckoutTaskError(PolarTaskError): ... 1a

11 

12 

13class CheckoutDoesNotExist(CheckoutTaskError): 1a

14 def __init__(self, checkout_id: uuid.UUID) -> None: 1a

15 self.checkout_id = checkout_id 

16 message = f"The checkout with id {checkout_id} does not exist." 

17 super().__init__(message) 

18 

19 

20@actor(actor_name="checkout.handle_free_success", priority=TaskPriority.HIGH) 1a

21async def handle_free_success(checkout_id: uuid.UUID) -> None: 1a

22 async with AsyncSessionMaker() as session: 

23 repository = CheckoutRepository.from_session(session) 

24 checkout = await repository.get_by_id( 

25 checkout_id, options=repository.get_eager_options() 

26 ) 

27 if checkout is None: 

28 raise CheckoutDoesNotExist(checkout_id) 

29 await checkout_service.handle_success(session, checkout) 

30 

31 

32@actor( 1a

33 actor_name="checkout.expire_open_checkouts", 

34 cron_trigger=CronTrigger.from_crontab("0,15,30,45 * * * *"), 

35 priority=TaskPriority.LOW, 

36) 

37async def expire_open_checkouts() -> None: 1a

38 async with AsyncSessionMaker() as session: 

39 repository = CheckoutRepository.from_session(session) 

40 await repository.expire_open_checkouts()