fix enroll and reenroll relationship
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
from abc import ABC
|
||||
from dataclasses import dataclass
|
||||
from datetime import timedelta
|
||||
from enum import Enum
|
||||
from typing import NotRequired, Self, TypedDict
|
||||
|
||||
from layercake.dateutils import now, ttl
|
||||
@@ -32,15 +35,16 @@ Subscription = TypedDict(
|
||||
)
|
||||
|
||||
|
||||
class LinkedEntity(str):
|
||||
def __new__(cls, id: str, type: str) -> Self:
|
||||
return super().__new__(cls, '#'.join([type.lower(), id]))
|
||||
class Kind(str, Enum):
|
||||
ORDER = 'ORDER'
|
||||
ENROLLMENT = 'ENROLLMENT'
|
||||
|
||||
def __init__(self, id: str, type: str) -> None:
|
||||
# __init__ is used to store the parameters for later reference.
|
||||
# For immutable types like str, __init__ cannot change the instance's value.
|
||||
self.id = id
|
||||
self.type = type
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class LinkedEntity(ABC):
|
||||
id: str
|
||||
kind: Kind
|
||||
table_name: str | None = None
|
||||
|
||||
|
||||
class DeduplicationConflictError(Exception):
|
||||
@@ -76,29 +80,27 @@ def enroll(
|
||||
)
|
||||
|
||||
# Relationships between this enrollment and its related entities
|
||||
for parent_entity in linked_entities:
|
||||
perent_id = parent_entity.id
|
||||
entity_sk = f'LINKED_ENTITIES#{parent_entity.type}'
|
||||
keyprefix = parent_entity.type.lower()
|
||||
|
||||
for entity in linked_entities:
|
||||
# Parent knows the child
|
||||
transact.put(
|
||||
item={
|
||||
'id': perent_id,
|
||||
'sk': f'{entity_sk}#CHILD',
|
||||
'id': entity.id,
|
||||
'sk': f'LINKED_ENTITIES#{entity.kind.value}#CHILD',
|
||||
'created_at': now_,
|
||||
f'{keyprefix}_id': enrollment.id,
|
||||
'enrollment_id': enrollment.id,
|
||||
},
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
table_name=entity.table_name,
|
||||
)
|
||||
|
||||
keyprefix = entity.kind.value.lower()
|
||||
# Child knows the parent
|
||||
transact.put(
|
||||
item={
|
||||
'id': enrollment.id,
|
||||
'sk': f'{entity_sk}#PARENT',
|
||||
'sk': f'LINKED_ENTITIES#{entity.kind.value}#PARENT',
|
||||
'created_at': now_,
|
||||
f'{keyprefix}_id': perent_id,
|
||||
f'{keyprefix}_id': entity.id,
|
||||
},
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user