add routes
This commit is contained in:
@@ -1,6 +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,
|
||||
ServiceError,
|
||||
)
|
||||
from aws_lambda_powertools.event_handler.openapi.params import Path
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair, SortKey, TransactKey
|
||||
from layercake.extra_types import CnpjStr, CpfStr
|
||||
@@ -14,36 +19,55 @@ router = Router()
|
||||
dyn = DynamoDBPersistenceLayer(OAUTH2_TABLE, dynamodb_client)
|
||||
|
||||
|
||||
class UserAlreadyOnboardedError(ServiceError):
|
||||
def __init__(self, msg: str | dict):
|
||||
super().__init__(HTTPStatus.CONFLICT, msg)
|
||||
|
||||
|
||||
@router.get('/lookup')
|
||||
def lookup(
|
||||
email: Annotated[EmailStr, Path] = 'unknown',
|
||||
cpf: Annotated[CpfStr, Path] = 'unknown',
|
||||
cnpj: Annotated[CnpjStr, Path] = 'unknown',
|
||||
email: Annotated[EmailStr | None, Path] = None,
|
||||
cpf: Annotated[CpfStr | None, Path] = None,
|
||||
cnpj: Annotated[CnpjStr | None, Path] = None,
|
||||
):
|
||||
if not any([email, cpf, cnpj]):
|
||||
raise BadRequestError('Malformed body request')
|
||||
|
||||
r = dyn.collection.get_items(
|
||||
KeyPair(
|
||||
pk='email',
|
||||
sk=SortKey(email, path_spec='user_id'),
|
||||
rename_key='id',
|
||||
sk=SortKey(email, path_spec='user_id'), # type: ignore
|
||||
rename_key='user_id',
|
||||
)
|
||||
+ KeyPair(
|
||||
pk='cpf',
|
||||
sk=SortKey(cpf, path_spec='user_id'),
|
||||
rename_key='id',
|
||||
sk=SortKey(cpf, path_spec='user_id'), # type: ignore
|
||||
rename_key='user_id',
|
||||
)
|
||||
+ KeyPair(
|
||||
pk='cnpj',
|
||||
sk=SortKey(cnpj, path_spec='user_id'),
|
||||
sk=SortKey(cnpj, path_spec='user_id'), # type: ignore
|
||||
rename_key='org_id',
|
||||
),
|
||||
flatten_top=False,
|
||||
)
|
||||
|
||||
if 'id' in r:
|
||||
user = dyn.collection.get_items(
|
||||
TransactKey(r['id']) + SortKey('0') + SortKey('FRESH_USER')
|
||||
if cnpj:
|
||||
return pick(('org_id',), r)
|
||||
|
||||
if 'user_id' not in r:
|
||||
return {}
|
||||
|
||||
user = dyn.collection.get_items(
|
||||
TransactKey(r['user_id'])
|
||||
+ SortKey('0')
|
||||
+ SortKey(
|
||||
sk='NEVER_LOGGED',
|
||||
rename_key='never_logged',
|
||||
)
|
||||
)
|
||||
|
||||
return r | pick(('name', 'email'), user) if 'FRESH_USER' in user else {}
|
||||
if 'never_logged' not in user:
|
||||
raise UserAlreadyOnboardedError('User is already onboarded')
|
||||
|
||||
return r
|
||||
return {'never_logged': True} | pick(('id', 'name', 'email'), user)
|
||||
|
||||
@@ -32,10 +32,7 @@ def register(
|
||||
email: Annotated[EmailStr, Body(embed=True)],
|
||||
password: Annotated[str, Body(min_length=6, embed=True)],
|
||||
cpf: Annotated[CpfStr, Body(embed=True)],
|
||||
user_id: Annotated[str | None, Body(embed=True)] = None,
|
||||
id: Annotated[str | None, Body(embed=True)] = None,
|
||||
org: Annotated[Org | None, Body(embed=True)] = None,
|
||||
):
|
||||
if user_id:
|
||||
...
|
||||
|
||||
return {}
|
||||
|
||||
Reference in New Issue
Block a user