update events

This commit is contained in:
2025-09-18 13:45:42 -03:00
parent db63dfa64d
commit a83d4b56aa
18 changed files with 109 additions and 181 deletions

View File

@@ -25,7 +25,8 @@ def update_progress(
# Update progress only if the enrollment status is `IN_PROGRESS`
transact.update(
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',
expr_attr_names={
'#status': 'status',
@@ -33,7 +34,7 @@ def update_progress(
expr_attr_values={
':in_progress': 'IN_PROGRESS',
':progress': progress,
':updated_at': now_,
':now': now_,
},
exc_cls=EnrollmentConflictError,
)
@@ -49,11 +50,12 @@ def update_progress(
except EnrollmentConflictError:
with dynamodb_persistence_layer.transact_writer() as transact:
# 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(
key=KeyPair(id, '0'),
update_expr='SET progress = :progress, #status = :in_progress, \
updated_at = :updated_at',
update_expr='SET progress = :progress, \
#status = :in_progress, \
updated_at = :now',
cond_expr='#status = :pending',
expr_attr_names={
'#status': 'status',
@@ -62,7 +64,7 @@ def update_progress(
':in_progress': 'IN_PROGRESS',
':pending': 'PENDING',
':progress': progress,
':updated_at': now_,
':now': now_,
},
exc_cls=EnrollmentConflictError,
)
@@ -189,15 +191,15 @@ def _set_status_as_completed(
update_expr='SET #status = :completed, \
progress = :progress, \
score = :score, \
updated_at = :updated_at',
updated_at = :now',
cond_expr='#status = :in_progress',
expr_attr_names={'#status': 'status'},
expr_attr_values={
':completed': 'COMPLETED',
':in_progress': 'IN_PROGRESS',
':score': score,
':updated_at': now_,
':progress': progress,
':now': now_,
},
exc_cls=EnrollmentConflictError,
)
@@ -214,7 +216,7 @@ def _set_status_as_completed(
transact.put(
item={
'id': id,
'sk': 'SCHEDULE#REMINDER_CERT_EXPIRATION_BEFORE_30_DAYS',
'sk': 'SCHEDULE#SET_CERT_EXPIRED',
'ttl': cert_exp_ttl,
'created_at': now_,
}
@@ -222,7 +224,7 @@ def _set_status_as_completed(
transact.put(
item={
'id': id,
'sk': 'SCHEDULE#SET_CERT_EXPIRED',
'sk': 'SCHEDULE#REMINDER_CERT_EXPIRATION_BEFORE_30_DAYS',
'ttl': cert_exp_reminder_ttl,
'created_at': now_,
}
@@ -288,8 +290,8 @@ def _set_status_as_failed(
update_expr='SET #status = :failed, \
progress = :progress, \
score = :score, \
access_expired = :access_expired, \
updated_at = :updated_at',
access_expired = :true, \
updated_at = :now',
cond_expr='#status = :in_progress',
expr_attr_names={'#status': 'status'},
expr_attr_values={
@@ -297,8 +299,8 @@ def _set_status_as_failed(
':in_progress': 'IN_PROGRESS',
':score': score,
':progress': progress,
':access_expired': True,
':updated_at': now_,
':true': True,
':now': now_,
},
exc_cls=EnrollmentConflictError,
)

View File

@@ -18,10 +18,12 @@ dyn = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
@logger.inject_lambda_context
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
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:
result = konviva.cancel_enrollment(data['enrollment_id'])
result = konviva.cancel_enrollment(r['enrollment_id'])
except Exception as exc:
logger.exception(exc)
return False

View File

@@ -47,10 +47,10 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
transact.update(
key=KeyPair(new_image['id'], '0'),
update_expr='SET metadata__konviva_user_id = :user_id, \
updated_at = :updated_at',
updated_at = :now',
expr_attr_values={
':user_id': user_id,
':updated_at': now_,
':now': now_,
},
cond_expr='attribute_exists(sk)',
)

View File

@@ -35,6 +35,8 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
with dyn.transact_writer() as transact:
transact.update(
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',
cond_expr='attribute_exists(sk)',
expr_attr_values={
@@ -45,6 +47,8 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
transact.put(
item={
'id': 'konviva',
# Post-migration: uncomment the following line
# 'id': 'KONVIVA',
'sk': str(enrollment_id),
'enrollment_id': new_image['id'],
'created_at': now_,

View File

@@ -1,4 +1,3 @@
import pprint
from http import HTTPMethod, HTTPStatus
from layercake.dateutils import now
@@ -159,65 +158,3 @@ def test_set_as_failed(
PartitionKey('6c7e3d9b-f5d1-4da4-9e55-0825bb6ff2b8')
)
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