feat(child): auto create parent-child conversations
This commit is contained in:
@@ -24,6 +24,7 @@ class ChildDAO(BaseDAO):
|
||||
child_name: str,
|
||||
child_gender: int = 2,
|
||||
child_birthday: Optional[date] = None,
|
||||
auto_commit: bool = True,
|
||||
) -> int:
|
||||
result = await self.execute(
|
||||
"""
|
||||
@@ -34,7 +35,8 @@ class ChildDAO(BaseDAO):
|
||||
)
|
||||
child_id = inserted_primary_key(result)
|
||||
await self._create_relation(user_id, child_id)
|
||||
await self.commit()
|
||||
if auto_commit:
|
||||
await self.commit()
|
||||
return child_id
|
||||
|
||||
async def get_by_id(self, child_id: int) -> Optional[Mapping]:
|
||||
|
||||
@@ -3,6 +3,7 @@ from datetime import date
|
||||
from typing import Optional
|
||||
|
||||
from banban.dao.child import ChildDAO
|
||||
from banban.dao.im import ImDAO
|
||||
from services.database_service_base import DatabaseServiceBase
|
||||
|
||||
|
||||
@@ -19,10 +20,24 @@ class ChildService(DatabaseServiceBase):
|
||||
) -> Mapping:
|
||||
db_session = await self.get_session()
|
||||
try:
|
||||
dao = ChildDAO(db_session)
|
||||
child_id = await dao.create(user_id, child_name, child_gender, child_birthday)
|
||||
child_dao = ChildDAO(db_session)
|
||||
im_dao = ImDAO(db_session)
|
||||
child_id = await child_dao.create(
|
||||
user_id,
|
||||
child_name,
|
||||
child_gender,
|
||||
child_birthday,
|
||||
auto_commit=False,
|
||||
)
|
||||
await im_dao.ensure_parent_child_conversation(
|
||||
parent_user_id=user_id,
|
||||
child_id=child_id,
|
||||
)
|
||||
await db_session.commit()
|
||||
return await self.get(child_id)
|
||||
return await child_dao.get_by_id(child_id)
|
||||
except Exception:
|
||||
await db_session.rollback()
|
||||
raise
|
||||
finally:
|
||||
await db_session.close()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user