wip subscription org
This commit is contained in:
@@ -16,7 +16,7 @@ DeduplicationWindow = TypedDict('DeduplicationWindow', {'offset_days': int})
|
||||
|
||||
class LinkedEntity(str):
|
||||
def __new__(cls, id: str, type: str) -> Self:
|
||||
return super().__new__(cls, '#'.join([type.upper(), id]))
|
||||
return super().__new__(cls, '#'.join([type.lower(), id]))
|
||||
|
||||
def __init__(self, id: str, type: str) -> None:
|
||||
# __init__ is used to store the parameters for later reference.
|
||||
@@ -33,7 +33,7 @@ class Slot:
|
||||
@property
|
||||
def order_id(self) -> LinkedEntity:
|
||||
idx, _ = self.sk.split('#')
|
||||
return LinkedEntity(idx, 'order')
|
||||
return LinkedEntity(idx, 'ORDER')
|
||||
|
||||
|
||||
class LifecycleEvents(str, Enum):
|
||||
@@ -142,13 +142,15 @@ def enroll(
|
||||
)
|
||||
|
||||
for entity in linked_entities:
|
||||
type = entity.type.lower()
|
||||
keyprefix = entity.type.lower()
|
||||
transact.put(
|
||||
item={
|
||||
'id': enrollment.id,
|
||||
'sk': f'linked_entities#{type}',
|
||||
# Post-migration: uncomment the following line
|
||||
# 'sk': f'LINKED_ENTITIES#{entity.type}',
|
||||
'sk': f'linked_entities#{entity.type}',
|
||||
'created_at': now_,
|
||||
f'{type}_id': entity.id,
|
||||
f'{keyprefix}_id': entity.id,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -157,7 +159,7 @@ def enroll(
|
||||
item={
|
||||
'id': enrollment.id,
|
||||
# Post-migration: uncomment the following line
|
||||
# 'sk': 'metadata#parent_slot',
|
||||
# 'sk': 'METADATA#SOURCE_SLOT',
|
||||
'sk': 'parent_vacancy',
|
||||
'vacancy': asdict(slot),
|
||||
'created_at': now_,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -71,7 +71,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
|
||||
class CourseNotFoundError(Exception):
|
||||
def __init__(self, *args):
|
||||
super().__init__('Course not found')
|
||||
super().__init__('Course not found in SQLite')
|
||||
|
||||
|
||||
# Post-migration: remove the following function
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
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,
|
||||
USER_TABLE,
|
||||
)
|
||||
|
||||
logger = Logger(__name__)
|
||||
user_layer = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
|
||||
enrollment_layer = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
|
||||
|
||||
|
||||
@event_source(data_class=EventBridgeEvent)
|
||||
@logger.inject_lambda_context
|
||||
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
new_image = event.detail['new_image']
|
||||
data = user_layer.get_item(
|
||||
# Post-migration: uncomment the following line
|
||||
# KeyPair(new_image['org_id'], 'METADATA#BILLING_TERMS'),
|
||||
KeyPair(new_image['tenant_id'], 'metadata#billing_policy'),
|
||||
)
|
||||
|
||||
if not data:
|
||||
return False
|
||||
|
||||
try:
|
||||
enrollment_layer.put_item(
|
||||
item={
|
||||
'id': new_image['id'],
|
||||
'sk': 'METADATA#BILLING_TERMS',
|
||||
'org_id': new_image['tenant_id'],
|
||||
'billing_day': data['billing_day'],
|
||||
'created_at': now(),
|
||||
},
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
)
|
||||
except Exception:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
Reference in New Issue
Block a user