update readm

This commit is contained in:
2025-08-29 18:25:53 -03:00
parent d05f660c9a
commit 0836e22d15
4 changed files with 365 additions and 150 deletions

View File

@@ -1,9 +1,11 @@
from dataclasses import asdict, dataclass
from datetime import timedelta
from enum import Enum
from typing import Self, TypedDict
from uuid import uuid4
from aws_lambda_powertools.event_handler.exceptions import (
BadRequestError,
NotFoundError,
)
from layercake.dateutils import now, ttl
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
from layercake.strutils import md5_hash
@@ -38,43 +40,21 @@ 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_3_DAYS = 'schedules#reminder_no_access_3_days'
DOES_NOT_ACCESS = 'schedules#does_not_access'
# When there is no activity 7 days after the first access
# NO_ACTIVITY_7_DAYS = 'schedules#no_activity_7_days'
NO_ACTIVITY = 'schedules#no_activity'
# Reminder 30 days before the access period expires
# ACCESS_PERIOD_REMINDER_30_DAYS = 'schedules#access_period_reminder_30_days'
ACCESS_PERIOD_ENDS = 'schedules#access_period_ends'
# Reminder for certificate expiration set to 30 days from now
CERT_EXPIRATION_REMINDER_30_DAYS = 'schedules#cert_expiration_reminder_30_days'
# Archive the course after the certificate expires
# SET_AS_ARCHIVE = 'schedules#set_as_archive'
ARCHIVE_IT = 'schedules#archive_it'
# When the access period ends for a course without a certificate
# SET_AS_EXPIRE = 'schedules#set_as_expire'
EXPIRATION = 'schedules#expiration'
class SlotDoesNotExistError(Exception):
class SlotDoesNotExistError(NotFoundError):
def __init__(self, *args):
super().__init__('Slot does not exist')
super().__init__('Slot not found')
class DeduplicationConflictError(Exception):
class DeduplicationConflictError(BadRequestError):
def __init__(self, *args):
super().__init__('Enrollment already exists')
class EnrollmentConflictError(BadRequestError):
def __init__(self, *_):
super().__init__('Enrollment status conflict')
def enroll(
enrollment: Enrollment,
*,
@@ -145,7 +125,7 @@ def enroll(
transact.put(
item={
'id': enrollment.id,
'sk': 'cancel_policy',
'sk': 'CANCEL_POLICY',
'created_at': now_,
}
)
@@ -165,9 +145,7 @@ 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_expiration = ttl(start_dt=now_, days=course.access_period - offset_days)
transact.put(
item={
'id': 'lock',
@@ -224,11 +202,13 @@ def set_status_as_canceled(
transact.update(
key=KeyPair(id, '0'),
update_expr='SET #status = :canceled, updated_at = :updated_at',
cond_expr='#status = :pending',
expr_attr_names={
'#status': 'status',
},
expr_attr_values={
':canceled': 'CANCELED',
':pending': 'PENDING',
':updated_at': now_,
},
)
@@ -241,20 +221,50 @@ def set_status_as_canceled(
},
)
transact.delete(
key=KeyPair(id, 'CANCEL_POLICY'),
key=KeyPair(
pk=id,
sk='CANCEL_POLICY',
),
cond_expr='attribute_exists(sk)',
exc_cls=EnrollmentConflictError,
)
# Remove schedules lifecycle events, referencies and locks
transact.delete(key=KeyPair(id, 'SCHEDULE#REMINDER_NO_ACCESS_AFTER_3_DAYS'))
transact.delete(key=KeyPair(id, 'SCHEDULE#REMINDER_NO_ACTIVITY_AFTER_7_DAYS'))
# Remove reminders and policies that no longer apply
transact.delete(
key=KeyPair(id, 'SCHEDULE#REMINDER_ACCESS_PERIOD_BEFORE_30_DAYS')
key=KeyPair(
pk=id,
sk='SCHEDULE#REMINDER_NO_ACCESS_AFTER_3_DAYS',
)
)
transact.delete(
key=KeyPair(id, 'SCHEDULE#REMINDER_CERT_EXPIRATION_BEFORE_30_DAYS')
key=KeyPair(
pk=id,
sk='SCHEDULE#REMINDER_NO_ACTIVITY_AFTER_7_DAYS',
)
)
transact.delete(
key=KeyPair(
pk=id,
sk='SCHEDULE#REMINDER_ACCESS_PERIOD_BEFORE_30_DAYS',
)
)
transact.delete(
key=KeyPair(
pk=id,
sk='SCHEDULE#REMINDER_CERT_EXPIRATION_BEFORE_30_DAYS',
)
)
transact.delete(
key=KeyPair(
pk=id,
sk='SCHEDULE#SET_AS_EXPIRED',
)
)
transact.delete(
key=KeyPair(
pk=id,
sk='parent_vacancy',
)
)
transact.delete(key=KeyPair(id, 'SCHEDULE#SET_AS_EXPIRED'))
transact.delete(key=KeyPair(id, 'parent_vacancy'))
if lock_hash:
transact.delete(key=KeyPair(id, 'LOCK'))