add billing period

This commit is contained in:
2025-08-21 13:32:44 -03:00
parent 6c301d320b
commit c70a74b94a
14 changed files with 282 additions and 98 deletions

View File

@@ -40,29 +40,41 @@ 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'
# REMINDER_NO_ACCESS_AFTER_3_DAYS = 'SCHEDULE#REMINDER_NO_ACCESS_AFTER_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'
# REMINDER_NO_ACTIVITY_AFTER_7_DAYS = 'SCHEDULE#REMINDER_NO_ACTIVITY_AFTER_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'
# REMINDER_ACCESS_PERIOD_BEFORE_30_DAYS = 'SCHEDULE#REMINDER_ACCESS_PERIOD_BEFORE_30_DAYS'
ACCESS_PERIOD_ENDS = 'schedules#access_period_ends'
# Reminder for certificate expiration set to 30 days from now
CERT_EXP_REMINDER_30_DAYS = 'SCHEDULES#CERT_EXP_REMINDER_30_DAYS'
REMINDER_CERT_EXPIRATION_BEFORE_30_DAYS = (
'SCHEDULE#REMINDER_CERT_EXPIRATION_BEFORE_30_DAYS'
)
# Archive the course after the certificate expires
# SET_AS_ARCHIVE = 'schedules#set_as_archive'
# SET_AS_ARCHIVE = 'SCHEDULE#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'
# SET_AS_EXPIRE = 'SCHEDULE#SET_AS_EXPIRE'
EXPIRATION = 'schedules#expiration'
class DeduplicationConflictError(Exception):
def __init__(self, *args):
super().__init__('Enrollment already exists')
class SlotDoesNotExistError(Exception):
def __init__(self, *args):
super().__init__('Slot does not exist')
def enroll(
enrollment: Enrollment,
*,
@@ -112,8 +124,8 @@ def enroll(
},
)
# Enrollment expires by default when the access period ends.
# When the course is finished, it is automatically removed,
# and the `schedules#course_archived` event is created.
# When the course is completed, it is automatically removed,
# and the `SCHEDULE#SET_AS_ARCHIVE` event is created.
transact.put(
item={
'id': enrollment.id,
@@ -146,9 +158,7 @@ def enroll(
transact.put(
item={
'id': enrollment.id,
# Post-migration: uncomment the following line
# 'sk': f'LINKED_ENTITIES#{entity.type}',
'sk': f'linked_entities#{entity.type}',
'sk': f'LINKED_ENTITIES#{entity.type}',
'created_at': now_,
f'{keyprefix}_id': entity.id,
}
@@ -166,10 +176,6 @@ def enroll(
}
)
class SlotDoesNotExistError(Exception):
def __init__(self, *args):
super().__init__('Slot does not exist')
transact.delete(
key=KeyPair(slot.id, slot.sk),
cond_expr='attribute_exists(sk)',
@@ -194,10 +200,6 @@ def enroll(
},
)
class DeduplicationConflictError(Exception):
def __init__(self, *args):
super().__init__('Enrollment already exists')
# Prevents the user from enrolling in the same course again until
# the deduplication window expires or is removed.
if deduplication_window:

View File

@@ -1,12 +0,0 @@
from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.data_classes import EventBridgeEvent, event_source
from aws_lambda_powertools.utilities.typing import LambdaContext
logger = Logger(__name__)
@event_source(data_classe=EventBridgeEvent)
@logger.inject_lambda_context
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
new_image = event.detail['new_image']
return True

View File

@@ -19,7 +19,7 @@ SUBJECT = 'Seu curso de {course} está esperando por você na EDUSEG®'
MESSAGE = """
Oi {first_name}, tudo bem?<br/><br/>
Há 3 dias você foi matriculado no curso de <b>{course}</b>, mas ainda não iniciou.<br/>
Você foi matriculado no curso de <b>{course}</b> há 3 dias, mas ainda não iniciou.<br/>
Não perca a oportunidade de aprender e aproveitar ao máximo seu curso!<br/><br/>
Clique no link para acessar seu curso:
@@ -39,10 +39,10 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
# Post-migration: Remove the following lines
if 'email' not in old_image:
# If email is missing, use enrollment email
data = enrollment_layer.get_item(KeyPair(old_image['id'], '0'))
old_image['name'] = data['user']['name']
old_image['email'] = data['user']['email']
old_image['course'] = data['course']['name']
cur_image = enrollment_layer.get_item(KeyPair(old_image['id'], '0'))
old_image['name'] = cur_image['user']['name']
old_image['email'] = cur_image['user']['email']
old_image['course'] = cur_image['course']['name']
emailmsg = Message(
from_=EMAIL_SENDER,
@@ -74,7 +74,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
enrollment_layer.put_item(
item={
'id': old_image['id'],
'sk': 'SCHEDULES#REMINDER_NO_ACCESS_AFTER_3_DAYS#FAILED',
'sk': 'SCHEDULE#REMINDER_NO_ACCESS_AFTER_3_DAYS#FAILED',
# Post-migration: Uncomment the following line
# 'sk': f'{old_image["sk"]}#FAILED',
'created_at': now_,
@@ -86,7 +86,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
enrollment_layer.put_item(
item={
'id': old_image['id'],
'sk': 'SCHEDULES#REMINDER_NO_ACCESS_AFTER_3_DAYS#EXECUTED',
'sk': 'SCHEDULE#REMINDER_NO_ACCESS_AFTER_3_DAYS#EXECUTED',
# Post-migration: Uncomment the following line
# 'sk': f'{old_image["sk"]}#EXECUTED',
'created_at': now_,

View File

@@ -1,12 +1,89 @@
from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.data_classes import EventBridgeEvent, event_source
from aws_lambda_powertools.utilities.data_classes import (
EventBridgeEvent,
event_source,
)
from aws_lambda_powertools.utilities.typing import LambdaContext
from layercake.dateutils import now
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
from layercake.email_ import Message
from layercake.strutils import first_word, truncate_str
from boto3clients import dynamodb_client, sesv2_client
from config import (
EMAIL_SENDER,
ENROLLMENT_TABLE,
)
SUBJECT = ''
MESSAGE = """
"""
logger = Logger(__name__)
enrollment_layer = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
@event_source(data_classe=EventBridgeEvent)
@event_source(data_class=EventBridgeEvent)
@logger.inject_lambda_context
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
new_image = event.detail['new_image']
return True
old_image = event.detail['old_image']
now_ = now()
# Post-migration: Remove the following lines
if 'email' not in old_image:
# If email is missing, use enrollment email
cur_image = enrollment_layer.get_item(KeyPair(old_image['id'], '0'))
old_image['name'] = cur_image['user']['name']
old_image['email'] = cur_image['user']['email']
old_image['course'] = cur_image['course']['name']
emailmsg = Message(
from_=EMAIL_SENDER,
to=(
old_image['name'],
old_image['email'],
),
subject=SUBJECT.format(course=truncate_str(old_image['course'])),
)
emailmsg.add_alternative(
MESSAGE.format(
first_name=first_word(old_image['name']),
course=old_image['course'],
)
)
try:
sesv2_client.send_email(
Content={
'Raw': {
'Data': emailmsg.as_bytes(),
},
}
)
logger.info('Email sent')
except Exception as exc:
logger.exception(exc)
enrollment_layer.put_item(
item={
'id': old_image['id'],
'sk': 'SCHEDULE#REMINDER_NO_ACTIVITY_AFTER_7_DAYS#FAILED',
# Post-migration: Uncomment the following line
# 'sk': f'{old_image["sk"]}#FAILED',
'created_at': now_,
}
)
return False
else:
enrollment_layer.put_item(
item={
'id': old_image['id'],
'sk': 'SCHEDULE#REMINDER_NO_ACTIVITY_AFTER_7_DAYS#EXECUTED',
# Post-migration: Uncomment the following line
# 'sk': f'{old_image["sk"]}#EXECUTED',
'created_at': now_,
}
)
return True

View File

@@ -30,7 +30,7 @@ processor = BatchProcessor()
@event_source(data_class=EventBridgeEvent)
@logger.inject_lambda_context
def lambda_handler(event: EventBridgeEvent, context: LambdaContext):
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
new_image = event.detail['new_image']
order = order_layer.collection.get_items(
TransactKey(new_image['id'])

View File

@@ -23,13 +23,13 @@ enrollment_layer = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
new_image = event.detail['new_image']
now_ = now()
data = user_layer.get_item(
terms = user_layer.get_item(
# Post-migration: uncomment the following line
# KeyPair(new_image['org_id'], 'METADATA#BILLING_TERMS'),
KeyPair(new_image['tenant_id'], 'metadata#billing_policy'),
)
if not data:
if not terms:
return False
try:
@@ -49,8 +49,8 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
'id': new_image['id'],
'sk': 'METADATA#SUBSCRIPTION_COVERED',
'org_id': new_image['tenant_id'],
'billing_day': data['billing_day'],
'created_at': now(),
'billing_day': terms['billing_day'],
'created_at': now_,
},
cond_expr='attribute_not_exists(sk)',
)