fix redirect to checkout when the org has not a subscription
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import re
|
||||
from decimal import Decimal
|
||||
from http import HTTPStatus
|
||||
from typing import Annotated, Literal
|
||||
from typing import Any, Literal
|
||||
from uuid import uuid4
|
||||
|
||||
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
|
||||
from layercake.extra_types import CnpjStr, CpfStr, NameStr
|
||||
from pydantic import (
|
||||
@@ -12,6 +13,7 @@ from pydantic import (
|
||||
BaseModel,
|
||||
ConfigDict,
|
||||
EmailStr,
|
||||
Field,
|
||||
field_validator,
|
||||
model_validator,
|
||||
)
|
||||
@@ -20,6 +22,7 @@ from api_gateway import JSONResponse
|
||||
from boto3clients import dynamodb_client
|
||||
from config import ORDER_TABLE
|
||||
from routes.enrollments.enroll import Enrollment
|
||||
from routes.orgs.address import address
|
||||
|
||||
router = Router()
|
||||
dyn = DynamoDBPersistenceLayer(ORDER_TABLE, dynamodb_client)
|
||||
@@ -62,6 +65,7 @@ class Coupon(BaseModel):
|
||||
class Checkout(BaseModel):
|
||||
model_config = ConfigDict(str_strip_whitespace=True)
|
||||
|
||||
id: UUID4 = Field(default_factory=uuid4)
|
||||
name: str
|
||||
email: EmailStr
|
||||
address: Address
|
||||
@@ -69,11 +73,11 @@ class Checkout(BaseModel):
|
||||
items: tuple[Item, ...]
|
||||
enrollments: tuple[Enrollment, ...] | None = None
|
||||
coupon: Coupon | None = None
|
||||
user: User | None = None
|
||||
org_id: UUID4 | str | None = None
|
||||
user_id: UUID4 | str | None = None
|
||||
cnpj: CnpjStr | None = None
|
||||
cpf: CpfStr | None = None
|
||||
created_by: User | None = None
|
||||
|
||||
@model_validator(mode='after')
|
||||
def verify_fields(self):
|
||||
@@ -84,15 +88,67 @@ class Checkout(BaseModel):
|
||||
if self.org_id is None:
|
||||
raise ValueError('org_id is missing')
|
||||
|
||||
if self.user is None:
|
||||
raise ValueError('user is missing')
|
||||
if self.created_by is None:
|
||||
raise ValueError('created_by is missing')
|
||||
|
||||
if self.cpf is not None and self.user_id is None:
|
||||
raise ValueError('user_id is missing')
|
||||
|
||||
return self
|
||||
|
||||
def model_dump(self, **kwargs) -> dict[str, Any]:
|
||||
return super().model_dump(
|
||||
exclude_none=True,
|
||||
exclude={'items', 'address', 'created_by'},
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
|
||||
@router.post('/')
|
||||
def checkout(body: Annotated[Checkout, Body()]):
|
||||
return JSONResponse(status_code=HTTPStatus.CREATED)
|
||||
def checkout(payload: Checkout):
|
||||
now_ = now()
|
||||
order_id = str(payload.id)
|
||||
address = payload.address
|
||||
coupon = payload.coupon
|
||||
|
||||
with dyn.transact_writer() as transact:
|
||||
transact.put(
|
||||
item={
|
||||
'id': order_id,
|
||||
'sk': '0',
|
||||
'total': '',
|
||||
'discount': '',
|
||||
'due_date': '',
|
||||
'created_at': now_,
|
||||
}
|
||||
| ({'coupon': coupon.code} if coupon else {})
|
||||
| payload.model_dump()
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
'id': order_id,
|
||||
'sk': 'ITEMS',
|
||||
'items': [],
|
||||
'created_at': now_,
|
||||
}
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
'id': order_id,
|
||||
'sk': 'ADDRESS',
|
||||
'created_at': now_,
|
||||
}
|
||||
| address.model_dump()
|
||||
)
|
||||
|
||||
if coupon:
|
||||
transact.put(
|
||||
item={
|
||||
'id': order_id,
|
||||
'sk': 'COUPON',
|
||||
'created_at': now_,
|
||||
}
|
||||
| coupon.model_dump()
|
||||
)
|
||||
|
||||
return JSONResponse(body={'id': order_id}, status_code=HTTPStatus.CREATED)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import json
|
||||
from http import HTTPMethod, HTTPStatus
|
||||
from pprint import pprint
|
||||
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer, PartitionKey
|
||||
|
||||
from ...conftest import HttpApiProxy, LambdaContext
|
||||
|
||||
@@ -20,9 +22,9 @@ def test_checkout(
|
||||
'org_id': 'f6000f79-6e5c-49a0-952f-3bda330ef278',
|
||||
'cnpj': '00000000000191',
|
||||
'name': 'Branco do Brasil',
|
||||
'email': 'sergio@somosbeta.com.br',
|
||||
'payment_method': 'MANUAL',
|
||||
'user': {
|
||||
'email': 'bb@users.noreply.saladeaula.digital',
|
||||
'payment_method': 'BANK_SLIP',
|
||||
'created_by': {
|
||||
'id': '15bacf02-1535-4bee-9022-19d106fd7518',
|
||||
'name': 'Sérgio R Siqueira',
|
||||
},
|
||||
@@ -46,46 +48,50 @@ def test_checkout(
|
||||
),
|
||||
lambda_context,
|
||||
)
|
||||
print(r)
|
||||
body = json.loads(r['body'])
|
||||
print(body)
|
||||
assert r['statusCode'] == HTTPStatus.CREATED
|
||||
|
||||
r = dynamodb_persistence_layer.collection.query(PartitionKey(body['id']))
|
||||
pprint(r['items'])
|
||||
|
||||
def test_checkout_from_user(
|
||||
app,
|
||||
seeds,
|
||||
http_api_proxy: HttpApiProxy,
|
||||
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
|
||||
lambda_context: LambdaContext,
|
||||
):
|
||||
r = app.lambda_handler(
|
||||
http_api_proxy(
|
||||
raw_path='/orders',
|
||||
method=HTTPMethod.POST,
|
||||
body={
|
||||
'user_id': '15bacf02-1535-4bee-9022-19d106fd7518',
|
||||
'cpf': '07879819908',
|
||||
'name': 'Sérgio R Siqueira',
|
||||
'email': 'sergio@somosbeta.com.br',
|
||||
'payment_method': 'MANUAL',
|
||||
'address': {
|
||||
'city': 'Curitiba',
|
||||
'postcode': '81280350',
|
||||
'neighborhood': 'Cidade Industrial',
|
||||
'address1': 'Rua Monsenhor Ivo Zanlorenzi',
|
||||
'address2': 'nº 5190, ap 1802',
|
||||
'state': 'PR',
|
||||
},
|
||||
'items': [
|
||||
{
|
||||
'id': 'e1c44881-2fe3-484e-ada2-12b6bf5b9398',
|
||||
'name': 'NR-35 Segurança nos Trabalhos em Altura',
|
||||
'quantity': 2,
|
||||
'unit_price': 119,
|
||||
}
|
||||
],
|
||||
},
|
||||
),
|
||||
lambda_context,
|
||||
)
|
||||
print(r)
|
||||
assert r['statusCode'] == HTTPStatus.CREATED
|
||||
|
||||
# def test_checkout_from_user(
|
||||
# app,
|
||||
# seeds,
|
||||
# http_api_proxy: HttpApiProxy,
|
||||
# dynamodb_persistence_layer: DynamoDBPersistenceLayer,
|
||||
# lambda_context: LambdaContext,
|
||||
# ):
|
||||
# r = app.lambda_handler(
|
||||
# http_api_proxy(
|
||||
# raw_path='/orders',
|
||||
# method=HTTPMethod.POST,
|
||||
# body={
|
||||
# 'user_id': '15bacf02-1535-4bee-9022-19d106fd7518',
|
||||
# 'cpf': '07879819908',
|
||||
# 'name': 'Sérgio R Siqueira',
|
||||
# 'email': 'sergio@somosbeta.com.br',
|
||||
# 'payment_method': 'MANUAL',
|
||||
# 'address': {
|
||||
# 'city': 'Curitiba',
|
||||
# 'postcode': '81280350',
|
||||
# 'neighborhood': 'Cidade Industrial',
|
||||
# 'address1': 'Rua Monsenhor Ivo Zanlorenzi',
|
||||
# 'address2': 'nº 5190, ap 1802',
|
||||
# 'state': 'PR',
|
||||
# },
|
||||
# 'items': [
|
||||
# {
|
||||
# 'id': 'e1c44881-2fe3-484e-ada2-12b6bf5b9398',
|
||||
# 'name': 'NR-35 Segurança nos Trabalhos em Altura',
|
||||
# 'quantity': 2,
|
||||
# 'unit_price': 119,
|
||||
# }
|
||||
# ],
|
||||
# },
|
||||
# ),
|
||||
# lambda_context,
|
||||
# )
|
||||
# print(r)
|
||||
# assert r['statusCode'] == HTTPStatus.CREATED
|
||||
|
||||
Reference in New Issue
Block a user