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): ...
|
||||
|
||||
@@ -51,6 +51,9 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
old_image = event.detail['old_image']
|
||||
# Key pattern `CERT#REPORTING#ORG#{org_id}`
|
||||
*_, org_id = old_image['id'].split('#')
|
||||
# Key pattern `MONTH#{month}#SCHEDULE#SEND_REPORT_EMAIL`
|
||||
_, month, *_ = old_image['sk'].split('#')
|
||||
|
||||
event_name = old_image['sk']
|
||||
target_month = old_image['target_month']
|
||||
pretty_month = _monthfmt(datetime.strptime(target_month, '%Y-%m').date())
|
||||
@@ -95,6 +98,28 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
emailmsg.attach(attachment)
|
||||
|
||||
try:
|
||||
with enrollment_layer.transact_writer() as transact:
|
||||
transact.update(
|
||||
key=KeyPair(
|
||||
pk=old_image['id'],
|
||||
sk=f'MONTH#{month}',
|
||||
),
|
||||
update_expr='SET #status = :status, updated_at = :updated_at',
|
||||
expr_attr_names={'#status': 'status'},
|
||||
expr_attr_values={
|
||||
':status': 'CLOSED',
|
||||
':updated_at': now_,
|
||||
},
|
||||
cond_expr='attribute_exists(sk)',
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
'id': old_image['id'],
|
||||
'sk': f'{event_name}#EXECUTED',
|
||||
'created_at': now_,
|
||||
}
|
||||
)
|
||||
|
||||
sesv2_client.send_email(
|
||||
Content={
|
||||
'Raw': {
|
||||
@@ -102,14 +127,6 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
},
|
||||
}
|
||||
)
|
||||
enrollment_layer.put_item(
|
||||
item={
|
||||
'id': old_image['id'],
|
||||
'sk': f'{event_name}#EXECUTED',
|
||||
'created_at': now_,
|
||||
}
|
||||
)
|
||||
logger.info('Email sent')
|
||||
except Exception as exc:
|
||||
logger.exception(exc)
|
||||
enrollment_layer.put_item(
|
||||
@@ -121,6 +138,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
)
|
||||
return False
|
||||
else:
|
||||
logger.info('Email sent')
|
||||
return True
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user