From a01e4329f01a7f3818b82b6e88110e6533cec702 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Rafael=20Siqueira?= Date: Thu, 22 Jan 2026 10:34:18 -0300 Subject: [PATCH] fix table --- .../app/routes/enrollments/enroll.py | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/api.saladeaula.digital/app/routes/enrollments/enroll.py b/api.saladeaula.digital/app/routes/enrollments/enroll.py index f3243ba..46b41da 100644 --- a/api.saladeaula.digital/app/routes/enrollments/enroll.py +++ b/api.saladeaula.digital/app/routes/enrollments/enroll.py @@ -5,6 +5,10 @@ from uuid import uuid4 import pytz from aws_lambda_powertools import Logger from aws_lambda_powertools.event_handler.api_gateway import Router +from aws_lambda_powertools.event_handler.exceptions import ( + BadRequestError, + NotFoundError, +) from aws_lambda_powertools.event_handler.openapi.params import Body from aws_lambda_powertools.shared.functions import extract_event_from_common_models from layercake.batch import BatchProcessor @@ -33,6 +37,9 @@ processor = BatchProcessor() class DeduplicationConflictError(ConflictError): ... +class SeatNotFoundError(NotFoundError): ... + + class User(BaseModel): id: str | UUID4 name: NameStr @@ -158,6 +165,7 @@ def enroll_now(enrollment: Enrollment, context: Context): now_ = now() user = enrollment.user course = enrollment.course + seat = enrollment.seat org: Org = context['org'] subscription: Subscription | None = context.get('subscription') created_by: Authenticated = context['created_by'] @@ -174,6 +182,9 @@ def enroll_now(enrollment: Enrollment, context: Context): days=course.access_period - offset_days, ) + if not subscription and not seat: + raise BadRequestError('Malformed body') + with dyn.transact_writer() as transact: transact.put( item={ @@ -185,10 +196,10 @@ def enroll_now(enrollment: Enrollment, context: Context): 'user': user.model_dump(), 'course': course.model_dump(), 'access_expires_at': access_expires_at, - 'subscription_covered': True, 'org_id': org.id, 'created_at': now_, } + | ({'subscription_covered': True} if subscription else {}) ) transact.put( item={ @@ -205,7 +216,16 @@ def enroll_now(enrollment: Enrollment, context: Context): 'sk': 'CANCEL_POLICY', 'created_at': now_, } + | ({'seat': seat.model_dump()} if seat else {}) ) + + if seat: + transact.delete( + key=KeyPair(seat.id, seat.sk), + cond_expr='attribute_exists(sk)', + exc_cls=SeatNotFoundError, + ) + transact.put( item={ 'id': enrollment.id, @@ -286,6 +306,7 @@ def enroll_later(enrollment: Enrollment, context: Context): now_ = now() user = enrollment.user course = enrollment.course + seat = enrollment.seat scheduled_for = date_to_midnight(enrollment.scheduled_for) # type: ignore dedup_window = enrollment.deduplication_window org: Org = context['org'] @@ -293,6 +314,9 @@ def enroll_later(enrollment: Enrollment, context: Context): created_by: Authenticated = context['created_by'] lock_hash = md5_hash(f'{user.id}{course.id}') + if not subscription and not seat: + raise BadRequestError('Malformed body') + with dyn.transact_writer() as transact: pk = f'SCHEDULED#ORG#{org.id}' sk = f'{scheduled_for.isoformat()}#{lock_hash}' @@ -311,6 +335,7 @@ def enroll_later(enrollment: Enrollment, context: Context): 'ttl': ttl(start_dt=scheduled_for), 'scheduled_at': now_, } + | ({'seat': seat.model_dump()} if seat else {}) | ( {'dedup_window_offset_days': dedup_window.offset_days} if dedup_window @@ -353,6 +378,7 @@ def enroll_later(enrollment: Enrollment, context: Context): ':billing_day': subscription.billing_day, }, exc_cls=SubscriptionConflictError, + table_name=USER_TABLE, ) transact.condition( key=KeyPair('SUBSCRIPTION#FROZEN', f'ORG#{org.id}'), @@ -361,6 +387,13 @@ def enroll_later(enrollment: Enrollment, context: Context): table_name=USER_TABLE, ) + if seat: + transact.delete( + key=KeyPair(seat.id, seat.sk), + cond_expr='attribute_exists(sk)', + exc_cls=SeatNotFoundError, + ) + return enrollment