TorbatYar/backend/services/loyalty/app/models/campaigns.py
Mortezakoohjani 071c484530 Ship Loyalty phases 7.2-7.6 (points through wallet) with production deploy.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-26 21:27:22 +03:30

58 lines
2.2 KiB
Python

"""Campaign application tracking — Phase 7.4 Campaign Engine.
Independent aggregate (no SQLAlchemy relationships), same as other Loyalty
aggregates. Points are always the ledger's responsibility; this row only
records that a campaign granted N points to a member at a given rule version.
"""
from __future__ import annotations
import uuid
from datetime import datetime
from sqlalchemy import Index, Integer, String, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.types import JSON
from app.core.database import Base
from app.models.base import TenantMixin, TimestampMixin, UUIDPrimaryKeyMixin
from app.models.types import CampaignApplicationStatus, GUID
class CampaignApplication(
Base,
UUIDPrimaryKeyMixin,
TenantMixin,
TimestampMixin,
):
"""Record of a campaign's point grant applied to a member's point account."""
__tablename__ = "campaign_applications"
campaign_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
program_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
member_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
point_account_id: Mapped[uuid.UUID] = mapped_column(GUID(), nullable=False)
rule_version: Mapped[int] = mapped_column(Integer, nullable=False)
points_granted: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
ledger_entry_id: Mapped[uuid.UUID | None] = mapped_column(GUID(), nullable=True)
status: Mapped[CampaignApplicationStatus] = mapped_column(
default=CampaignApplicationStatus.APPLIED, nullable=False
)
idempotency_key: Mapped[str | None] = mapped_column(String(100), nullable=True)
actor_user_id: Mapped[str | None] = mapped_column(String(100), nullable=True)
metadata_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)
__table_args__ = (
UniqueConstraint(
"tenant_id",
"idempotency_key",
name="uq_campaign_applications_tenant_idempotency",
),
Index(
"ix_campaign_applications_campaign_member",
"tenant_id",
"campaign_id",
"member_id",
),
)