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

View File

@@ -9,9 +9,7 @@ from aws_lambda_powertools.utilities.typing import LambdaContext
from layercake.dynamodb import DynamoDBPersistenceLayer, SortKey, TransactKey
from boto3clients import dynamodb_client
from config import (
ENROLLMENT_TABLE,
)
from config import ENROLLMENT_TABLE
from enrollment import LinkedEntity, enroll
from schemas import Course, Enrollment, User
@@ -46,6 +44,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
return enroll(
enrollment,
org=metadata.get('org', None),
subscription=subscription,
deduplication_window={'offset_days': metadata['dedup_window_offset_days']},
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)',
)
transact.delete(
key=KeyPair(
pk=new_image['id'],
sk='parent_vacancy',
),
)
except Exception:
return False
else: