add test mode to enrollments

This commit is contained in:
2026-01-28 19:11:52 -03:00
parent fc14d425f2
commit 2b34caa3be
14 changed files with 65 additions and 260 deletions

View File

@@ -28,6 +28,7 @@ from config import (
)
from exceptions import (
ConflictError,
NotAcceptableError,
OrderNotFoundError,
SubscriptionConflictError,
SubscriptionFrozenError,
@@ -47,6 +48,9 @@ class DeduplicationConflictError(ConflictError): ...
class SeatNotFoundError(NotFoundError): ...
class TestModeRequiredError(NotAcceptableError): ...
class User(BaseModel):
id: str | UUID4
name: NameStr
@@ -91,6 +95,7 @@ def enroll(
org_id: Annotated[str | UUID4, Body(embed=True)],
enrollments: Annotated[tuple[Enrollment, ...], Body(embed=True)],
subscription: Annotated[Subscription | None, Body(embed=True)] = None,
test_mode: Annotated[bool, Body(embed=True)] = False,
):
now_ = now()
created_by: Authenticated = router.context['user']
@@ -106,6 +111,7 @@ def enroll(
'org': Org.model_validate(org),
'created_by': created_by,
'subscription': subscription,
'test_mode': test_mode,
}
immediate = [e for e in enrollments if not e.scheduled_for]
@@ -125,12 +131,13 @@ def enroll(
'cause': r.cause,
}
expires_after_days = 7 if test_mode else 30 * 3
item = {
'id': f'SUBMISSION#ORG#{org_id}',
'sk': now_,
'enrolled': list(map(fmt, now_out)) if now_out else None,
'scheduled': list(map(fmt, later_out)) if later_out else None,
'ttl': ttl(start_dt=now_, days=30 * 3),
'ttl': ttl(start_dt=now_, days=expires_after_days),
'created_by': {
'id': created_by.id,
'name': created_by.name,
@@ -151,6 +158,7 @@ Context = TypedDict(
'org': Org,
'created_by': Authenticated,
'subscription': NotRequired[Subscription],
'test_mode': NotRequired[bool],
},
)
@@ -161,6 +169,7 @@ def enroll_now(enrollment: Enrollment, context: Context):
course = enrollment.course
seat = enrollment.seat
org = context['org']
test_mode = context.get('test_mode')
subscription = context.get('subscription')
created_by = context['created_by']
lock_hash = md5_hash(f'{user.id}{course.id}')
@@ -194,7 +203,16 @@ def enroll_now(enrollment: Enrollment, context: Context):
'created_at': now_,
}
| ({'subscription_covered': True} if subscription else {})
| ({'is_test': True} if test_mode else {})
)
if test_mode:
transact.condition(
key=KeyPair(str(org.id), 'METADATA#TEST_MODE'),
cond_expr='attribute_exists(sk)',
exc_cls=TestModeRequiredError,
)
transact.put(
item={
'id': enrollment.id,
@@ -344,6 +362,7 @@ def _enroll_later(enrollment: Enrollment, context: Context):
scheduled_for = _date_to_midnight(enrollment.scheduled_for) # type: ignore
dedup_window = enrollment.deduplication_window
org = context['org']
test_mode = context.get('test_mode')
subscription = context.get('subscription')
created_by = context['created_by']
lock_hash = md5_hash(f'{user.id}{course.id}')
@@ -371,6 +390,7 @@ def _enroll_later(enrollment: Enrollment, context: Context):
'scheduled_at': now_,
}
| ({'seat': seat.model_dump()} if seat else {})
| ({'is_test': True} if test_mode else {})
| (
{'dedup_window_offset_days': dedup_window.offset_days}
if dedup_window
@@ -385,6 +405,13 @@ def _enroll_later(enrollment: Enrollment, context: Context):
),
)
if test_mode:
transact.condition(
key=KeyPair(str(org.id), 'METADATA#TEST_MODE'),
cond_expr='attribute_exists(sk)',
exc_cls=TestModeRequiredError,
)
if seat:
transact.condition(
key=KeyPair(str(seat.order_id), '0'),