This commit is contained in:
2025-07-03 15:36:30 -03:00
parent ce1c1b367e
commit 208c43f398
18 changed files with 261 additions and 78 deletions

View File

@@ -2,4 +2,13 @@ import os
USER_TABLE: str = os.getenv('USER_TABLE') # type: ignore
ORDER_TABLE: str = os.getenv('ORDER_TABLE') # type: ignore
COURSE_TABLE: str = os.getenv('COURSE_TABLE') # type: ignore
ENROLLMENT_TABLE: str = os.getenv('ENROLLMENT_TABLE') # type: ignore
# Post-migration: remove the lines below
if os.getenv('AWS_LAMBDA_FUNCTION_NAME'):
SQLITE_DATABASE = 'courses_export_2025-06-18_110214.db'
else:
SQLITE_DATABASE = 'app/courses_export_2025-06-18_110214.db'
SQLITE_TABLE = 'courses'

Binary file not shown.

View File

@@ -46,15 +46,24 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
with order_layer.transact_writer() as transact:
transact.update(
key=KeyPair(new_image['id'], '0'),
update_expr='SET metadata__tenant_id = :tenant_id, \
metadata__related_ids = :related_ids, \
update_date = :update_date',
update_expr='SET tenant = :tenant_id, \
updated_at = :updated_at',
expr_attr_values={
':tenant_id': ids['org_id'],
':related_ids': set(ids.values()),
':update_date': now_,
':updated_at': now_,
},
)
transact.update(
key=KeyPair(new_image['id'], 'author'),
update_expr='SET user_id = :user_id, updated_at = :updated_at',
expr_attr_values={
':user_id': ids['user_id'],
':updated_at': now_,
},
)
# Post-migration: remove the following line
transact.put(
item={
'id': new_image['id'],
@@ -64,15 +73,4 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
}
)
for k, v in ids.items():
kind = k.removesuffix('_id')
transact.put(
item={
'id': new_image['id'],
'sk': f'related_ids#{kind}', # e.g. related_ids#user
'create_date': now_,
k: v,
}
)
return True

View File

@@ -29,13 +29,17 @@ class TenantDoesNotExistError(Exception):
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
new_image = event.detail['new_image']
order_id = new_image['id']
# Post-migration: remove the following line
tenant_id = order_layer.collection.get_item(
KeyPair(
order_id,
SortKey('metadata#tenant', path_spec='tenant_id'),
),
exc_cls=TenantDoesNotExistError,
)
).removeprefix('ORG#')
# Post-migration: uncomment the following line
# tenant_id = new_image['tenant']
result = enrollment_layer.collection.query(
KeyPair(
@@ -45,11 +49,12 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
order_id,
)
)
with enrollment_layer.batch_writer() as batch:
for pair in result['items']:
batch.delete_item(
Key={
# Post-migration: rename `vacancies` to `slots`
# Post-migration: rename `vacancies` to `slots#org`
'id': {'S': ComposeKey(pair['id'], prefix='vacancies')},
'sk': {'S': pair['sk']},
}

View File

@@ -0,0 +1,73 @@
import json
import sqlite3
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 sqlite_utils import Database
from boto3clients import dynamodb_client
from config import ORDER_TABLE, SQLITE_DATABASE, SQLITE_TABLE
sqlite3.register_converter('json', json.loads)
logger = Logger(__name__)
order_layer = DynamoDBPersistenceLayer(ORDER_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']
now_ = now()
items = new_image['items']
new_items: list[dict] = []
for item in items:
course = _get_course(item['id'])
new_items.append(
item
| (
{
'id': course.get('metadata__betaeducacao_id'),
}
if course
else {}
)
)
order_layer.update_item(
key=KeyPair(new_image['id'], 'items'),
update_expr='SET #items = :items, updated_at = :updated_at',
expr_attr_names={'#items': 'items'},
expr_attr_values={':items': new_items, ':updated_at': now_},
)
return True
class CourseNotFoundError(Exception):
def __init__(self, *args):
super().__init__('Course not found')
def _get_course(course_id: str) -> dict:
with sqlite3.connect(
database=SQLITE_DATABASE, detect_types=sqlite3.PARSE_DECLTYPES
) as conn:
db = Database(conn)
rows = db[SQLITE_TABLE].rows_where('id = ?', [course_id])
for row in rows:
return row['json']
raise CourseNotFoundError

View File

@@ -24,8 +24,8 @@ enrollment_layer = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
@event_source(data_class=EventBridgeEvent)
@logger.inject_lambda_context
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
"""Remove slots if the Tenant has a `metadata#billing_policy` and
the order is positive."""
"""Remove slots if the tenant has a `metadata#billing_policy` and
the total is greater than zero."""
new_image = event.detail['new_image']
order_id = new_image['id']
data = order_layer.collection.get_items(

View File

@@ -20,26 +20,27 @@ order_layer = DynamoDBPersistenceLayer(ORDER_TABLE, dynamodb_client)
@event_source(data_class=EventBridgeEvent)
@logger.inject_lambda_context
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
"""Set to `PAID` if the status is `PENDING` and the total is zero."""
new_image = event.detail['new_image']
now_ = now()
with order_layer.transact_writer() as transact:
transact.update(
key=KeyPair(new_image['id'], '0'),
update_expr='SET #status = :status, update_date = :update_date',
update_expr='SET #status = :status, updated_at = :updated_at',
expr_attr_names={
'#status': 'status',
},
expr_attr_values={
':status': 'PAID',
':update_date': now_,
':updated_at': now_,
},
)
transact.put(
item={
'id': new_image['id'],
'sk': 'paid_date',
'create_date': now_,
'sk': 'paid_at',
'paid_at': now_,
}
)