File size: 16,049 Bytes
fbb917f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import os
import json
import logging
from typing import Optional, List
from fastapi import APIRouter, Depends, HTTPException

from auth.models import User
from auth.service import get_current_user
from models.admin_models import RuleCreateRequest, RuleUpdateRequest
from core.pipeline import reload_pipeline_rules

logger = logging.getLogger("befree.admin_rules")

router = APIRouter(prefix="/api/v1/admin/rules", tags=["admin_rules"])


@router.get("")
async def list_rules(
    category_id: Optional[str] = None,
    match_on: Optional[str] = None,
    include_inactive: bool = False,
    current_user: User = Depends(get_current_user),
):
    """List all classification rules, optionally filtered."""
    from auth.database import SessionLocal as _SL
    from auth.rule_models import ClassificationRule, ClassificationCategory
    _db = _SL()
    try:
        from sqlalchemy.orm import joinedload

        q = _db.query(ClassificationRule).options(
            joinedload(ClassificationRule.category).joinedload(
                ClassificationCategory.group
            )
        )
        if current_user.role != "admin" or not include_inactive:
            q = q.filter(ClassificationRule.is_active == True)
        if category_id:
            q = q.filter(ClassificationRule.category_id == category_id)
        if match_on:
            q = q.filter(ClassificationRule.match_on == match_on)
        rules = q.order_by(ClassificationRule.priority.desc(), ClassificationRule.name).all()
        
        learned_rules_list = []
        learned_path = "/home/luv/Desktop/t_1/data/logs/learned_rules.json"
        
        cats = _db.query(ClassificationCategory).filter(ClassificationCategory.is_active == True).all()
        cat_name_to_id = {c.name.lower().strip(): c.id for c in cats}
        
        if os.path.exists(learned_path):
            try:
                with open(learned_path, "r", encoding="utf-8") as f:
                    data = json.load(f)
                    for kw, entry in data.items():
                        cat_name = entry.get("category", "")
                        cat_id = cat_name_to_id.get(cat_name.lower().strip())

                        if category_id and cat_id != category_id:
                            continue
                        if match_on and match_on != "merchant":
                            continue

                        learned_rules_list.append({
                            "id": f"learned_rule_{kw}",
                            "name": f"Auto-Learned: {kw}",
                            "category_id": cat_id,
                            "category": cat_name,
                            "keywords": [kw],
                            "priority": 50,
                            "match_on": "merchant",
                            "tx_type_filter": None,
                            "amount_range": None,
                            "is_active": True,
                            "created_at": entry.get("learned_at"),
                            "updated_at": None,
                            "created_by": f"RuleEngine ({entry.get('source', 'unknown')})",
                            "updated_by": None,
                            "is_learned": True,
                        })
            except Exception as e:
                logger.warning("Failed to load learned rules: %s", e)

        try:
            from auth.rule_models import LearnedRuleLog
            db_logs = _db.query(LearnedRuleLog).options(
                joinedload(LearnedRuleLog.category_obj).joinedload(ClassificationCategory.group)
            ).filter(LearnedRuleLog.status == 'pending').all()
            for log in db_logs:
                cat_id = log.category_id or cat_name_to_id.get((log.category or '').lower().strip())
                if category_id and cat_id != category_id:
                    continue
                learned_rules_list.append({
                    "id": f"learned_log_{log.id}",
                    "name": f"Auto-Learned: {log.keyword}",
                    "category_id": cat_id,
                    "category": log.category_name,
                    "group_name": log.group_name,
                    "keywords": [log.keyword],
                    "priority": 50,
                    "match_on": "merchant",
                    "tx_type_filter": None,
                    "amount_range": None,
                    "is_active": False,
                    "created_at": log.created_at.isoformat() if log.created_at else None,
                    "updated_at": log.updated_at.isoformat() if log.updated_at else None,
                    "created_by": f"RuleEngine ({log.source})",
                    "updated_by": log.updated_by,
                    "is_learned": True,
                    "hit_count": log.hit_count,
                    "confidence": log.confidence,
                    "source": log.source,
                    "example_description": log.example_description,
                    "example_descriptions": [
                        ex for ex in (log.example_description or "").splitlines() if ex.strip()
                    ],
                })
        except Exception:
            pass

        items = [r.to_dict() for r in rules] + learned_rules_list
        return {"items": items, "total": len(items)}
    finally:
        _db.close()


