feat(child): auto create parent-child conversations
This commit is contained in:
@@ -26,6 +26,7 @@ class ChildDAO(BaseDAO):
|
||||
child_name: str,
|
||||
child_gender: int = 2,
|
||||
child_birthday: Optional[date] = None,
|
||||
auto_commit: bool = True,
|
||||
) -> int:
|
||||
result = self.db.execute(
|
||||
text(
|
||||
@@ -38,7 +39,8 @@ class ChildDAO(BaseDAO):
|
||||
)
|
||||
child_id = inserted_primary_key(result)
|
||||
self._create_relation(user_id, child_id)
|
||||
self.commit()
|
||||
if auto_commit:
|
||||
self.commit()
|
||||
return child_id
|
||||
|
||||
def get_by_id(self, child_id: int) -> Optional[Mapping]:
|
||||
|
||||
@@ -3,10 +3,12 @@ from datetime import date
|
||||
from typing import Optional
|
||||
|
||||
from app.dao.child import ChildDAO
|
||||
from app.service.im import ensure_parent_child_conversation
|
||||
|
||||
|
||||
class ChildService:
|
||||
def __init__(self, db):
|
||||
self.db = db
|
||||
self.dao = ChildDAO(db)
|
||||
|
||||
def create(
|
||||
@@ -16,8 +18,24 @@ class ChildService:
|
||||
child_gender: int = 2,
|
||||
child_birthday: Optional[date] = None,
|
||||
) -> Mapping:
|
||||
child_id = self.dao.create(user_id, child_name, child_gender, child_birthday)
|
||||
return self.dao.get_by_id(child_id)
|
||||
try:
|
||||
child_id = self.dao.create(
|
||||
user_id,
|
||||
child_name,
|
||||
child_gender,
|
||||
child_birthday,
|
||||
auto_commit=False,
|
||||
)
|
||||
ensure_parent_child_conversation(
|
||||
self.db,
|
||||
parent_user_id=user_id,
|
||||
child_id=child_id,
|
||||
)
|
||||
self.db.commit()
|
||||
return self.dao.get_by_id(child_id)
|
||||
except Exception:
|
||||
self.db.rollback()
|
||||
raise
|
||||
|
||||
def list_children(self, user_id: int, limit: int = 20, cursor: int = None) -> tuple[list, bool]:
|
||||
rows = self.dao.list_by_parent(user_id, limit, cursor)
|
||||
@@ -39,4 +57,4 @@ class ChildService:
|
||||
if not self.dao.has_access(child_id, user_id):
|
||||
raise PermissionError("No access to this child")
|
||||
self.dao.update(child_id, child_name, child_gender, child_birthday)
|
||||
return self.dao.get_by_id(child_id)
|
||||
return self.dao.get_by_id(child_id)
|
||||
|
||||
@@ -217,6 +217,28 @@ def create_parent_child_message(
|
||||
)
|
||||
|
||||
|
||||
def ensure_parent_child_conversation(
|
||||
db: Session,
|
||||
*,
|
||||
parent_user_id: int,
|
||||
child_id: int,
|
||||
) -> int:
|
||||
child_row = assert_parent_child_access(db, user_id=parent_user_id, child_id=child_id)
|
||||
parent_row = _get_parent_row(db, parent_user_id)
|
||||
if not parent_row:
|
||||
raise HTTPException(status_code=404, detail="parent not found")
|
||||
|
||||
return _get_or_create_conversation(
|
||||
db=db,
|
||||
conversation_type=PARENT_CHILD_CONVERSATION_TYPE,
|
||||
participant_a_type=CHILD_PARTICIPANT_TYPE,
|
||||
participant_a_id=str(child_id),
|
||||
participant_b_type=PARENT_PARTICIPANT_TYPE,
|
||||
participant_b_id=str(parent_user_id),
|
||||
pair_key=f"{child_id}:{parent_user_id}",
|
||||
)
|
||||
|
||||
|
||||
def create_device_message(
|
||||
db: Session,
|
||||
*,
|
||||
|
||||
Reference in New Issue
Block a user