add postgres
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
from dataclasses import asdict, dataclass
|
||||
from datetime import timedelta
|
||||
from enum import Enum
|
||||
from typing import Self, TypedDict
|
||||
|
||||
from layercake.dateutils import now, ttl
|
||||
@@ -36,35 +34,6 @@ class Slot:
|
||||
return LinkedEntity(idx, 'ORDER')
|
||||
|
||||
|
||||
class LifecycleEvents(str, Enum):
|
||||
"""Lifecycle events related to scheduling actions."""
|
||||
|
||||
# Reminder if the user does not access within 3 days
|
||||
# REMINDER_NO_ACCESS_AFTER_3_DAYS = 'SCHEDULE#REMINDER_NO_ACCESS_AFTER_3_DAYS'
|
||||
DOES_NOT_ACCESS = 'schedules#does_not_access'
|
||||
|
||||
# When there is no activity 7 days after the first access
|
||||
# REMINDER_NO_ACTIVITY_AFTER_7_DAYS = 'SCHEDULE#REMINDER_NO_ACTIVITY_AFTER_7_DAYS'
|
||||
NO_ACTIVITY = 'schedules#no_activity'
|
||||
|
||||
# Reminder 30 days before the access period expires
|
||||
# REMINDER_ACCESS_PERIOD_BEFORE_30_DAYS = 'SCHEDULE#REMINDER_ACCESS_PERIOD_BEFORE_30_DAYS'
|
||||
ACCESS_PERIOD_ENDS = 'schedules#access_period_ends'
|
||||
|
||||
# Reminder for certificate expiration set to 30 days from now
|
||||
REMINDER_CERT_EXPIRATION_BEFORE_30_DAYS = (
|
||||
'SCHEDULE#REMINDER_CERT_EXPIRATION_BEFORE_30_DAYS'
|
||||
)
|
||||
|
||||
# Archive the course after the certificate expires
|
||||
# SET_AS_ARCHIVE = 'SCHEDULE#SET_AS_ARCHIVE'
|
||||
ARCHIVE_IT = 'schedules#archive_it'
|
||||
|
||||
# When the access period ends for a course without a certificate
|
||||
# SET_AS_EXPIRE = 'SCHEDULE#SET_AS_EXPIRE'
|
||||
EXPIRATION = 'schedules#expiration'
|
||||
|
||||
|
||||
class DeduplicationConflictError(Exception):
|
||||
def __init__(self, *args):
|
||||
super().__init__('Enrollment already exists')
|
||||
@@ -105,53 +74,11 @@ def enroll(
|
||||
transact.put(
|
||||
item={
|
||||
'id': enrollment.id,
|
||||
'sk': 'metadata#course',
|
||||
'sk': 'METADATA#COURSE',
|
||||
'created_at': now_,
|
||||
**course.model_dump(include={'cert', 'access_period'}),
|
||||
}
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
'id': enrollment.id,
|
||||
# Post-migration: uncomment the following line
|
||||
# 'sk': LifecycleEvents.REMINDER_NO_ACCESS_3_DAYS,
|
||||
'sk': LifecycleEvents.DOES_NOT_ACCESS,
|
||||
'name': user.name,
|
||||
'email': user.email,
|
||||
'course': course.name,
|
||||
'created_at': now_,
|
||||
'ttl': ttl(days=3, start_dt=now_),
|
||||
},
|
||||
)
|
||||
# Enrollment expires by default when the access period ends.
|
||||
# When the course is completed, it is automatically removed,
|
||||
# and the `SCHEDULE#SET_AS_ARCHIVE` event is created.
|
||||
transact.put(
|
||||
item={
|
||||
'id': enrollment.id,
|
||||
'sk': LifecycleEvents.EXPIRATION,
|
||||
# Post-migration: uncomment the following line
|
||||
# 'sk': LifecycleEvents.COURSE_EXPIRED,
|
||||
'name': user.name,
|
||||
'email': user.email,
|
||||
'course': course.name,
|
||||
'created_at': now_,
|
||||
'ttl': ttl(start_dt=now_ + timedelta(days=course.access_period)),
|
||||
},
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
'id': enrollment.id,
|
||||
# Post-migration: uncomment the following line
|
||||
# 'sk': LifecycleEvents.ACCESS_PERIOD_REMINDER_30_DAYS,
|
||||
'sk': LifecycleEvents.ACCESS_PERIOD_ENDS,
|
||||
'name': user.name,
|
||||
'email': user.email,
|
||||
'course': course.name,
|
||||
'created_at': now_,
|
||||
'ttl': ttl(start_dt=now_ + timedelta(days=course.access_period - 30)),
|
||||
},
|
||||
)
|
||||
|
||||
for entity in linked_entities:
|
||||
keyprefix = entity.type.lower()
|
||||
@@ -184,7 +111,7 @@ def enroll(
|
||||
transact.put(
|
||||
item={
|
||||
'id': enrollment.id,
|
||||
'sk': 'cancel_policy',
|
||||
'sk': 'CANCEL_POLICY',
|
||||
'created_at': now_,
|
||||
}
|
||||
)
|
||||
@@ -204,16 +131,17 @@ def enroll(
|
||||
# the deduplication window expires or is removed.
|
||||
if deduplication_window:
|
||||
offset_days = deduplication_window['offset_days']
|
||||
ttl_expiration = ttl(
|
||||
start_dt=now_ + timedelta(days=course.access_period - offset_days)
|
||||
ttl_ = ttl(
|
||||
start_dt=now_,
|
||||
days=course.access_period - offset_days,
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
'id': 'lock',
|
||||
'id': 'LOCK',
|
||||
'sk': lock_hash,
|
||||
'enrollment_id': enrollment.id,
|
||||
'created_at': now_,
|
||||
'ttl': ttl_expiration,
|
||||
'ttl': ttl_,
|
||||
},
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
exc_cls=DeduplicationConflictError,
|
||||
@@ -221,24 +149,24 @@ def enroll(
|
||||
transact.put(
|
||||
item={
|
||||
'id': enrollment.id,
|
||||
'sk': 'lock',
|
||||
'sk': 'LOCK',
|
||||
'hash': lock_hash,
|
||||
'created_at': now_,
|
||||
'ttl': ttl_expiration,
|
||||
'ttl': ttl_,
|
||||
},
|
||||
)
|
||||
# Deduplication window can be recalculated if needed
|
||||
transact.put(
|
||||
item={
|
||||
'id': enrollment.id,
|
||||
'sk': 'metadata#deduplication_window',
|
||||
'sk': 'METADATA#DEDUPLICATION_WINDOW',
|
||||
'offset_days': offset_days,
|
||||
'created_at': now_,
|
||||
},
|
||||
)
|
||||
else:
|
||||
transact.condition(
|
||||
key=KeyPair('lock', lock_hash),
|
||||
key=KeyPair('LOCK', lock_hash),
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
exc_cls=DeduplicationConflictError,
|
||||
)
|
||||
|
||||
@@ -32,10 +32,15 @@ def send_email(
|
||||
emailmsg = Message(
|
||||
from_=sender,
|
||||
to=to,
|
||||
subject=subject.format(course=truncate_str(context['course'])),
|
||||
subject=subject.format(
|
||||
course=truncate_str(context['course']),
|
||||
),
|
||||
)
|
||||
emailmsg.add_alternative(
|
||||
message.format(first_name=first_word(name), course=context['course'])
|
||||
message.format(
|
||||
first_name=first_word(name),
|
||||
course=context['course'],
|
||||
)
|
||||
)
|
||||
|
||||
try:
|
||||
@@ -46,6 +51,13 @@ def send_email(
|
||||
},
|
||||
}
|
||||
)
|
||||
dynamodb_persistence_layer.put_item(
|
||||
item={
|
||||
'id': event['id'],
|
||||
'sk': f'{event["sk"]}#EXECUTED',
|
||||
'created_at': now_,
|
||||
}
|
||||
)
|
||||
logger.info('Email sent')
|
||||
except Exception as exc:
|
||||
logger.exception(exc)
|
||||
@@ -59,13 +71,5 @@ def send_email(
|
||||
)
|
||||
|
||||
return False
|
||||
else:
|
||||
dynamodb_persistence_layer.put_item(
|
||||
item={
|
||||
'id': event['id'],
|
||||
'sk': f'{event["sk"]}#EXECUTED',
|
||||
'created_at': now_,
|
||||
}
|
||||
)
|
||||
|
||||
return True
|
||||
return True
|
||||
|
||||
@@ -18,7 +18,7 @@ logger = Logger(__name__)
|
||||
enrollment_layer = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
|
||||
|
||||
|
||||
SUBJECT = 'Seu acesso ao curso de {course} termina em 30 dias'
|
||||
SUBJECT = 'Seu acesso ao curso {course} termina em 30 dias'
|
||||
MESSAGE = """
|
||||
Oi {first_name}, tudo bem?<br/><br/>
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ logger = Logger(__name__)
|
||||
enrollment_layer = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
|
||||
|
||||
|
||||
SUBJECT = 'Seu certificado de {course} está prestes a expirar'
|
||||
SUBJECT = 'Seu certificado {course} vai expirar em breve'
|
||||
MESSAGE = """
|
||||
Oi {first_name}, tudo bem?<br/><br/>
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ logger = Logger(__name__)
|
||||
enrollment_layer = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
|
||||
|
||||
|
||||
SUBJECT = 'Seu curso de {course} está esperando por você na EDUSEG®'
|
||||
SUBJECT = 'Seu curso {course} está esperando por você na EDUSEG®'
|
||||
MESSAGE = """
|
||||
Oi {first_name}, tudo bem?<br/><br/>
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ logger = Logger(__name__)
|
||||
enrollment_layer = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
|
||||
|
||||
|
||||
SUBJECT = 'Seu curso de {course} está parado há 7 dias...'
|
||||
SUBJECT = 'Seu curso {course} está parado há 7 dias...'
|
||||
MESSAGE = """
|
||||
Oi {first_name}, tudo bem?<br><br>
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ from aws_lambda_powertools.utilities.data_classes import (
|
||||
event_source,
|
||||
)
|
||||
from aws_lambda_powertools.utilities.typing import LambdaContext
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair, SortKey, TransactKey
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer, SortKey, TransactKey
|
||||
|
||||
from boto3clients import dynamodb_client
|
||||
from config import (
|
||||
@@ -21,8 +21,8 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
new_image = event.detail['new_image']
|
||||
enrollment = enrollment_layer.collection.get_items(
|
||||
TransactKey(pk=new_image['id'])
|
||||
+ SortKey('METADATA#DEDUPLICATION_WINDOW', rename_key='DEDUPLICATION_WINDOW')
|
||||
+ SortKey('METADATA#COURSE', rename_key='COURSE'),
|
||||
+ SortKey('METADATA#DEDUPLICATION_WINDOW', rename_key='deduplication_window')
|
||||
+ SortKey('METADATA#COURSE', rename_key='course'),
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
@@ -19,16 +19,11 @@ enrollment_layer = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
|
||||
@logger.inject_lambda_context
|
||||
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
new_image = event.detail['new_image']
|
||||
subscription = enrollment_layer.collection.get_items(
|
||||
data = enrollment_layer.collection.get_items(
|
||||
TransactKey(new_image['id'])
|
||||
+ SortKey('METADATA#SUBSCRIPTION_COVERED')
|
||||
+ SortKey('author', rename_key='CREATED_BY')
|
||||
+ SortKey('tenant', rename_key='ORG')
|
||||
+ SortKey('CREATED_BY')
|
||||
+ SortKey('ORG')
|
||||
+ SortKey('METADATA#SUBSCRIPTION_COVERED', rename_key='subscription')
|
||||
+ SortKey('author', rename_key='created_by')
|
||||
+ SortKey('tenant', rename_key='org')
|
||||
)
|
||||
|
||||
with enrollment_layer.transact_writer() as transact_writer:
|
||||
...
|
||||
|
||||
return True
|
||||
|
||||
@@ -37,8 +37,10 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
'ttl': ttl(days=3, start_dt=now_),
|
||||
},
|
||||
)
|
||||
|
||||
# By default, the enrollment will expire when the access period ends
|
||||
# (scheduled below).
|
||||
|
||||
# If the enrollment is completed earlier (e.g., certificate issued),
|
||||
# the expiration schedule is canceled and an archive schedule
|
||||
# (`SCHEDULE#SET_AS_ARCHIVED`) is created instead.
|
||||
@@ -53,7 +55,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
transact.put(
|
||||
item={
|
||||
'id': enrollment_id,
|
||||
'sk': 'SCHEDULE#REMINDER_CERT_EXPIRATION_BEFORE_30_DAYS',
|
||||
'sk': 'SCHEDULE#REMINDER_ACCESS_PERIOD_BEFORE_30_DAYS',
|
||||
'name': user.name,
|
||||
'email': user.email,
|
||||
'course': course.name,
|
||||
|
||||
@@ -8,11 +8,12 @@ from aws_lambda_powertools.utilities.data_classes import (
|
||||
)
|
||||
from aws_lambda_powertools.utilities.typing import LambdaContext
|
||||
from layercake.dateutils import now
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
|
||||
from sqlite_utils import Database
|
||||
|
||||
from boto3clients import dynamodb_client
|
||||
from config import (
|
||||
COURSE_TABLE,
|
||||
ENROLLMENT_TABLE,
|
||||
SQLITE_DATABASE,
|
||||
SQLITE_TABLE,
|
||||
@@ -22,6 +23,7 @@ sqlite3.register_converter('json', json.loads)
|
||||
|
||||
logger = Logger(__name__)
|
||||
enrollment_layer = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
|
||||
course_layer = DynamoDBPersistenceLayer(COURSE_TABLE, dynamodb_client)
|
||||
|
||||
|
||||
@event_source(data_class=EventBridgeEvent)
|
||||
@@ -37,7 +39,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
transact.put(
|
||||
item={
|
||||
'id': new_image['id'],
|
||||
'sk': 'metadata#deduplication_window',
|
||||
'sk': 'METADATA#DEDUPLICATION_WINDOW',
|
||||
'offset_days': 90,
|
||||
'created_at': now_,
|
||||
}
|
||||
@@ -45,7 +47,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
transact.put(
|
||||
item={
|
||||
'id': new_image['id'],
|
||||
'sk': 'metadata#course',
|
||||
'sk': 'METADATA#COURSE',
|
||||
'created_at': now_,
|
||||
'access_period': int(course['access_period']),
|
||||
'cert': {
|
||||
@@ -66,6 +68,10 @@ class CourseNotFoundError(Exception):
|
||||
|
||||
|
||||
def _get_course(course_id: str) -> dict:
|
||||
course = course_layer.get_item(KeyPair(pk=course_id, sk='0'))
|
||||
if course:
|
||||
return course
|
||||
|
||||
with sqlite3.connect(
|
||||
database=SQLITE_DATABASE, detect_types=sqlite3.PARSE_DECLTYPES
|
||||
) as conn:
|
||||
|
||||
@@ -76,6 +76,8 @@ Resources:
|
||||
Policies:
|
||||
- DynamoDBCrudPolicy:
|
||||
TableName: !Ref EnrollmentTable
|
||||
- DynamoDBCrudPolicy:
|
||||
TableName: !Ref CourseTable
|
||||
Events:
|
||||
DynamoDBEvent:
|
||||
Type: EventBridgeRule
|
||||
@@ -281,7 +283,7 @@ Resources:
|
||||
detail:
|
||||
keys:
|
||||
sk:
|
||||
- SCHEDULE#REMINDER_CERT_EXPIRATION_BEFORE_30_DAYS
|
||||
- SCHEDULE#REMINDER_ACCESS_PERIOD_BEFORE_30_DAYS
|
||||
# Post-migration: remove the following line
|
||||
- schedules#access_period_ends
|
||||
|
||||
@@ -310,6 +312,8 @@ Resources:
|
||||
# Post-migration: remove the following line
|
||||
- schedules#expiration
|
||||
|
||||
# If there is no certificate and the access period has ended,
|
||||
# the enrollment will be marked as expired
|
||||
EventSetAsExpiredFunction:
|
||||
Type: AWS::Serverless::Function
|
||||
Properties:
|
||||
@@ -328,28 +332,55 @@ Resources:
|
||||
detail-type: [EXPIRE]
|
||||
detail:
|
||||
keys:
|
||||
sk: [SCHEDULE#SET_AS_EXPIRED]
|
||||
sk:
|
||||
- SCHEDULE#SET_AS_EXPIRED
|
||||
# Post-migration: remove the following line
|
||||
- schedules#access_period_ends
|
||||
|
||||
# EventScheduleRemindersFunction:
|
||||
# Type: AWS::Serverless::Function
|
||||
# Properties:
|
||||
# Handler: events.schedule_reminders.lambda_handler
|
||||
# LoggingConfig:
|
||||
# LogGroup: !Ref EventLog
|
||||
# Policies:
|
||||
# - DynamoDBCrudPolicy:
|
||||
# TableName: !Ref EnrollmentTable
|
||||
# Events:
|
||||
# DynamoDBEvent:
|
||||
# Type: EventBridgeRule
|
||||
# Properties:
|
||||
# Pattern:
|
||||
# resources: [!Ref EnrollmentTable]
|
||||
# detail-type: [INSERT]
|
||||
# detail:
|
||||
# new_image:
|
||||
# sk: ["0"]
|
||||
# status: [PENDING]
|
||||
# After the certificate expires, the enrollment will be marked as archived
|
||||
EventSetAsArchivedFunction:
|
||||
Type: AWS::Serverless::Function
|
||||
Properties:
|
||||
Handler: events.set_as_archived.lambda_handler
|
||||
LoggingConfig:
|
||||
LogGroup: !Ref EventLog
|
||||
Policies:
|
||||
- DynamoDBCrudPolicy:
|
||||
TableName: !Ref EnrollmentTable
|
||||
Events:
|
||||
DynamoDBEvent:
|
||||
Type: EventBridgeRule
|
||||
Properties:
|
||||
Pattern:
|
||||
resources: [!Ref EnrollmentTable]
|
||||
detail-type: [EXPIRE]
|
||||
detail:
|
||||
keys:
|
||||
sk:
|
||||
- SCHEDULE#SET_AS_ARCHIVED
|
||||
# Post-migration: remove the following line
|
||||
- schedules#archive_it
|
||||
|
||||
EventScheduleRemindersFunction:
|
||||
Type: AWS::Serverless::Function
|
||||
Properties:
|
||||
Handler: events.schedule_reminders.lambda_handler
|
||||
LoggingConfig:
|
||||
LogGroup: !Ref EventLog
|
||||
Policies:
|
||||
- DynamoDBCrudPolicy:
|
||||
TableName: !Ref EnrollmentTable
|
||||
Events:
|
||||
DynamoDBEvent:
|
||||
Type: EventBridgeRule
|
||||
Properties:
|
||||
Pattern:
|
||||
resources: [!Ref EnrollmentTable]
|
||||
detail-type: [INSERT]
|
||||
detail:
|
||||
new_image:
|
||||
sk: ["0"]
|
||||
status: [PENDING]
|
||||
|
||||
EventIssueCertFunction:
|
||||
Type: AWS::Serverless::Function
|
||||
|
||||
@@ -30,9 +30,9 @@ def test_enroll(
|
||||
result = dynamodb_persistence_layer.collection.get_items(
|
||||
TransactKey('47ZxxcVBjvhDS5TE98tpfQ')
|
||||
+ SortKey('0')
|
||||
+ SortKey('metadata#deduplication_window')
|
||||
+ SortKey('metadata#course')
|
||||
+ SortKey('METADATA#DEDUPLICATION_WINDOW')
|
||||
+ SortKey('METADATA#COURSE')
|
||||
)
|
||||
|
||||
assert 'metadata#course' in result
|
||||
assert 'metadata#deduplication_window' in result
|
||||
assert 'METADATA#COURSE' in result
|
||||
assert 'METADATA#DEDUPLICATION_WINDOW' in result
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
{"id": "123", "sk": "0", "access_period": 360, "cert": {"exp_interval": 360}, "created_at": "2025-07-14T15:09:18.559528-03:00", "metadata__konviva_class_id": "281", "name": "pytest", "tenant_id": "*"}
|
||||
{"id": "a955518e-ebcb-4441-b914-ddc9ecef84f0", "sk": "0", "access_period": "360", "cert": {"exp_interval": 360}, "created_at": "2025-07-14T15:09:18.559528-03:00", "metadata__konviva_class_id": "281", "name": "NR-11 Operador de Munck", "tenant_id": "*"}
|
||||
{"id": "6a403773-aeac-4e6a-ac39-dc958e4be52a", "sk": "0", "access_period": "360", "cert": {"exp_interval": 360}, "created_at": "2025-07-14T15:09:18.559528-03:00", "metadata__konviva_class_id": "281", "name": "Reciclagem em NR-11 - Operador de Empilhadeira", "tenant_id": "*"}
|
||||
{"id": "e1c44881-2fe3-484e-ada2-12b6bf5b9398", "sk": "0", "name": "NR-35 Segurança nos Trabalhos em Altura (Teórico)", "updated_at": "2025-08-22T00:00:24.431267-03:00", "access_period": 360, "created_at": "2024-12-30T00:11:33.088916-03:00", "metadata__konviva_class_id": 1, "tenant_id": "*", "cert": {"exp_interval": 700}, "metadata__unit_price": 119}
|
||||
|
||||
// User data
|
||||
{"id": "5OxmMjL-ujoR5IMGegQz", "sk": "konviva", "konvivaId": 26943}
|
||||
|
||||
Reference in New Issue
Block a user