61 lines
1.9 KiB
Python
61 lines
1.9 KiB
Python
from uuid import uuid4
|
|
|
|
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, ORDER_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:
|
|
old_image = event.detail['old_image']
|
|
order_id = old_image['seat']['order_id']
|
|
enrollment_id = old_image['id']
|
|
enrollment = dyn.get_item(key=KeyPair(enrollment_id, '0'))
|
|
now_ = now()
|
|
|
|
if enrollment['status'] != 'CANCELED':
|
|
return False
|
|
|
|
with dyn.transact_writer() as transact:
|
|
org_id = enrollment['org_id']
|
|
|
|
transact.update(
|
|
key=KeyPair(order_id, f'ENROLLMENT#{enrollment_id}'),
|
|
update_expr='SET #status = :rollback, \
|
|
rollback_at = :now, \
|
|
reason = :reason',
|
|
cond_expr='attribute_exists(sk) AND #status = :executed',
|
|
expr_attr_names={
|
|
'#status': 'status',
|
|
},
|
|
expr_attr_values={
|
|
':rollback': 'ROLLBACK',
|
|
':executed': 'EXECUTED',
|
|
':reason': 'CANCELLATION',
|
|
':now': now_,
|
|
},
|
|
table_name=ORDER_TABLE,
|
|
)
|
|
transact.put(
|
|
item={
|
|
'id': f'SEAT#ORG#{org_id}',
|
|
'sk': f'ORDER#{order_id}#ENROLLMENT#{uuid4()}',
|
|
'course': enrollment['course'],
|
|
'created_at': now_,
|
|
}
|
|
)
|
|
|
|
return True
|