add endpoint
This commit is contained in:
@@ -17,6 +17,7 @@ from aws_lambda_powertools.utilities.typing import LambdaContext
|
||||
from api_gateway import JSONResponse
|
||||
from middlewares import AuthenticationMiddleware
|
||||
from routes import (
|
||||
billing,
|
||||
courses,
|
||||
enrollments,
|
||||
lookup,
|
||||
@@ -62,6 +63,7 @@ app.include_router(users.router, prefix='/users')
|
||||
app.include_router(users.logs, prefix='/users')
|
||||
app.include_router(users.emails, prefix='/users')
|
||||
app.include_router(users.orgs, prefix='/users')
|
||||
app.include_router(billing.router, prefix='/billing')
|
||||
app.include_router(orgs.policies, prefix='/orgs')
|
||||
app.include_router(orgs.address, prefix='/orgs')
|
||||
app.include_router(orgs.custom_pricing, prefix='/orgs')
|
||||
|
||||
@@ -40,4 +40,7 @@
|
||||
{"sk": {"S": "user"}, "user_id": {"S": "mESNbpk4pMTASxtnstd37B"}, "id": {"S": "bTRW6w69aYYKjKy3FZeVjt"}, "create_date": {"S": "2024-02-06T10:28:06.906367-03:00"}}
|
||||
{"id": {"S": "QV4sXY3DvSTUMGJ4QqsrwJ"},"sk": {"S": "0"},"assignee": {"M": {"name": {"S": "Sérgio Rafael de Siqueira"}}},"cnpj": {"S": "15608435000190"},"create_date": {"S": "2024-11-02T19:39:57.174669-03:00"},"due_date": {"S": "2024-11-03T19:39:09.897000-03:00"},"email": {"S": "sergio@somosbeta.com.br"},"name": {"S": "Beta Educação"},"payment_date": {"S": "2024-11-02T19:40:12.143000-03:00"},"payment_method": {"S": "MANUAL"},"status": {"S": "PAID"},"total": {"N": "10"},"update_date": {"S": "2024-11-02T19:40:12.831120-03:00"}}
|
||||
{"id": {"S": "QV4sXY3DvSTUMGJ4QqsrwJ"},"sk": {"S": "generated_items#7bzQhaPEB9uR8jRkzkFe2h"},"create_date": {"S": "2025-04-11T15:08:00.256341-03:00"},"status": {"S": "SUCCESS"}}
|
||||
{"id": {"S": "123"}, "sk": {"S": "0"}}
|
||||
{"id": {"S": "BILLING#ORG#edp8njvgQuzNkLx2ySNfAD"},"sk": {"S": "START#2025-07-01#END#2025-07-31"},"created_at": {"S": "2025-07-24T15:46:43.312549-03:00"},"status": {"S": "PENDING"}}
|
||||
{"id": {"S": "BILLING#ORG#edp8njvgQuzNkLx2ySNfAD"},"sk": {"S": "START#2025-07-01#END#2025-07-31#ENROLLMENT#556e99cf-18b2-459c-a46d-f71a807ba551"},"author": {"M": {"id": {"S": "SMEXYk5MQkKCzknJpxqr8n"},"name": {"S": "Carolina Brand"}}},"course": {"M": {"id": {"S": "5c119d4b-573c-4d8d-a99d-63756af2f4c5"},"name": {"S": "NR-06 - Equipamento de Proteção Individual - EPI"}}},"created_at": {"S": "2025-07-24T16:42:41.673797-03:00"},"enrolled_at": {"S": "2025-07-24T15:46:37.162960-03:00"},"unit_price": {"N": "79.2"},"user": {"M": {"id": {"S": "02157895558"},"name": {"S": "ERICK ALVES DOS SANTOS"}}}}
|
||||
{"id": {"S": "BILLING#ORG#edp8njvgQuzNkLx2ySNfAD"},"sk": {"S": "START#2025-07-01#END#2025-07-31#ENROLLMENT#d2124d5a-caaf-4e27-9edb-8380faf15f35"},"author": {"M": {"id": {"S": "SMEXYk5MQkKCzknJpxqr8n"},"name": {"S": "Carolina Brand"}}},"course": {"M": {"id": {"S": "a810dd22-56c0-4d9b-8cd2-7e2ee9c45839"},"name": {"S": "NR-11 – Transporte, movimentação, armazenagem e manuseio de materiais"}}},"created_at": {"S": "2025-07-25T03:31:17.306858-03:00"},"enrolled_at": {"S": "2025-07-25T03:31:11.736247-03:00"},"unit_price": {"N": "87.2"},"user": {"M": {"id": {"S": "02157895558"},"name": {"S": "ERICK ALVES DOS SANTOS"}}}}
|
||||
{"id": {"S": "BILLING#ORG#edp8njvgQuzNkLx2ySNfAD"},"sk": {"S": "START#2025-07-01#END#2025-07-31#SCHEDULE#AUTO_CLOSE"},"created_at": {"S": "2025-07-24T15:46:43.312549-03:00"},"ttl": {"N": "1754017200"}}
|
||||
69
http-api/tests/routes/test_billing.py
Normal file
69
http-api/tests/routes/test_billing.py
Normal file
@@ -0,0 +1,69 @@
|
||||
import json
|
||||
from http import HTTPMethod, HTTPStatus
|
||||
|
||||
from ..conftest import HttpApiProxy, LambdaContext
|
||||
|
||||
|
||||
def test_billing(
|
||||
mock_app,
|
||||
dynamodb_seeds,
|
||||
http_api_proxy: HttpApiProxy,
|
||||
lambda_context: LambdaContext,
|
||||
):
|
||||
org_id = 'edp8njvgQuzNkLx2ySNfAD'
|
||||
# This data was added from seeds
|
||||
r = mock_app.lambda_handler(
|
||||
http_api_proxy(
|
||||
raw_path=f'/billing/{org_id}',
|
||||
method=HTTPMethod.GET,
|
||||
queryStringParameters={
|
||||
'start_date': '2025-07-01',
|
||||
'end_date': '2025-07-31',
|
||||
},
|
||||
),
|
||||
lambda_context,
|
||||
)
|
||||
|
||||
assert r['statusCode'] == HTTPStatus.OK
|
||||
data = json.loads(r['body'])
|
||||
expected = [
|
||||
{
|
||||
'sk': 'START#2025-07-01#END#2025-07-31#SCHEDULE#AUTO_CLOSE',
|
||||
'created_at': '2025-07-24T15:46:43.312549-03:00',
|
||||
'id': 'BILLING#ORG#edp8njvgQuzNkLx2ySNfAD',
|
||||
'ttl': 1754017200,
|
||||
},
|
||||
{
|
||||
'enrolled_at': '2025-07-25T03:31:11.736247-03:00',
|
||||
'author': {'name': 'Carolina Brand', 'id': 'SMEXYk5MQkKCzknJpxqr8n'},
|
||||
'sk': 'START#2025-07-01#END#2025-07-31#ENROLLMENT#d2124d5a-caaf-4e27-9edb-8380faf15f35',
|
||||
'course': {
|
||||
'name': 'NR-11 – Transporte, movimentação, armazenagem e manuseio de materiais',
|
||||
'id': 'a810dd22-56c0-4d9b-8cd2-7e2ee9c45839',
|
||||
},
|
||||
'created_at': '2025-07-25T03:31:17.306858-03:00',
|
||||
'id': 'BILLING#ORG#edp8njvgQuzNkLx2ySNfAD',
|
||||
'unit_price': 87.2,
|
||||
'user': {'name': 'ERICK ALVES DOS SANTOS', 'id': '02157895558'},
|
||||
},
|
||||
{
|
||||
'enrolled_at': '2025-07-24T15:46:37.162960-03:00',
|
||||
'author': {'name': 'Carolina Brand', 'id': 'SMEXYk5MQkKCzknJpxqr8n'},
|
||||
'sk': 'START#2025-07-01#END#2025-07-31#ENROLLMENT#556e99cf-18b2-459c-a46d-f71a807ba551',
|
||||
'course': {
|
||||
'name': 'NR-06 - Equipamento de Proteção Individual - EPI',
|
||||
'id': '5c119d4b-573c-4d8d-a99d-63756af2f4c5',
|
||||
},
|
||||
'created_at': '2025-07-24T16:42:41.673797-03:00',
|
||||
'id': 'BILLING#ORG#edp8njvgQuzNkLx2ySNfAD',
|
||||
'unit_price': 79.2,
|
||||
'user': {'name': 'ERICK ALVES DOS SANTOS', 'id': '02157895558'},
|
||||
},
|
||||
{
|
||||
'sk': 'START#2025-07-01#END#2025-07-31',
|
||||
'created_at': '2025-07-24T15:46:43.312549-03:00',
|
||||
'id': 'BILLING#ORG#edp8njvgQuzNkLx2ySNfAD',
|
||||
'status': 'PENDING',
|
||||
},
|
||||
]
|
||||
assert data['items'] == expected
|
||||
@@ -35,3 +35,7 @@
|
||||
{"sk": {"S": "user"}, "user_id": {"S": "5AZXXXCWa2bU4spsxfLznx"}, "id": {"S": "KpZTYvu4RzgMJW3A2DF6cC"}, "create_date": {"S": "2024-02-08T08:42:05.190415-03:00"}}
|
||||
{"id": {"S": "15ee05a3-4ceb-4b7e-9979-db75b28c9ade"}, "sk": {"S": "0"}, "name": {"S": "pytest"}}
|
||||
{"id": {"S": "CUSTOM_PRICING#ORG#cJtK9SsnJhKPyxESe7g3DG"}, "sk": {"S": "COURSE#281198c2-f293-4acc-b96e-e4a2d5f6b73c"}, "unit_price": {"N": "199"}}
|
||||
{"id": {"S": "BILLING#ORG#edp8njvgQuzNkLx2ySNfAD"},"sk": {"S": "START#2025-07-01#END#2025-07-31"},"created_at": {"S": "2025-07-24T15:46:43.312549-03:00"},"status": {"S": "PENDING"}}
|
||||
{"id": {"S": "BILLING#ORG#edp8njvgQuzNkLx2ySNfAD"},"sk": {"S": "START#2025-07-01#END#2025-07-31#ENROLLMENT#556e99cf-18b2-459c-a46d-f71a807ba551"},"author": {"M": {"id": {"S": "SMEXYk5MQkKCzknJpxqr8n"},"name": {"S": "Carolina Brand"}}},"course": {"M": {"id": {"S": "5c119d4b-573c-4d8d-a99d-63756af2f4c5"},"name": {"S": "NR-06 - Equipamento de Proteção Individual - EPI"}}},"created_at": {"S": "2025-07-24T16:42:41.673797-03:00"},"enrolled_at": {"S": "2025-07-24T15:46:37.162960-03:00"},"unit_price": {"N": "79.2"},"user": {"M": {"id": {"S": "02157895558"},"name": {"S": "ERICK ALVES DOS SANTOS"}}}}
|
||||
{"id": {"S": "BILLING#ORG#edp8njvgQuzNkLx2ySNfAD"},"sk": {"S": "START#2025-07-01#END#2025-07-31#ENROLLMENT#d2124d5a-caaf-4e27-9edb-8380faf15f35"},"author": {"M": {"id": {"S": "SMEXYk5MQkKCzknJpxqr8n"},"name": {"S": "Carolina Brand"}}},"course": {"M": {"id": {"S": "a810dd22-56c0-4d9b-8cd2-7e2ee9c45839"},"name": {"S": "NR-11 – Transporte, movimentação, armazenagem e manuseio de materiais"}}},"created_at": {"S": "2025-07-25T03:31:17.306858-03:00"},"enrolled_at": {"S": "2025-07-25T03:31:11.736247-03:00"},"unit_price": {"N": "87.2"},"user": {"M": {"id": {"S": "02157895558"},"name": {"S": "ERICK ALVES DOS SANTOS"}}}}
|
||||
{"id": {"S": "BILLING#ORG#edp8njvgQuzNkLx2ySNfAD"},"sk": {"S": "START#2025-07-01#END#2025-07-31#SCHEDULE#AUTO_CLOSE"},"created_at": {"S": "2025-07-24T15:46:43.312549-03:00"},"ttl": {"N": "1754017200"}}
|
||||
@@ -1,125 +0,0 @@
|
||||
from datetime import datetime, time, timedelta
|
||||
|
||||
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, ttl
|
||||
from layercake.dynamodb import (
|
||||
DynamoDBPersistenceLayer,
|
||||
KeyPair,
|
||||
SortKey,
|
||||
TransactKey,
|
||||
)
|
||||
from layercake.funcs import pick
|
||||
|
||||
from boto3clients import dynamodb_client
|
||||
from config import COURSE_TABLE, ENROLLMENT_TABLE, ORDER_TABLE
|
||||
from utils import get_billing_period
|
||||
|
||||
logger = Logger(__name__)
|
||||
order_layer = DynamoDBPersistenceLayer(ORDER_TABLE, dynamodb_client)
|
||||
enrollment_layer = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
|
||||
course_layer = DynamoDBPersistenceLayer(COURSE_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()
|
||||
enrollment_id = new_image['id']
|
||||
org_id = new_image['org_id']
|
||||
data = enrollment_layer.collection.get_items(
|
||||
TransactKey(enrollment_id) + SortKey('0') + SortKey('author')
|
||||
)
|
||||
|
||||
if not data:
|
||||
logger.debug('Enrollment not found')
|
||||
return False
|
||||
|
||||
start_date, end_date = get_billing_period(new_image['billing_day'])
|
||||
pk = 'BILLING#ORG#{org_id}'.format(org_id=org_id)
|
||||
sk = 'START#{start}#END#{end}'.format(
|
||||
start=start_date.isoformat(),
|
||||
end=end_date.isoformat(),
|
||||
)
|
||||
|
||||
try:
|
||||
with order_layer.transact_writer() as transact:
|
||||
transact.put(
|
||||
item={
|
||||
'id': pk,
|
||||
'sk': sk,
|
||||
'status': 'PENDING',
|
||||
'created_at': now_,
|
||||
},
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
exc_cls=ExistingBillingConflictError,
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
'id': pk,
|
||||
'sk': f'{sk}#SCHEDULE#AUTO_CLOSE',
|
||||
'ttl': ttl(
|
||||
start_dt=datetime.combine(end_date, time()) + timedelta(days=1)
|
||||
),
|
||||
'created_at': now_,
|
||||
}
|
||||
)
|
||||
except ExistingBillingConflictError:
|
||||
pass
|
||||
|
||||
try:
|
||||
with order_layer.transact_writer() as transact:
|
||||
author = data['author']
|
||||
course_id = data['course']['id']
|
||||
course = course_layer.collection.get_items(
|
||||
KeyPair(
|
||||
pk=course_id,
|
||||
sk=SortKey('0', path_spec='metadata__unit_price'),
|
||||
rename_key='unit_price',
|
||||
)
|
||||
+ KeyPair(
|
||||
pk=f'CUSTOM_PRICING#ORG#{org_id}',
|
||||
sk=SortKey(f'COURSE#{course_id}', path_spec='unit_price'),
|
||||
rename_key='unit_price',
|
||||
),
|
||||
flatten_top=False,
|
||||
)
|
||||
|
||||
transact.condition(
|
||||
key=KeyPair(pk, sk),
|
||||
cond_expr='attribute_exists(sk)',
|
||||
exc_cls=BillingNotFoundError,
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
'id': pk,
|
||||
'sk': f'{sk}#ENROLLMENT#{enrollment_id}',
|
||||
'user': pick(('id', 'name'), data['user']),
|
||||
'course': pick(('id', 'name'), data['course']),
|
||||
'unit_price': course['unit_price'],
|
||||
'author': {
|
||||
'id': author['user_id'],
|
||||
'name': author['name'],
|
||||
},
|
||||
# Post-migration: uncomment the following line
|
||||
# 'enrolled_at': data['created_at'],
|
||||
'enrolled_at': data['create_date'],
|
||||
'created_at': now_,
|
||||
},
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
)
|
||||
except Exception:
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
|
||||
class ExistingBillingConflictError(Exception): ...
|
||||
|
||||
|
||||
class BillingNotFoundError(Exception): ...
|
||||
@@ -42,10 +42,10 @@ Resources:
|
||||
Properties:
|
||||
RetentionInDays: 90
|
||||
|
||||
EventReportingAddItemFunction:
|
||||
EventBillingAddEnrollmentFunction:
|
||||
Type: AWS::Serverless::Function
|
||||
Properties:
|
||||
Handler: events.reporting.add_item.lambda_handler
|
||||
Handler: events.billing.add_enrollment.lambda_handler
|
||||
LoggingConfig:
|
||||
LogGroup: !Ref EventLog
|
||||
Policies:
|
||||
|
||||
Reference in New Issue
Block a user