File size: 6,012 Bytes
369b6c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdc2d18
 
 
 
 
 
 
 
 
 
 
348ea76
 
 
 
 
bdc2d18
 
 
 
 
 
 
 
 
 
 
369b6c9
348ea76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
369b6c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""Per-feature entitlements (Work Programme / Cash Flow) and their saved history.

Each feature is sold as a one-time 50 AED product that grants ONE credit. A credit
is consumed when a NEW module project is saved. Mirrors the existing
`extra_projects_allowance` add-on model. Provider/Dodo wiring lives in
utils/payments.py; this module only touches the database.
"""
from __future__ import annotations

import json
from datetime import datetime

from utils.db import safe_query, safe_execute

# feature key -> credits column on qto_users
FEATURES = {"programme": "programme_credits", "cashflow": "cashflow_credits"}

# Every tool has its OWN purchasable "extra project" (+1) allowance. This maps a
# feature to the qto_users column that stores that allowance, so the add-on for
# one tool never leaks into another tool's limit.
FEATURE_EXTRA_COL = {
    "qto": "extra_projects_allowance",
    "programme": "programme_credits",
    "cashflow": "cashflow_credits",
}


def extra_projects_for(user_id: int, feature: str) -> int:
    """Purchasable +1 project add-ons the user still has (UNUSED) for THIS tool.

    These are ONE-TIME credits: each is consumed when a project is created beyond
    the monthly tier quota. They are NOT a permanent bump to the monthly limit.
    """
    col = FEATURE_EXTRA_COL.get(feature)
    if not col:
        return 0
    df = safe_query(f"SELECT {col} AS c FROM qto_users WHERE id=%s", (user_id,))
    if df.empty:
        return 0
    try:
        return int(df.iloc[0]["c"] or 0)
    except Exception:
        return 0


def consume_extra_project(user_id: int, feature: str, n: int = 1) -> bool:
    """Atomically spend ONE one-time '+1 project' add-on for this tool.

    The guarded `>= n` clause makes it safe under concurrency and never lets the
    balance go negative. Returns whether the row still had enough to spend.
    """
    col = FEATURE_EXTRA_COL.get(feature)
    if not col:
        return False
    ok, _ = safe_execute(
        f"UPDATE qto_users SET {col} = {col} - %s WHERE id=%s AND {col} >= %s",
        (n, user_id, n),
    )
    return bool(ok)


def _credit_col(feature: str) -> str:
    col = FEATURES.get(feature)
    if not col:
        raise ValueError(f"Unknown feature: {feature}")
    return col


def get_credits(user_id: int, feature: str) -> int:
    col = _credit_col(feature)
    df = safe_query(f"SELECT {col} AS c FROM qto_users WHERE id=%s", (user_id,))
    if df.empty:
        return 0
    try:
        return int(df.iloc[0]["c"] or 0)
    except Exception:
        return 0


def grant_credit(user_id: int, feature: str, n: int = 1):
    col = _credit_col(feature)
    return safe_execute(f"UPDATE qto_users SET {col} = {col} + %s WHERE id=%s", (n, user_id))


def consume_credit(user_id: int, feature: str, n: int = 1):
    col = _credit_col(feature)
    return safe_execute(
        f"UPDATE qto_users SET {col} = {col} - %s WHERE id=%s AND {col} >= %s",
        (n, user_id, n),
    )


def count_projects(user_id: int, feature: str) -> int:
    df = safe_query(
        "SELECT COUNT(*) AS c FROM qto_module_projects WHERE user_id=%s AND feature=%s",
        (user_id, feature),
    )
    if df.empty:
        return 0
    try:
        return int(df.iloc[0]["c"] or 0)
    except Exception:
        return 0


def has_access(user_id: int, feature: str, is_admin: bool = False) -> bool:
    """The tool is visible if the user has a credit to spend OR already owns a
    saved project for that feature (so they keep access to view/edit it)."""
    if is_admin:
        return True
    return get_credits(user_id, feature) > 0 or count_projects(user_id, feature) > 0


def _parse(v):
    try:
        return json.loads(v) if isinstance(v, str) else (v or {})
    except Exception:
        return {}


def save_module_project(user_id, feature, name, config, summary, project_id=None):
    config_json = json.dumps(config or {}, ensure_ascii=False, default=str)
    summary_json = json.dumps(summary or {}, ensure_ascii=False, default=str)
    date_str = datetime.now().strftime("%Y-%m-%d %H:%M")
    if project_id:
        safe_execute(
            "UPDATE qto_module_projects SET name=%s, date=%s, config_data=%s, summary_data=%s WHERE id=%s AND user_id=%s AND feature=%s",
            (name, date_str, config_json, summary_json, project_id, user_id, feature),
        )
        return project_id
    ok, err = safe_execute(
        "INSERT INTO qto_module_projects (user_id, feature, name, date, config_data, summary_data) VALUES (%s, %s, %s, %s, %s, %s)",
        (user_id, feature, name, date_str, config_json, summary_json),
    )
    if not ok:
        return None
    df = safe_query(
        "SELECT id FROM qto_module_projects WHERE user_id=%s AND feature=%s ORDER BY id DESC LIMIT 1",
        (user_id, feature),
    )
    return int(df.iloc[0]["id"]) if not df.empty else None


def list_module_projects(user_id, feature):
    df = safe_query(
        "SELECT id, name, date, summary_data FROM qto_module_projects WHERE user_id=%s AND feature=%s ORDER BY id DESC",
        (user_id, feature),
    )
    out = []
    for _, row in df.iterrows():
        out.append({
            "id": int(row["id"]), "name": row["name"], "date": row["date"],
            "summary": _parse(row["summary_data"]),
        })
    return out


def get_module_project(user_id, feature, project_id):
    df = safe_query(
        "SELECT id, name, date, config_data, summary_data FROM qto_module_projects WHERE id=%s AND user_id=%s AND feature=%s",
        (project_id, user_id, feature),
    )
    if df.empty:
        return None
    row = df.iloc[0]
    return {
        "id": int(row["id"]), "name": row["name"], "date": row["date"],
        "config": _parse(row["config_data"]), "summary": _parse(row["summary_data"]),
    }


def delete_module_project(user_id, feature, project_id):
    return safe_execute(
        "DELETE FROM qto_module_projects WHERE id=%s AND user_id=%s AND feature=%s",
        (project_id, user_id, feature),
    )