add server to gen pdf
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import json
|
||||
import sqlite3
|
||||
from datetime import datetime, time, timedelta
|
||||
|
||||
from aws_lambda_powertools import Logger
|
||||
@@ -14,9 +16,16 @@ from layercake.dynamodb import (
|
||||
TransactKey,
|
||||
)
|
||||
from layercake.funcs import pick
|
||||
from sqlite_utils import Database
|
||||
|
||||
from boto3clients import dynamodb_client
|
||||
from config import COURSE_TABLE, ENROLLMENT_TABLE, ORDER_TABLE
|
||||
from config import (
|
||||
COURSE_TABLE,
|
||||
ENROLLMENT_TABLE,
|
||||
ORDER_TABLE,
|
||||
SQLITE_DATABASE,
|
||||
SQLITE_TABLE,
|
||||
)
|
||||
from utils import get_billing_period
|
||||
|
||||
logger = Logger(__name__)
|
||||
@@ -24,6 +33,8 @@ order_layer = DynamoDBPersistenceLayer(ORDER_TABLE, dynamodb_client)
|
||||
enrollment_layer = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
|
||||
course_layer = DynamoDBPersistenceLayer(COURSE_TABLE, dynamodb_client)
|
||||
|
||||
sqlite3.register_converter('json', json.loads)
|
||||
|
||||
|
||||
@event_source(data_class=EventBridgeEvent)
|
||||
@logger.inject_lambda_context
|
||||
@@ -41,6 +52,11 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
logger.debug('Enrollment not found')
|
||||
return False
|
||||
|
||||
# Keep it until the migration has been completed
|
||||
old_course = _get_course(data['course']['id'])
|
||||
if old_course:
|
||||
data['course'] = old_course
|
||||
|
||||
start_date, end_date = get_billing_period(
|
||||
new_image['billing_day'],
|
||||
year=created_at.year,
|
||||
@@ -52,6 +68,8 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
end=end_date.isoformat(),
|
||||
)
|
||||
|
||||
logger.info('Enrollment found', data=data)
|
||||
|
||||
try:
|
||||
with order_layer.transact_writer() as transact:
|
||||
transact.put(
|
||||
@@ -78,47 +96,44 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
pass
|
||||
|
||||
try:
|
||||
with order_layer.transact_writer() as transact:
|
||||
author = data['author']
|
||||
course_id = data['course']['id']
|
||||
course = course_layer.collection.get_items(
|
||||
KeyPair(
|
||||
pk=course_id,
|
||||
sk=SortKey('0', path_spec='metadata__unit_price'),
|
||||
rename_key='unit_price',
|
||||
)
|
||||
+ KeyPair(
|
||||
pk=f'CUSTOM_PRICING#ORG#{org_id}',
|
||||
sk=SortKey(f'COURSE#{course_id}', path_spec='unit_price'),
|
||||
rename_key='unit_price',
|
||||
),
|
||||
flatten_top=False,
|
||||
author = data['author']
|
||||
course_id = data['course']['id']
|
||||
course = course_layer.collection.get_items(
|
||||
KeyPair(
|
||||
pk=course_id,
|
||||
sk=SortKey('0', path_spec='metadata__unit_price'),
|
||||
rename_key='unit_price',
|
||||
)
|
||||
|
||||
transact.condition(
|
||||
key=KeyPair(pk, sk),
|
||||
cond_expr='attribute_exists(sk)',
|
||||
exc_cls=BillingNotFoundError,
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
'id': pk,
|
||||
'sk': f'{sk}#ENROLLMENT#{enrollment_id}',
|
||||
'user': pick(('id', 'name'), data['user']),
|
||||
'course': pick(('id', 'name'), data['course']),
|
||||
'unit_price': course['unit_price'],
|
||||
'author': {
|
||||
'id': author['user_id'],
|
||||
'name': author['name'],
|
||||
},
|
||||
# Post-migration: uncomment the following line
|
||||
# 'enrolled_at': data['created_at'],
|
||||
'enrolled_at': data['create_date'],
|
||||
'created_at': now_,
|
||||
+ KeyPair(
|
||||
pk=f'CUSTOM_PRICING#ORG#{org_id}',
|
||||
sk=SortKey(f'COURSE#{course_id}', path_spec='unit_price'),
|
||||
rename_key='unit_price',
|
||||
),
|
||||
flatten_top=False,
|
||||
)
|
||||
order_layer.put_item(
|
||||
item={
|
||||
'id': pk,
|
||||
'sk': f'{sk}#ENROLLMENT#{enrollment_id}',
|
||||
'user': pick(('id', 'name'), data['user']),
|
||||
'course': pick(('id', 'name'), data['course']),
|
||||
'unit_price': course['unit_price'],
|
||||
'author': {
|
||||
'id': author['user_id'],
|
||||
'name': author['name'],
|
||||
},
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
)
|
||||
except Exception:
|
||||
# Post-migration: uncomment the following line
|
||||
# 'enrolled_at': data['created_at'],
|
||||
'enrolled_at': data['create_date'],
|
||||
'created_at': now_,
|
||||
},
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
)
|
||||
except Exception as exc:
|
||||
logger.exception(
|
||||
exc,
|
||||
keypair={'pk': pk, 'sk': sk},
|
||||
)
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
@@ -128,3 +143,18 @@ class ExistingBillingConflictError(Exception): ...
|
||||
|
||||
|
||||
class BillingNotFoundError(Exception): ...
|
||||
|
||||
|
||||
def _get_course(course_id: str) -> dict | None:
|
||||
with sqlite3.connect(
|
||||
database=SQLITE_DATABASE, detect_types=sqlite3.PARSE_DECLTYPES
|
||||
) as conn:
|
||||
db = Database(conn)
|
||||
rows = db[SQLITE_TABLE].rows_where(
|
||||
"json->>'$.metadata__betaeducacao_id' = ?", [course_id]
|
||||
)
|
||||
|
||||
for row in rows:
|
||||
return row['json']
|
||||
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user