feat(child): auto create parent-child conversations
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user