add billing

This commit is contained in:
2025-12-12 20:28:47 -03:00
parent 3147ec2317
commit c516960b01
16 changed files with 496 additions and 97 deletions

View File

@@ -56,7 +56,7 @@ class DeduplicationWindow(BaseModel):
offset_days: int
class SubscriptionTerms(BaseModel):
class Subscription(BaseModel):
billing_day: int
@@ -88,8 +88,8 @@ def enroll(
)
+ KeyPair(
pk=str(org_id),
sk='METADATA#SUBSCRIPTION_TERMS',
rename_key='terms',
sk='METADATA#SUBSCRIPTION',
rename_key='subscription',
table_name=USER_TABLE,
)
+ KeyPair(
@@ -106,7 +106,7 @@ def enroll(
ctx = {
'org': Org.model_validate(org),
'created_by': created_by,
'terms': SubscriptionTerms.model_validate(org['terms']),
'subscription': Subscription.model_validate(org['subscription']),
}
immediate = [e for e in enrollments if not e.scheduled_for]
@@ -150,7 +150,7 @@ Context = TypedDict(
'Context',
{
'org': Org,
'terms': SubscriptionTerms,
'subscription': Subscription,
'created_by': Authenticated,
},
)
@@ -161,7 +161,7 @@ def enroll_now(enrollment: Enrollment, context: Context):
user = enrollment.user
course = enrollment.course
org: Org = context['org']
subscription_terms: SubscriptionTerms = context['terms']
subscription: Subscription = context['subscription']
created_by: Authenticated = context['created_by']
lock_hash = md5_hash(f'{user.id}{course.id}')
access_expires_at = now_ + timedelta(days=course.access_period)
@@ -213,7 +213,7 @@ def enroll_now(enrollment: Enrollment, context: Context):
'id': enrollment.id,
'sk': 'METADATA#SUBSCRIPTION_COVERED',
'org_id': org.id,
'billing_day': subscription_terms.billing_day,
'billing_day': subscription.billing_day,
'created_at': now_,
}
)
@@ -270,7 +270,7 @@ def enroll_later(enrollment: Enrollment, context: Context):
scheduled_for = date_to_midnight(enrollment.scheduled_for) # type: ignore
deduplication_window = enrollment.deduplication_window
org: Org = context['org']
subscription_terms: SubscriptionTerms = context['terms']
subscription: Subscription = context['subscription']
created_by: Authenticated = context['created_by']
lock_hash = md5_hash(f'{user.id}{course.id}')
@@ -289,7 +289,7 @@ def enroll_later(enrollment: Enrollment, context: Context):
'id': created_by.id,
'name': created_by.name,
},
'subscription_billing_day': subscription_terms.billing_day,
'subscription_billing_day': subscription.billing_day,
'ttl': ttl(start_dt=scheduled_for),
'created_at': now_,
}

View File

@@ -6,14 +6,27 @@ from aws_lambda_powertools.event_handler.openapi.params import Query
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
from boto3clients import dynamodb_client
from config import COURSE_TABLE
from config import ORDER_TABLE, USER_TABLE
router = Router()
dyn = DynamoDBPersistenceLayer(COURSE_TABLE, dynamodb_client)
dyn = DynamoDBPersistenceLayer(ORDER_TABLE, dynamodb_client)
@router.get('/<org_id>/subscription')
def subscription(org_id: str):
return dyn.collection.get_item(
KeyPair(
pk=org_id,
sk='METADATA#SUBSCRIPTION',
table_name=USER_TABLE,
),
raise_on_error=False,
default={},
)
@router.get('/<org_id>/billing')
def get_custom_pricing(
def billing(
org_id: str,
start_date: Annotated[date, Query()],
end_date: Annotated[date, Query()],