improve report
This commit is contained in:
@@ -29,46 +29,62 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool | No
|
||||
expires_at = fromisoformat(new_image['cert_expires_at']).replace( # type: ignore
|
||||
tzinfo=pytz.timezone(tz)
|
||||
)
|
||||
# The reporting month is the month before the certificate expires
|
||||
month_start = (expires_at.replace(day=1) - timedelta(days=1)).replace(day=1)
|
||||
|
||||
target_month = expires_at.strftime('%Y-%m')
|
||||
now_ = now()
|
||||
pk = f'CERT#REPORTING#ORG#{org_id}'
|
||||
sk = 'MONTH#{}'.format(expires_at.strftime('%Y-%m'))
|
||||
|
||||
if now_ > expires_at:
|
||||
return None
|
||||
|
||||
try:
|
||||
if now_ > expires_at:
|
||||
raise InvalidDateError()
|
||||
|
||||
# The reporting month is the month before the certificate expires
|
||||
report_month = (expires_at.replace(day=1) - timedelta(days=1)).replace(day=1)
|
||||
report_sk = report_month.strftime('%Y-%m')
|
||||
|
||||
with dyn.transact_writer() as transact:
|
||||
transact.put(
|
||||
item={
|
||||
'id': pk,
|
||||
'sk': 'MONTH#{}#SCHEDULE#SEND_REPORT_EMAIL'.format(
|
||||
month_start.strftime('%Y-%m')
|
||||
),
|
||||
'target_month': expires_at.strftime('%Y-%m'),
|
||||
'ttl': ttl(start_dt=month_start),
|
||||
}
|
||||
'sk': f'MONTH#{report_sk}',
|
||||
'status': 'PENDING',
|
||||
'target_month': target_month,
|
||||
},
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
exc_cls=ReportingConflictError,
|
||||
)
|
||||
|
||||
transact.put(
|
||||
item={
|
||||
'id': pk,
|
||||
'sk': f'{sk}#ENROLLMENT#{enrollment_id}',
|
||||
'user': pick(('id', 'name'), new_image['user']),
|
||||
'course': pick(('id', 'name'), new_image['course']),
|
||||
'enrolled_at': new_image['created_at'],
|
||||
'expires_at': expires_at,
|
||||
'completed_at': new_image['completed_at'],
|
||||
'created_at': now_,
|
||||
'sk': f'MONTH#{report_sk}#SCHEDULE#SEND_REPORT_EMAIL',
|
||||
'target_month': target_month,
|
||||
'ttl': ttl(start_dt=report_month),
|
||||
},
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
exc_cls=EnrollmentConflictError,
|
||||
)
|
||||
except EnrollmentConflictError:
|
||||
except Exception as exc:
|
||||
logger.exception(exc)
|
||||
|
||||
try:
|
||||
dyn.put_item(
|
||||
item={
|
||||
'id': pk,
|
||||
'sk': f'MONTH#{target_month}#ENROLLMENT#{enrollment_id}',
|
||||
'user': pick(('id', 'name'), new_image['user']),
|
||||
'course': pick(('id', 'name'), new_image['course']),
|
||||
'enrolled_at': new_image['created_at'],
|
||||
'expires_at': expires_at,
|
||||
'completed_at': new_image['completed_at'],
|
||||
'created_at': now_,
|
||||
},
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
)
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
return True
|
||||
else:
|
||||
return True
|
||||
|
||||
|
||||
class EnrollmentConflictError(Exception): ...
|
||||
class InvalidDateError(Exception): ...
|
||||
|
||||
|
||||
class ReportingConflictError(Exception): ...
|
||||
|
||||
Reference in New Issue
Block a user