refactor: move services to a unit of work over repository ports
Services depend only on src/core/ports: async with self.uow is one transaction, commit is explicit, leaving without it rolls back. A connection is held per step, the JSON routers keep a shared request deadline, and rollback runs under the block deadline. Adds service-tests with fakes, a repo contract run on both the fake and SQL, and make service_test / service_test_db. API, schema and settings are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import abc
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
|
||||
from src.schemas import dataclasses
|
||||
|
||||
|
||||
class AbstractIdempotencyRepository(abc.ABC):
|
||||
@abc.abstractmethod
|
||||
async def claim_key(
|
||||
self,
|
||||
*,
|
||||
user_id: UUID,
|
||||
chat_id: UUID,
|
||||
key: str,
|
||||
request_hash: bytes,
|
||||
message_id: UUID,
|
||||
created_at: datetime,
|
||||
expired_before: datetime,
|
||||
) -> bool:
|
||||
"""Захват ключа под новое сообщение `message_id`.
|
||||
|
||||
`True` — ключ свободен: новый или просроченный (`created_at` раньше
|
||||
`expired_before`), он заменён этим же вызовом. `False` — ключ занят и
|
||||
действует, запись не изменилась. Занятая строка заблокирована до конца
|
||||
транзакции.
|
||||
"""
|
||||
|
||||
@abc.abstractmethod
|
||||
async def get_key_result(
|
||||
self,
|
||||
*,
|
||||
user_id: UUID,
|
||||
chat_id: UUID,
|
||||
key: str,
|
||||
) -> dataclasses.IdempotencyRecordDTO | None:
|
||||
"""Хэш тела и сообщение с вложениями, сохранённые под ключом; `None` —
|
||||
ключа нет."""
|
||||
Reference in New Issue
Block a user