wip subscription org
This commit is contained in:
@@ -1,69 +0,0 @@
|
||||
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,
|
||||
SortKey,
|
||||
)
|
||||
|
||||
from boto3clients import dynamodb_client
|
||||
from config import ORDER_TABLE, USER_TABLE
|
||||
|
||||
logger = Logger(__name__)
|
||||
user_layer = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
|
||||
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()
|
||||
data = user_layer.collection.get_items(
|
||||
KeyPair(
|
||||
pk='cnpj',
|
||||
sk=SortKey(new_image['cnpj'], path_spec='user_id'),
|
||||
rename_key='org_id',
|
||||
)
|
||||
+ KeyPair(
|
||||
pk='email',
|
||||
sk=SortKey(new_image['email'], path_spec='user_id'),
|
||||
rename_key='user_id',
|
||||
),
|
||||
flatten_top=False,
|
||||
)
|
||||
|
||||
# Sometimes the function executes before the user insertion completes,
|
||||
# so an exception is raised to trigger a retry.
|
||||
if len(data) < 2:
|
||||
raise ValueError('IDs not found')
|
||||
|
||||
logger.info('IDs found', data=data)
|
||||
|
||||
with order_layer.transact_writer() as transact:
|
||||
transact.update(
|
||||
key=KeyPair(new_image['id'], '0'),
|
||||
update_expr='SET tenant_id = :org_id, updated_at = :updated_at',
|
||||
expr_attr_values={
|
||||
':org_id': data['org_id'],
|
||||
':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': data['user_id'],
|
||||
':updated_at': now_,
|
||||
},
|
||||
)
|
||||
|
||||
logger.info('IDs updated')
|
||||
|
||||
return True
|
||||
@@ -1,55 +0,0 @@
|
||||
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,
|
||||
SortKey,
|
||||
)
|
||||
|
||||
from boto3clients import dynamodb_client
|
||||
from config import ORDER_TABLE, USER_TABLE
|
||||
|
||||
logger = Logger(__name__)
|
||||
user_layer = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
|
||||
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()
|
||||
data = user_layer.collection.get_items(
|
||||
KeyPair(
|
||||
pk='cpf',
|
||||
sk=SortKey(new_image['cpf'], path_spec='user_id'),
|
||||
rename_key='user_id',
|
||||
)
|
||||
+ KeyPair(
|
||||
pk='email',
|
||||
sk=SortKey(new_image['email'], path_spec='user_id'),
|
||||
rename_key='user_id',
|
||||
),
|
||||
flatten_top=False,
|
||||
)
|
||||
|
||||
# Sometimes the function executes before the user insertion completes,
|
||||
# so an exception is raised to trigger a retry.
|
||||
if not data:
|
||||
raise ValueError('User ID not found')
|
||||
|
||||
order_layer.update_item(
|
||||
key=KeyPair(new_image['id'], '0'),
|
||||
update_expr='SET user_id = :user_id, updated_at = :updated_at',
|
||||
expr_attr_values={
|
||||
':user_id': data['user_id'],
|
||||
':updated_at': now_,
|
||||
},
|
||||
)
|
||||
|
||||
return True
|
||||
@@ -34,15 +34,17 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
limit=100,
|
||||
)
|
||||
|
||||
logger.info('Slots found', total_items=len(result['items']), slots=result['items'])
|
||||
logger.info(
|
||||
'Slots found',
|
||||
total_items=len(result['items']),
|
||||
slots=result['items'],
|
||||
)
|
||||
|
||||
with enrollment_layer.batch_writer() as batch:
|
||||
for pair in result['items']:
|
||||
batch.delete_item(
|
||||
Key={
|
||||
# Post-migration: Uncomment the following line
|
||||
# 'id': {'S': f'SLOT#ORG#{org_id}'},
|
||||
'id': {'S': f'vacancies#{org_id}'},
|
||||
'id': {'S': pair['id']},
|
||||
'sk': {'S': pair['sk']},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -35,7 +35,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
|
||||
# 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.')
|
||||
logger.info('Missing billing policy')
|
||||
return False
|
||||
|
||||
logger.info(f'Billing policy from Org ID "{org_id}" found', policy=policy)
|
||||
@@ -56,10 +56,9 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
|
||||
with enrollment_layer.batch_writer() as batch:
|
||||
for pair in result['items']:
|
||||
org_id = pair['id']
|
||||
batch.delete_item(
|
||||
Key={
|
||||
'id': {'S': f'vacancies#{org_id}'},
|
||||
'id': {'S': pair['id']},
|
||||
'sk': {'S': pair['sk']},
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user