fix table
This commit is contained in:
@@ -5,6 +5,10 @@ from uuid import uuid4
|
|||||||
import pytz
|
import pytz
|
||||||
from aws_lambda_powertools import Logger
|
from aws_lambda_powertools import Logger
|
||||||
from aws_lambda_powertools.event_handler.api_gateway import Router
|
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.event_handler.openapi.params import Body
|
||||||
from aws_lambda_powertools.shared.functions import extract_event_from_common_models
|
from aws_lambda_powertools.shared.functions import extract_event_from_common_models
|
||||||
from layercake.batch import BatchProcessor
|
from layercake.batch import BatchProcessor
|
||||||
@@ -33,6 +37,9 @@ processor = BatchProcessor()
|
|||||||
class DeduplicationConflictError(ConflictError): ...
|
class DeduplicationConflictError(ConflictError): ...
|
||||||
|
|
||||||
|
|
||||||
|
class SeatNotFoundError(NotFoundError): ...
|
||||||
|
|
||||||
|
|
||||||
class User(BaseModel):
|
class User(BaseModel):
|
||||||
id: str | UUID4
|
id: str | UUID4
|
||||||
name: NameStr
|
name: NameStr
|
||||||
@@ -158,6 +165,7 @@ def enroll_now(enrollment: Enrollment, context: Context):
|
|||||||
now_ = now()
|
now_ = now()
|
||||||
user = enrollment.user
|
user = enrollment.user
|
||||||
course = enrollment.course
|
course = enrollment.course
|
||||||
|
seat = enrollment.seat
|
||||||
org: Org = context['org']
|
org: Org = context['org']
|
||||||
subscription: Subscription | None = context.get('subscription')
|
subscription: Subscription | None = context.get('subscription')
|
||||||
created_by: Authenticated = context['created_by']
|
created_by: Authenticated = context['created_by']
|
||||||
@@ -174,6 +182,9 @@ def enroll_now(enrollment: Enrollment, context: Context):
|
|||||||
days=course.access_period - offset_days,
|
days=course.access_period - offset_days,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if not subscription and not seat:
|
||||||
|
raise BadRequestError('Malformed body')
|
||||||
|
|
||||||
with dyn.transact_writer() as transact:
|
with dyn.transact_writer() as transact:
|
||||||
transact.put(
|
transact.put(
|
||||||
item={
|
item={
|
||||||
@@ -185,10 +196,10 @@ def enroll_now(enrollment: Enrollment, context: Context):
|
|||||||
'user': user.model_dump(),
|
'user': user.model_dump(),
|
||||||
'course': course.model_dump(),
|
'course': course.model_dump(),
|
||||||
'access_expires_at': access_expires_at,
|
'access_expires_at': access_expires_at,
|
||||||
'subscription_covered': True,
|
|
||||||
'org_id': org.id,
|
'org_id': org.id,
|
||||||
'created_at': now_,
|
'created_at': now_,
|
||||||
}
|
}
|
||||||
|
| ({'subscription_covered': True} if subscription else {})
|
||||||
)
|
)
|
||||||
transact.put(
|
transact.put(
|
||||||
item={
|
item={
|
||||||
@@ -205,7 +216,16 @@ def enroll_now(enrollment: Enrollment, context: Context):
|
|||||||
'sk': 'CANCEL_POLICY',
|
'sk': 'CANCEL_POLICY',
|
||||||
'created_at': now_,
|
'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(
|
transact.put(
|
||||||
item={
|
item={
|
||||||
'id': enrollment.id,
|
'id': enrollment.id,
|
||||||
@@ -286,6 +306,7 @@ def enroll_later(enrollment: Enrollment, context: Context):
|
|||||||
now_ = now()
|
now_ = now()
|
||||||
user = enrollment.user
|
user = enrollment.user
|
||||||
course = enrollment.course
|
course = enrollment.course
|
||||||
|
seat = enrollment.seat
|
||||||
scheduled_for = date_to_midnight(enrollment.scheduled_for) # type: ignore
|
scheduled_for = date_to_midnight(enrollment.scheduled_for) # type: ignore
|
||||||
dedup_window = enrollment.deduplication_window
|
dedup_window = enrollment.deduplication_window
|
||||||
org: Org = context['org']
|
org: Org = context['org']
|
||||||
@@ -293,6 +314,9 @@ def enroll_later(enrollment: Enrollment, context: Context):
|
|||||||
created_by: Authenticated = context['created_by']
|
created_by: Authenticated = context['created_by']
|
||||||
lock_hash = md5_hash(f'{user.id}{course.id}')
|
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:
|
with dyn.transact_writer() as transact:
|
||||||
pk = f'SCHEDULED#ORG#{org.id}'
|
pk = f'SCHEDULED#ORG#{org.id}'
|
||||||
sk = f'{scheduled_for.isoformat()}#{lock_hash}'
|
sk = f'{scheduled_for.isoformat()}#{lock_hash}'
|
||||||
@@ -311,6 +335,7 @@ def enroll_later(enrollment: Enrollment, context: Context):
|
|||||||
'ttl': ttl(start_dt=scheduled_for),
|
'ttl': ttl(start_dt=scheduled_for),
|
||||||
'scheduled_at': now_,
|
'scheduled_at': now_,
|
||||||
}
|
}
|
||||||
|
| ({'seat': seat.model_dump()} if seat else {})
|
||||||
| (
|
| (
|
||||||
{'dedup_window_offset_days': dedup_window.offset_days}
|
{'dedup_window_offset_days': dedup_window.offset_days}
|
||||||
if dedup_window
|
if dedup_window
|
||||||
@@ -353,6 +378,7 @@ def enroll_later(enrollment: Enrollment, context: Context):
|
|||||||
':billing_day': subscription.billing_day,
|
':billing_day': subscription.billing_day,
|
||||||
},
|
},
|
||||||
exc_cls=SubscriptionConflictError,
|
exc_cls=SubscriptionConflictError,
|
||||||
|
table_name=USER_TABLE,
|
||||||
)
|
)
|
||||||
transact.condition(
|
transact.condition(
|
||||||
key=KeyPair('SUBSCRIPTION#FROZEN', f'ORG#{org.id}'),
|
key=KeyPair('SUBSCRIPTION#FROZEN', f'ORG#{org.id}'),
|
||||||
@@ -361,6 +387,13 @@ def enroll_later(enrollment: Enrollment, context: Context):
|
|||||||
table_name=USER_TABLE,
|
table_name=USER_TABLE,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if seat:
|
||||||
|
transact.delete(
|
||||||
|
key=KeyPair(seat.id, seat.sk),
|
||||||
|
cond_expr='attribute_exists(sk)',
|
||||||
|
exc_cls=SeatNotFoundError,
|
||||||
|
)
|
||||||
|
|
||||||
return enrollment
|
return enrollment
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user