This commit is contained in:
2025-08-01 19:33:54 -03:00
parent f5bd1e80d6
commit 5c57da7ecb
4 changed files with 80 additions and 42 deletions

View File

@@ -46,21 +46,22 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
data = enrollment_layer.collection.get_items(
TransactKey(enrollment_id) + SortKey('0') + SortKey('author')
)
created_at: datetime = fromisoformat(data['create_date']) # type: ignore
if not data:
logger.debug('Enrollment not found')
return False
logger.info('Enrollment found', data=data)
# Keep it until the migration has been completed
old_course = _get_course(data['course']['id'])
if old_course:
data['course'] = old_course
created_at: datetime = fromisoformat(data['create_date']) # type: ignore
start_date, end_date = get_billing_period(
new_image['billing_day'],
year=created_at.year,
month=created_at.month,
billing_day=new_image['billing_day'],
date_=created_at,
)
pk = 'BILLING#ORG#{org_id}'.format(org_id=org_id)
sk = 'START#{start}#END#{end}'.format(

View File

@@ -1,35 +1,39 @@
import calendar
from datetime import date
from layercake.dateutils import now
from datetime import date, timedelta
def get_billing_period(
billing_day: int,
year: int | None = None,
month: int | None = None,
date_: date,
) -> tuple[date, date]:
today = now()
year_ = year or today.year
month_ = month or today.month
# Determine the anchor month and year
if date_.day >= billing_day:
anchor_month = date_.month
anchor_year = date_.year
else:
# Move to previous month
if date_.month == 1:
anchor_month = 12
anchor_year = date_.year - 1
else:
anchor_month = date_.month - 1
anchor_year = date_.year
_, last_day = calendar.monthrange(year_, month_)
start_day = min(billing_day, last_day)
start_date = date(year_, month_, start_day)
# Calculate start date
_, last_day = calendar.monthrange(anchor_year, anchor_month)
start_date = date(anchor_year, anchor_month, min(billing_day, last_day))
if month_ == 12:
# Calculate next month and year
if anchor_month == 12:
next_month = 1
next_year = year_ + 1
next_year = anchor_year + 1
else:
next_month = month_ + 1
next_year = year_
next_month = anchor_month + 1
next_year = anchor_year
_, last_day_next_month = calendar.monthrange(next_year, next_month)
end_day = min(billing_day, last_day_next_month) - 1
if end_day == 0:
end_date = date(year_, month_, last_day)
else:
end_date = date(next_year, next_month, end_day)
# Calculate end date
_, next_last_day = calendar.monthrange(next_year, next_month)
end_day = min(billing_day, next_last_day)
end_date = date(next_year, next_month, end_day) - timedelta(days=1)
return start_date, end_date