fix
This commit is contained in:
102
http-api/app/rules/enrollment.py
Normal file
102
http-api/app/rules/enrollment.py
Normal file
@@ -0,0 +1,102 @@
|
||||
from typing import TypedDict
|
||||
from uuid import uuid4
|
||||
|
||||
from layercake.dateutils import now
|
||||
from layercake.dynamodb import DynamoDBPersistenceLayer, KeyPair, TransactItems
|
||||
|
||||
from conf import ORDER_TABLE
|
||||
|
||||
|
||||
class Author(TypedDict):
|
||||
id: str
|
||||
name: str
|
||||
|
||||
|
||||
class Course(TypedDict):
|
||||
id: str
|
||||
name: str
|
||||
time_in_days: int
|
||||
|
||||
|
||||
def set_status_as_canceled(
|
||||
id: str,
|
||||
*,
|
||||
lock_hash: str,
|
||||
author: Author,
|
||||
course: Course | None = None,
|
||||
vacancy_key: KeyPair | None = None,
|
||||
persistence_layer: DynamoDBPersistenceLayer,
|
||||
):
|
||||
"""Cancel the enrollment if there's a `cancel_policy`
|
||||
and put its vacancy back if `vacancy_key` is provided."""
|
||||
now_ = now()
|
||||
transact = TransactItems(persistence_layer.table_name)
|
||||
transact.update(
|
||||
key=KeyPair(id, '0'),
|
||||
update_expr='SET #status = :canceled, update_date = :update',
|
||||
expr_attr_names={
|
||||
'#status': 'status',
|
||||
},
|
||||
expr_attr_values={
|
||||
':canceled': 'CANCELED',
|
||||
':update': now_,
|
||||
},
|
||||
)
|
||||
transact.put(
|
||||
item={
|
||||
'id': id,
|
||||
'sk': 'canceled_date',
|
||||
'author': author,
|
||||
'create_date': now_,
|
||||
},
|
||||
)
|
||||
transact.delete(
|
||||
key=KeyPair(id, 'cancel_policy'),
|
||||
cond_expr='attribute_exists(sk)',
|
||||
)
|
||||
# Remove schedules lifecycle events, referencies and locks
|
||||
transact.delete(key=KeyPair(id, 'schedules#archive_it'))
|
||||
transact.delete(key=KeyPair(id, 'schedules#no_activity'))
|
||||
transact.delete(key=KeyPair(id, 'schedules#access_period_ends'))
|
||||
transact.delete(key=KeyPair(id, 'schedules#does_not_access'))
|
||||
transact.delete(key=KeyPair(id, 'parent_vacancy'))
|
||||
transact.delete(key=KeyPair(id, 'lock'))
|
||||
transact.delete(key=KeyPair('lock', lock_hash))
|
||||
|
||||
if vacancy_key and course:
|
||||
vacancy_pk, vacancy_sk = vacancy_key.values()
|
||||
org_id = vacancy_pk.removeprefix('vacancies#')
|
||||
order_id, enrollment_id = vacancy_sk.split('#')
|
||||
|
||||
transact.condition(
|
||||
key=KeyPair(order_id, '0'),
|
||||
cond_expr='attribute_exists(id)',
|
||||
table_name=ORDER_TABLE,
|
||||
)
|
||||
# Put the vacancy back and assign a new ID
|
||||
transact.put(
|
||||
item={
|
||||
'id': f'vacancies#{org_id}',
|
||||
'sk': f'{order_id}#{uuid4()}',
|
||||
'course': course,
|
||||
'create_date': now_,
|
||||
},
|
||||
cond_expr='attribute_not_exists(sk)',
|
||||
)
|
||||
# Set the status of `generated_items` to `ROLLBACK` to know
|
||||
# which vacancy is available for reuse
|
||||
transact.update(
|
||||
key=KeyPair(order_id, f'generated_items#{enrollment_id}'),
|
||||
update_expr='SET #status = :status, update_date = :update',
|
||||
expr_attr_names={
|
||||
'#status': 'status',
|
||||
},
|
||||
expr_attr_values={
|
||||
':status': 'ROLLBACK',
|
||||
':update': now_,
|
||||
},
|
||||
cond_expr='attribute_exists(sk)',
|
||||
table_name=ORDER_TABLE,
|
||||
)
|
||||
|
||||
return persistence_layer.transact_write_items(transact)
|
||||
Reference in New Issue
Block a user