cancel enrollment on billing
This commit is contained in:
@@ -41,24 +41,23 @@ sqlite3.register_converter('json', json.loads)
|
||||
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
new_image = event.detail['new_image']
|
||||
now_ = now()
|
||||
enrollment_id = new_image['id']
|
||||
org_id = new_image['org_id']
|
||||
data = enrollment_layer.collection.get_items(
|
||||
TransactKey(enrollment_id) + SortKey('0') + SortKey('author')
|
||||
enrollment = enrollment_layer.collection.get_items(
|
||||
TransactKey(new_image['id']) + SortKey('0') + SortKey('author')
|
||||
)
|
||||
|
||||
if not data:
|
||||
if not enrollment:
|
||||
logger.debug('Enrollment not found')
|
||||
return False
|
||||
|
||||
logger.info('Enrollment found', data=data)
|
||||
logger.info('Enrollment found', data=enrollment)
|
||||
|
||||
# Keep it until the migration has been completed
|
||||
old_course = _get_course(data['course']['id'])
|
||||
old_course = _get_course(enrollment['course']['id'])
|
||||
if old_course:
|
||||
data['course'] = old_course
|
||||
enrollment['course'] = old_course
|
||||
|
||||
created_at: datetime = fromisoformat(data['create_date']) # type: ignore
|
||||
created_at: datetime = fromisoformat(enrollment['create_date']) # type: ignore
|
||||
start_date, end_date = get_billing_period(
|
||||
billing_day=new_image['billing_day'],
|
||||
date_=created_at,
|
||||
@@ -69,7 +68,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
end=end_date.isoformat(),
|
||||
)
|
||||
|
||||
logger.info('Enrollment found', data=data)
|
||||
logger.info('Enrollment found', data=enrollment)
|
||||
|
||||
try:
|
||||
with order_layer.transact_writer() as transact:
|
||||
@@ -81,7 +80,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
'created_at': now_,
|
||||
},
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
exc_cls=ExistingBillingConflictError,
|
||||
exc_cls=BillingConflictError,
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
@@ -93,12 +92,13 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
'created_at': now_,
|
||||
}
|
||||
)
|
||||
except ExistingBillingConflictError:
|
||||
except BillingConflictError:
|
||||
pass
|
||||
|
||||
# Add enrollment entry to billing
|
||||
try:
|
||||
author = data.get('author')
|
||||
course_id = data['course']['id']
|
||||
author = enrollment.get('author')
|
||||
course_id = enrollment['course']['id']
|
||||
course = course_layer.collection.get_items(
|
||||
KeyPair(
|
||||
pk=course_id,
|
||||
@@ -115,15 +115,16 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
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']),
|
||||
'sk': f'{sk}#ENROLLMENT#{enrollment["id"]}',
|
||||
'user': pick(('id', 'name'), enrollment['user']),
|
||||
'course': pick(('id', 'name'), enrollment['course']),
|
||||
'unit_price': course['unit_price'],
|
||||
# Post-migration: uncomment the following line
|
||||
# 'enrolled_at': data['created_at'],
|
||||
'enrolled_at': data['create_date'],
|
||||
# 'enrolled_at': enrollment['created_at'],
|
||||
'enrolled_at': enrollment['create_date'],
|
||||
'created_at': now_,
|
||||
}
|
||||
# Add author if present
|
||||
| (
|
||||
{
|
||||
'author': {
|
||||
@@ -146,7 +147,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
class ExistingBillingConflictError(Exception): ...
|
||||
class BillingConflictError(Exception): ...
|
||||
|
||||
|
||||
class BillingNotFoundError(Exception): ...
|
||||
|
||||
Reference in New Issue
Block a user