37 lines
1.4 KiB
Python
37 lines
1.4 KiB
Python
"""A record of a request that has already been answered."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime, timezone
|
|
|
|
from sqlalchemy import Column, DateTime, ForeignKey, Integer, String, func
|
|
from sqlalchemy.dialects.postgresql import JSONB, UUID
|
|
|
|
from app.config.database import Base
|
|
|
|
|
|
class IdempotencyRecord(Base):
|
|
__tablename__ = "idempotency_records"
|
|
|
|
id = Column(UUID(as_uuid=True), primary_key=True,
|
|
server_default=func.gen_random_uuid())
|
|
tenant_id = Column(UUID(as_uuid=True), ForeignKey("tenants.id", ondelete="CASCADE"))
|
|
user_id = Column(UUID(as_uuid=True), ForeignKey("users.id", ondelete="CASCADE"))
|
|
idempotency_key = Column(String(255), nullable=False)
|
|
endpoint = Column(String(255), nullable=False)
|
|
request_hash = Column(String(64), nullable=False)
|
|
state = Column(String(20), nullable=False, default="in_progress")
|
|
response_status = Column(Integer)
|
|
response_body = Column(JSONB)
|
|
expires_at = Column(DateTime(timezone=True), nullable=False)
|
|
created_at = Column(DateTime(timezone=True), nullable=False, server_default=func.now())
|
|
completed_at = Column(DateTime(timezone=True))
|
|
|
|
@property
|
|
def is_expired(self) -> bool:
|
|
return self.expires_at <= datetime.now(timezone.utc)
|
|
|
|
@property
|
|
def is_replayable(self) -> bool:
|
|
return self.state == "completed" and not self.is_expired
|