update events

This commit is contained in:
2025-09-18 13:45:42 -03:00
parent db63dfa64d
commit a83d4b56aa
18 changed files with 109 additions and 181 deletions

View File

@@ -64,13 +64,13 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
return order_layer.update_item(
key=KeyPair(new_image['id'], new_image['sk']),
update_expr='SET #status = :status, updated_at = :updated_at',
update_expr='SET #status = :status, updated_at = :now',
expr_attr_names={
'#status': 'status',
},
expr_attr_values={
':status': 'SUCCESS',
':updated_at': now_,
':now': now_,
},
cond_expr='attribute_exists(sk)',
)

View File

@@ -29,6 +29,7 @@ def send_email(
) -> bool:
now_ = now()
name, _ = to
event_name = event['sk']
emailmsg = Message(
from_=sender,
to=to,
@@ -54,7 +55,7 @@ def send_email(
dynamodb_persistence_layer.put_item(
item={
'id': event['id'],
'sk': f'{event["sk"]}#EXECUTED',
'sk': f'{event_name}#EXECUTED',
'created_at': now_,
}
)
@@ -65,7 +66,7 @@ def send_email(
dynamodb_persistence_layer.put_item(
item={
'id': event['id'],
'sk': f'{event["sk"]}#FAILED',
'sk': f'{event_name}#FAILED',
'created_at': now_,
}
)

View File

@@ -62,7 +62,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
},
) as fp:
result = fp.process()
logger.info('Processed courses', result=result)
logger.debug('Processed courses', result=result)
return order_layer.update_item(
key=KeyPair(new_image['id'], new_image['sk']),

View File

@@ -33,6 +33,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
)
if 'cert' not in course:
logger.debug('Certificate not found')
# There is no certificate to issue from metadata
return False
@@ -73,7 +74,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
logger.exception(exc)
raise
logger.info(f'PDF uploaded successfully to {s3_uri}')
logger.debug(f'PDF uploaded successfully to {s3_uri}')
return enrollment_layer.update_item(
key=KeyPair(

View File

@@ -39,8 +39,12 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
)
user = User.model_validate(new_image['user'])
course = Course.model_validate(new_image['course'] | metadata['course'])
enrollment = Enrollment(id=uuid4(), course=course, user=user)
subscription = metadata['subscription'] if 'subscription' in metadata else None
enrollment = Enrollment(
id=uuid4(),
course=course,
user=user,
)
return enroll(
enrollment,

View File

@@ -27,6 +27,8 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
enrollment_id = new_image['id']
user = new_image['user']
course_name = glom(new_image, 'course.name')
# Post-migration: after removing the stopgap file `patch_course_metadata.py`,
# use `access_expires_at` from `new_image`
access_period = int(
dyn.collection.get_item(
KeyPair(
@@ -49,20 +51,16 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
},
)
# By default, the enrollment will expire when the access period ends
# (scheduled below).
# If the enrollment is completed earlier (e.g., certificate issued),
# the expiration schedule is canceled and an archive schedule
# (`SCHEDULE#SET_AS_ARCHIVED`) is created instead.
# Schedule the event to add `access_expired` after the access period ends
transact.put(
item={
'id': enrollment_id,
'sk': 'SCHEDULE#SET_AS_EXPIRED',
'sk': 'SCHEDULE#SET_ACCESS_EXPIRED',
'created_at': now_,
'ttl': ttl(start_dt=now_, days=access_period),
},
)
transact.put(
item={
'id': enrollment_id,

View File

@@ -19,8 +19,6 @@ dyn = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
@event_source(data_class=EventBridgeEvent)
@logger.inject_lambda_context
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
"""If there is no certificate and the access period has ended,
the enrollment will be marked as expired."""
old_image = event.detail['old_image']
now_ = now()
@@ -31,30 +29,17 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
pk=old_image['id'],
sk='0',
),
update_expr='SET #status = :expired, updated_at = :updated_at',
cond_expr='#status = :in_progress OR #status = :pending',
expr_attr_names={
'#status': 'status',
},
update_expr='SET access_expired = :true, \
updated_at = :now',
expr_attr_values={
':pending': 'PENDING',
':in_progress': 'IN_PROGRESS',
':expired': 'EXPIRED',
':updated_at': now_,
':true': True,
':now': now_,
},
)
transact.put(
item={
'id': old_image['id'],
'sk': 'EXPIRED',
'expired_at': now_,
},
cond_expr='attribute_not_exists(sk)',
)
transact.put(
item={
'id': old_image['id'],
'sk': 'SCHEDULE#SET_AS_EXPIRED#EXECUTED',
'sk': 'SCHEDULE#SET_ACCESS_EXPIRED#EXECUTED',
'created_at': now_,
},
)
@@ -64,7 +49,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
dyn.put_item(
item={
'id': old_image['id'],
'sk': 'SCHEDULE#SET_AS_EXPIRED#FAILED',
'sk': 'SCHEDULE#SET_ACCESS_EXPIRED#FAILED',
'reason': str(exc),
'created_at': now_,
},

View File

@@ -5,7 +5,7 @@ from aws_lambda_powertools.utilities.data_classes import (
)
from aws_lambda_powertools.utilities.typing import LambdaContext
from layercake.dateutils import now
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair, SortKey
from boto3clients import dynamodb_client
from config import (
@@ -19,9 +19,16 @@ dyn = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
@event_source(data_class=EventBridgeEvent)
@logger.inject_lambda_context
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
"""After the certificate expires, the enrollment will be marked as archived."""
old_image = event.detail['old_image']
now_ = now()
issued_cert = dyn.collection.get_item(
KeyPair(
pk=old_image['id'],
sk=SortKey('0', path_spec='issued_cert'),
),
raise_on_error=False,
default={},
)
try:
with dyn.transact_writer() as transact:
@@ -30,29 +37,20 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
pk=old_image['id'],
sk='0',
),
update_expr='SET #status = :archived, updated_at = :updated_at',
update_expr='SET issued_cert = :issued_cert, \
updated_at = :now',
cond_expr='#status = :completed',
expr_attr_names={
'#status': 'status',
},
expr_attr_names={'#status': 'status'},
expr_attr_values={
':issued_cert': issued_cert | {'expired': True},
':completed': 'COMPLETED',
':archived': 'ARCHIVED',
':updated_at': now_,
':now': now_,
},
)
transact.put(
item={
'id': old_image['id'],
'sk': 'ARCHIVED',
'archived_at': now_,
},
cond_expr='attribute_not_exists(sk)',
)
transact.put(
item={
'id': old_image['id'],
'sk': 'SCHEDULE#SET_AS_ARCHIVED#EXECUTED',
'sk': 'SCHEDULE#SET_CERT_EXPIRED#EXECUTED',
'created_at': now_,
},
)
@@ -62,7 +60,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
dyn.put_item(
item={
'id': old_image['id'],
'sk': 'SCHEDULE#SET_AS_ARCHIVED#FAILED',
'sk': 'SCHEDULE#SET_CERT_EXPIRED#FAILED',
'reason': str(exc),
'created_at': now_,
},

View File

@@ -37,10 +37,10 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
transact.update(
key=KeyPair(new_image['id'], '0'),
update_expr='SET subscription_covered = :subscription_covered, \
updated_at = :updated_at',
updated_at = :now',
expr_attr_values={
':subscription_covered': True,
':updated_at': now_,
':now': now_,
},
cond_expr='attribute_exists(sk)',
)