add tests to canceled enrollment or scheduled with seat
This commit is contained in:
@@ -38,7 +38,7 @@ class Course(BaseModel):
|
||||
|
||||
|
||||
class Enrollment(BaseModel):
|
||||
id: UUID4 | str = Field(default_factory=uuid4)
|
||||
id: UUID4 | str
|
||||
user: User
|
||||
course: Course
|
||||
progress: int = Field(default=0, ge=0, le=100)
|
||||
@@ -61,7 +61,7 @@ Org = TypedDict('Org', {'org_id': str, 'name': str})
|
||||
|
||||
CreatedBy = TypedDict('CreatedBy', {'id': str, 'name': str})
|
||||
|
||||
Seat = TypedDict('Seat', {'order_id': str})
|
||||
Seat = TypedDict('Seat', {'order_id': str, 'enrollment_id': NotRequired[str]})
|
||||
|
||||
DeduplicationWindow = TypedDict('DeduplicationWindow', {'offset_days': int})
|
||||
|
||||
|
||||
@@ -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,
|
||||
)
|
||||
|
||||
@@ -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'],
|
||||
)
|
||||
|
||||
@@ -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={
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user