fix issued cert
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
"""
|
||||
If a certificate exists, remind the user that the certificate expired.
|
||||
"""
|
||||
|
||||
SUBJECT = 'Seu certificado {course} expirou!'
|
||||
MESSAGE = """
|
||||
Oi {first_name}, tudo bem?<br/><br/>
|
||||
|
||||
O certificado do curso <b>{course}</b> expirou.<br/>
|
||||
Para manter sua certificação válida, é necessário refazer o curso.<br/><br/>
|
||||
|
||||
<a href="https://saladeaula.digital">👉 Acesse o curso e renove sua certificação</a>
|
||||
"""
|
||||
@@ -15,7 +15,7 @@ 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)
|
||||
dyn = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
|
||||
|
||||
|
||||
@event_source(data_class=EventBridgeEvent)
|
||||
@@ -24,24 +24,23 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
new_image = event.detail['new_image']
|
||||
now_ = now()
|
||||
enrollment_id = new_image['id']
|
||||
course = enrollment_layer.collection.get_items(
|
||||
TransactKey(new_image['id'])
|
||||
+ SortKey('METADATA#COURSE', path_spec='cert', rename_key='cert')
|
||||
# Post-migration: remove the following lines
|
||||
+ SortKey('STARTED', path_spec='started_at', rename_key='started_at')
|
||||
+ SortKey('COMPLETED', path_spec='completed_at', rename_key='completed_at'),
|
||||
flatten_top=False,
|
||||
cert = dyn.collection.get_item(
|
||||
KeyPair(
|
||||
pk=new_image['id'],
|
||||
sk=SortKey('METADATA#COURSE', path_spec='cert', rename_key='cert'),
|
||||
),
|
||||
raise_on_error=False,
|
||||
default=False,
|
||||
)
|
||||
|
||||
if 'cert' not in course:
|
||||
if not cert:
|
||||
logger.debug('Certificate not found')
|
||||
# There is no certificate to issue from metadata
|
||||
return False
|
||||
|
||||
cert = course['cert']
|
||||
started_at: datetime = fromisoformat(course['started_at']) # type: ignore
|
||||
completed_at: datetime = fromisoformat(course['completed_at']) # type: ignore
|
||||
cert_expires_at = now_ + timedelta(days=int(cert['exp_interval']))
|
||||
started_at: datetime = fromisoformat(new_image['started_at']) # type: ignore
|
||||
completed_at: datetime = fromisoformat(new_image['completed_at']) # type: ignore
|
||||
cert_expires_at = completed_at + timedelta(days=int(cert['exp_interval']))
|
||||
|
||||
try:
|
||||
# Send template URI and data to Paperforge API to generate a PDF
|
||||
@@ -83,13 +82,14 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||
logger.exception(exc)
|
||||
raise
|
||||
|
||||
return enrollment_layer.update_item(
|
||||
return dyn.update_item(
|
||||
key=KeyPair(
|
||||
pk=enrollment_id,
|
||||
sk='0',
|
||||
),
|
||||
update_expr='SET issued_cert = :issued_cert',
|
||||
update_expr='SET issued_cert = :issued_cert, uploaded_at = :now',
|
||||
expr_attr_values={
|
||||
':now': now_,
|
||||
':issued_cert': {
|
||||
'issued_at': now_,
|
||||
'expires_at': cert_expires_at,
|
||||
|
||||
@@ -14,6 +14,8 @@ def test_issue_cert(
|
||||
'detail': {
|
||||
'new_image': {
|
||||
'id': enrollment_id,
|
||||
'completed_at': '2025-09-21T14:20:36.276467-03:00',
|
||||
'started_at': '2025-09-19T14:34:54.704548-03:00',
|
||||
'user': {
|
||||
'name': 'Jimi Hendrix',
|
||||
'cpf': '74630003037',
|
||||
|
||||
@@ -72,8 +72,8 @@ def get_enrollment(id: str):
|
||||
record = enrollment_layer.collection.get_items(
|
||||
TransactKey(id)
|
||||
+ SortKey('0')
|
||||
+ SortKey('STARTED', rename_key='started_at', path_spec='started_at')
|
||||
+ SortKey('COMPLETED', rename_key='completed_at', path_spec='completed_at')
|
||||
# + SortKey('STARTED', rename_key='started_at', path_spec='started_at')
|
||||
# + SortKey('COMPLETED', rename_key='completed_at', path_spec='completed_at')
|
||||
+ SortKey('FAILED', rename_key='failed_at', path_spec='failed_at')
|
||||
+ SortKey('CANCELED', rename_key='canceled')
|
||||
+ SortKey('ARCHIVED', rename_key='archived_at', path_spec='archived_at')
|
||||
|
||||
@@ -201,7 +201,10 @@ def set_status_as_canceled(
|
||||
with persistence_layer.transact_writer() as transact:
|
||||
transact.update(
|
||||
key=KeyPair(id, '0'),
|
||||
update_expr='SET #status = :canceled, updated_at = :updated_at',
|
||||
update_expr='SET #status = :canceled, \
|
||||
access_expired = :true, \
|
||||
canceled_at = :now, \
|
||||
updated_at = :now',
|
||||
cond_expr='#status = :pending',
|
||||
expr_attr_names={
|
||||
'#status': 'status',
|
||||
@@ -209,7 +212,8 @@ def set_status_as_canceled(
|
||||
expr_attr_values={
|
||||
':canceled': 'CANCELED',
|
||||
':pending': 'PENDING',
|
||||
':updated_at': now_,
|
||||
':true': True,
|
||||
':now': now_,
|
||||
},
|
||||
)
|
||||
transact.put(
|
||||
@@ -217,7 +221,7 @@ def set_status_as_canceled(
|
||||
'id': id,
|
||||
'sk': 'CANCELED',
|
||||
'canceled_by': created_by,
|
||||
'canceled_at': now_,
|
||||
'created_at': now_,
|
||||
},
|
||||
)
|
||||
transact.delete(
|
||||
@@ -256,7 +260,7 @@ def set_status_as_canceled(
|
||||
transact.delete(
|
||||
key=KeyPair(
|
||||
pk=id,
|
||||
sk='SCHEDULE#SET_AS_EXPIRED',
|
||||
sk='SCHEDULE#SET_ACCESS_EXPIRED',
|
||||
)
|
||||
)
|
||||
transact.delete(
|
||||
|
||||
Reference in New Issue
Block a user