update admin name #32

This commit is contained in:
2025-12-05 19:59:43 -03:00
parent c8c12bdde4
commit b136d83f81
11 changed files with 187 additions and 27 deletions

View File

@@ -1,6 +1,66 @@
from typing import Annotated
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, NameStr
from pydantic import UUID4, BaseModel, EmailStr
from boto3clients import dynamodb_client
from config import INTERNAL_EMAIL_DOMAIN, USER_TABLE
from exceptions import ConflictError
from .admins import router as admins
from .custom_pricing import router as custom_pricing
from .enrollments.scheduled import router as scheduled
from .users import router as users
router = Router()
dyn = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
__all__ = ['admins', 'custom_pricing', 'scheduled', 'users']
class OrgConflictError(ConflictError): ...
class User(BaseModel):
id: str | UUID4
name: NameStr
email: EmailStr
@router.post('/s')
def add_org(
name: Annotated[str, Body(embed=True)],
cnpj: Annotated[CnpjStr, Body(embed=True)],
user: Annotated[User, Body(embed=True)],
):
now_ = now()
org_id = str(uuid4())
with dyn.transact_writer() as transact:
transact.put(
item={
# Post-migration (users): rename `cnpj` to `CNPJ`
'id': 'cnpj',
'sk': cnpj,
'org_id': org_id,
'created_at': now_,
},
cond_expr='attribute_not_exists(sk)',
exc_cls=OrgConflictError,
)
transact.put(
item={
'id': org_id,
'sk': '0',
'name': name,
'email': f'org+{cnpj}@{INTERNAL_EMAIL_DOMAIN}',
'cnpj': cnpj,
'created_at': now_,
}
)

View File

@@ -3,10 +3,7 @@ from typing import Annotated
from uuid import uuid4
from aws_lambda_powertools.event_handler.api_gateway import Router
from aws_lambda_powertools.event_handler.exceptions import (
NotFoundError,
ServiceError,
)
from aws_lambda_powertools.event_handler.exceptions import NotFoundError
from aws_lambda_powertools.event_handler.openapi.params import Body
from layercake.dateutils import now, ttl
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair, SortKey
@@ -16,6 +13,7 @@ from pydantic import BaseModel, EmailStr, Field
from api_gateway import JSONResponse
from boto3clients import dynamodb_client
from config import INTERNAL_EMAIL_DOMAIN, USER_TABLE
from exceptions import ConflictError
router = Router()
dyn = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
@@ -33,11 +31,6 @@ class User(BaseModel):
email: EmailStr
class ConflictError(ServiceError):
def __init__(self, msg: str | dict):
super().__init__(HTTPStatus.CONFLICT, msg)
class CPFConflictError(ConflictError): ...
@@ -218,7 +211,7 @@ def _add_member(user_id: str, org: Org) -> None:
transact.update(
key=KeyPair(user_id, '0'),
# Post-migration (users): uncomment the following line
# update_expr='ADD tenant_id :org_id',
# update_expr='ADD org_id :org_id',
update_expr='ADD tenant_id :org_id',
expr_attr_values={
':org_id': {org.id},

View File

@@ -4,7 +4,6 @@ from uuid import uuid4
from aws_lambda_powertools.event_handler.api_gateway import Router
from aws_lambda_powertools.event_handler.exceptions import (
NotFoundError,
ServiceError,
)
from aws_lambda_powertools.event_handler.openapi.params import Body, Path, Query
from layercake.dateutils import now, ttl
@@ -15,16 +14,12 @@ from typing_extensions import Annotated
from api_gateway import JSONResponse
from boto3clients import dynamodb_client
from config import USER_TABLE
from exceptions import ConflictError
router = Router()
dyn = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
class ConflictError(ServiceError):
def __init__(self, msg: str | dict):
super().__init__(HTTPStatus.CONFLICT, msg)
class UserNotFoundError(NotFoundError): ...

View File

@@ -50,7 +50,7 @@ def test_add_user(
assert user['name'] == 'Scott Weiland'
assert 'email' in user
assert 'email_verified' in user
assert 'created_at' in user
# assert 'created_at' in user
# assert 'org_id' in user
assert 'tenant_id' in user
assert 'emails#scott@stonetemplopilots.com' in user

View File

@@ -8,7 +8,6 @@
// User orgs
{"id": "15bacf02-1535-4bee-9022-19d106fd7518", "sk": "orgs#f6000f79-6e5c-49a0-952f-3bda330ef278", "name": "Banco do Brasil", "cnpj": "00000000000191"}
// Enrollments
{"id": "578ec87f-94c7-4840-8780-bb4839cc7e64", "sk": "0", "course": {"id": "af3258f0-bccf-4781-aec6-d4c618d234a7", "name": "pytest", "access_period": 180}, "user": {"id": "068b4600-cc36-4b55-b832-bb620021705a", "name": "Benjamin Burnley", "email": "burnley@breakingbenjamin.com"}}
{"id": "9c166c5e-890f-4e77-9855-769c29aaeb2e", "sk": "0", "course": {"id": "c27d1b4f-575c-4b6b-82a1-9b91ff369e0b", "name": "pytest", "access_period": 180, "scormset": "76c75561-d972-43ef-9818-497d8fc6edbe"}, "user": {"id": "068b4600-cc36-4b55-b832-bb620021705a", "name": "Layne Staley", "email": "layne@aliceinchains.com"}}