add event
This commit is contained in:
@@ -55,12 +55,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
class CourseNotFoundError(Exception):
|
||||
def __init__(self, *args):
|
||||
super().__init__('Course not found')
|
||||
|
||||
|
||||
def _get_course(course_id: str) -> dict:
|
||||
def _get_course(course_id: str) -> dict | None:
|
||||
with sqlite3.connect(
|
||||
database=SQLITE_DATABASE, detect_types=sqlite3.PARSE_DECLTYPES
|
||||
) as conn:
|
||||
@@ -70,4 +65,4 @@ def _get_course(course_id: str) -> dict:
|
||||
for row in rows:
|
||||
return row['json']
|
||||
|
||||
raise CourseNotFoundError
|
||||
return None
|
||||
|
||||
@@ -8,8 +8,6 @@ from layercake.dynamodb import (
|
||||
ComposeKey,
|
||||
DynamoDBPersistenceLayer,
|
||||
KeyPair,
|
||||
SortKey,
|
||||
TransactKey,
|
||||
)
|
||||
|
||||
from boto3clients import dynamodb_client
|
||||
@@ -27,21 +25,8 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
"""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(
|
||||
TransactKey(order_id)
|
||||
+ SortKey('0')
|
||||
+ KeyPair(
|
||||
pk=order_id,
|
||||
sk=SortKey(
|
||||
sk='metadata#tenant',
|
||||
path_spec='tenant_id',
|
||||
remove_prefix='metadata#',
|
||||
),
|
||||
rename_key='tenant_id',
|
||||
)
|
||||
)
|
||||
tenant_id = data['tenant_id'].removeprefix('ORG#')
|
||||
data = order_layer.get_item(KeyPair(new_image['id'], '0'))
|
||||
tenant_id = data['tenant']
|
||||
|
||||
policy = user_layer.collection.get_item(
|
||||
KeyPair(pk=tenant_id, sk='metadata#billing_policy'),
|
||||
@@ -49,16 +34,22 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
default=False,
|
||||
)
|
||||
|
||||
# Skip if missing billing policy or order is zero/negative
|
||||
# Skip if billing policy is missing or order is less than or equal to zero
|
||||
if not policy or data['total'] <= 0:
|
||||
logger.info('Missing billing policy.')
|
||||
return False
|
||||
|
||||
logger.info(f'Billing policy from Tenant ID "{tenant_id}" found', policy=policy)
|
||||
|
||||
result = enrollment_layer.collection.query(
|
||||
KeyPair(
|
||||
ComposeKey(tenant_id, prefix='vacancies'),
|
||||
order_id,
|
||||
new_image['id'],
|
||||
)
|
||||
)
|
||||
|
||||
logger.info('Slots found', slots=result['items'])
|
||||
|
||||
with enrollment_layer.batch_writer() as batch:
|
||||
for pair in result['items']:
|
||||
batch.delete_item(
|
||||
@@ -68,4 +59,6 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
}
|
||||
)
|
||||
|
||||
logger.info('Deleted all slots')
|
||||
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user