add cert_expires_at
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import os
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import timedelta
|
||||
|
||||
import pytz
|
||||
from aws_lambda_powertools import Logger
|
||||
@@ -8,7 +8,6 @@ from aws_lambda_powertools.utilities.data_classes import (
|
||||
event_source,
|
||||
)
|
||||
from aws_lambda_powertools.utilities.typing import LambdaContext
|
||||
from glom import glom
|
||||
from layercake.dateutils import fromisoformat, now, ttl
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer
|
||||
from layercake.funcs import pick
|
||||
@@ -25,14 +24,11 @@ tz = os.getenv('TZ', 'UTC')
|
||||
@logger.inject_lambda_context
|
||||
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool | None:
|
||||
new_image = event.detail['new_image']
|
||||
expires_at = glom(new_image, 'cert.expires_at', default=None)
|
||||
|
||||
if not expires_at:
|
||||
return None
|
||||
|
||||
enrollment_id = new_image['id']
|
||||
org_id = new_image['org_id']
|
||||
expires_at: datetime = fromisoformat(expires_at).replace(tzinfo=pytz.timezone(tz)) # type: ignore
|
||||
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)
|
||||
now_ = now()
|
||||
@@ -59,11 +55,10 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool | No
|
||||
item={
|
||||
'id': pk,
|
||||
'sk': f'{sk}#ENROLLMENT#{enrollment_id}',
|
||||
'enrollment_id': new_image['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, # type: ignore
|
||||
'expires_at': expires_at,
|
||||
'completed_at': new_image['completed_at'],
|
||||
'created_at': now_,
|
||||
},
|
||||
|
||||
@@ -52,14 +52,14 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
# Key pattern `CERT#REPORTING#ORG#{org_id}`
|
||||
*_, org_id = old_image['id'].split('#')
|
||||
event_name = old_image['sk']
|
||||
target_month = datetime.strptime(old_image['target_month'], '%Y-%m').date()
|
||||
month = _monthfmt(target_month)
|
||||
target_month = old_image['target_month']
|
||||
pretty_month = _monthfmt(datetime.strptime(target_month, '%Y-%m').date())
|
||||
now_ = now()
|
||||
|
||||
result = enrollment_layer.collection.query(
|
||||
r = enrollment_layer.collection.query(
|
||||
KeyPair(
|
||||
pk=old_image['id'],
|
||||
sk='MONTH#{}#ENROLLMENT'.format(target_month.strftime('%Y-%m')),
|
||||
sk=f'MONTH#{target_month}#ENROLLMENT',
|
||||
),
|
||||
limit=150,
|
||||
)
|
||||
@@ -68,8 +68,8 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
{
|
||||
'template_uri': CERT_REPORTING_URI,
|
||||
'args': {
|
||||
'month': month,
|
||||
'items': result['items'],
|
||||
'month': pretty_month,
|
||||
'items': r['items'],
|
||||
},
|
||||
},
|
||||
cls=Encoder,
|
||||
@@ -83,14 +83,14 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
to=_get_admin_emails(org_id),
|
||||
reply_to=REPLY_TO,
|
||||
bcc=BCC,
|
||||
subject=SUBJECT.format(month=month),
|
||||
subject=SUBJECT.format(month=pretty_month),
|
||||
)
|
||||
emailmsg.add_alternative(MESSAGE.format(month=month))
|
||||
emailmsg.add_alternative(MESSAGE.format(month=pretty_month))
|
||||
attachment = MIMEApplication(r.content)
|
||||
attachment.add_header(
|
||||
'Content-Disposition',
|
||||
'attachment',
|
||||
filename='{}.pdf'.format(target_month.strftime('%Y-%m')),
|
||||
filename=f'{target_month}.pdf',
|
||||
)
|
||||
emailmsg.attach(attachment)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user