fix address
This commit is contained in:
@@ -1,11 +1,13 @@
|
|||||||
|
import re
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
|
|
||||||
from aws_lambda_powertools.event_handler.api_gateway import Router
|
from aws_lambda_powertools.event_handler.api_gateway import Router
|
||||||
|
from layercake.dateutils import now
|
||||||
from layercake.dynamodb import (
|
from layercake.dynamodb import (
|
||||||
DynamoDBPersistenceLayer,
|
DynamoDBPersistenceLayer,
|
||||||
KeyPair,
|
KeyPair,
|
||||||
)
|
)
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel, ConfigDict, field_validator
|
||||||
|
|
||||||
from api_gateway import JSONResponse
|
from api_gateway import JSONResponse
|
||||||
from boto3clients import dynamodb_client
|
from boto3clients import dynamodb_client
|
||||||
@@ -24,10 +26,14 @@ org_layer = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
|
|||||||
def get_address(id: str):
|
def get_address(id: str):
|
||||||
return org_layer.collection.get_item(
|
return org_layer.collection.get_item(
|
||||||
KeyPair(id, 'metadata#address'),
|
KeyPair(id, 'metadata#address'),
|
||||||
|
raise_on_error=False,
|
||||||
|
default={},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class Address(BaseModel):
|
class Address(BaseModel):
|
||||||
|
model_config = ConfigDict(str_strip_whitespace=True)
|
||||||
|
|
||||||
postcode: str
|
postcode: str
|
||||||
address1: str
|
address1: str
|
||||||
address2: str | None = None
|
address2: str | None = None
|
||||||
@@ -35,13 +41,19 @@ class Address(BaseModel):
|
|||||||
city: str
|
city: str
|
||||||
state: str
|
state: str
|
||||||
|
|
||||||
|
@field_validator('postcode')
|
||||||
|
@classmethod
|
||||||
|
def _only_numbers(cls, v: str) -> str:
|
||||||
|
return re.sub(r'\D', '', v)
|
||||||
|
|
||||||
|
|
||||||
@router.post('/<id>/address', compress=True, tags=['Organization'])
|
@router.post('/<id>/address', compress=True, tags=['Organization'])
|
||||||
def post_address(id: str, payload: Address):
|
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(
|
org_layer.collection.put_item(
|
||||||
key=KeyPair(id, 'metadata#address'),
|
key=KeyPair(id, 'metadata#address'),
|
||||||
cond_expr='attribute_exists(sk)',
|
updated_at=now_,
|
||||||
**address,
|
**address,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ from aws_lambda_powertools.event_handler.exceptions import (
|
|||||||
BadRequestError as PowertoolsBadRequestError,
|
BadRequestError as PowertoolsBadRequestError,
|
||||||
)
|
)
|
||||||
from layercake.dynamodb import (
|
from layercake.dynamodb import (
|
||||||
DynamoDBCollection,
|
|
||||||
DynamoDBPersistenceLayer,
|
DynamoDBPersistenceLayer,
|
||||||
KeyPair,
|
KeyPair,
|
||||||
MissingError,
|
MissingError,
|
||||||
@@ -25,7 +24,6 @@ class BadRequestError(MissingError, PowertoolsBadRequestError): ...
|
|||||||
|
|
||||||
router = Router()
|
router = Router()
|
||||||
user_layer = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
|
user_layer = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
|
||||||
user_collect = DynamoDBCollection(user_layer, exc_cls=BadRequestError)
|
|
||||||
|
|
||||||
|
|
||||||
@router.get(
|
@router.get(
|
||||||
@@ -37,7 +35,7 @@ user_collect = DynamoDBCollection(user_layer, exc_cls=BadRequestError)
|
|||||||
def get_emails(id: str):
|
def get_emails(id: str):
|
||||||
start_key = router.current_event.get_query_string_value('start_key', None)
|
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')),
|
KeyPair(id, PrefixKey('emails')),
|
||||||
start_key=start_key,
|
start_key=start_key,
|
||||||
)
|
)
|
||||||
@@ -52,7 +50,7 @@ class Email(BaseModel):
|
|||||||
compress=True,
|
compress=True,
|
||||||
tags=['User'],
|
tags=['User'],
|
||||||
summary='Add user email',
|
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):
|
def post_email(id: str, payload: Email):
|
||||||
add_email(id, payload.email, persistence_layer=user_layer)
|
add_email(id, payload.email, persistence_layer=user_layer)
|
||||||
@@ -77,7 +75,7 @@ class EmailAsPrimary(BaseModel):
|
|||||||
middlewares=[
|
middlewares=[
|
||||||
AuditLogMiddleware(
|
AuditLogMiddleware(
|
||||||
'EMAIL_CHANGE',
|
'EMAIL_CHANGE',
|
||||||
user_collect,
|
user_layer.collection,
|
||||||
(
|
(
|
||||||
'new_email',
|
'new_email',
|
||||||
'old_email',
|
'old_email',
|
||||||
@@ -105,7 +103,7 @@ def patch_email(id: str, payload: EmailAsPrimary):
|
|||||||
compress=True,
|
compress=True,
|
||||||
tags=['User'],
|
tags=['User'],
|
||||||
summary='Delete user email',
|
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):
|
def delete_email(id: str, payload: Email):
|
||||||
del_email(
|
del_email(
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ def test_post_address(
|
|||||||
raw_path='/orgs/cJtK9SsnJhKPyxESe7g3DG/address',
|
raw_path='/orgs/cJtK9SsnJhKPyxESe7g3DG/address',
|
||||||
method=HTTPMethod.POST,
|
method=HTTPMethod.POST,
|
||||||
body={
|
body={
|
||||||
'postcode': '81280350',
|
'postcode': '81280-350',
|
||||||
'address1': 'Rua Monsenhor Ivo Zanlorenzi, 5190 ',
|
'address1': 'Rua Monsenhor Ivo Zanlorenzi, 5190 ',
|
||||||
'address2': ' ap 1802',
|
'address2': ' ap 1802',
|
||||||
'neighborhood': 'Cidade Industrial',
|
'neighborhood': 'Cidade Industrial',
|
||||||
@@ -109,3 +109,4 @@ def test_post_address(
|
|||||||
KeyPair('cJtK9SsnJhKPyxESe7g3DG', 'metadata#address')
|
KeyPair('cJtK9SsnJhKPyxESe7g3DG', 'metadata#address')
|
||||||
)
|
)
|
||||||
assert data['address1'] == 'Rua Monsenhor Ivo Zanlorenzi, 5190'
|
assert data['address1'] == 'Rua Monsenhor Ivo Zanlorenzi, 5190'
|
||||||
|
assert data['postcode'] == '81280350'
|
||||||
|
|||||||
Reference in New Issue
Block a user