add payment section

This commit is contained in:
2026-01-19 20:16:57 -03:00
parent ed58e26e7e
commit 6732e07dfa
13 changed files with 222 additions and 58 deletions

View File

@@ -43,6 +43,7 @@ def get_org(org_id: str):
TransactKey(org_id)
+ SortKey('0')
+ SortKey('METADATA#ADDRESS', rename_key='address')
+ SortKey('METADATA#BILLING', rename_key='billing')
+ SortKey('METADATA#SUBSCRIPTION', rename_key='subscription')
+ KeyPair(
pk='SUBSCRIPTION#FROZEN',

View File

@@ -1,13 +1,19 @@
from datetime import date
from decimal import Decimal
from enum import Enum
from http import HTTPStatus
from typing import Annotated
from aws_lambda_powertools.event_handler.api_gateway import Router
from aws_lambda_powertools.event_handler.exceptions import NotFoundError
from aws_lambda_powertools.event_handler.openapi.params import Query
from aws_lambda_powertools.event_handler.openapi.params import Body, Query
from layercake.dateutils import now
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
from api_gateway import JSONResponse
from boto3clients import dynamodb_client
from config import ORDER_TABLE
from config import ORDER_TABLE, USER_TABLE
from exceptions import OrgNotFoundError
router = Router()
dyn = DynamoDBPersistenceLayer(ORDER_TABLE, dynamodb_client)
@@ -16,6 +22,11 @@ dyn = DynamoDBPersistenceLayer(ORDER_TABLE, dynamodb_client)
class BillingNotFoundError(NotFoundError): ...
class PaymentMethod(str, Enum):
BANK_SLIP = 'BANK_SLIP'
MANUAL = 'MANUAL'
@router.get('/<org_id>/billing')
def billing(
org_id: str,
@@ -36,3 +47,30 @@ def billing(
),
limit=150,
)
@router.put('/<org_id>/billing')
def update(
org_id: str,
due_days: Annotated[Decimal, Body(embed=True, ge=1, le=90)],
payment_method: Annotated[PaymentMethod, Body(embed=True)],
):
with dyn.transact_writer() as transact:
transact.condition(
key=KeyPair(org_id, '0'),
cond_expr='attribute_exists(sk)',
exc_cls=OrgNotFoundError,
table_name=USER_TABLE,
)
transact.put(
item={
'id': org_id,
'sk': 'METADATA#BILLING',
'due_days': due_days,
'payment_method': payment_method.value,
'created_at': now(),
},
table_name=USER_TABLE,
)
return JSONResponse(status_code=HTTPStatus.NO_CONTENT)

View File

@@ -1,4 +1,3 @@
from enum import Enum
from http import HTTPStatus
from typing import Annotated
@@ -20,17 +19,11 @@ router = Router()
dyn = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
class PaymentMethod(str, Enum):
BANK_SLIP = 'BANK_SLIP'
MANUAL = 'MANUAL'
@router.post('/<org_id>/subscription')
def add(
org_id: str,
name: Annotated[str, Body(embed=True)],
billing_day: Annotated[int, Body(embed=True, ge=1, le=31)],
payment_method: Annotated[PaymentMethod, Body(embed=True)],
subscription_frozen: Annotated[bool, Body(embed=True)] = False,
):
now_ = now()
@@ -55,7 +48,6 @@ def add(
'id': org_id,
'sk': 'METADATA#SUBSCRIPTION',
'billing_day': billing_day,
'payment_method': payment_method.value,
'created_at': now_,
},
cond_expr='attribute_not_exists(sk)',
@@ -88,7 +80,6 @@ def add(
def edit(
org_id: str,
billing_day: Annotated[int, Body(embed=True, ge=1, le=31)],
payment_method: Annotated[PaymentMethod, Body(embed=True)],
subscription_frozen: Annotated[bool, Body(embed=True)] = False,
):
now_ = now()
@@ -102,11 +93,9 @@ def edit(
transact.update(
key=KeyPair(org_id, 'METADATA#SUBSCRIPTION'),
update_expr='SET billing_day = :billing_day, \
payment_method = :payment_method, \
updated_at = :now',
expr_attr_values={
':billing_day': billing_day,
':payment_method': payment_method.value,
':now': now_,
},
cond_expr='attribute_exists(sk)',