Skip to content

Commit badb2a2

Browse files
Fix sending websocket message when editing cdr user membership (#812)
1 parent 84702bb commit badb2a2

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

app/modules/cdr/endpoints_cdr.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
is_user_in_a_seller_group,
4747
validate_payment,
4848
)
49+
from app.types.exceptions import ObjectExpectedInDbNotFoundError
4950
from app.types.module import Module
5051
from app.types.websocket import (
5152
HyperionWebsocketsRoom,
@@ -303,17 +304,30 @@ async def update_cdr_user(
303304
detail="User not found.",
304305
)
305306

306-
curriculum = await cruds_cdr.get_cdr_user_curriculum(db, user_id)
307+
curriculum_membership = await cruds_cdr.get_cdr_user_curriculum(db, user_id)
308+
curriculum: schemas_cdr.CurriculumComplete | None = None
309+
if curriculum_membership:
310+
curriculum_db = await cruds_cdr.get_curriculum_by_id(
311+
db=db,
312+
curriculum_id=curriculum_membership.curriculum_id,
313+
)
314+
if curriculum_db is None:
315+
raise ObjectExpectedInDbNotFoundError(
316+
object_name="curriculum",
317+
object_id=curriculum_membership.curriculum_id,
318+
)
319+
curriculum = schemas_cdr.CurriculumComplete(
320+
id=curriculum_db.id,
321+
name=curriculum_db.name,
322+
)
307323

308324
cdr_status = await get_core_data(schemas_cdr.Status, db)
309325
if cdr_status.status == CdrStatus.onsite:
310326
try:
311327
await ws_manager.send_message_to_room(
312328
message=schemas_cdr.UpdateUserWSMessageModel(
313329
data=schemas_cdr.CdrUser(
314-
curriculum=schemas_cdr.CurriculumComplete(
315-
**curriculum.__dict__,
316-
),
330+
curriculum=curriculum,
317331
school_id=user_db.school_id,
318332
account_type=user_db.account_type,
319333
name=user_db.name,

app/types/exceptions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import Any
2+
from uuid import UUID
23

34
from fastapi import HTTPException
45

@@ -210,3 +211,17 @@ def __init__(self, subfolder: str):
210211
super().__init__(
211212
f"Invalid S3 subfolder: {subfolder} - it should not contain '/'",
212213
)
214+
215+
216+
class ObjectExpectedInDbNotFoundError(Exception):
217+
"""
218+
This exception should be raised when an object is expected to be found in database, but the select crud return None.
219+
It may be used when selecting an object to load relationship just after this creation, or selecting an object we know existing because of a foreign key.
220+
221+
This should never happen. Raising this exception should lead to a logged 500 internal server error.
222+
"""
223+
224+
def __init__(self, object_name: str, object_id: str | UUID):
225+
super().__init__(
226+
f"Object {object_name} with id {object_id} was expected in database but not found",
227+
)

0 commit comments

Comments
 (0)