65 lines
1.8 KiB
Python
65 lines
1.8 KiB
Python
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.openapi.params import Body
|
|
from layercake.dateutils import now
|
|
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
|
|
|
|
from api_gateway import JSONResponse
|
|
from boto3clients import dynamodb_client
|
|
from config import USER_TABLE
|
|
from exceptions import OrgNotFoundError
|
|
|
|
router = Router()
|
|
dyn = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
|
|
|
|
|
|
class PaymentMethod(str, Enum):
|
|
PIX = 'PIX'
|
|
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)],
|
|
payment_method: Annotated[PaymentMethod, Body(embed=True)],
|
|
):
|
|
now_ = now()
|
|
|
|
with dyn.transact_writer() as transact:
|
|
transact.update(
|
|
key=KeyPair(org_id, '0'),
|
|
update_expr='SET subscription_covered = :true, updated_at = :now',
|
|
expr_attr_values={
|
|
':true': True,
|
|
':now': now_,
|
|
},
|
|
cond_expr='attribute_exists(sk)',
|
|
exc_cls=OrgNotFoundError,
|
|
)
|
|
transact.put(
|
|
item={
|
|
'id': 'SUBSCRIPTION',
|
|
'sk': f'ORG#{org_id}',
|
|
'name': name,
|
|
'created_at': now_,
|
|
},
|
|
cond_expr='attribute_not_exists(sk)',
|
|
)
|
|
transact.put(
|
|
item={
|
|
'id': org_id,
|
|
'sk': 'METADATA#SUBSCRIPTION',
|
|
'billing_day': billing_day,
|
|
'payment_method': payment_method.value,
|
|
'created_at': now_,
|
|
}
|
|
)
|
|
|
|
return JSONResponse(status_code=HTTPStatus.CREATED)
|