149 lines
4.6 KiB
Python
149 lines
4.6 KiB
Python
from typing import NotRequired, Self, TypedDict
|
|
|
|
from layercake.dateutils import now, ttl
|
|
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
|
|
from layercake.strutils import md5_hash
|
|
|
|
from schemas import Enrollment
|
|
|
|
Tenant = TypedDict('Tenant', {'id': str, 'name': str})
|
|
CreatedBy = TypedDict('CreatedBy', {'id': str, 'name': str})
|
|
DeduplicationWindow = TypedDict('DeduplicationWindow', {'offset_days': int})
|
|
Subscription = TypedDict(
|
|
'Subscription',
|
|
{
|
|
'org_id': str,
|
|
'billing_day': int,
|
|
'billing_period': NotRequired[str],
|
|
},
|
|
)
|
|
|
|
|
|
class LinkedEntity(str):
|
|
def __new__(cls, id: str, type: str) -> Self:
|
|
return super().__new__(cls, '#'.join([type.lower(), id]))
|
|
|
|
def __init__(self, id: str, type: str) -> None:
|
|
# __init__ is used to store the parameters for later reference.
|
|
# For immutable types like str, __init__ cannot change the instance's value.
|
|
self.id = id
|
|
self.type = type
|
|
|
|
|
|
class DeduplicationConflictError(Exception):
|
|
def __init__(self, *args):
|
|
super().__init__('Enrollment already exists')
|
|
|
|
|
|
def enroll(
|
|
enrollment: Enrollment,
|
|
*,
|
|
created_by: CreatedBy | None = None,
|
|
subscription: Subscription | None = None,
|
|
linked_entities: frozenset[LinkedEntity] = frozenset(),
|
|
deduplication_window: DeduplicationWindow | None = None,
|
|
persistence_layer: DynamoDBPersistenceLayer,
|
|
) -> bool:
|
|
"""Enrolls a user into a course and schedules lifecycle events."""
|
|
now_ = now()
|
|
user = enrollment.user
|
|
course = enrollment.course
|
|
lock_hash = md5_hash('%s%s' % (user.id, course.id))
|
|
|
|
with persistence_layer.transact_writer() as transact:
|
|
transact.put(
|
|
item={
|
|
'sk': '0',
|
|
'created_at': now_,
|
|
**enrollment.model_dump(),
|
|
},
|
|
)
|
|
transact.put(
|
|
item={
|
|
'id': enrollment.id,
|
|
'sk': 'METADATA#COURSE',
|
|
'created_at': now_,
|
|
**course.model_dump(include={'cert', 'access_period'}),
|
|
}
|
|
)
|
|
|
|
for entity in linked_entities:
|
|
keyprefix = entity.type.lower()
|
|
transact.put(
|
|
item={
|
|
'id': enrollment.id,
|
|
'sk': f'LINKED_ENTITIES#{entity.type}',
|
|
'created_at': now_,
|
|
f'{keyprefix}_id': entity.id,
|
|
}
|
|
)
|
|
|
|
if created_by:
|
|
transact.put(
|
|
item={
|
|
'id': enrollment.id,
|
|
'sk': 'author',
|
|
# Post-migration: uncomment the following line
|
|
# 'sk': 'created_by',
|
|
'user_id': created_by['id'],
|
|
'name': created_by['name'],
|
|
'created_at': now_,
|
|
},
|
|
)
|
|
|
|
if subscription:
|
|
transact.put(
|
|
item={
|
|
'id': enrollment.id,
|
|
'sk': 'METADATA#SUBSCRIPTION_COVERED',
|
|
'created_at': now_,
|
|
}
|
|
| subscription,
|
|
)
|
|
|
|
# Prevents the user from enrolling in the same course again until
|
|
# the deduplication window expires or is removed.
|
|
if deduplication_window:
|
|
offset_days = int(deduplication_window['offset_days'])
|
|
ttl_ = ttl(
|
|
start_dt=now_,
|
|
days=course.access_period - offset_days,
|
|
)
|
|
transact.put(
|
|
item={
|
|
'id': 'LOCK',
|
|
'sk': lock_hash,
|
|
'enrollment_id': enrollment.id,
|
|
'created_at': now_,
|
|
'ttl': ttl_,
|
|
},
|
|
cond_expr='attribute_not_exists(sk)',
|
|
exc_cls=DeduplicationConflictError,
|
|
)
|
|
transact.put(
|
|
item={
|
|
'id': enrollment.id,
|
|
'sk': 'LOCK',
|
|
'hash': lock_hash,
|
|
'created_at': now_,
|
|
'ttl': ttl_,
|
|
},
|
|
)
|
|
# Deduplication window can be recalculated if needed
|
|
transact.put(
|
|
item={
|
|
'id': enrollment.id,
|
|
'sk': 'METADATA#DEDUPLICATION_WINDOW',
|
|
'offset_days': offset_days,
|
|
'created_at': now_,
|
|
},
|
|
)
|
|
else:
|
|
transact.condition(
|
|
key=KeyPair('LOCK', lock_hash),
|
|
cond_expr='attribute_not_exists(sk)',
|
|
exc_cls=DeduplicationConflictError,
|
|
)
|
|
|
|
return True
|