fix duplicate user
This commit is contained in:
@@ -1,9 +1,5 @@
|
||||
from abc import ABC
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime, timedelta
|
||||
from enum import Enum
|
||||
from typing import Any, Literal, TypedDict
|
||||
from uuid import uuid4
|
||||
from typing import Literal, TypedDict
|
||||
|
||||
from layercake.dateutils import now, ttl
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
|
||||
@@ -27,7 +23,6 @@ class User(BaseModel):
|
||||
id: UUID4 | str
|
||||
name: NameStr
|
||||
email: EmailStr
|
||||
email_verified: bool = False
|
||||
cpf: CpfStr | None = None
|
||||
|
||||
|
||||
@@ -44,18 +39,6 @@ class Enrollment(BaseModel):
|
||||
progress: int = Field(default=0, ge=0, le=100)
|
||||
status: Literal['PENDING'] = 'PENDING'
|
||||
|
||||
def model_dump(
|
||||
self,
|
||||
exclude=None,
|
||||
*args,
|
||||
**kwargs,
|
||||
) -> dict[str, Any]:
|
||||
return super().model_dump(
|
||||
exclude={'user': {'email_verified'}},
|
||||
*args,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
|
||||
Org = TypedDict('Org', {'org_id': str, 'name': str})
|
||||
|
||||
@@ -75,18 +58,6 @@ Subscription = TypedDict(
|
||||
)
|
||||
|
||||
|
||||
class Kind(str, Enum):
|
||||
ORDER = 'ORDER'
|
||||
ENROLLMENT = 'ENROLLMENT'
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class LinkedEntity(ABC):
|
||||
id: str
|
||||
kind: Kind
|
||||
table_name: str | None = None
|
||||
|
||||
|
||||
class DeduplicationConflictError(Exception):
|
||||
def __init__(self, *args):
|
||||
super().__init__('Enrollment already exists')
|
||||
@@ -121,7 +92,7 @@ def enroll(
|
||||
created_by: CreatedBy | None = None,
|
||||
scheduled_at: datetime | None = None,
|
||||
seat: Seat | None = None,
|
||||
linked_entities: frozenset[LinkedEntity] = frozenset(),
|
||||
parent_entity: str | None = None,
|
||||
deduplication_window: DeduplicationWindow | None = None,
|
||||
persistence_layer: DynamoDBPersistenceLayer,
|
||||
) -> bool:
|
||||
@@ -156,43 +127,51 @@ def enroll(
|
||||
)
|
||||
|
||||
if seat:
|
||||
order_id = seat['order_id']
|
||||
transact.condition(
|
||||
key=KeyPair(str(seat['order_id']), '0'),
|
||||
key=KeyPair(order_id, '0'),
|
||||
cond_expr='attribute_exists(sk)',
|
||||
exc_cls=OrderNotFoundError,
|
||||
table_name=ORDER_TABLE,
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
'id': seat['order_id'],
|
||||
'id': order_id,
|
||||
'sk': f'ENROLLMENT#{enrollment.id}',
|
||||
'course': course.model_dump(),
|
||||
'user': user.model_dump(),
|
||||
'user': user.model_dump(exclude={'cpf'}),
|
||||
'status': 'EXECUTED',
|
||||
'executed_at': now_,
|
||||
'created_at': now_,
|
||||
},
|
||||
table_name=ORDER_TABLE,
|
||||
)
|
||||
|
||||
# Relationships between this enrollment and its related entities
|
||||
for entity in linked_entities:
|
||||
# Parent knows the child
|
||||
# Enrollment should know where it comes from
|
||||
transact.put(
|
||||
item={
|
||||
'id': entity.id,
|
||||
'sk': f'LINKED_ENTITIES#CHILD#ENROLLMENT#{enrollment.id}',
|
||||
'id': enrollment.id,
|
||||
'sk': f'LINKED_ENTITY#PARENT#ORDER#{order_id}',
|
||||
'created_at': now_,
|
||||
},
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
)
|
||||
|
||||
if parent_entity:
|
||||
# Parent knows the child
|
||||
transact.put(
|
||||
item={
|
||||
'id': parent_entity,
|
||||
'sk': f'LINKED_ENTITY#CHILD#ENROLLMENT#{enrollment.id}',
|
||||
'created_at': now_,
|
||||
},
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
table_name=entity.table_name,
|
||||
)
|
||||
|
||||
# Child knows the parent
|
||||
transact.put(
|
||||
item={
|
||||
'id': enrollment.id,
|
||||
'sk': f'LINKED_ENTITIES#PARENT#{entity.kind.value}#{entity.id}',
|
||||
'sk': f'LINKED_ENTITY#PARENT#ENROLLMENT#{parent_entity}',
|
||||
'created_at': now_,
|
||||
},
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
|
||||
Reference in New Issue
Block a user