This commit is contained in:
2025-04-13 01:11:44 -03:00
parent bef51f492a
commit 273c580139
20 changed files with 552 additions and 29 deletions

View File

@@ -1,11 +1,11 @@
import json
from typing import TypedDict
from aws_lambda_powertools.event_handler.api_gateway import Router
from elasticsearch import Elasticsearch
from layercake.dynamodb import (
DynamoDBCollection,
DynamoDBPersistenceLayer,
KeyPair,
SortKey,
TransactKey,
)
@@ -13,6 +13,7 @@ from pydantic import UUID4, BaseModel
import elastic
from boto3clients import dynamodb_client
from enrollment import set_status_as_canceled
from middlewares.audit_log_middleware import AuditLogMiddleware
from middlewares.authorizer_middleware import User
from settings import ELASTIC_CONN, ENROLLMENT_TABLE, USER_TABLE
@@ -20,8 +21,8 @@ from settings import ELASTIC_CONN, ENROLLMENT_TABLE, USER_TABLE
router = Router()
elastic_client = Elasticsearch(**ELASTIC_CONN)
enrollment_layer = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
user_layer = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
enrollment_collect = DynamoDBCollection(enrollment_layer)
user_layer = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
user_collect = DynamoDBCollection(user_layer)
@@ -50,7 +51,7 @@ def get_enrollment(id: str):
+ SortKey('canceled_date')
+ SortKey('archived_date')
+ SortKey('cancel_policy')
+ SortKey('parent_vacancy')
+ SortKey('parent_vacancy', path_spec='vacancy')
+ SortKey('lock', path_spec='hash')
+ SortKey('author')
+ SortKey('tenant')
@@ -58,14 +59,11 @@ def get_enrollment(id: str):
)
class Course(TypedDict):
id: str
name: str
class Cancel(BaseModel):
id: UUID4 | str
course: Course
lock_hash: str
course: dict = {}
vacancy: dict = {}
@router.patch(
@@ -78,6 +76,16 @@ class Cancel(BaseModel):
)
def cancel(id: str, payload: Cancel):
user: User = router.context['user']
set_status_as_canceled(
id,
lock_hash=payload.lock_hash,
author=user.model_dump(), # type: ignore
course=payload.course, # type: ignore
vacancy_key=KeyPair.parse_obj(payload.vacancy),
persistence_layer=enrollment_layer,
)
return payload

View File

@@ -0,0 +1,69 @@
from http import HTTPStatus
from aws_lambda_powertools.event_handler import Response, content_types
from aws_lambda_powertools.event_handler.api_gateway import Router
from aws_lambda_powertools.event_handler.exceptions import (
BadRequestError,
)
from layercake.dynamodb import (
DynamoDBCollection,
DynamoDBPersistenceLayer,
SortKey,
TransactKey,
)
from pydantic.main import BaseModel
from typing_extensions import Literal
from boto3clients import dynamodb_client
from org import update_policies
from settings import USER_TABLE
router = Router()
org_layer = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
org_collect = DynamoDBCollection(org_layer, exception_cls=BadRequestError)
@router.get(
'/<id>/policies',
compress=True,
tags=['Organization'],
summary='Get organization policies',
)
def get_policies(id: str):
return org_collect.get_items(
TransactKey(id) + SortKey('billing_policy') + SortKey('payment_policy'),
flatten_top=False,
)
class BillingPolicy(BaseModel):
billing_day: int
payment_method: Literal['PIX', 'BANK_SLIP', 'MANUAL']
class PaymentPolicy(BaseModel):
due_days: int
class Policies(BaseModel):
billing_policy: BillingPolicy | None = None
payment_policy: PaymentPolicy | None = None
@router.put('/<id>/policies', compress=True, tags=['Organization'])
def put_policies(id: str, payload: Policies):
payment_policy = payload.payment_policy
billing_policy = payload.billing_policy
update_policies(
id,
payment_policy=payment_policy.model_dump() if payment_policy else {},
billing_policy=billing_policy.model_dump() if billing_policy else {},
persistence_layer=org_layer,
)
return Response(
body=payload,
content_type=content_types.APPLICATION_JSON,
status_code=HTTPStatus.OK,
)

View File

@@ -2,6 +2,7 @@ import json
from http import HTTPStatus
from typing import Annotated
from aws_lambda_powertools.event_handler import content_types
from aws_lambda_powertools.event_handler.api_gateway import (
Response,
Router,
@@ -19,7 +20,7 @@ from layercake.dynamodb import (
PartitionKey,
PrefixKey,
)
from pydantic import UUID4, BaseModel, StringConstraints
from pydantic import UUID4, BaseModel, EmailStr, StringConstraints
import cognito
import elastic
@@ -27,6 +28,7 @@ from boto3clients import dynamodb_client, idp_client
from middlewares import AuditLogMiddleware
from models import User
from settings import ELASTIC_CONN, USER_POOOL_ID, USER_TABLE
from user import add_email, del_email
class BadRequestError(MissingError, PowertoolsBadRequestError): ...
@@ -63,13 +65,13 @@ def post_user(payload: User):
return Response(status_code=HTTPStatus.CREATED)
class NewPassword(BaseModel):
class Password(BaseModel):
cognito_sub: UUID4
new_password: Annotated[str, StringConstraints(min_length=6)]
@router.patch('/<id>', compress=True, tags=['User'])
def patch_newpassword(id: str, payload: NewPassword):
@router.post('/<id>/password', compress=True, tags=['User'], include_in_schema=False)
def new_password(id: str, payload: Password):
return Response(status_code=HTTPStatus.OK)
@@ -100,6 +102,38 @@ def get_emails(id: str):
)
class Email(BaseModel):
email: EmailStr
@router.post(
'/<id>/emails',
compress=True,
tags=['User'],
summary='Add user email',
middlewares=[AuditLogMiddleware('EMAIL_ADD', user_collect, ('email',))],
)
def post_email(id: str, payload: Email):
assert add_email(id, payload.email, persistence_layer=user_layer)
return Response(
body=payload,
content_type=content_types.APPLICATION_JSON,
status_code=HTTPStatus.CREATED,
)
@router.delete(
'/<id>/emails',
compress=True,
tags=['User'],
summary='Delete user email',
middlewares=[AuditLogMiddleware('EMAIL_DEL', user_collect, ('email',))],
)
def delete_email(id: str, payload: Email):
assert del_email(id, payload.email, persistence_layer=user_layer)
return payload
@router.get(
'/<id>/logs',
compress=True,
@@ -109,8 +143,8 @@ def get_emails(id: str):
def get_logs(id: str):
return user_collect.query(
# Post-migration: uncomment to enable PartitionKey with a composite key (id with `logs` prefix).
# PartitionKey(ComposeKey(id, prefix='logs')),
PartitionKey(ComposeKey(id, prefix='log', delimiter=':')),
# PartitionKey(ComposeKey(id, 'logs')),
PartitionKey(ComposeKey(id, 'log', delimiter=':')),
start_key=router.current_event.get_query_string_value('start_key', None),
)