update
This commit is contained in:
@@ -5,7 +5,6 @@ from uuid import uuid4
|
||||
|
||||
from layercake.dateutils import now, ttl
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair, TransactItems
|
||||
from layercake.strutils import md5_hash
|
||||
|
||||
from conf import ORDER_TABLE
|
||||
from models import Course, Enrollment
|
||||
@@ -30,7 +29,7 @@ class Rel(TypedDict):
|
||||
|
||||
|
||||
class LifecycleEvents(str, Enum):
|
||||
"""Schedules lifecycle events."""
|
||||
"""Lifecycle events related to scheduling actions."""
|
||||
|
||||
# Reminder if the user does not access within 3 days
|
||||
REMINDER_NO_ACCESS_3_DAYS = 'schedules#reminder_no_access_3_days'
|
||||
@@ -38,11 +37,11 @@ class LifecycleEvents(str, Enum):
|
||||
# When there is no activity 7 days after the first access
|
||||
NO_ACTIVITY_7_DAYS = 'schedules#no_activity_7_days'
|
||||
|
||||
# When the access period expires
|
||||
ACCESS_PERIOD_EXPIRED = 'schedules#access_period_expired'
|
||||
# Reminder 30 days before the access period expires
|
||||
ACCESS_PERIOD_REMINDER_30_DAYS = 'schedules#access_period_reminder_30_days'
|
||||
|
||||
# When the course certificate expires
|
||||
CERTIFICATE_EXPIRATION = 'schedules#certificate_expiration'
|
||||
# Reminder for certificate expiration set to 30 days from now
|
||||
CERT_EXPIRATION_REMINDER_30_DAYS = 'schedules#cert_expiration_reminder_30_days'
|
||||
|
||||
# Archive the course after the certificate expires
|
||||
COURSE_ARCHIVED = 'schedules#course_archived'
|
||||
@@ -52,19 +51,12 @@ def enroll(
|
||||
enrollment: Enrollment,
|
||||
*,
|
||||
tenant: Tenant,
|
||||
rel: tuple[Rel, ...] | Rel = (),
|
||||
author: Author | None = None,
|
||||
vacancy: Vacancy | None = None,
|
||||
ensure_vacancy: bool = True,
|
||||
persistence_layer: DynamoDBPersistenceLayer,
|
||||
) -> bool:
|
||||
"""Enrolls a user into a course and schedules lifecycle events."""
|
||||
now_ = now()
|
||||
user = enrollment.user
|
||||
course = enrollment.course
|
||||
exp_interval = course.exp_interval
|
||||
lock_hash = md5_hash('%s%s' % (user.id, course.id))
|
||||
ttl_date = now_ + timedelta(days=exp_interval - 30)
|
||||
|
||||
transact = TransactItems(persistence_layer.table_name)
|
||||
transact.put(
|
||||
@@ -72,6 +64,7 @@ def enroll(
|
||||
'sk': '0',
|
||||
'create_date': now_,
|
||||
'metadata__tenant_id': tenant['id'],
|
||||
'metadata__related_ids': {tenant['id'], user.id},
|
||||
**enrollment.model_dump(),
|
||||
},
|
||||
)
|
||||
@@ -79,19 +72,11 @@ def enroll(
|
||||
item={
|
||||
'id': enrollment.id,
|
||||
'sk': 'metadata#tenant',
|
||||
'org_id': tenant['id'],
|
||||
'tenant_id': f'ORG#{tenant["id"]}',
|
||||
'name': tenant['name'],
|
||||
'create_date': now_,
|
||||
},
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
'id': enrollment.id,
|
||||
'sk': LifecycleEvents.COURSE_ARCHIVED,
|
||||
'create_date': now_,
|
||||
'ttl': ttl(days=exp_interval, start_dt=now_),
|
||||
},
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
'id': enrollment.id,
|
||||
@@ -106,7 +91,7 @@ def enroll(
|
||||
transact.put(
|
||||
item={
|
||||
'id': enrollment.id,
|
||||
'sk': LifecycleEvents.ACCESS_PERIOD_EXPIRED,
|
||||
'sk': LifecycleEvents.ACCESS_PERIOD_REMINDER_30_DAYS,
|
||||
'name': user.name,
|
||||
'email': user.email,
|
||||
'course': course.name,
|
||||
@@ -115,75 +100,6 @@ def enroll(
|
||||
},
|
||||
)
|
||||
|
||||
for r in rel:
|
||||
print(r['id'])
|
||||
transact.put(
|
||||
item={
|
||||
'id': enrollment.id,
|
||||
# 'sk': 'rel#{}' % r['id'],
|
||||
'create_date': now_,
|
||||
},
|
||||
)
|
||||
|
||||
if author:
|
||||
transact.put(
|
||||
item={
|
||||
'id': enrollment.id,
|
||||
'sk': 'metadata#author',
|
||||
'user_id': author['id'],
|
||||
'name': author['name'],
|
||||
'create_date': now_,
|
||||
},
|
||||
)
|
||||
|
||||
if vacancy:
|
||||
transact.put(
|
||||
item={
|
||||
'id': enrollment.id,
|
||||
'sk': 'parent_vacancy',
|
||||
# 'vacancy': vacancy.model_dump(),
|
||||
}
|
||||
)
|
||||
|
||||
if ensure_vacancy:
|
||||
# Ensures that there's a vacancy
|
||||
transact.delete(
|
||||
key=vacancy.model_dump(),
|
||||
cond_expr='attribute_exists(sk)',
|
||||
)
|
||||
|
||||
# Add cancel policy if there is a vacancy
|
||||
if vacancy:
|
||||
transact.put(
|
||||
item={
|
||||
'id': enrollment.id,
|
||||
'sk': 'metadata#cancel_policy',
|
||||
'create_date': now_,
|
||||
}
|
||||
)
|
||||
|
||||
# To ensure that the user does not enroll in the same course again until
|
||||
# the certificate expires.
|
||||
transact.put(
|
||||
item={
|
||||
'id': 'metadata#lock',
|
||||
'sk': lock_hash,
|
||||
'enrollment_id': enrollment.id,
|
||||
'create_date': vacancy,
|
||||
'ttl': ttl(start_dt=ttl_date),
|
||||
},
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
'id': enrollment.id,
|
||||
'sk': 'lock',
|
||||
'hash': lock_hash,
|
||||
'create_date': vacancy,
|
||||
'ttl': ttl(start_dt=ttl_date),
|
||||
},
|
||||
)
|
||||
|
||||
return persistence_layer.transact_write_items(transact)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user