add notification
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
from typing import Annotated
|
||||
|
||||
from aws_lambda_powertools import Logger
|
||||
from aws_lambda_powertools.event_handler.api_gateway import Router
|
||||
from aws_lambda_powertools.event_handler.openapi.params import Query
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer, PartitionKey
|
||||
|
||||
from boto3clients import dynamodb_client
|
||||
@@ -11,9 +14,7 @@ dyn = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
|
||||
|
||||
|
||||
@router.get('/<org_id>/enrollments/scheduled')
|
||||
def scheduled(org_id: str):
|
||||
start_key = router.current_event.get_query_string_value('start_key', None)
|
||||
|
||||
def scheduled(org_id: str, start_key: Annotated[str | None, Query] = None):
|
||||
return dyn.collection.query(
|
||||
# Post-migration: rename `scheduled_items` to `SCHEDULED#ORG#{org_id}`
|
||||
key=PartitionKey(f'scheduled_items#{org_id}'),
|
||||
|
||||
@@ -8,7 +8,7 @@ from aws_lambda_powertools.event_handler.exceptions import (
|
||||
ServiceError,
|
||||
)
|
||||
from aws_lambda_powertools.event_handler.openapi.params import Body
|
||||
from layercake.dateutils import now
|
||||
from layercake.dateutils import now, ttl
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair, SortKey
|
||||
from layercake.extra_types import CnpjStr, CpfStr, NameStr
|
||||
from pydantic import BaseModel, EmailStr, Field
|
||||
@@ -126,9 +126,26 @@ def _create_user(user: User, org: Org) -> bool:
|
||||
'sk': f'emails#{user.email}',
|
||||
'email_verified': email_verified,
|
||||
'email_primary': True,
|
||||
'mx_record_exists': email_verified,
|
||||
'created_at': now_,
|
||||
}
|
||||
)
|
||||
|
||||
if not email_verified:
|
||||
transact.put(
|
||||
item={
|
||||
'id': user_id,
|
||||
'sk': f'EMAIL_VERIFICATION#{uuid4()}',
|
||||
'fresh_user': True,
|
||||
'name': user.name,
|
||||
'email': user.email,
|
||||
'email_primary': True,
|
||||
'org_name': org.name,
|
||||
'ttl': ttl(start_dt=now_, days=30),
|
||||
'created_at': now_,
|
||||
}
|
||||
)
|
||||
|
||||
transact.put(
|
||||
item={
|
||||
# Post-migration: rename `cpf` to `CPF`
|
||||
|
||||
@@ -9,6 +9,7 @@ from aws_lambda_powertools.event_handler.exceptions import (
|
||||
from aws_lambda_powertools.event_handler.openapi.params import Body, Path, Query
|
||||
from layercake.dateutils import now, ttl
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair, SortKey
|
||||
from layercake.funcs import pick
|
||||
from pydantic import EmailStr
|
||||
from typing_extensions import Annotated
|
||||
|
||||
@@ -29,6 +30,9 @@ def get_emails(user_id: str, start_key: Annotated[str | None, Query] = None):
|
||||
)
|
||||
|
||||
|
||||
class UserNotFoundError(NotFoundError): ...
|
||||
|
||||
|
||||
class EmailConflictError(ServiceError):
|
||||
def __init__(self, msg: str | dict):
|
||||
super().__init__(HTTPStatus.CONFLICT, msg)
|
||||
@@ -46,13 +50,19 @@ def add(
|
||||
)
|
||||
|
||||
with dyn.transact_writer() as transact:
|
||||
transact.condition(
|
||||
key=KeyPair(user_id, '0'),
|
||||
cond_expr='attribute_exists(sk)',
|
||||
exc_cls=UserNotFoundError,
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
'id': user_id,
|
||||
# Post-migration (users): rename `emails` to `EMAIL`
|
||||
'sk': f'emails#{email}',
|
||||
'email_verified': False,
|
||||
'email_primary': True,
|
||||
'mx_record_exists': False,
|
||||
'email_primary': False,
|
||||
'created_at': now_,
|
||||
}
|
||||
)
|
||||
@@ -82,10 +92,16 @@ def add(
|
||||
|
||||
|
||||
@router.post('/<user_id>/emails/<email>/request-verification')
|
||||
def request_verification(user_id: str, email: Annotated[EmailStr, Path]):
|
||||
def request_verification(
|
||||
user_id: str,
|
||||
email: Annotated[EmailStr, Path],
|
||||
):
|
||||
now_ = now()
|
||||
name = dyn.collection.get_item(
|
||||
KeyPair(user_id, SortKey('0', path_spec='name')),
|
||||
KeyPair(
|
||||
pk=user_id,
|
||||
sk=SortKey('0', path_spec='name'),
|
||||
),
|
||||
raise_on_error=False,
|
||||
)
|
||||
dyn.put_item(
|
||||
@@ -107,13 +123,14 @@ class EmailVerificationNotFoundError(NotFoundError): ...
|
||||
|
||||
@router.post('/<user_id>/emails/<hash>/verify')
|
||||
def verify(user_id: str, hash: str):
|
||||
email = dyn.collection.get_item(
|
||||
verification = dyn.collection.get_item(
|
||||
KeyPair(
|
||||
pk=user_id,
|
||||
sk=SortKey(f'EMAIL_VERIFICATION#{hash}', path_spec='email'),
|
||||
sk=f'EMAIL_VERIFICATION#{hash}',
|
||||
),
|
||||
exc_cls=EmailVerificationNotFoundError,
|
||||
)
|
||||
email, primary = pick(('email', 'email_primary'), verification, default=False)
|
||||
|
||||
with dyn.transact_writer() as transact:
|
||||
transact.delete(
|
||||
@@ -129,6 +146,20 @@ def verify(user_id: str, hash: str):
|
||||
},
|
||||
)
|
||||
|
||||
if primary:
|
||||
transact.update(
|
||||
key=KeyPair(user_id, '0'),
|
||||
update_expr='SET email_verified = :true, \
|
||||
updated_at = :now',
|
||||
expr_attr_values={
|
||||
':email': email,
|
||||
':true': True,
|
||||
':now': now(),
|
||||
},
|
||||
cond_expr='attribute_exists(sk)',
|
||||
exc_cls=UserNotFoundError,
|
||||
)
|
||||
|
||||
return JSONResponse(status_code=HTTPStatus.NO_CONTENT)
|
||||
|
||||
|
||||
@@ -140,7 +171,7 @@ def primary(
|
||||
email_verified: Annotated[bool, Body(embed=True)],
|
||||
):
|
||||
now_ = now()
|
||||
expr = 'SET email_primary = :email_primary, updated_at = :updated_at'
|
||||
expr = 'SET email_primary = :email_primary, updated_at = :now'
|
||||
|
||||
with dyn.transact_writer() as transact:
|
||||
# Set the old email as non-primary
|
||||
@@ -150,7 +181,7 @@ def primary(
|
||||
update_expr=expr,
|
||||
expr_attr_values={
|
||||
':email_primary': False,
|
||||
':updated_at': now_,
|
||||
':now': now_,
|
||||
},
|
||||
cond_expr='attribute_exists(sk)',
|
||||
)
|
||||
@@ -161,7 +192,7 @@ def primary(
|
||||
update_expr=expr,
|
||||
expr_attr_values={
|
||||
':email_primary': True,
|
||||
':updated_at': now_,
|
||||
':now': now_,
|
||||
},
|
||||
cond_expr='attribute_exists(sk)',
|
||||
)
|
||||
@@ -170,13 +201,15 @@ def primary(
|
||||
update_expr='DELETE emails :email_set \
|
||||
SET email = :email, \
|
||||
email_verified = :email_verified, \
|
||||
updated_at = :updated_at',
|
||||
updated_at = :now',
|
||||
expr_attr_values={
|
||||
':email': new_email,
|
||||
':email_set': {new_email},
|
||||
':email_verified': email_verified,
|
||||
':updated_at': now_,
|
||||
':now': now_,
|
||||
},
|
||||
cond_expr='attribute_exists(sk)',
|
||||
exc_cls=UserNotFoundError,
|
||||
)
|
||||
|
||||
return JSONResponse(status_code=HTTPStatus.NO_CONTENT)
|
||||
@@ -204,10 +237,14 @@ def remove(
|
||||
)
|
||||
transact.update(
|
||||
key=KeyPair(user_id, '0'),
|
||||
update_expr='DELETE emails :email',
|
||||
update_expr='DELETE emails :email \
|
||||
SET updated_at = :now',
|
||||
expr_attr_values={
|
||||
':email': {email},
|
||||
':now': now(),
|
||||
},
|
||||
cond_expr='attribute_exists(sk)',
|
||||
exc_cls=UserNotFoundError,
|
||||
)
|
||||
|
||||
return JSONResponse(status_code=HTTPStatus.NO_CONTENT)
|
||||
|
||||
Reference in New Issue
Block a user