reenroll when failed

This commit is contained in:
2025-09-12 20:16:08 -03:00
parent e51964bc8b
commit 9fb1895557
4 changed files with 25 additions and 15 deletions

View File

@@ -6,8 +6,7 @@ from layercake.strutils import md5_hash
from schemas import Enrollment from schemas import Enrollment
Tenant = TypedDict('Tenant', {'id': str, 'name': str}) Org = TypedDict('Org', {'org_id': str, 'name': str})
CreatedBy = TypedDict('CreatedBy', {'id': str, 'name': str})
DeduplicationWindow = TypedDict('DeduplicationWindow', {'offset_days': int}) DeduplicationWindow = TypedDict('DeduplicationWindow', {'offset_days': int})
Subscription = TypedDict( Subscription = TypedDict(
'Subscription', 'Subscription',
@@ -38,7 +37,7 @@ class DeduplicationConflictError(Exception):
def enroll( def enroll(
enrollment: Enrollment, enrollment: Enrollment,
*, *,
created_by: CreatedBy | None = None, org: Org | None = None,
subscription: Subscription | None = None, subscription: Subscription | None = None,
linked_entities: frozenset[LinkedEntity] = frozenset(), linked_entities: frozenset[LinkedEntity] = frozenset(),
deduplication_window: DeduplicationWindow | None = None, deduplication_window: DeduplicationWindow | None = None,
@@ -56,7 +55,11 @@ def enroll(
'sk': '0', 'sk': '0',
'created_at': now_, 'created_at': now_,
**enrollment.model_dump(), **enrollment.model_dump(),
}, }
| ({'subscription_covered': True} if subscription else {})
| ({'tenant_id': org['org_id']} if org else {}),
# Post-migration: uncomment the following line
# | ({'org_id': org['org_id']} if org else {}),
) )
transact.put( transact.put(
item={ item={
@@ -67,6 +70,8 @@ def enroll(
} }
) )
print(course.model_dump(include={'cert', 'access_period'}))
for entity in linked_entities: for entity in linked_entities:
keyprefix = entity.type.lower() keyprefix = entity.type.lower()
transact.put( transact.put(
@@ -78,17 +83,16 @@ def enroll(
} }
) )
if created_by: if org:
transact.put( transact.put(
item={ item={
'id': enrollment.id, 'id': enrollment.id,
'sk': 'author', # Post-migration: uncomment the following line
# Post-migration: uncomment the following line # 'sk': 'ORG',
# 'sk': 'created_by', 'sk': 'tenant',
'user_id': created_by['id'],
'name': created_by['name'],
'created_at': now_, 'created_at': now_,
}, }
| org
) )
if subscription: if subscription:

View File

@@ -9,9 +9,7 @@ from aws_lambda_powertools.utilities.typing import LambdaContext
from layercake.dynamodb import DynamoDBPersistenceLayer, SortKey, TransactKey from layercake.dynamodb import DynamoDBPersistenceLayer, SortKey, TransactKey
from boto3clients import dynamodb_client from boto3clients import dynamodb_client
from config import ( from config import ENROLLMENT_TABLE
ENROLLMENT_TABLE,
)
from enrollment import LinkedEntity, enroll from enrollment import LinkedEntity, enroll
from schemas import Course, Enrollment, User from schemas import Course, Enrollment, User
@@ -46,6 +44,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
return enroll( return enroll(
enrollment, enrollment,
org=metadata.get('org', None),
subscription=subscription, subscription=subscription,
deduplication_window={'offset_days': metadata['dedup_window_offset_days']}, deduplication_window={'offset_days': metadata['dedup_window_offset_days']},
linked_entities=frozenset({LinkedEntity(new_image['id'], 'ENROLLMENT')}), linked_entities=frozenset({LinkedEntity(new_image['id'], 'ENROLLMENT')}),

View File

@@ -54,6 +54,12 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
}, },
cond_expr='attribute_not_exists(sk)', cond_expr='attribute_not_exists(sk)',
) )
transact.delete(
key=KeyPair(
pk=new_image['id'],
sk='parent_vacancy',
),
)
except Exception: except Exception:
return False return False
else: else:

View File

@@ -33,4 +33,5 @@
{"id": "294e9864-8284-4287-b153-927b15d90900", "sk": "METADATA#SUBSCRIPTION_COVERED", "updated_at": "2025-09-09T15:20:17.187461-03:00", "billing_day": 1, "org_id": "123", "created_at": "2025-09-09T15:20:14.021500-03:00", "billing_period": "START#2025-09-01#END#2025-09-30"} {"id": "294e9864-8284-4287-b153-927b15d90900", "sk": "METADATA#SUBSCRIPTION_COVERED", "updated_at": "2025-09-09T15:20:17.187461-03:00", "billing_day": 1, "org_id": "123", "created_at": "2025-09-09T15:20:14.021500-03:00", "billing_period": "START#2025-09-01#END#2025-09-30"}
{"id": "294e9864-8284-4287-b153-927b15d90900", "sk": "METADATA#COURSE", "access_period": 360, "created_at": "2025-09-09T09:11:29.292760-03:00", "cert": {"exp_interval": 360}} {"id": "294e9864-8284-4287-b153-927b15d90900", "sk": "METADATA#COURSE", "access_period": 360, "created_at": "2025-09-09T09:11:29.292760-03:00", "cert": {"exp_interval": 360}}
{"id": "294e9864-8284-4287-b153-927b15d90900", "sk": "konviva", "class_id": 34, "user_id": 26943, "created_at": "2025-09-09T09:11:29.315247-03:00", "enrollment_id": 244488} {"id": "294e9864-8284-4287-b153-927b15d90900", "sk": "konviva", "class_id": 34, "user_id": 26943, "created_at": "2025-09-09T09:11:29.315247-03:00", "enrollment_id": 244488}
{"id": "294e9864-8284-4287-b153-927b15d90900", "sk": "METADATA#DEDUPLICATION_WINDOW", "offset_days": 90, "created_at": "2025-09-11T09:00:45.923035-03:00"} {"id": "294e9864-8284-4287-b153-927b15d90900", "sk": "METADATA#DEDUPLICATION_WINDOW", "offset_days": 90, "created_at": "2025-09-11T09:00:45.923035-03:00"}
{"id": "294e9864-8284-4287-b153-927b15d90900", "sk": "tenant", "org_id": "123", "name": "EDUSEG", "create_date": "2025-09-12T17:11:00.556907-03:00"}