add support to milti transaction
This commit is contained in:
@@ -4,9 +4,10 @@ from aws_lambda_powertools.utilities.data_classes import (
|
||||
event_source,
|
||||
)
|
||||
from aws_lambda_powertools.utilities.typing import LambdaContext
|
||||
from layercake.dateutils import now
|
||||
from layercake.dateutils import now, ttl
|
||||
from layercake.dynamodb import (
|
||||
DynamoDBPersistenceLayer,
|
||||
KeyPair,
|
||||
SortKey,
|
||||
TransactKey,
|
||||
)
|
||||
@@ -41,7 +42,8 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
r = dyn.collection.get_items(
|
||||
TransactKey(order_id)
|
||||
+ SortKey('ADDRESS', rename_key='address')
|
||||
+ SortKey('ITEMS', path_spec='items', rename_key='items'),
|
||||
+ SortKey('ITEMS', path_spec='items', rename_key='items')
|
||||
+ SortKey('CREDIT_CARD#PAYMENT_INTENT', rename_key='credit_card'),
|
||||
flatten_top=False,
|
||||
)
|
||||
|
||||
@@ -59,21 +61,40 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
)
|
||||
|
||||
try:
|
||||
dyn.put_item(
|
||||
item={
|
||||
'id': order_id,
|
||||
'sk': 'INVOICE',
|
||||
'payment_method': payment_method,
|
||||
'secure_id': invoice['secure_id'],
|
||||
'secure_url': invoice['secure_url'],
|
||||
'created_at': now_,
|
||||
# Uncomment this when adding for multiple payment providers
|
||||
# 'payment_provider': 'iugu',
|
||||
}
|
||||
| ({'bank_slip': invoice['bank_slip']} if is_bank_slip else {})
|
||||
| ({'pix': invoice['pix']} if is_pix else {}),
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
)
|
||||
with dyn.transact_writer() as transact:
|
||||
transact.put(
|
||||
item={
|
||||
'id': order_id,
|
||||
'sk': 'INVOICE',
|
||||
'payment_method': payment_method,
|
||||
'secure_id': invoice['secure_id'],
|
||||
'secure_url': invoice['secure_url'],
|
||||
'created_at': now_,
|
||||
# Uncomment this when adding for multiple payment providers
|
||||
# 'payment_provider': 'iugu',
|
||||
}
|
||||
| ({'bank_slip': invoice['bank_slip']} if is_bank_slip else {})
|
||||
| ({'pix': invoice['pix']} if is_pix else {}),
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
)
|
||||
|
||||
if 'credit_card' in r:
|
||||
transact.delete(
|
||||
key=KeyPair(order_id, 'CREDIT_CARD#PAYMENT_INTENT'),
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
'id': order_id,
|
||||
'sk': 'TRANSACTION',
|
||||
'invoice_id': invoice['secure_id'],
|
||||
'credit_card': r['credit_card'],
|
||||
'installments': int(new_image.get('installments', 1)),
|
||||
'ttl': ttl(start_dt=now_, minutes=5),
|
||||
'created_at': now_,
|
||||
},
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
)
|
||||
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
Reference in New Issue
Block a user