51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
from aws_lambda_powertools import Logger
|
|
from aws_lambda_powertools.event_handler.api_gateway import Router
|
|
from layercake.dateutils import now
|
|
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair
|
|
|
|
from boto3clients import dynamodb_client
|
|
from config import ENROLLMENT_TABLE
|
|
from exceptions import CancelPolicyConflictError
|
|
from middlewares.authentication_middleware import User as Authenticated
|
|
|
|
logger = Logger(__name__)
|
|
router = Router()
|
|
dyn = DynamoDBPersistenceLayer(ENROLLMENT_TABLE, dynamodb_client)
|
|
|
|
|
|
@router.patch('/<enrollment_id>/cancel')
|
|
def cancel(enrollment_id: str):
|
|
now_ = now()
|
|
canceled_by: Authenticated = router.context['user']
|
|
|
|
with dyn.transact_writer() as transact:
|
|
transact.update(
|
|
key=KeyPair(enrollment_id, '0'),
|
|
cond_expr='#status = :pending',
|
|
update_expr='SET #status = :canceled, \
|
|
canceled_at = :now, \
|
|
updated_at = :now',
|
|
expr_attr_names={
|
|
'#status': 'status',
|
|
},
|
|
expr_attr_values={
|
|
':pending': 'PENDING',
|
|
':canceled': 'CANCELED',
|
|
':now': now_,
|
|
},
|
|
)
|
|
transact.put(
|
|
item={
|
|
'id': enrollment_id,
|
|
'sk': 'CANCELED',
|
|
'user_id': canceled_by.id,
|
|
'name': canceled_by.name,
|
|
'created_at': now_,
|
|
}
|
|
)
|
|
transact.delete(
|
|
key=KeyPair(enrollment_id, 'CANCEL_POLICY'),
|
|
cond_expr='attribute_exists(sk)',
|
|
exc_cls=CancelPolicyConflictError,
|
|
)
|