This commit is contained in:
2025-05-27 12:15:22 -03:00
parent 270e408c1d
commit 42e62ec183
30 changed files with 287 additions and 178 deletions

View File

@@ -39,7 +39,7 @@ from layercake.funcs import pick
from boto3clients import dynamodb_client, idp_client
from cognito import get_user
from conf import USER_TABLE
from config import USER_TABLE
APIKEY_PREFIX = 'sk-'

View File

@@ -1,36 +0,0 @@
import os
USER_TABLE: str = os.getenv('USER_TABLE') # type: ignore
ORDER_TABLE: str = os.getenv('ORDER_TABLE') # type: ignore
ENROLLMENT_TABLE: str = os.getenv('ENROLLMENT_TABLE') # type: ignore
COURSE_TABLE: str = os.getenv('COURSE_TABLE') # type: ignore
KONVIVA_API_URL: str = os.getenv('KONVIVA_API_URL') # type: ignore
KONVIVA_SECRET_KEY: str = os.getenv('KONVIVA_SECRET_KEY') # type: ignore
MEILISEARCH_HOST: str = os.getenv('MEILISEARCH_HOST') # type: ignore
MEILISEARCH_API_KEY: str = os.getenv('MEILISEARCH_API_KEY') # type: ignore
match os.getenv('AWS_SAM_LOCAL'), os.getenv('PYTEST_VERSION'):
case str() as SAM_LOCAL, _ if SAM_LOCAL: # Only when running `sam local start-api`
MEILISEARCH_HOST = 'http://host.docker.internal:7700'
ELASTIC_CONN = {
'hosts': 'http://host.docker.internal:9200',
}
case _, str() as PYTEST if PYTEST: # Only when running `pytest`
MEILISEARCH_HOST = 'http://127.0.0.1:7700'
ELASTIC_CONN = {
'hosts': 'http://127.0.0.1:9200',
}
case _:
MEILISEARCH_HOST: str = os.getenv('MEILISEARCH_HOST') # type: ignore
ELASTIC_CLOUD_ID = os.getenv('ELASTIC_CLOUD_ID')
ELASTIC_AUTH_PASS = os.getenv('ELASTIC_AUTH_PASS')
ELASTIC_CONN = {
'cloud_id': ELASTIC_CLOUD_ID,
'basic_auth': ('elastic', ELASTIC_AUTH_PASS),
}
USER_POOOL_ID = 'sa-east-1_s6YmVSfXj'

View File

@@ -6,7 +6,7 @@ import requests
from aws_lambda_powertools.event_handler.exceptions import BadRequestError
from glom import glom
from conf import KONVIVA_API_URL, KONVIVA_SECRET_KEY
from config import KONVIVA_API_URL, KONVIVA_SECRET_KEY
class KonvivaError(BadRequestError):

View File

