add tests to canceled enrollment or scheduled with seat

This commit is contained in:
2026-01-26 13:00:36 -03:00
parent 3e080a2d21
commit 2d191c5fc8
14 changed files with 176 additions and 13 deletions

View File

@@ -1,3 +1,5 @@
from uuid import uuid4
from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.data_classes import (
EventBridgeEvent,
@@ -84,6 +86,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
def _handler(course: Course, context: dict) -> Enrollment:
enrollment = Enrollment(
id=uuid4(),
user=context['user'],
course=course,
)

View File

@@ -1,4 +1,5 @@
from datetime import datetime
from uuid import uuid4
from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.data_classes import (
@@ -37,8 +38,9 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
offset_days = old_image.get('dedup_window_offset_days')
billing_day = old_image.get('subscription_billing_day')
created_by = old_image.get('created_by')
seat: Seat | None = old_image.get('seat')
seat: Seat = old_image.get('seat', {})
enrollment = Enrollment(
id=old_image['enrollment_id'],
course=old_image['course'],
user=old_image['user'],
)

View File

@@ -37,7 +37,6 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
rollback_at = :now, \
reason = :reason',
cond_expr='attribute_exists(sk) AND #status = :executed',
table_name=ORDER_TABLE,
expr_attr_names={
'#status': 'status',
},
@@ -47,6 +46,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
':reason': 'CANCELLATION',
':now': now_,
},
table_name=ORDER_TABLE,
)
transact.put(
item={

View File

@@ -0,0 +1,59 @@
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['enrollment_id']
*_, org_id = old_image['id'].split('#')
now_ = now()
with dyn.transact_writer() as transact:
transact.update(
key=KeyPair(
pk=order_id,
sk=f'ENROLLMENT#{enrollment_id}',
table_name=ORDER_TABLE,
),
cond_expr='attribute_exists(sk) AND #status = :scheduled',
update_expr='SET #status = :rollback, \
rollback_at = :now, \
reason = :reason',
expr_attr_names={
'#status': 'status',
},
expr_attr_values={
':rollback': 'ROLLBACK',
':scheduled': 'SCHEDULED',
':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': old_image['course'],
'created_at': now_,
}
)
return True