201 lines
9.4 KiB
Python
Executable File
201 lines
9.4 KiB
Python
Executable File
# /opt/docker/dev/service_finder/backend/app/models/identity/social.py
|
|
import enum
|
|
import uuid
|
|
from datetime import datetime
|
|
from typing import Optional, List, TYPE_CHECKING
|
|
from sqlalchemy import String, Integer, ForeignKey, DateTime, Boolean, Text, UniqueConstraint, text
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
from sqlalchemy.dialects.postgresql import ENUM as PG_ENUM, UUID as PG_UUID, JSONB, ARRAY
|
|
from sqlalchemy.sql import func
|
|
from app.database import Base
|
|
|
|
if TYPE_CHECKING:
|
|
from app.models.identity.address import Address
|
|
|
|
class ModerationStatus(str, enum.Enum):
|
|
pending = "pending"
|
|
approved = "approved"
|
|
rejected = "rejected"
|
|
|
|
class SourceType(str, enum.Enum):
|
|
manual = "manual"
|
|
ocr = "ocr"
|
|
api = "api"
|
|
api_import = "import"
|
|
|
|
class ServiceProvider(Base):
|
|
""" Közösség által beküldött szolgáltatók (v1.3.1).
|
|
|
|
P0 HYBRID VENDOR REFACTOR: Kibővítve atomizált címmezőkkel és
|
|
kapcsolatfelvételi adatokkal a quick_add_provider() refactorhoz.
|
|
"""
|
|
__tablename__ = "service_providers"
|
|
__table_args__ = {"schema": "marketplace", "extend_existing": True}
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
|
|
name: Mapped[str] = mapped_column(String, nullable=False)
|
|
address: Mapped[str] = mapped_column(String, nullable=False)
|
|
category: Mapped[Optional[str]] = mapped_column(String)
|
|
|
|
# === P0 ADDRESS UNIFICATION: Normalizált cím FK a system.addresses táblához ===
|
|
address_id: Mapped[Optional[uuid.UUID]] = mapped_column(
|
|
PG_UUID(as_uuid=True), ForeignKey("system.addresses.id"), nullable=True
|
|
)
|
|
|
|
# === P0 HYBRID VENDOR REFACTOR: Atomizált címmezők (DEPRECATED - Phase 3-ban törlendő) ===
|
|
city: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
|
|
address_zip: Mapped[Optional[str]] = mapped_column(String(20), nullable=True)
|
|
address_street_name: Mapped[Optional[str]] = mapped_column(String(150), nullable=True)
|
|
address_street_type: Mapped[Optional[str]] = mapped_column(String(50), nullable=True)
|
|
address_house_number: Mapped[Optional[str]] = mapped_column(String(20), nullable=True)
|
|
plus_code: Mapped[Optional[str]] = mapped_column(String(20), nullable=True)
|
|
|
|
# === P0 HYBRID VENDOR REFACTOR: Kapcsolatfelvételi adatok ===
|
|
contact_phone: Mapped[Optional[str]] = mapped_column(String(50), nullable=True)
|
|
contact_email: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
|
website: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
|
|
|
status: Mapped[ModerationStatus] = mapped_column(
|
|
PG_ENUM(ModerationStatus, name="moderation_status", inherit_schema=True),
|
|
default=ModerationStatus.pending
|
|
)
|
|
source: Mapped[SourceType] = mapped_column(
|
|
PG_ENUM(SourceType, name="source_type", inherit_schema=True),
|
|
default=SourceType.manual
|
|
)
|
|
|
|
validation_score: Mapped[int] = mapped_column(Integer, default=0)
|
|
evidence_image_path: Mapped[Optional[str]] = mapped_column(String)
|
|
added_by_user_id: Mapped[Optional[int]] = mapped_column(Integer, ForeignKey("identity.users.id"))
|
|
|
|
# === P0 3D FILTERING MATRIX: Járműosztályok és specializációk ===
|
|
supported_vehicle_classes: Mapped[Optional[list]] = mapped_column(
|
|
ARRAY(String), server_default=text("'{}'"), nullable=True
|
|
)
|
|
specializations: Mapped[Optional[dict]] = mapped_column(
|
|
JSONB, server_default=text("'{}'::jsonb"), nullable=True
|
|
)
|
|
|
|
# === P0 ADDRESS UNIFICATION: Kapcsolat a normalizált címhez ===
|
|
address_rel: Mapped[Optional["Address"]] = relationship(
|
|
"Address", foreign_keys=[address_id], lazy="selectin"
|
|
)
|
|
|
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
|
|
|
class Vote(Base):
|
|
""" Közösségi validációs szavazatok. """
|
|
__tablename__ = "votes"
|
|
__table_args__ = (
|
|
UniqueConstraint('user_id', 'provider_id', name='uq_user_provider_vote'),
|
|
{"schema": "marketplace"}
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
|
user_id: Mapped[int] = mapped_column(Integer, ForeignKey("identity.users.id"), nullable=False)
|
|
provider_id: Mapped[int] = mapped_column(Integer, ForeignKey("marketplace.service_providers.id"), nullable=False)
|
|
vote_value: Mapped[int] = mapped_column(Integer, nullable=False) # +1 vagy -1
|
|
|
|
|
|
class ProviderValidation(Base):
|
|
"""
|
|
Provider validációs rekordok (XP farming prevention).
|
|
|
|
Minden egyes admin moderációs akció (approve/reject/flag) egy rekordot hoz létre
|
|
ebben a táblában. A Vote táblával ellentétben itt az admin által végzett
|
|
validációk kerülnek rögzítésre, súlyozott értékkel.
|
|
|
|
Features:
|
|
- UniqueConstraint(voter_user_id, provider_id): egy user csak egyszer validálhat
|
|
- weight: az admin szintjétől függő súly (pl. superadmin=10, admin=5)
|
|
- metadata: JSONB a validáció részleteivel (reason, evidence, stb.)
|
|
- Küszöbérték: ha a weight-ek összege >= VALIDATION_THRESHOLD, a provider auto-approved
|
|
"""
|
|
__tablename__ = "provider_validations"
|
|
__table_args__ = (
|
|
UniqueConstraint('voter_user_id', 'provider_id', name='uq_voter_provider_validation'),
|
|
{"schema": "marketplace", "extend_existing": True}
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
|
provider_id: Mapped[int] = mapped_column(
|
|
Integer, ForeignKey("marketplace.service_providers.id", ondelete="CASCADE"), nullable=False, index=True
|
|
)
|
|
voter_user_id: Mapped[int] = mapped_column(
|
|
Integer, ForeignKey("identity.users.id", ondelete="CASCADE"), nullable=False
|
|
)
|
|
validation_type: Mapped[str] = mapped_column(
|
|
String(20), nullable=False, default="approve"
|
|
) # approve | reject | flag
|
|
weight: Mapped[int] = mapped_column(Integer, default=1, nullable=False)
|
|
validation_metadata: Mapped[Optional[dict]] = mapped_column("metadata", JSONB, server_default=text("'{}'::jsonb"))
|
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
|
|
|
# Relationships
|
|
provider: Mapped["ServiceProvider"] = relationship("ServiceProvider", foreign_keys=[provider_id])
|
|
voter: Mapped["User"] = relationship("User", foreign_keys=[voter_user_id])
|
|
|
|
class Competition(Base):
|
|
""" Gamifikált versenyek (pl. Januári Feltöltő Verseny). """
|
|
__tablename__ = "competitions"
|
|
__table_args__ = {"schema": "gamification"}
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
|
name: Mapped[str] = mapped_column(String, nullable=False)
|
|
description: Mapped[Optional[str]] = mapped_column(Text)
|
|
start_date: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
|
end_date: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
|
is_active: Mapped[bool] = mapped_column(Boolean, default=True)
|
|
|
|
class UserScore(Base):
|
|
""" Versenyenkénti ranglista pontszámok. """
|
|
__tablename__ = "user_scores"
|
|
__table_args__ = (
|
|
UniqueConstraint('user_id', 'competition_id', name='uq_user_competition_score'),
|
|
{"schema": "gamification"}
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
|
user_id: Mapped[int] = mapped_column(Integer, ForeignKey("identity.users.id"))
|
|
competition_id: Mapped[int] = mapped_column(Integer, ForeignKey("gamification.competitions.id"))
|
|
points: Mapped[int] = mapped_column(Integer, default=0)
|
|
last_updated: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())
|
|
|
|
|
|
class ServiceReview(Base):
|
|
"""
|
|
Verifikált szerviz értékelések (Social 3).
|
|
Csak igazolt pénzügyi tranzakció után lehet értékelni.
|
|
"""
|
|
__tablename__ = "service_reviews"
|
|
__table_args__ = (
|
|
UniqueConstraint('transaction_id', name='uq_service_review_transaction'),
|
|
{"schema": "marketplace"}
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
|
service_id: Mapped[int] = mapped_column(Integer, ForeignKey("marketplace.service_profiles.id", ondelete="CASCADE"), nullable=False)
|
|
user_id: Mapped[int] = mapped_column(Integer, ForeignKey("identity.users.id", ondelete="SET NULL"), nullable=False)
|
|
transaction_id: Mapped[uuid.UUID] = mapped_column(
|
|
PG_UUID(as_uuid=True),
|
|
ForeignKey("audit.financial_ledger.transaction_id", ondelete="RESTRICT"),
|
|
nullable=False,
|
|
index=True
|
|
)
|
|
|
|
# Rating dimensions (1-10)
|
|
price_rating: Mapped[int] = mapped_column(Integer, nullable=False) # 1-10
|
|
quality_rating: Mapped[int] = mapped_column(Integer, nullable=False) # 1-10
|
|
time_rating: Mapped[int] = mapped_column(Integer, nullable=False) # 1-10
|
|
communication_rating: Mapped[int] = mapped_column(Integer, nullable=False) # 1-10
|
|
|
|
comment: Mapped[Optional[str]] = mapped_column(Text)
|
|
is_verified: Mapped[bool] = mapped_column(Boolean, default=True, server_default=text("true"))
|
|
|
|
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
|
updated_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), onupdate=func.now())
|
|
|
|
# Relationships
|
|
service: Mapped["ServiceProfile"] = relationship("ServiceProfile", back_populates="reviews")
|
|
user: Mapped["User"] = relationship("User", back_populates="service_reviews")
|
|
financial_transaction: Mapped["FinancialLedger"] = relationship("FinancialLedger", foreign_keys=[transaction_id]) |