@@ -7,7 +7,7 @@ from meilisearch import Client as Meilisearch
from api_gateway import JSONResponse
from boto3clients import dynamodb_client
from conf import (
from config import (
COURSE_TABLE,
MEILISEARCH_API_KEY,
MEILISEARCH_HOST,

View File

@@ -11,7 +11,7 @@ from layercake.dynamodb import (
import elastic
from boto3clients import dynamodb_client
from conf import ELASTIC_CONN, ENROLLMENT_TABLE, USER_TABLE
from config import ELASTIC_CONN, ENROLLMENT_TABLE, USER_TABLE
from .cancel import router as cancel
from .enroll import router as enroll

View File

@@ -7,7 +7,7 @@ from layercake.dynamodb import (
)
from boto3clients import dynamodb_client
from conf import (
from config import (
ENROLLMENT_TABLE,
USER_TABLE,
)

View File

@@ -6,7 +6,7 @@ from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search
from layercake.funcs import pick
from conf import ELASTIC_CONN, USER_TABLE
from config import ELASTIC_CONN, USER_TABLE
router = Router()
elastic_client = Elasticsearch(**ELASTIC_CONN)

View File

@@ -13,7 +13,7 @@ from layercake.dynamodb import (
import elastic
from boto3clients import dynamodb_client
from conf import ELASTIC_CONN, ORDER_TABLE
from config import ELASTIC_CONN, ORDER_TABLE
router = Router()
order_layer = DynamoDBPersistenceLayer(ORDER_TABLE, dynamodb_client)

View File

@@ -15,7 +15,7 @@ from layercake.dynamodb import (
from pydantic.main import BaseModel
from boto3clients import dynamodb_client
from conf import USER_TABLE
from config import USER_TABLE
from rules.org import update_policies
router = Router()

View File

@@ -8,7 +8,7 @@ from layercake.dynamodb import (
import konviva
from boto3clients import dynamodb_client
from conf import USER_TABLE
from config import USER_TABLE
from middlewares import User
router = Router()

View File

@@ -21,7 +21,7 @@ import cognito
import elastic
from api_gateway import JSONResponse
from boto3clients import dynamodb_client, idp_client
from conf import ELASTIC_CONN, USER_POOOL_ID, USER_TABLE
from config import ELASTIC_CONN, USER_POOOL_ID, USER_TABLE
from middlewares import AuditLogMiddleware
from models import User
from rules.user import update_user

View File

@@ -15,7 +15,7 @@ from pydantic import BaseModel, EmailStr
from api_gateway import JSONResponse
from boto3clients import dynamodb_client
from conf import USER_TABLE
from config import USER_TABLE
from middlewares import AuditLogMiddleware
from rules.user import add_email, del_email, set_email_as_primary

View File

@@ -11,7 +11,7 @@ from layercake.dynamodb import (
)
from boto3clients import dynamodb_client
from conf import USER_TABLE
from config import USER_TABLE
from .orgs import router as orgs

View File

@@ -16,7 +16,7 @@ from pydantic import BaseModel
from api_gateway import JSONResponse
from boto3clients import dynamodb_client
from conf import USER_TABLE
from config import USER_TABLE
from middlewares.audit_log_middleware import AuditLogMiddleware
from rules.user import del_org_member

View File

@@ -5,8 +5,9 @@ from uuid import uuid4
from layercake.dateutils import now, ttl
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair, TransactItems
from layercake.strutils import md5_hash
from conf import ORDER_TABLE
from config import ORDER_TABLE
from models import Course, Enrollment
@@ -23,6 +24,10 @@ class Author(TypedDict):
class Vacancy(TypedDict): ...
class DeduplicationWindow(TypedDict):
offset_days: int
class LifecycleEvents(str, Enum):
"""Lifecycle events related to scheduling actions."""
@@ -49,20 +54,23 @@ def enroll(
enrollment: Enrollment,
*,
tenant: Tenant,
vacancy: Vacancy | None = None,
deduplication_window: DeduplicationWindow | None = None,
persistence_layer: DynamoDBPersistenceLayer,
) -> bool:
"""Enrolls a user into a course and schedules lifecycle events."""
now_ = now()
user = enrollment.user
course = enrollment.course
tenant_id = tenant['id']
transact = TransactItems(persistence_layer.table_name)
transact.put(
item={
'sk': '0',
'create_date': now_,
'metadata__tenant_id': tenant['id'],
'metadata__related_ids': {tenant['id'], user.id},
'metadata__tenant_id': tenant_id,
'metadata__related_ids': {tenant_id, user.id},
**enrollment.model_dump(),
},
)
@@ -70,7 +78,7 @@ def enroll(
item={
'id': enrollment.id,
'sk': 'metadata#tenant',
'tenant_id': f'ORG#{tenant["id"]}',
'tenant_id': f'ORG#{tenant_id}',
'name': tenant['name'],
'create_date': now_,
},
@@ -97,7 +105,6 @@ def enroll(
'ttl': ttl(start_dt=now_ + timedelta(days=course.access_period - 30)),
},
)
transact.put(
item={
'id': enrollment.id,
@@ -110,6 +117,43 @@ def enroll(
},
)
# Prevents the user from enrolling in the same course again until
# the deduplication window expires or is removed
if deduplication_window:
lock_hash = md5_hash('%s%s' % (user.id, course.id))
offset_days = deduplication_window['offset_days']
ttl_expiration = ttl(
start_dt=now_ + timedelta(days=course.access_period - offset_days)
)
transact.put(
item={
'id': 'lock',
'sk': lock_hash,
'enrollment_id': enrollment.id,
'create_date': now_,
'ttl': ttl_expiration,
},
cond_expr='attribute_not_exists(sk)',
)
transact.put(
item={
'id': enrollment.id,
'sk': 'metadata#lock',
'hash': lock_hash,
'create_date': now_,
'ttl': ttl_expiration,
},
)
# Deduplication window can be recalculated if needed
transact.put(
item={
'id': enrollment.id,
'sk': 'metadata#deduplication_window',
'offset_days': offset_days,
'create_date': now_,
},
)
return persistence_layer.transact_write_items(transact)