diff --git a/app/routes/reports.py b/app/routes/reports.py index c73e4e5..354c430 100644 --- a/app/routes/reports.py +++ b/app/routes/reports.py @@ -183,23 +183,24 @@ def index(): Issue.reported_at <= end, )).group_by(Issue.status).all() - # Top inspectors by inspection count — inspector sees only their own row - top_inspectors = [] - if current_user.role != 'customer': - top_insp_q = db.session.query( - User.username, - func.count(Inspection.id).label('count'), - func.avg(Inspection.overall_score).label('avg_score'), - ).join(Inspection, User.id == Inspection.inspector_id)\ - .filter( - Inspection.inspection_date >= start, - Inspection.inspection_date <= end, - Inspection.status == 'completed', - ) - if is_inspector: - top_insp_q = top_insp_q.filter(Inspection.inspector_id == current_user.id) - top_inspectors = top_insp_q.group_by(User.id, User.username)\ - .order_by(func.count(Inspection.id).desc()).limit(10).all() + # Top inspectors — scoped per role: + # customer → inspectors who worked at the customer's assigned facilities + # inspector → own row only + # others → org-wide top 10 (filtered by inspector_filter if set) + top_insp_q = db.session.query( + User.full_name, + User.username, + func.count(Inspection.id).label('count'), + func.avg(Inspection.overall_score).label('avg_score'), + ).join(Inspection, User.id == Inspection.inspector_id)\ + .filter( + Inspection.inspection_date >= start, + Inspection.inspection_date <= end, + Inspection.status == 'completed', + ) + top_insp_q = _scope_insp(top_insp_q) + top_inspectors = top_insp_q.group_by(User.id, User.full_name, User.username)\ + .order_by(func.count(Inspection.id).desc()).limit(10).all() # Recent issues (critical/high) — scoped for customers critical_issues = _scope_issue(Issue.query.filter( @@ -224,7 +225,7 @@ def index(): daily_scores=[{'day': str(r.day), 'avg': round(float(r.avg), 2), 'count': r.count} for r in daily_scores], issue_severity=[{'severity': r.severity, 'count': r.count} for r in issue_severity], issue_status=[{'status': r.status, 'count': r.count} for r in issue_status], - top_inspectors=[{'username': r.username, 'count': r.count, 'avg_score': round(float(r.avg_score), 2) if r.avg_score else None} for r in top_inspectors], + top_inspectors=[{'display_name': (r.full_name.strip() if r.full_name and r.full_name.strip() else r.username), 'count': r.count, 'avg_score': round(float(r.avg_score), 2) if r.avg_score else None} for r in top_inspectors], critical_issues=critical_issues, is_inspector=is_inspector, inspectors=inspectors, diff --git a/app/templates/reports/index.html b/app/templates/reports/index.html index e0b7208..eb775d9 100644 --- a/app/templates/reports/index.html +++ b/app/templates/reports/index.html @@ -126,7 +126,7 @@ {% for row in top_inspectors %} - {{ row.username }} + {{ row.display_name }} {{ row.count }} {% if row.avg_score %}