update events
This commit is contained in:
@@ -64,13 +64,13 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
|||||||
|
|
||||||
return order_layer.update_item(
|
return order_layer.update_item(
|
||||||
key=KeyPair(new_image['id'], new_image['sk']),
|
key=KeyPair(new_image['id'], new_image['sk']),
|
||||||
update_expr='SET #status = :status, updated_at = :updated_at',
|
update_expr='SET #status = :status, updated_at = :now',
|
||||||
expr_attr_names={
|
expr_attr_names={
|
||||||
'#status': 'status',
|
'#status': 'status',
|
||||||
},
|
},
|
||||||
expr_attr_values={
|
expr_attr_values={
|
||||||
':status': 'SUCCESS',
|
':status': 'SUCCESS',
|
||||||
':updated_at': now_,
|
':now': now_,
|
||||||
},
|
},
|
||||||
cond_expr='attribute_exists(sk)',
|
cond_expr='attribute_exists(sk)',
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ def send_email(
|
|||||||
) -> bool:
|
) -> bool:
|
||||||
now_ = now()
|
now_ = now()
|
||||||
name, _ = to
|
name, _ = to
|
||||||
|
event_name = event['sk']
|
||||||
emailmsg = Message(
|
emailmsg = Message(
|
||||||
from_=sender,
|
from_=sender,
|
||||||
to=to,
|
to=to,
|
||||||
@@ -54,7 +55,7 @@ def send_email(
|
|||||||
dynamodb_persistence_layer.put_item(
|
dynamodb_persistence_layer.put_item(
|
||||||
item={
|
item={
|
||||||
'id': event['id'],
|
'id': event['id'],
|
||||||
'sk': f'{event["sk"]}#EXECUTED',
|
'sk': f'{event_name}#EXECUTED',
|
||||||
'created_at': now_,
|
'created_at': now_,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -65,7 +66,7 @@ def send_email(
|
|||||||
dynamodb_persistence_layer.put_item(
|
dynamodb_persistence_layer.put_item(
|
||||||
item={
|
item={
|
||||||
'id': event['id'],
|
'id': event['id'],
|
||||||
'sk': f'{event["sk"]}#FAILED',
|
'sk': f'{event_name}#FAILED',
|
||||||
'created_at': now_,
|
'created_at': now_,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
|||||||
},
|
},
|
||||||
) as fp:
|
) as fp:
|
||||||
result = fp.process()
|
result = fp.process()
|
||||||
logger.info('Processed courses', result=result)
|
logger.debug('Processed courses', result=result)
|
||||||
|
|
||||||
return order_layer.update_item(
|
return order_layer.update_item(
|
||||||
key=KeyPair(new_image['id'], new_image['sk']),
|
key=KeyPair(new_image['id'], new_image['sk']),
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
|||||||
)
|
)
|
||||||
|
|
||||||
if 'cert' not in course:
|
if 'cert' not in course:
|
||||||
|
logger.debug('Certificate not found')
|
||||||
# There is no certificate to issue from metadata
|
# There is no certificate to issue from metadata
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@@ -73,7 +74,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
|||||||
logger.exception(exc)
|
logger.exception(exc)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
logger.info(f'PDF uploaded successfully to {s3_uri}')
|
logger.debug(f'PDF uploaded successfully to {s3_uri}')
|
||||||
|
|
||||||
return enrollment_layer.update_item(
|
return enrollment_layer.update_item(
|
||||||
key=KeyPair(
|
key=KeyPair(
|
||||||
|
|||||||
@@ -39,8 +39,12 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
|||||||
)
|
)
|
||||||
user = User.model_validate(new_image['user'])
|
user = User.model_validate(new_image['user'])
|
||||||
course = Course.model_validate(new_image['course'] | metadata['course'])
|
course = Course.model_validate(new_image['course'] | metadata['course'])
|
||||||
enrollment = Enrollment(id=uuid4(), course=course, user=user)
|
|
||||||
subscription = metadata['subscription'] if 'subscription' in metadata else None
|
subscription = metadata['subscription'] if 'subscription' in metadata else None
|
||||||
|
enrollment = Enrollment(
|
||||||
|
id=uuid4(),
|
||||||
|
course=course,
|
||||||
|
user=user,
|
||||||
|
)
|
||||||
|
|
||||||
return enroll(
|
return enroll(
|
||||||
enrollment,
|
enrollment,
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
|||||||
enrollment_id = new_image['id']
|
enrollment_id = new_image['id']
|
||||||
user = new_image['user']
|
user = new_image['user']
|
||||||
course_name = glom(new_image, 'course.name')
|
course_name = glom(new_image, 'course.name')
|
||||||
|
# Post-migration: after removing the stopgap file `patch_course_metadata.py`,
|
||||||
|
# use `access_expires_at` from `new_image`
|
||||||
access_period = int(
|
access_period = int(
|
||||||
dyn.collection.get_item(
|
dyn.collection.get_item(
|
||||||
KeyPair(
|
KeyPair(
|
||||||
@@ -49,20 +51,16 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
# By default, the enrollment will expire when the access period ends
|
# Schedule the event to add `access_expired` after the access period ends
|
||||||
# (scheduled below).
|
|
||||||
|
|
||||||
# If the enrollment is completed earlier (e.g., certificate issued),
|
|
||||||
# the expiration schedule is canceled and an archive schedule
|
|
||||||
# (`SCHEDULE#SET_AS_ARCHIVED`) is created instead.
|
|
||||||
transact.put(
|
transact.put(
|
||||||
item={
|
item={
|
||||||
'id': enrollment_id,
|
'id': enrollment_id,
|
||||||
'sk': 'SCHEDULE#SET_AS_EXPIRED',
|
'sk': 'SCHEDULE#SET_ACCESS_EXPIRED',
|
||||||
'created_at': now_,
|
'created_at': now_,
|
||||||
'ttl': ttl(start_dt=now_, days=access_period),
|
'ttl': ttl(start_dt=now_, days=access_period),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
transact.put(
|
transact.put(
|
||||||
item={
|
item={
|
||||||
'id': enrollment_id,
|
'id': enrollment_id,
|
||||||
|
|||||||
@@ -19,8 +19,6 @@ dyn = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
|
|||||||
@event_source(data_class=EventBridgeEvent)
|
@event_source(data_class=EventBridgeEvent)
|
||||||
@logger.inject_lambda_context
|
@logger.inject_lambda_context
|
||||||
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||||
"""If there is no certificate and the access period has ended,
|
|
||||||
the enrollment will be marked as expired."""
|
|
||||||
old_image = event.detail['old_image']
|
old_image = event.detail['old_image']
|
||||||
now_ = now()
|
now_ = now()
|
||||||
|
|
||||||
@@ -31,30 +29,17 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
|||||||
pk=old_image['id'],
|
pk=old_image['id'],
|
||||||
sk='0',
|
sk='0',
|
||||||
),
|
),
|
||||||
update_expr='SET #status = :expired, updated_at = :updated_at',
|
update_expr='SET access_expired = :true, \
|
||||||
cond_expr='#status = :in_progress OR #status = :pending',
|
updated_at = :now',
|
||||||
expr_attr_names={
|
|
||||||
'#status': 'status',
|
|
||||||
},
|
|
||||||
expr_attr_values={
|
expr_attr_values={
|
||||||
':pending': 'PENDING',
|
':true': True,
|
||||||
':in_progress': 'IN_PROGRESS',
|
':now': now_,
|
||||||
':expired': 'EXPIRED',
|
|
||||||
':updated_at': now_,
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
transact.put(
|
transact.put(
|
||||||
item={
|
item={
|
||||||
'id': old_image['id'],
|
'id': old_image['id'],
|
||||||
'sk': 'EXPIRED',
|
'sk': 'SCHEDULE#SET_ACCESS_EXPIRED#EXECUTED',
|
||||||
'expired_at': now_,
|
|
||||||
},
|
|
||||||
cond_expr='attribute_not_exists(sk)',
|
|
||||||
)
|
|
||||||
transact.put(
|
|
||||||
item={
|
|
||||||
'id': old_image['id'],
|
|
||||||
'sk': 'SCHEDULE#SET_AS_EXPIRED#EXECUTED',
|
|
||||||
'created_at': now_,
|
'created_at': now_,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@@ -64,7 +49,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
|||||||
dyn.put_item(
|
dyn.put_item(
|
||||||
item={
|
item={
|
||||||
'id': old_image['id'],
|
'id': old_image['id'],
|
||||||
'sk': 'SCHEDULE#SET_AS_EXPIRED#FAILED',
|
'sk': 'SCHEDULE#SET_ACCESS_EXPIRED#FAILED',
|
||||||
'reason': str(exc),
|
'reason': str(exc),
|
||||||
'created_at': now_,
|
'created_at': now_,
|
||||||
},
|
},
|
||||||
@@ -5,7 +5,7 @@ from aws_lambda_powertools.utilities.data_classes import (
|
|||||||
)
|
)
|
||||||
from aws_lambda_powertools.utilities.typing import LambdaContext
|
from aws_lambda_powertools.utilities.typing import LambdaContext
|
||||||
from layercake.dateutils import now
|
from layercake.dateutils import now
|
||||||
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
|
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair, SortKey
|
||||||
|
|
||||||
from boto3clients import dynamodb_client
|
from boto3clients import dynamodb_client
|
||||||
from config import (
|
from config import (
|
||||||
@@ -19,9 +19,16 @@ dyn = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
|
|||||||
@event_source(data_class=EventBridgeEvent)
|
@event_source(data_class=EventBridgeEvent)
|
||||||
@logger.inject_lambda_context
|
@logger.inject_lambda_context
|
||||||
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||||
"""After the certificate expires, the enrollment will be marked as archived."""
|
|
||||||
old_image = event.detail['old_image']
|
old_image = event.detail['old_image']
|
||||||
now_ = now()
|
now_ = now()
|
||||||
|
issued_cert = dyn.collection.get_item(
|
||||||
|
KeyPair(
|
||||||
|
pk=old_image['id'],
|
||||||
|
sk=SortKey('0', path_spec='issued_cert'),
|
||||||
|
),
|
||||||
|
raise_on_error=False,
|
||||||
|
default={},
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with dyn.transact_writer() as transact:
|
with dyn.transact_writer() as transact:
|
||||||
@@ -30,29 +37,20 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
|||||||
pk=old_image['id'],
|
pk=old_image['id'],
|
||||||
sk='0',
|
sk='0',
|
||||||
),
|
),
|
||||||
update_expr='SET #status = :archived, updated_at = :updated_at',
|
update_expr='SET issued_cert = :issued_cert, \
|
||||||
|
updated_at = :now',
|
||||||
cond_expr='#status = :completed',
|
cond_expr='#status = :completed',
|
||||||
expr_attr_names={
|
expr_attr_names={'#status': 'status'},
|
||||||
'#status': 'status',
|
|
||||||
},
|
|
||||||
expr_attr_values={
|
expr_attr_values={
|
||||||
|
':issued_cert': issued_cert | {'expired': True},
|
||||||
':completed': 'COMPLETED',
|
':completed': 'COMPLETED',
|
||||||
':archived': 'ARCHIVED',
|
':now': now_,
|
||||||
':updated_at': now_,
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
transact.put(
|
transact.put(
|
||||||
item={
|
item={
|
||||||
'id': old_image['id'],
|
'id': old_image['id'],
|
||||||
'sk': 'ARCHIVED',
|
'sk': 'SCHEDULE#SET_CERT_EXPIRED#EXECUTED',
|
||||||
'archived_at': now_,
|
|
||||||
},
|
|
||||||
cond_expr='attribute_not_exists(sk)',
|
|
||||||
)
|
|
||||||
transact.put(
|
|
||||||
item={
|
|
||||||
'id': old_image['id'],
|
|
||||||
'sk': 'SCHEDULE#SET_AS_ARCHIVED#EXECUTED',
|
|
||||||
'created_at': now_,
|
'created_at': now_,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@@ -62,7 +60,7 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
|||||||
dyn.put_item(
|
dyn.put_item(
|
||||||
item={
|
item={
|
||||||
'id': old_image['id'],
|
'id': old_image['id'],
|
||||||
'sk': 'SCHEDULE#SET_AS_ARCHIVED#FAILED',
|
'sk': 'SCHEDULE#SET_CERT_EXPIRED#FAILED',
|
||||||
'reason': str(exc),
|
'reason': str(exc),
|
||||||
'created_at': now_,
|
'created_at': now_,
|
||||||
},
|
},
|
||||||
@@ -37,10 +37,10 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
|||||||
transact.update(
|
transact.update(
|
||||||
key=KeyPair(new_image['id'], '0'),
|
key=KeyPair(new_image['id'], '0'),
|
||||||
update_expr='SET subscription_covered = :subscription_covered, \
|
update_expr='SET subscription_covered = :subscription_covered, \
|
||||||
updated_at = :updated_at',
|
updated_at = :now',
|
||||||
expr_attr_values={
|
expr_attr_values={
|
||||||
':subscription_covered': True,
|
':subscription_covered': True,
|
||||||
':updated_at': now_,
|
':now': now_,
|
||||||
},
|
},
|
||||||
cond_expr='attribute_exists(sk)',
|
cond_expr='attribute_exists(sk)',
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -307,12 +307,10 @@ Resources:
|
|||||||
keys:
|
keys:
|
||||||
sk: [SCHEDULE#REMINDER_CERT_EXPIRATION_BEFORE_30_DAYS]
|
sk: [SCHEDULE#REMINDER_CERT_EXPIRATION_BEFORE_30_DAYS]
|
||||||
|
|
||||||
# If there is no certificate and the access period has ended,
|
EventSetAccessExpiredFunction:
|
||||||
# the enrollment will be marked as expired
|
|
||||||
EventSetAsExpiredFunction:
|
|
||||||
Type: AWS::Serverless::Function
|
Type: AWS::Serverless::Function
|
||||||
Properties:
|
Properties:
|
||||||
Handler: events.set_as_expired.lambda_handler
|
Handler: events.set_access_expired.lambda_handler
|
||||||
LoggingConfig:
|
LoggingConfig:
|
||||||
LogGroup: !Ref EventLog
|
LogGroup: !Ref EventLog
|
||||||
Policies:
|
Policies:
|
||||||
@@ -327,13 +325,12 @@ Resources:
|
|||||||
detail-type: [EXPIRE]
|
detail-type: [EXPIRE]
|
||||||
detail:
|
detail:
|
||||||
keys:
|
keys:
|
||||||
sk: [SCHEDULE#SET_AS_EXPIRED]
|
sk: [SCHEDULE#SET_ACCESS_EXPIRED, SCHEDULE#SET_AS_EXPIRED]
|
||||||
|
|
||||||
# After the certificate expires, the enrollment will be marked as archived
|
EventSetCertExpiredFunction:
|
||||||
EventSetAsArchivedFunction:
|
|
||||||
Type: AWS::Serverless::Function
|
Type: AWS::Serverless::Function
|
||||||
Properties:
|
Properties:
|
||||||
Handler: events.set_as_archived.lambda_handler
|
Handler: events.set_cert_expired.lambda_handler
|
||||||
LoggingConfig:
|
LoggingConfig:
|
||||||
LogGroup: !Ref EventLog
|
LogGroup: !Ref EventLog
|
||||||
Policies:
|
Policies:
|
||||||
@@ -348,7 +345,7 @@ Resources:
|
|||||||
detail-type: [EXPIRE]
|
detail-type: [EXPIRE]
|
||||||
detail:
|
detail:
|
||||||
keys:
|
keys:
|
||||||
sk: [SCHEDULE#SET_AS_ARCHIVED]
|
sk: [SCHEDULE#SET_CERT_EXPIRED, SCHEDULE#SET_AS_ARCHIVED]
|
||||||
|
|
||||||
EventScheduleRemindersFunction:
|
EventScheduleRemindersFunction:
|
||||||
Type: AWS::Serverless::Function
|
Type: AWS::Serverless::Function
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import app.events.set_as_archived as app
|
import app.events.set_access_expired as app
|
||||||
from aws_lambda_powertools.utilities.typing import LambdaContext
|
from aws_lambda_powertools.utilities.typing import LambdaContext
|
||||||
from layercake.dynamodb import (
|
from layercake.dynamodb import (
|
||||||
DynamoDBPersistenceLayer,
|
DynamoDBPersistenceLayer,
|
||||||
@@ -7,7 +7,7 @@ from layercake.dynamodb import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_set_as_archived(
|
def test_set_access_expired(
|
||||||
seeds,
|
seeds,
|
||||||
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
|
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
|
||||||
lambda_context: LambdaContext,
|
lambda_context: LambdaContext,
|
||||||
@@ -15,7 +15,7 @@ def test_set_as_archived(
|
|||||||
event = {
|
event = {
|
||||||
'detail': {
|
'detail': {
|
||||||
'old_image': {
|
'old_image': {
|
||||||
'id': '845fe390-e3c3-4514-97f8-c42de0566cf0',
|
'id': '6437a282-6fe8-4e4d-9eb0-da1007238007',
|
||||||
'sk': '0',
|
'sk': '0',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -23,9 +23,10 @@ def test_set_as_archived(
|
|||||||
assert app.lambda_handler(event, lambda_context) # type: ignore
|
assert app.lambda_handler(event, lambda_context) # type: ignore
|
||||||
|
|
||||||
r = dynamodb_persistence_layer.collection.get_items(
|
r = dynamodb_persistence_layer.collection.get_items(
|
||||||
TransactKey('845fe390-e3c3-4514-97f8-c42de0566cf0')
|
TransactKey('6437a282-6fe8-4e4d-9eb0-da1007238007')
|
||||||
+ SortKey('0')
|
+ SortKey('0')
|
||||||
+ SortKey('SCHEDULE#SET_AS_ARCHIVED#EXECUTED', rename_key='executed')
|
+ SortKey('SCHEDULE#SET_ACCESS_EXPIRED#EXECUTED', rename_key='executed')
|
||||||
)
|
)
|
||||||
assert r['status'] == 'ARCHIVED'
|
assert r['status'] == 'IN_PROGRESS'
|
||||||
assert 'executed' in r
|
assert 'executed' in r
|
||||||
|
assert 'access_expired' in r
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import app.events.set_as_expired as app
|
import app.events.set_cert_expired as app
|
||||||
from aws_lambda_powertools.utilities.typing import LambdaContext
|
from aws_lambda_powertools.utilities.typing import LambdaContext
|
||||||
from layercake.dynamodb import (
|
from layercake.dynamodb import (
|
||||||
DynamoDBPersistenceLayer,
|
DynamoDBPersistenceLayer,
|
||||||
@@ -7,31 +7,7 @@ from layercake.dynamodb import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_set_as_expired(
|
def test_set_cert_expired(
|
||||||
seeds,
|
|
||||||
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
|
|
||||||
lambda_context: LambdaContext,
|
|
||||||
):
|
|
||||||
event = {
|
|
||||||
'detail': {
|
|
||||||
'old_image': {
|
|
||||||
'id': '6437a282-6fe8-4e4d-9eb0-da1007238007',
|
|
||||||
'sk': '0',
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assert app.lambda_handler(event, lambda_context) # type: ignore
|
|
||||||
|
|
||||||
r = dynamodb_persistence_layer.collection.get_items(
|
|
||||||
TransactKey('6437a282-6fe8-4e4d-9eb0-da1007238007')
|
|
||||||
+ SortKey('0')
|
|
||||||
+ SortKey('SCHEDULE#SET_AS_EXPIRED#EXECUTED', rename_key='executed')
|
|
||||||
)
|
|
||||||
assert r['status'] == 'EXPIRED'
|
|
||||||
assert 'executed' in r
|
|
||||||
|
|
||||||
|
|
||||||
def test_set_as_expired_failed(
|
|
||||||
seeds,
|
seeds,
|
||||||
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
|
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
|
||||||
lambda_context: LambdaContext,
|
lambda_context: LambdaContext,
|
||||||
@@ -44,12 +20,34 @@ def test_set_as_expired_failed(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert not app.lambda_handler(event, lambda_context) # type: ignore
|
assert app.lambda_handler(event, lambda_context) # type: ignore
|
||||||
|
|
||||||
r = dynamodb_persistence_layer.collection.get_items(
|
r = dynamodb_persistence_layer.collection.get_items(
|
||||||
TransactKey('845fe390-e3c3-4514-97f8-c42de0566cf0')
|
TransactKey('845fe390-e3c3-4514-97f8-c42de0566cf0')
|
||||||
+ SortKey('0')
|
+ SortKey('0')
|
||||||
+ SortKey('SCHEDULE#SET_AS_EXPIRED#FAILED', rename_key='failed')
|
+ SortKey('SCHEDULE#SET_CERT_EXPIRED#EXECUTED', rename_key='executed')
|
||||||
)
|
)
|
||||||
assert r['status'] == 'COMPLETED'
|
assert r['status'] == 'COMPLETED'
|
||||||
assert 'failed' in r
|
assert 'executed' in r
|
||||||
|
assert 'issued_cert' in r
|
||||||
|
|
||||||
|
|
||||||
|
def test_existing_issued_cert(
|
||||||
|
seeds,
|
||||||
|
dynamodb_persistence_layer: DynamoDBPersistenceLayer,
|
||||||
|
lambda_context: LambdaContext,
|
||||||
|
):
|
||||||
|
event = {
|
||||||
|
'detail': {
|
||||||
|
'old_image': {
|
||||||
|
'id': '1ee108ae-67d4-4545-bf6d-4e641cdaa4e0',
|
||||||
|
'sk': '0',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert app.lambda_handler(event, lambda_context) # type: ignore
|
||||||
|
|
||||||
|
r = dynamodb_persistence_layer.collection.get_items(
|
||||||
|
TransactKey('1ee108ae-67d4-4545-bf6d-4e641cdaa4e0') + SortKey('0')
|
||||||
|
)
|
||||||
|
assert 's3_uri' in r['issued_cert']
|
||||||
@@ -24,7 +24,7 @@
|
|||||||
{"id": "845fe390-e3c3-4514-97f8-c42de0566cf0", "sk": "0", "status": "COMPLETED", "progress": 100}
|
{"id": "845fe390-e3c3-4514-97f8-c42de0566cf0", "sk": "0", "status": "COMPLETED", "progress": 100}
|
||||||
{"id": "14682b79-3df2-4351-9229-8b558af046a0", "sk": "METADATA#COURSE", "access_period": 360}
|
{"id": "14682b79-3df2-4351-9229-8b558af046a0", "sk": "METADATA#COURSE", "access_period": 360}
|
||||||
|
|
||||||
{"id": "1ee108ae-67d4-4545-bf6d-4e641cdaa4e0", "sk": "0", "score": 100, "course": {"name": "CIPA Grau de Risco 1"}, "user": {"name": "Kurt Cobain"}}
|
{"id": "1ee108ae-67d4-4545-bf6d-4e641cdaa4e0", "sk": "0", "status": "COMPLETED", "score": 100, "course": {"name": "CIPA Grau de Risco 1"}, "user": {"name": "Kurt Cobain"}, "issued_cert": {"s3_uri": "s3://saladeaula.digital/issuedcerts/1ee108ae-67d4-4545-bf6d-4e641cdaa4e0.pdf"}}
|
||||||
{"id": "1ee108ae-67d4-4545-bf6d-4e641cdaa4e0", "sk": "METADATA#COURSE", "cert": {"s3_uri": "s3://saladeaula.digital/certs/samples/cipa-grau-de-risco-1.html", "exp_interval": 700}}
|
{"id": "1ee108ae-67d4-4545-bf6d-4e641cdaa4e0", "sk": "METADATA#COURSE", "cert": {"s3_uri": "s3://saladeaula.digital/certs/samples/cipa-grau-de-risco-1.html", "exp_interval": 700}}
|
||||||
{"id": "1ee108ae-67d4-4545-bf6d-4e641cdaa4e0", "sk": "STARTED", "started_at": "2025-08-24T01:44:42.703012-03:06"}
|
{"id": "1ee108ae-67d4-4545-bf6d-4e641cdaa4e0", "sk": "STARTED", "started_at": "2025-08-24T01:44:42.703012-03:06"}
|
||||||
{"id": "1ee108ae-67d4-4545-bf6d-4e641cdaa4e0", "sk": "COMPLETED", "completed_at": "2025-08-31T21:59:10.842467-03:00"}
|
{"id": "1ee108ae-67d4-4545-bf6d-4e641cdaa4e0", "sk": "COMPLETED", "completed_at": "2025-08-31T21:59:10.842467-03:00"}
|
||||||
|
|||||||
@@ -25,7 +25,8 @@ def update_progress(
|
|||||||
# Update progress only if the enrollment status is `IN_PROGRESS`
|
# Update progress only if the enrollment status is `IN_PROGRESS`
|
||||||
transact.update(
|
transact.update(
|
||||||
key=KeyPair(id, '0'),
|
key=KeyPair(id, '0'),
|
||||||
update_expr='SET progress = :progress, updated_at = :updated_at',
|
update_expr='SET progress = :progress, \
|
||||||
|
updated_at = :now',
|
||||||
cond_expr='#status = :in_progress',
|
cond_expr='#status = :in_progress',
|
||||||
expr_attr_names={
|
expr_attr_names={
|
||||||
'#status': 'status',
|
'#status': 'status',
|
||||||
@@ -33,7 +34,7 @@ def update_progress(
|
|||||||
expr_attr_values={
|
expr_attr_values={
|
||||||
':in_progress': 'IN_PROGRESS',
|
':in_progress': 'IN_PROGRESS',
|
||||||
':progress': progress,
|
':progress': progress,
|
||||||
':updated_at': now_,
|
':now': now_,
|
||||||
},
|
},
|
||||||
exc_cls=EnrollmentConflictError,
|
exc_cls=EnrollmentConflictError,
|
||||||
)
|
)
|
||||||
@@ -49,11 +50,12 @@ def update_progress(
|
|||||||
except EnrollmentConflictError:
|
except EnrollmentConflictError:
|
||||||
with dynamodb_persistence_layer.transact_writer() as transact:
|
with dynamodb_persistence_layer.transact_writer() as transact:
|
||||||
# If the enrollment status is `PENDING`, set it to `IN_PROGRESS`
|
# If the enrollment status is `PENDING`, set it to `IN_PROGRESS`
|
||||||
# and update progress and updated_at date
|
# and update `progress` and `updated_at`
|
||||||
transact.update(
|
transact.update(
|
||||||
key=KeyPair(id, '0'),
|
key=KeyPair(id, '0'),
|
||||||
update_expr='SET progress = :progress, #status = :in_progress, \
|
update_expr='SET progress = :progress, \
|
||||||
updated_at = :updated_at',
|
#status = :in_progress, \
|
||||||
|
updated_at = :now',
|
||||||
cond_expr='#status = :pending',
|
cond_expr='#status = :pending',
|
||||||
expr_attr_names={
|
expr_attr_names={
|
||||||
'#status': 'status',
|
'#status': 'status',
|
||||||
@@ -62,7 +64,7 @@ def update_progress(
|
|||||||
':in_progress': 'IN_PROGRESS',
|
':in_progress': 'IN_PROGRESS',
|
||||||
':pending': 'PENDING',
|
':pending': 'PENDING',
|
||||||
':progress': progress,
|
':progress': progress,
|
||||||
':updated_at': now_,
|
':now': now_,
|
||||||
},
|
},
|
||||||
exc_cls=EnrollmentConflictError,
|
exc_cls=EnrollmentConflictError,
|
||||||
)
|
)
|
||||||
@@ -189,15 +191,15 @@ def _set_status_as_completed(
|
|||||||
update_expr='SET #status = :completed, \
|
update_expr='SET #status = :completed, \
|
||||||
progress = :progress, \
|
progress = :progress, \
|
||||||
score = :score, \
|
score = :score, \
|
||||||
updated_at = :updated_at',
|
updated_at = :now',
|
||||||
cond_expr='#status = :in_progress',
|
cond_expr='#status = :in_progress',
|
||||||
expr_attr_names={'#status': 'status'},
|
expr_attr_names={'#status': 'status'},
|
||||||
expr_attr_values={
|
expr_attr_values={
|
||||||
':completed': 'COMPLETED',
|
':completed': 'COMPLETED',
|
||||||
':in_progress': 'IN_PROGRESS',
|
':in_progress': 'IN_PROGRESS',
|
||||||
':score': score,
|
':score': score,
|
||||||
':updated_at': now_,
|
|
||||||
':progress': progress,
|
':progress': progress,
|
||||||
|
':now': now_,
|
||||||
},
|
},
|
||||||
exc_cls=EnrollmentConflictError,
|
exc_cls=EnrollmentConflictError,
|
||||||
)
|
)
|
||||||
@@ -214,7 +216,7 @@ def _set_status_as_completed(
|
|||||||
transact.put(
|
transact.put(
|
||||||
item={
|
item={
|
||||||
'id': id,
|
'id': id,
|
||||||
'sk': 'SCHEDULE#REMINDER_CERT_EXPIRATION_BEFORE_30_DAYS',
|
'sk': 'SCHEDULE#SET_CERT_EXPIRED',
|
||||||
'ttl': cert_exp_ttl,
|
'ttl': cert_exp_ttl,
|
||||||
'created_at': now_,
|
'created_at': now_,
|
||||||
}
|
}
|
||||||
@@ -222,7 +224,7 @@ def _set_status_as_completed(
|
|||||||
transact.put(
|
transact.put(
|
||||||
item={
|
item={
|
||||||
'id': id,
|
'id': id,
|
||||||
'sk': 'SCHEDULE#SET_CERT_EXPIRED',
|
'sk': 'SCHEDULE#REMINDER_CERT_EXPIRATION_BEFORE_30_DAYS',
|
||||||
'ttl': cert_exp_reminder_ttl,
|
'ttl': cert_exp_reminder_ttl,
|
||||||
'created_at': now_,
|
'created_at': now_,
|
||||||
}
|
}
|
||||||
@@ -288,8 +290,8 @@ def _set_status_as_failed(
|
|||||||
update_expr='SET #status = :failed, \
|
update_expr='SET #status = :failed, \
|
||||||
progress = :progress, \
|
progress = :progress, \
|
||||||
score = :score, \
|
score = :score, \
|
||||||
access_expired = :access_expired, \
|
access_expired = :true, \
|
||||||
updated_at = :updated_at',
|
updated_at = :now',
|
||||||
cond_expr='#status = :in_progress',
|
cond_expr='#status = :in_progress',
|
||||||
expr_attr_names={'#status': 'status'},
|
expr_attr_names={'#status': 'status'},
|
||||||
expr_attr_values={
|
expr_attr_values={
|
||||||
@@ -297,8 +299,8 @@ def _set_status_as_failed(
|
|||||||
':in_progress': 'IN_PROGRESS',
|
':in_progress': 'IN_PROGRESS',
|
||||||
':score': score,
|
':score': score,
|
||||||
':progress': progress,
|
':progress': progress,
|
||||||
':access_expired': True,
|
':true': True,
|
||||||
':updated_at': now_,
|
':now': now_,
|
||||||
},
|
},
|
||||||
exc_cls=EnrollmentConflictError,
|
exc_cls=EnrollmentConflictError,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -18,10 +18,12 @@ dyn = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
|
|||||||
@logger.inject_lambda_context
|
@logger.inject_lambda_context
|
||||||
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
||||||
new_image = event.detail['new_image']
|
new_image = event.detail['new_image']
|
||||||
data = dyn.get_item(KeyPair(new_image['id'], 'konviva'))
|
r = dyn.get_item(KeyPair(new_image['id'], 'konviva'))
|
||||||
|
# Post-migration: uncomment the following line
|
||||||
|
# r = dyn.get_item(KeyPair(new_image['id'], 'KONVIVA'))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
result = konviva.cancel_enrollment(data['enrollment_id'])
|
result = konviva.cancel_enrollment(r['enrollment_id'])
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.exception(exc)
|
logger.exception(exc)
|
||||||
return False
|
return False
|
||||||
|
|||||||
@@ -47,10 +47,10 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
|||||||
transact.update(
|
transact.update(
|
||||||
key=KeyPair(new_image['id'], '0'),
|
key=KeyPair(new_image['id'], '0'),
|
||||||
update_expr='SET metadata__konviva_user_id = :user_id, \
|
update_expr='SET metadata__konviva_user_id = :user_id, \
|
||||||
updated_at = :updated_at',
|
updated_at = :now',
|
||||||
expr_attr_values={
|
expr_attr_values={
|
||||||
':user_id': user_id,
|
':user_id': user_id,
|
||||||
':updated_at': now_,
|
':now': now_,
|
||||||
},
|
},
|
||||||
cond_expr='attribute_exists(sk)',
|
cond_expr='attribute_exists(sk)',
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -35,6 +35,8 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
|||||||
with dyn.transact_writer() as transact:
|
with dyn.transact_writer() as transact:
|
||||||
transact.update(
|
transact.update(
|
||||||
key=KeyPair(new_image['id'], 'konviva'),
|
key=KeyPair(new_image['id'], 'konviva'),
|
||||||
|
# Post-migration: uncomment the following line
|
||||||
|
# key=KeyPair(new_image['id'], 'KONVIVA'),
|
||||||
update_expr='SET enrollment_id = :enrollment_id',
|
update_expr='SET enrollment_id = :enrollment_id',
|
||||||
cond_expr='attribute_exists(sk)',
|
cond_expr='attribute_exists(sk)',
|
||||||
expr_attr_values={
|
expr_attr_values={
|
||||||
@@ -45,6 +47,8 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
|
|||||||
transact.put(
|
transact.put(
|
||||||
item={
|
item={
|
||||||
'id': 'konviva',
|
'id': 'konviva',
|
||||||
|
# Post-migration: uncomment the following line
|
||||||
|
# 'id': 'KONVIVA',
|
||||||
'sk': str(enrollment_id),
|
'sk': str(enrollment_id),
|
||||||
'enrollment_id': new_image['id'],
|
'enrollment_id': new_image['id'],
|
||||||
'created_at': now_,
|
'created_at': now_,
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import pprint
|
|
||||||
from http import HTTPMethod, HTTPStatus
|
from http import HTTPMethod, HTTPStatus
|
||||||
|
|
||||||
from layercake.dateutils import now
|
from layercake.dateutils import now
|
||||||
@@ -159,65 +158,3 @@ def test_set_as_failed(
|
|||||||
PartitionKey('6c7e3d9b-f5d1-4da4-9e55-0825bb6ff2b8')
|
PartitionKey('6c7e3d9b-f5d1-4da4-9e55-0825bb6ff2b8')
|
||||||
)
|
)
|
||||||
assert any(item.get('sk') == 'FAILED' for item in r['items'])
|
assert any(item.get('sk') == 'FAILED' for item in r['items'])
|
||||||
|
|
||||||
|
|
||||||
# def test_set_as_archived(
|
|
||||||
# app,
|
|
||||||
# seeds,
|
|
||||||
# dynamodb_persistence_layer: DynamoDBPersistenceLayer,
|
|
||||||
# http_api_proxy: HttpApiProxy,
|
|
||||||
# lambda_context: LambdaContext,
|
|
||||||
# ):
|
|
||||||
# # This data was added from seeds
|
|
||||||
# r = app.lambda_handler(
|
|
||||||
# http_api_proxy(
|
|
||||||
# raw_path='/',
|
|
||||||
# method=HTTPMethod.POST,
|
|
||||||
# body={
|
|
||||||
# 'ID_MATRICULA': '899',
|
|
||||||
# 'APROVEITAMENTO': '70',
|
|
||||||
# 'ANDAMENTO': '100',
|
|
||||||
# 'status': 'COMPLETED',
|
|
||||||
# },
|
|
||||||
# ),
|
|
||||||
# lambda_context,
|
|
||||||
# )
|
|
||||||
# assert r['statusCode'] == HTTPStatus.NO_CONTENT
|
|
||||||
|
|
||||||
# # Check `seeds.jsonl` for sample data related to this query
|
|
||||||
# r = dynamodb_persistence_layer.collection.query(
|
|
||||||
# PartitionKey('cc2c3bce-c34a-4e82-aa6c-1a19e70ec5ae')
|
|
||||||
# )
|
|
||||||
# assert any(item.get('sk') == 'ARCHIVED' for item in r['items'])
|
|
||||||
# assert len(r['items']) == 4
|
|
||||||
|
|
||||||
|
|
||||||
# def test_set_as_expired(
|
|
||||||
# app,
|
|
||||||
# seeds,
|
|
||||||
# dynamodb_persistence_layer: DynamoDBPersistenceLayer,
|
|
||||||
# http_api_proxy: HttpApiProxy,
|
|
||||||
# lambda_context: LambdaContext,
|
|
||||||
# ):
|
|
||||||
# # This data was added from seeds
|
|
||||||
# r = app.lambda_handler(
|
|
||||||
# http_api_proxy(
|
|
||||||
# raw_path='/',
|
|
||||||
# method=HTTPMethod.POST,
|
|
||||||
# body={
|
|
||||||
# 'ID_MATRICULA': '221',
|
|
||||||
# 'APROVEITAMENTO': '69',
|
|
||||||
# 'ANDAMENTO': '100',
|
|
||||||
# 'status': 'COMPLETED',
|
|
||||||
# },
|
|
||||||
# ),
|
|
||||||
# lambda_context,
|
|
||||||
# )
|
|
||||||
# assert r['statusCode'] == HTTPStatus.NO_CONTENT
|
|
||||||
|
|
||||||
# # Check `seeds.jsonl` for sample data related to this query
|
|
||||||
# r = dynamodb_persistence_layer.collection.query(
|
|
||||||
# PartitionKey('5db53b35-0bae-4907-afda-a213cb5bf651')
|
|
||||||
# )
|
|
||||||
# assert any(item.get('sk') == 'EXPIRED' for item in r['items'])
|
|
||||||
# assert len(r['items']) == 3
|
|
||||||
|
|||||||
Reference in New Issue
Block a user