update user on konviva

This commit is contained in:
2025-07-18 13:05:03 -03:00
parent 61e7ac0f4b
commit 64946471b9
6 changed files with 94 additions and 6 deletions

View File

@@ -46,7 +46,9 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
'sk': 'metadata#course', 'sk': 'metadata#course',
'created_at': now_, 'created_at': now_,
'access_period': int(course['access_period']), 'access_period': int(course['access_period']),
'cert': course['cert'], 'cert': {
'exp_interval': int(course['cert']['exp_interval']),
},
} }
) )

View File

@@ -35,10 +35,10 @@ def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
except konviva.EmailAlreadyExistsError as exc: except konviva.EmailAlreadyExistsError as exc:
logger.exception(exc, email=new_image['email']) logger.exception(exc, email=new_image['email'])
r = konviva.get_users_by_email(new_image['email']) result = konviva.get_users_by_email(new_image['email'])
user_id = glom(r, '0.IDUsuario') user_id = glom(result, '0.IDUsuario')
if not r: if not result:
raise UserNotFoundError() raise UserNotFoundError()
except Exception: except Exception:
raise raise

View File

@@ -0,0 +1,44 @@
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
import konviva
from boto3clients import dynamodb_client
from config import ENROLLMENT_TABLE
logger = Logger(__name__)
enrollment_layer = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
@event_source(data_class=EventBridgeEvent)
@logger.inject_lambda_context
def lambda_handler(event: EventBridgeEvent, context: LambdaContext) -> bool:
new_image = event.detail['new_image']
changes = event.detail['changes']
user_id = new_image['metadata__konviva_user_id']
payload = {}
for attr in changes:
match attr:
case 'name':
payload.update(NomeUsuario=new_image['name'])
case 'email':
payload.update(
Email=new_image['email'],
Login=new_image['email'],
)
case 'cpf':
payload.update(CPF=new_image['cpf'])
try:
result = konviva.update_user(id=user_id, **payload)
except Exception as exc:
logger.exception(exc)
return False
else:
logger.info('User updated', result=result, payload=payload)
return True

View File

@@ -104,3 +104,26 @@ Resources:
status: [CANCELED] status: [CANCELED]
old_image: old_image:
status: [PENDING] status: [PENDING]
EventUpdateUserFunction:
Type: AWS::Serverless::Function
Properties:
Handler: events.update_user.lambda_handler
LoggingConfig:
LogGroup: !Ref EventLog
Policies:
- DynamoDBReadPolicy:
TableName: !Ref UserTable
Events:
DynamoDBEvent:
Type: EventBridgeRule
Properties:
Pattern:
resources: [!Ref UserTable]
detail-type: [MODIFY]
detail:
new_image:
sk: ["0"]
metadata__konviva_user_id:
- exists: true
changes: [name, email, cpf]

View File

@@ -0,0 +1,17 @@
from aws_lambda_powertools.utilities.typing import LambdaContext
import events.update_user as app
def test_update_user(lambda_context: LambdaContext):
event = {
'detail': {
'new_image': {
'name': 'Sérgio Rafael Siqueira',
'metadata__konviva_user_id': 26943,
},
'changes': ['name'],
},
}
assert app.lambda_handler(event, lambda_context)

View File

@@ -36,7 +36,7 @@ def record_handler(record: DynamoDBRecord):
new_image: dict = record.dynamodb.new_image # type: ignore new_image: dict = record.dynamodb.new_image # type: ignore
old_image: dict = record.dynamodb.old_image # type: ignore old_image: dict = record.dynamodb.old_image # type: ignore
record_ttl: int | None = old_image.get('ttl') record_ttl: int | None = old_image.get('ttl')
modified = diff(new_image, old_image) changes = diff(new_image, old_image)
now_ = now() now_ = now()
# Should be EXPIRE if event is REMOVE and TTL has elapsed # Should be EXPIRE if event is REMOVE and TTL has elapsed
@@ -51,7 +51,9 @@ def record_handler(record: DynamoDBRecord):
'keys': record.dynamodb.keys, # type: ignore 'keys': record.dynamodb.keys, # type: ignore
'new_image': new_image, 'new_image': new_image,
'old_image': old_image, 'old_image': old_image,
'modified': modified, 'changes': changes,
# Post-migration: remove the following line
'modified': changes,
} }
result = client.put_events( result = client.put_events(