1+ # path: policylens/apps/claims/queue.py
2+ """
3+ Queue building logic for ops review.
4+
5+ Ordering rules (Week 3):
6+ 1. SLA breached first
7+ 2. SLA due soon (within N hours) next
8+ 3. Higher priority first
9+ 4. Older claims first (created_at ascending)
10+
11+ Filters:
12+ - status (optional)
13+ - priority (optional)
14+ - sla: breached | due_soon | ok (optional)
15+ """
16+
17+ from __future__ import annotations
18+
19+ from datetime import timedelta
20+
21+ from django .db .models import Case , IntegerField , Q , Value , When
22+ from django .utils import timezone
23+
24+ from apps .claims .models import Claim
25+
26+
27+ DUE_SOON_WINDOW = timedelta (hours = 6 )
28+
29+ PRIORITY_WEIGHT = {
30+ Claim .Priority .HIGH : 3 ,
31+ Claim .Priority .NORMAL : 2 ,
32+ Claim .Priority .LOW : 1 ,
33+ }
34+
35+
36+ def _priority_weight_case () -> Case :
37+ """Return a DB CASE expression for priority weighting."""
38+ return Case (
39+ When (priority = Claim .Priority .HIGH , then = Value (PRIORITY_WEIGHT [Claim .Priority .HIGH ])),
40+ When (priority = Claim .Priority .NORMAL , then = Value (PRIORITY_WEIGHT [Claim .Priority .NORMAL ])),
41+ When (priority = Claim .Priority .LOW , then = Value (PRIORITY_WEIGHT [Claim .Priority .LOW ])),
42+ default = Value (PRIORITY_WEIGHT [Claim .Priority .NORMAL ]),
43+ output_field = IntegerField (),
44+ )
0 commit comments