File size: 2,744 Bytes
17101d7
d896946
 
 
17101d7
 
 
 
 
 
 
 
d896946
 
 
 
 
 
17101d7
 
 
 
 
 
 
 
 
 
d896946
 
 
17101d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d896946
 
 
17101d7
 
 
 
d896946
 
17101d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

from datetime import datetime
from typing import Optional

from sqlalchemy import (
    BigInteger,
    DateTime,
    Integer,
    String,
    UniqueConstraint,
    func,
)
from sqlalchemy.orm import Mapped, mapped_column

from app.db.base import Base


class XpEvent(Base):
    """
    Immutable XP ledger.

    users.points / week_xp are only a fast cache of this table.

    UNIQUE(user_id, kind, ref) ensures every rewarded action
    is idempotent and cannot be paid twice.

    The legacy 'week' column stores the 60-day season key.
    """

    __tablename__ = "xp_events"

    __table_args__ = (
        UniqueConstraint(
            "user_id",
            "kind",
            "ref",
            name="uq_xp_idem",
        ),
    )

    id: Mapped[int] = mapped_column(
        Integer,
        primary_key=True,
        autoincrement=True,
    )

    user_id: Mapped[int] = mapped_column(
        BigInteger,
        index=True,
    )

    kind: Mapped[str] = mapped_column(
        String(24),
    )

    amount: Mapped[int] = mapped_column(
        Integer,
    )

    ref: Mapped[Optional[str]] = mapped_column(
        String(64),
    )

    # 60-day season key: S60-YYYY-MM-DD (14 characters)
    week: Mapped[str] = mapped_column(
        String(16),
        index=True,
    )

    day: Mapped[str] = mapped_column(
        String(10),
    )

    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        server_default=func.now(),
    )


class SeasonResult(Base):
    """
    One row per winner per 60-day season,
    plus a 'closed' marker (rank 0) per season.
    """

    __tablename__ = "season_results"

    __table_args__ = (
        UniqueConstraint(
            "week",
            "kind",
            "rank",
            name="uq_season_slot",
        ),
    )

    id: Mapped[int] = mapped_column(
        Integer,
        primary_key=True,
        autoincrement=True,
    )

    # 60-day season key: S60-YYYY-MM-DD (14 characters)
    week: Mapped[str] = mapped_column(
        String(16),
        index=True,
    )

    kind: Mapped[str] = mapped_column(
        String(8),
    )  # top | raffle | closed

    rank: Mapped[int] = mapped_column(
        Integer,
        default=0,
    )

    user_id: Mapped[int] = mapped_column(
        BigInteger,
        default=0,
    )

    xp: Mapped[int] = mapped_column(
        Integer,
        default=0,
    )

    prize_birr: Mapped[int] = mapped_column(
        Integer,
        default=0,
    )

    status: Mapped[str] = mapped_column(
        String(12),
        default="info",
    )  # info | pending | paid

    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        server_default=func.now(),
    )