Files
saladeaula.digital/enrollments-events/app/events/set_as_expired.py

76 lines
2.3 KiB
Python

from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.data_classes import (
EventBridgeEvent,
event_source,
)
from aws_lambda_powertools.utilities.typing import LambdaContext
from layercake.dateutils import now
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
from boto3clients import dynamodb_client
from config import (
ENROLLMENT_TABLE,
)
logger = Logger(__name__)
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()
try:
with dyn.transact_writer() as transact:
transact.update(
key=KeyPair(
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',
},
expr_attr_values={
':pending': 'PENDING',
':in_progress': 'IN_PROGRESS',
':expired': 'EXPIRED',
':updated_at': 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',
'created_at': now_,
},
)
except Exception as exc:
logger.exception(exc)
dyn.put_item(
item={
'id': old_image['id'],
'sk': 'SCHEDULE#SET_AS_EXPIRED#FAILED',
'reason': str(exc),
'created_at': now_,
},
)
return False
else:
return True