@router.post("")
async def create_rule(
    payload: RuleCreateRequest,
    current_user: User = Depends(get_current_user),
):
    """Create a new classification rule. Admin only."""
    if current_user.role != "admin":
        raise HTTPException(status_code=403, detail="Admin access required")

    from auth.database import SessionLocal as _SL
    from auth.rule_models import ClassificationRule, ClassificationCategory
    _db = _SL()
    try:
        cat = _db.query(ClassificationCategory).filter_by(id=payload.category_id).first()
        if not cat:
            raise HTTPException(status_code=404, detail="Category not found")

        rule = ClassificationRule(
            name=payload.name,
            category_id=payload.category_id,
            priority=payload.priority,
            match_on=payload.match_on,
            tx_type_filter=payload.tx_type_filter,
            created_by=current_user.username,
            updated_by=current_user.username,
        )
        rule.keywords = payload.keywords
        if payload.amount_range and len(payload.amount_range) == 2:
            rule.amount_min = payload.amount_range[0]
            rule.amount_max = payload.amount_range[1]

        _db.add(rule)
        _db.commit()
        _db.refresh(rule)
        reload_pipeline_rules()
        return rule.to_dict()
    except HTTPException:
        raise
    except Exception as e:
        _db.rollback()
        raise HTTPException(status_code=500, detail=str(e))
    finally:
        _db.close()


@router.patch("/{rule_id}")
async def update_rule(
    rule_id: str,
    payload: RuleUpdateRequest,
    current_user: User = Depends(get_current_user),
):
    """Update a rule's keywords, priority, category, etc. Admin only."""
    if current_user.role != "admin":
        raise HTTPException(status_code=403, detail="Admin access required")

    if rule_id.startswith("learned_rule_"):
        from auth.database import SessionLocal as _SL
        from auth.rule_models import ClassificationRule, ClassificationCategory
        _db = _SL()
        try:
            cat_id = payload.category_id
            if not cat_id:
                raise HTTPException(status_code=400, detail="category_id is required to promote rule")
            
            cat = _db.query(ClassificationCategory).filter_by(id=cat_id).first()
            if not cat:
                raise HTTPException(status_code=404, detail="Category not found")
            
            keyword = rule_id.replace("learned_rule_", "")
            
            rule = ClassificationRule(
                name=payload.name or f"Promoted: {keyword}",
                category_id=cat_id,
                priority=payload.priority if payload.priority is not None else 50,
                match_on=payload.match_on or "merchant",
                tx_type_filter=payload.tx_type_filter if payload.tx_type_filter != "" else None,
                created_by=current_user.username,
                updated_by=current_user.username,
            )
            rule.keywords = payload.keywords if payload.keywords is not None else [keyword]
            if payload.amount_range and len(payload.amount_range) == 2:
                rule.amount_min, rule.amount_max = payload.amount_range
                
            _db.add(rule)
            _db.commit()
            _db.refresh(rule)
            
            learned_path = "/home/luv/Desktop/t_1/data/logs/learned_rules.json"
            if os.path.exists(learned_path):
                try:
                    with open(learned_path, "r", encoding="utf-8") as f:
                        data = json.load(f)
                    if keyword in data:
                        del data[keyword]
                        with open(learned_path, "w", encoding="utf-8") as f:
                            json.dump(data, f, indent=4)
                except Exception as e:
                    logger.warning("Failed to remove promoted rule from JSON: %s", e)
                    
            reload_pipeline_rules()
            return rule.to_dict()
        except HTTPException:
            raise
        except Exception as e:
            _db.rollback()
            raise HTTPException(status_code=500, detail=str(e))
        finally:
            _db.close()

    if rule_id.startswith("learned_log_"):
        from auth.database import SessionLocal as _SL
        from auth.rule_models import ClassificationRule, ClassificationCategory, LearnedRuleLog
        _db = _SL()
        try:
            log_id = rule_id.replace("learned_log_", "")
            log_entry = _db.query(LearnedRuleLog).filter_by(id=log_id).first()
            if not log_entry:
                raise HTTPException(status_code=404, detail="Learned log entry not found")

            cat_id = payload.category_id or log_entry.category_id
            if not cat_id:
                raise HTTPException(status_code=400, detail="category_id is required to promote rule")

            cat = _db.query(ClassificationCategory).filter_by(id=cat_id).first()
            if not cat:
                raise HTTPException(status_code=404, detail="Category not found")

            keyword = log_entry.keyword

            rule = ClassificationRule(
                name=payload.name or f"Promoted: {keyword}",
                category_id=cat_id,
                priority=payload.priority if payload.priority is not None else 50,
                match_on=payload.match_on or "merchant",
                tx_type_filter=payload.tx_type_filter if payload.tx_type_filter != "" else None,
                created_by=current_user.username,
                updated_by=current_user.username,
            )
            rule.keywords = payload.keywords if payload.keywords is not None else [keyword]
            if payload.amount_range and len(payload.amount_range) == 2:
                rule.amount_min, rule.amount_max = payload.amount_range

            _db.add(rule)
            _db.commit()
            _db.refresh(rule)

            log_entry.status = 'approved'
            log_entry.promoted_rule_id = rule.id
            log_entry.updated_by = current_user.username
            _db.commit()

            reload_pipeline_rules()
            return rule.to_dict()
        except HTTPException:
            raise
        except Exception as e:
            _db.rollback()
            raise HTTPException(status_code=500, detail=str(e))
        finally:
            _db.close()

    from auth.database import SessionLocal as _SL
    from auth.rule_models import ClassificationRule, ClassificationCategory
    _db = _SL()
    try:
        rule = _db.query(ClassificationRule).filter_by(id=rule_id).first()
        if not rule:
            raise HTTPException(status_code=404, detail="Rule not found")

        if payload.name is not None:
            rule.name = payload.name
        if payload.category_id is not None:
            cat = _db.query(ClassificationCategory).filter_by(id=payload.category_id).first()
            if not cat:
                raise HTTPException(status_code=404, detail="Target category not found")
            rule.category_id = payload.category_id
        if payload.keywords is not None:
            rule.keywords = payload.keywords
        if payload.priority is not None:
            rule.priority = payload.priority
        if payload.match_on is not None:
            rule.match_on = payload.match_on
        if payload.tx_type_filter is not None:
            rule.tx_type_filter = payload.tx_type_filter if payload.tx_type_filter != "" else None
        if payload.amount_range is not None:
            if len(payload.amount_range) == 2:
                rule.amount_min, rule.amount_max = payload.amount_range
            else:
                rule.amount_min = rule.amount_max = None
        if payload.is_active is not None:
            rule.is_active = payload.is_active

        rule.updated_by = current_user.username
        _db.commit()
        _db.refresh(rule)
        reload_pipeline_rules()
        return rule.to_dict()
    except HTTPException:
        raise
    except Exception as e:
        _db.rollback()
        raise HTTPException(status_code=500, detail=str(e))
    finally:
        _db.close()


