diff --git a/http-api/app/routes/orgs/address.py b/http-api/app/routes/orgs/address.py index 134172c..3920b4a 100644 --- a/http-api/app/routes/orgs/address.py +++ b/http-api/app/routes/orgs/address.py @@ -1,11 +1,13 @@ +import re from http import HTTPStatus from aws_lambda_powertools.event_handler.api_gateway import Router +from layercake.dateutils import now from layercake.dynamodb import ( DynamoDBPersistenceLayer, KeyPair, ) -from pydantic import BaseModel +from pydantic import BaseModel, ConfigDict, field_validator from api_gateway import JSONResponse from boto3clients import dynamodb_client @@ -24,10 +26,14 @@ org_layer = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client) def get_address(id: str): return org_layer.collection.get_item( KeyPair(id, 'metadata#address'), + raise_on_error=False, + default={}, ) class Address(BaseModel): + model_config = ConfigDict(str_strip_whitespace=True) + postcode: str address1: str address2: str | None = None @@ -35,13 +41,19 @@ class Address(BaseModel): city: str state: str + @field_validator('postcode') + @classmethod + def _only_numbers(cls, v: str) -> str: + return re.sub(r'\D', '', v) + @router.post('//address', compress=True, tags=['Organization']) def post_address(id: str, payload: Address): - address = payload.model_dump() + now_ = now() + address = payload.model_dump(exclude_none=True) org_layer.collection.put_item( key=KeyPair(id, 'metadata#address'), - cond_expr='attribute_exists(sk)', + updated_at=now_, **address, ) diff --git a/http-api/app/routes/users/emails.py b/http-api/app/routes/users/emails.py index 5079a97..0a4a1e7 100644 --- a/http-api/app/routes/users/emails.py +++ b/http-api/app/routes/users/emails.py @@ -5,7 +5,6 @@ from aws_lambda_powertools.event_handler.exceptions import ( BadRequestError as PowertoolsBadRequestError, ) from layercake.dynamodb import ( - DynamoDBCollection, DynamoDBPersistenceLayer, KeyPair, MissingError, @@ -25,7 +24,6 @@ class BadRequestError(MissingError, PowertoolsBadRequestError): ... router = Router() user_layer = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client) -user_collect = DynamoDBCollection(user_layer, exc_cls=BadRequestError) @router.get( @@ -37,7 +35,7 @@ user_collect = DynamoDBCollection(user_layer, exc_cls=BadRequestError) def get_emails(id: str): start_key = router.current_event.get_query_string_value('start_key', None) - return user_collect.query( + return user_layer.collection.query( KeyPair(id, PrefixKey('emails')), start_key=start_key, ) @@ -52,7 +50,7 @@ class Email(BaseModel): compress=True, tags=['User'], summary='Add user email', - middlewares=[AuditLogMiddleware('EMAIL_ADD', user_collect, ('email',))], + middlewares=[AuditLogMiddleware('EMAIL_ADD', user_layer.collection, ('email',))], ) def post_email(id: str, payload: Email): add_email(id, payload.email, persistence_layer=user_layer) @@ -77,7 +75,7 @@ class EmailAsPrimary(BaseModel): middlewares=[ AuditLogMiddleware( 'EMAIL_CHANGE', - user_collect, + user_layer.collection, ( 'new_email', 'old_email', @@ -105,7 +103,7 @@ def patch_email(id: str, payload: EmailAsPrimary): compress=True, tags=['User'], summary='Delete user email', - middlewares=[AuditLogMiddleware('EMAIL_DEL', user_collect, ('email',))], + middlewares=[AuditLogMiddleware('EMAIL_DEL', user_layer.collection, ('email',))], ) def delete_email(id: str, payload: Email): del_email( diff --git a/http-api/tests/routes/test_orgs.py b/http-api/tests/routes/test_orgs.py index fa482d8..9797162 100644 --- a/http-api/tests/routes/test_orgs.py +++ b/http-api/tests/routes/test_orgs.py @@ -93,9 +93,9 @@ def test_post_address( raw_path='/orgs/cJtK9SsnJhKPyxESe7g3DG/address', method=HTTPMethod.POST, body={ - 'postcode': '81280350', - 'address1': 'Rua Monsenhor Ivo Zanlorenzi, 5190', - 'address2': 'ap 1802', + 'postcode': '81280-350', + 'address1': 'Rua Monsenhor Ivo Zanlorenzi, 5190 ', + 'address2': ' ap 1802', 'neighborhood': 'Cidade Industrial', 'city': 'Curitiba', 'state': 'PR', @@ -109,3 +109,4 @@ def test_post_address( KeyPair('cJtK9SsnJhKPyxESe7g3DG', 'metadata#address') ) assert data['address1'] == 'Rua Monsenhor Ivo Zanlorenzi, 5190' + assert data['postcode'] == '81280350'