wip subscription org
This commit is contained in:
@@ -6,6 +6,7 @@ from aws_lambda_powertools.utilities.data_classes import (
|
||||
event_source,
|
||||
)
|
||||
from aws_lambda_powertools.utilities.typing import LambdaContext
|
||||
from layercake.batch import BatchProcessor
|
||||
from layercake.dateutils import now
|
||||
from layercake.dynamodb import (
|
||||
DynamoDBPersistenceLayer,
|
||||
@@ -24,16 +25,17 @@ logger = Logger(__name__)
|
||||
order_layer = DynamoDBPersistenceLayer(ORDER_TABLE, dynamodb_client)
|
||||
course_layer = DynamoDBPersistenceLayer(COURSE_TABLE, dynamodb_client)
|
||||
enrollment_layer = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
|
||||
processor = BatchProcessor()
|
||||
|
||||
|
||||
@event_source(data_class=EventBridgeEvent)
|
||||
@logger.inject_lambda_context
|
||||
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> list[str]:
|
||||
def lambda_handler(event: EventBridgeEvent, context: LambdaContext):
|
||||
new_image = event.detail['new_image']
|
||||
now_ = now()
|
||||
order_id = new_image['id']
|
||||
order = order_layer.collection.get_items(
|
||||
TransactKey(order_id) + SortKey('0') + SortKey('items', path_spec='items'),
|
||||
TransactKey(new_image['id'])
|
||||
+ SortKey('0')
|
||||
+ SortKey('items', path_spec='items'),
|
||||
)
|
||||
items = {
|
||||
item['id']: int(item['quantity'])
|
||||
@@ -51,23 +53,18 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> list[str]
|
||||
cpf=order['cpf'],
|
||||
)
|
||||
|
||||
ids = []
|
||||
with processor(
|
||||
records=courses,
|
||||
handler=_handler,
|
||||
context={
|
||||
'user': user,
|
||||
'order_id': new_image['id'],
|
||||
},
|
||||
) as fp:
|
||||
result = fp.process()
|
||||
logger.info('Processed courses', result=result)
|
||||
|
||||
for course in courses:
|
||||
enrollment = Enrollment(
|
||||
id=uuid4(),
|
||||
user=user,
|
||||
course=course,
|
||||
)
|
||||
enroll(
|
||||
enrollment,
|
||||
persistence_layer=enrollment_layer,
|
||||
deduplication_window=DeduplicationWindow(offset_days=90),
|
||||
linked_entities=frozenset({LinkedEntity(order_id, 'ORDER')}),
|
||||
)
|
||||
ids.append(str(enrollment.id))
|
||||
|
||||
order_layer.update_item(
|
||||
return order_layer.update_item(
|
||||
key=KeyPair(new_image['id'], new_image['sk']),
|
||||
update_expr='SET #status = :status, updated_at = :updated_at',
|
||||
expr_attr_names={
|
||||
@@ -75,12 +72,27 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> list[str]
|
||||
},
|
||||
expr_attr_values={
|
||||
':status': 'SUCCESS',
|
||||
':updated_at': now_,
|
||||
':updated_at': now(),
|
||||
},
|
||||
cond_expr='attribute_exists(sk)',
|
||||
)
|
||||
|
||||
return ids
|
||||
|
||||
def _handler(record: Course, context: dict) -> Enrollment:
|
||||
enrollment = Enrollment(
|
||||
id=uuid4(),
|
||||
user=context['user'],
|
||||
course=record,
|
||||
)
|
||||
|
||||
enroll(
|
||||
enrollment,
|
||||
persistence_layer=enrollment_layer,
|
||||
deduplication_window=DeduplicationWindow(offset_days=90),
|
||||
linked_entities=frozenset({LinkedEntity(context['order_id'], 'ORDER')}),
|
||||
)
|
||||
|
||||
return enrollment
|
||||
|
||||
|
||||
def _get_courses(ids: set) -> tuple[Course, ...]:
|
||||
@@ -89,5 +101,4 @@ def _get_courses(ids: set) -> tuple[Course, ...]:
|
||||
KeyChain(pairs),
|
||||
flatten_top=False,
|
||||
)
|
||||
courses = tuple(Course(id=idx, **obj) for idx, obj in result.items()) # type: ignore
|
||||
return courses
|
||||
return tuple(Course(id=idx, **obj) for idx, obj in result.items()) # type: ignore
|
||||
|
||||
Reference in New Issue
Block a user