@router.delete("/{rule_id}")
async def delete_rule(
    rule_id: str,
    current_user: User = Depends(get_current_user),
):
    """Hard-delete a rule. Admin only."""
    if current_user.role != "admin":
        raise HTTPException(status_code=403, detail="Admin access required")

    if rule_id.startswith("learned_log_"):
        from auth.database import SessionLocal as _SL
        from auth.rule_models import LearnedRuleLog
        _db = _SL()
        try:
            log_id = rule_id.replace("learned_log_", "")
            entry = _db.query(LearnedRuleLog).filter_by(id=log_id).first()
            if not entry:
                raise HTTPException(status_code=404, detail="Learned log entry not found")
            entry.status = 'rejected'
            entry.updated_by = current_user.username
            _db.commit()
            return {"ok": True}
        except HTTPException:
            raise
        except Exception as e:
            _db.rollback()
            raise HTTPException(status_code=500, detail=str(e))
        finally:
            _db.close()

    if rule_id.startswith("learned_rule_"):
        keyword = rule_id.replace("learned_rule_", "")
        learned_path = "/home/luv/Desktop/t_1/data/logs/learned_rules.json"
        if os.path.exists(learned_path):
            try:
                with open(learned_path, "r", encoding="utf-8") as f:
                    data = json.load(f)
                if keyword in data:
                    del data[keyword]
                    with open(learned_path, "w", encoding="utf-8") as f:
                        json.dump(data, f, indent=4)
                    reload_pipeline_rules()
                    return {"ok": True}
            except Exception as e:
                raise HTTPException(status_code=500, detail=f"Failed to delete learned rule: {e}")
        raise HTTPException(status_code=404, detail="Learned rule not found")

    from auth.database import SessionLocal as _SL
    from auth.rule_models import ClassificationRule
    _db = _SL()
    try:
        rule = _db.query(ClassificationRule).filter_by(id=rule_id).first()
        if not rule:
            raise HTTPException(status_code=404, detail="Rule not found")
        _db.delete(rule)
        _db.commit()
        reload_pipeline_rules()
        return {"ok": True}
    except HTTPException:
        raise
    except Exception as e:
        _db.rollback()
        raise HTTPException(status_code=500, detail=str(e))
    finally:
        _db.close()