add relationship

This commit is contained in:
2025-09-23 11:36:07 -03:00
parent 18674d8831
commit 9c38e68a34
3 changed files with 44 additions and 7 deletions

View File

@@ -73,15 +73,34 @@ def enroll(
}
)
for entity in linked_entities:
keyprefix = entity.type.lower()
# 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()
# Child knows the parent
transact.put(
item={
'id': enrollment.id,
'sk': f'LINKED_ENTITIES#{entity.type}',
'sk': entity_sk,
'kind': 'PARENT',
'created_at': now_,
f'{keyprefix}_id': entity.id,
}
f'{keyprefix}_id': perent_id,
},
cond_expr='attribute_not_exists(sk)',
)
# Parent knows the child
transact.put(
item={
'id': perent_id,
'sk': entity_sk,
'kind': 'CHILD',
'created_at': now_,
f'{keyprefix}_id': enrollment.id,
},
cond_expr='attribute_not_exists(sk)',
)
if org:

View File

@@ -222,7 +222,7 @@ Resources:
EventSendReminderEmailsFunction:
Type: AWS::Serverless::Function
Properties:
Handler: events.emails.send_reminder_emails.lambda_handler
Handler: events.send_reminder_emails.lambda_handler
LoggingConfig:
LogGroup: !Ref EventLog
Policies:

View File

@@ -1,6 +1,6 @@
import app.events.reenroll_if_failed as app
from aws_lambda_powertools.utilities.typing import LambdaContext
from layercake.dynamodb import DynamoDBPersistenceLayer
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
def test_reenroll(
@@ -27,3 +27,21 @@ def test_reenroll(
}
}
assert app.lambda_handler(event, lambda_context) # type: ignore
parent_entity = dynamodb_persistence_layer.collection.get_item(
KeyPair(
pk='294e9864-8284-4287-b153-927b15d90900',
sk='LINKED_ENTITIES#ENROLLMENT',
)
)
assert parent_entity['kind'] == 'CHILD'
child_entity = dynamodb_persistence_layer.collection.get_item(
KeyPair(
pk=parent_entity['enrollment_id'],
sk='LINKED_ENTITIES#ENROLLMENT',
)
)
assert child_entity['enrollment_id'] == parent_entity['id']
assert child_entity['kind'] == 'PARENT'