add certs
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
import json
|
||||
from datetime import datetime
|
||||
|
||||
import requests
|
||||
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
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
|
||||
from layercake.dateutils import fromisoformat, now
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer, SortKey, TransactKey
|
||||
|
||||
from boto3clients import dynamodb_client
|
||||
from config import (
|
||||
ENROLLMENT_TABLE,
|
||||
)
|
||||
from boto3clients import dynamodb_client, s3_client
|
||||
from config import BUCKET_NAME, ENROLLMENT_TABLE, PAPERFORGE_API
|
||||
|
||||
logger = Logger(__name__)
|
||||
enrollment_layer = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
|
||||
@@ -19,11 +22,87 @@ enrollment_layer = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
|
||||
@logger.inject_lambda_context
|
||||
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
new_image = event.detail['new_image']
|
||||
course_metadata = enrollment_layer.collection.get_item(
|
||||
KeyPair(
|
||||
pk=new_image['id'],
|
||||
sk='METADATA#COURSE',
|
||||
),
|
||||
now_ = now()
|
||||
course = enrollment_layer.collection.get_items(
|
||||
TransactKey(new_image['id'])
|
||||
+ SortKey('METADATA#COURSE', path_spec='cert', rename_key='cert')
|
||||
+ SortKey('STARTED', path_spec='started_at', rename_key='started_at')
|
||||
+ SortKey('COMPLETED', path_spec='completed_at', rename_key='completed_at'),
|
||||
flatten_top=False,
|
||||
)
|
||||
|
||||
if 'cert' not in course:
|
||||
# There is no certificate to issue from metadata
|
||||
return False
|
||||
|
||||
started_at: datetime = fromisoformat(course['started_at']) # type: ignore
|
||||
completed_at: datetime = fromisoformat(course['completed_at']) # type: ignore
|
||||
|
||||
json_data = json.dumps(
|
||||
{
|
||||
'template_s3_uri': course['cert']['s3_uri'],
|
||||
'template_vars': {
|
||||
'name': new_image['user']['name'],
|
||||
'cpf': _cpffmt(new_image['user']['cpf']),
|
||||
'score': new_image['score'],
|
||||
'started_at': started_at.strftime('%d/%m/%Y'),
|
||||
'completed_at': completed_at.strftime('%d/%m/%Y'),
|
||||
'today': _datefmt(now_),
|
||||
},
|
||||
},
|
||||
)
|
||||
# Send template URI and data to Paperforge API to generate a PDF
|
||||
r = requests.post(PAPERFORGE_API, data=json_data)
|
||||
r.raise_for_status()
|
||||
|
||||
object_key = 'issuedcerts/{completed_at}_{id}.pdf'.format(
|
||||
completed_at=completed_at.strftime('%Y-%m-%d'),
|
||||
id=new_image['id'],
|
||||
)
|
||||
s3_uri = f's3://{BUCKET_NAME}/{object_key}'
|
||||
|
||||
try:
|
||||
s3_client.put_object(
|
||||
Bucket=BUCKET_NAME,
|
||||
Key=object_key,
|
||||
Body=r.content,
|
||||
ContentType='application/pdf',
|
||||
)
|
||||
except Exception as exc:
|
||||
logger.exception(exc)
|
||||
raise
|
||||
|
||||
enrollment_layer.put_item(
|
||||
item={
|
||||
'id': new_image['id'],
|
||||
'sk': 'ISSUED_CERT',
|
||||
's3_uri': s3_uri,
|
||||
'created_at': now_,
|
||||
}
|
||||
)
|
||||
|
||||
logger.info(f'PDF uploaded successfully to {s3_uri}')
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def _cpffmt(s: str) -> str:
|
||||
return '{}.{}.{}-{}'.format(s[:3], s[3:6], s[6:9], s[9:])
|
||||
|
||||
|
||||
def _datefmt(dt: datetime) -> str:
|
||||
months = [
|
||||
'Janeiro',
|
||||
'Fevereiro',
|
||||
'Março',
|
||||
'Abril',
|
||||
'Maio',
|
||||
'Junho',
|
||||
'Julho',
|
||||
'Agosto',
|
||||
'Setembro',
|
||||
'Outubro',
|
||||
'Novembro',
|
||||
'Dezembro',
|
||||
]
|
||||
return f'{dt.day:02d} de {months[dt.month - 1]} de {dt.year}'
|
||||
|
||||
@@ -50,9 +50,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
'sk': 'METADATA#COURSE',
|
||||
'created_at': now_,
|
||||
'access_period': int(course['access_period']),
|
||||
'cert': {
|
||||
'exp_interval': int(course['cert']['exp_interval']),
|
||||
},
|
||||
'cert': course.get('cert', None),
|
||||
}
|
||||
)
|
||||
except Exception as exc:
|
||||
|
||||
Reference in New Issue
Block a user