update
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
from http import HTTPStatus
|
||||
from typing import Annotated
|
||||
|
||||
from aws_lambda_powertools.event_handler.api_gateway import Router
|
||||
from aws_lambda_powertools.event_handler.exceptions import (
|
||||
BadRequestError as PowertoolsBadRequestError,
|
||||
)
|
||||
from aws_lambda_powertools.event_handler.openapi.params import Body
|
||||
from layercake.dynamodb import (
|
||||
DynamoDBPersistenceLayer,
|
||||
KeyPair,
|
||||
MissingError,
|
||||
PrefixKey,
|
||||
)
|
||||
from pydantic import BaseModel, EmailStr
|
||||
@@ -15,23 +13,13 @@ from pydantic import BaseModel, EmailStr
|
||||
from api_gateway import JSONResponse
|
||||
from boto3clients import dynamodb_client
|
||||
from config import USER_TABLE
|
||||
from middlewares import AuditLogMiddleware
|
||||
from rules.user import add_email, del_email, set_email_as_primary
|
||||
|
||||
|
||||
class BadRequestError(MissingError, PowertoolsBadRequestError): ...
|
||||
|
||||
from rules.user import add_email, remove_email, set_email_as_primary
|
||||
|
||||
router = Router()
|
||||
user_layer = DynamoDBPersistenceLayer(USER_TABLE, dynamodb_client)
|
||||
|
||||
|
||||
@router.get(
|
||||
'/<id>/emails',
|
||||
compress=True,
|
||||
tags=['User'],
|
||||
summary='Get user emails',
|
||||
)
|
||||
@router.get('/<id>/emails', compress=True)
|
||||
def get_emails(id: str):
|
||||
start_key = router.current_event.get_query_string_value('start_key', None)
|
||||
|
||||
@@ -45,18 +33,12 @@ class Email(BaseModel):
|
||||
email: EmailStr
|
||||
|
||||
|
||||
@router.post(
|
||||
'/<id>/emails',
|
||||
compress=True,
|
||||
tags=['User'],
|
||||
summary='Add user 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)
|
||||
@router.post('/<id>/emails', compress=True)
|
||||
def add_email_(id: str, email: Annotated[str, Body(embed=True)]):
|
||||
add_email(id, email, persistence_layer=user_layer)
|
||||
|
||||
return JSONResponse(
|
||||
body=payload,
|
||||
body={'email': email},
|
||||
status_code=HTTPStatus.CREATED,
|
||||
)
|
||||
|
||||
@@ -67,22 +49,7 @@ class EmailAsPrimary(BaseModel):
|
||||
email_verified: bool = False
|
||||
|
||||
|
||||
@router.patch(
|
||||
'/<id>/emails',
|
||||
compress=True,
|
||||
tags=['User'],
|
||||
summary='Add user email as primary',
|
||||
middlewares=[
|
||||
AuditLogMiddleware(
|
||||
'EMAIL_CHANGE',
|
||||
user_layer.collection,
|
||||
(
|
||||
'new_email',
|
||||
'old_email',
|
||||
),
|
||||
)
|
||||
],
|
||||
)
|
||||
@router.patch('/<id>/emails', compress=True)
|
||||
def patch_email(id: str, payload: EmailAsPrimary):
|
||||
set_email_as_primary(
|
||||
id,
|
||||
@@ -98,15 +65,9 @@ def patch_email(id: str, payload: EmailAsPrimary):
|
||||
)
|
||||
|
||||
|
||||
@router.delete(
|
||||
'/<id>/emails',
|
||||
compress=True,
|
||||
tags=['User'],
|
||||
summary='Delete user email',
|
||||
middlewares=[AuditLogMiddleware('EMAIL_DEL', user_layer.collection, ('email',))],
|
||||
)
|
||||
@router.delete('/<id>/emails', compress=True)
|
||||
def delete_email(id: str, payload: Email):
|
||||
del_email(
|
||||
remove_email(
|
||||
id,
|
||||
payload.email,
|
||||
persistence_layer=user_layer,
|
||||
|
||||
Reference in New Issue
Block a user