Mar 04 2026: Implement customer's view functionalities - Phase 3
This commit is contained in:
+58
-8
@@ -6,6 +6,7 @@ from app.models.facility import Facility
|
||||
from app.models.issue import Issue
|
||||
from app.models.user import User
|
||||
from app.utils.sla import sla_status, SLA_HOURS
|
||||
from app.utils.scope import get_customer_scope
|
||||
from sqlalchemy import func
|
||||
from datetime import datetime, timedelta
|
||||
from app.utils.time_utils import now_eastern
|
||||
@@ -22,13 +23,23 @@ def index():
|
||||
today_end = today_start + timedelta(days=1)
|
||||
thirty_days_ago = now - timedelta(days=30)
|
||||
|
||||
is_inspector = current_user.role == 'inspector'
|
||||
is_privileged = current_user.role in ['admin', 'supervisor']
|
||||
is_inspector = current_user.role == 'inspector'
|
||||
is_privileged = current_user.role in ['admin', 'supervisor']
|
||||
is_customer = current_user.role == 'customer'
|
||||
is_project_manager = current_user.role == 'project_manager'
|
||||
|
||||
# Resolve facility scope for customer users
|
||||
customer_facility_ids = get_customer_scope(current_user) # None for non-customers
|
||||
|
||||
# ── Today's stats ─────────────────────────────────────────────────────
|
||||
base_q = Inspection.query
|
||||
if is_inspector:
|
||||
base_q = base_q.filter(Inspection.inspector_id == current_user.id)
|
||||
elif is_customer:
|
||||
if not customer_facility_ids:
|
||||
base_q = base_q.filter(False) # no access
|
||||
else:
|
||||
base_q = base_q.filter(Inspection.facility_id.in_(customer_facility_ids))
|
||||
|
||||
today_inspections = base_q.filter(
|
||||
Inspection.inspection_date >= today_start,
|
||||
@@ -47,6 +58,14 @@ def index():
|
||||
open_issues_q = open_issues_q.join(
|
||||
Inspection, Issue.inspection_id == Inspection.id
|
||||
).filter(Inspection.inspector_id == current_user.id)
|
||||
elif is_customer:
|
||||
if not customer_facility_ids:
|
||||
open_issues_q = open_issues_q.filter(False)
|
||||
else:
|
||||
from app.models.facility import Area
|
||||
open_issues_q = open_issues_q.join(
|
||||
Area, Issue.area_id == Area.id
|
||||
).filter(Area.facility_id.in_(customer_facility_ids))
|
||||
open_issues = open_issues_q.count()
|
||||
|
||||
# ── Average score (last 30 days) ───────────────────────────────────────
|
||||
@@ -57,12 +76,22 @@ def index():
|
||||
)
|
||||
if is_inspector:
|
||||
score_q = score_q.filter(Inspection.inspector_id == current_user.id)
|
||||
elif is_customer:
|
||||
if customer_facility_ids:
|
||||
score_q = score_q.filter(Inspection.facility_id.in_(customer_facility_ids))
|
||||
else:
|
||||
score_q = score_q.filter(False)
|
||||
avg_score = score_q.scalar()
|
||||
|
||||
# ── Recent inspections ─────────────────────────────────────────────────
|
||||
recent_q = Inspection.query.order_by(Inspection.inspection_date.desc())
|
||||
if is_inspector:
|
||||
recent_q = recent_q.filter(Inspection.inspector_id == current_user.id)
|
||||
elif is_customer:
|
||||
if customer_facility_ids:
|
||||
recent_q = recent_q.filter(Inspection.facility_id.in_(customer_facility_ids))
|
||||
else:
|
||||
recent_q = recent_q.filter(False)
|
||||
recent_inspections = recent_q.limit(5).all()
|
||||
|
||||
# ── System stats (admin/supervisor) ───────────────────────────────────
|
||||
@@ -70,13 +99,27 @@ def index():
|
||||
total_templates = InspectionTemplate.query.count() if is_privileged else 0
|
||||
total_users = User.query.count() if current_user.role == 'admin' else 0
|
||||
|
||||
# ── Customer: scoped facilities summary ───────────────────────────────
|
||||
customer_facilities = []
|
||||
if is_customer and customer_facility_ids:
|
||||
customer_facilities = Facility.query.filter(
|
||||
Facility.id.in_(customer_facility_ids),
|
||||
Facility.active == True,
|
||||
).order_by(Facility.name).all()
|
||||
|
||||
# ── SLA summary (open + in_progress issues only) ──────────────────────
|
||||
all_open_issues = Issue.query.filter(Issue.status.in_(['open', 'in_progress'])).all()
|
||||
sla_q = Issue.query.filter(Issue.status.in_(['open', 'in_progress']))
|
||||
if is_customer and customer_facility_ids:
|
||||
from app.models.facility import Area
|
||||
sla_q = sla_q.join(Area, Issue.area_id == Area.id).filter(
|
||||
Area.facility_id.in_(customer_facility_ids)
|
||||
)
|
||||
all_open_issues = sla_q.all() if not is_customer or customer_facility_ids else []
|
||||
sla_breached = sum(1 for i in all_open_issues if sla_status(i) == 'breached')
|
||||
sla_at_risk = sum(1 for i in all_open_issues if sla_status(i) == 'at_risk')
|
||||
|
||||
# ── Score trend (last 30 days, grouped by day) ────────────────────────
|
||||
trend_rows = (
|
||||
trend_q = (
|
||||
db.session.query(
|
||||
func.date(Inspection.inspection_date).label('day'),
|
||||
func.avg(Inspection.overall_score).label('avg'),
|
||||
@@ -86,16 +129,22 @@ def index():
|
||||
Inspection.overall_score.isnot(None),
|
||||
Inspection.inspection_date >= thirty_days_ago,
|
||||
)
|
||||
.group_by(func.date(Inspection.inspection_date))
|
||||
.order_by(func.date(Inspection.inspection_date))
|
||||
.all()
|
||||
)
|
||||
if is_inspector:
|
||||
trend_q = trend_q.filter(Inspection.inspector_id == current_user.id)
|
||||
elif is_customer:
|
||||
if customer_facility_ids:
|
||||
trend_q = trend_q.filter(Inspection.facility_id.in_(customer_facility_ids))
|
||||
else:
|
||||
trend_q = trend_q.filter(False)
|
||||
trend_rows = trend_q.group_by(func.date(Inspection.inspection_date))\
|
||||
.order_by(func.date(Inspection.inspection_date)).all()
|
||||
trend_labels = [str(r.day) for r in trend_rows]
|
||||
trend_data = [round(float(r.avg), 2) for r in trend_rows]
|
||||
|
||||
# ── Facility performance (last 30 days, privileged users only) ─────────
|
||||
facility_perf = []
|
||||
if is_privileged:
|
||||
if is_privileged or is_project_manager:
|
||||
perf_rows = (
|
||||
db.session.query(
|
||||
Facility.name,
|
||||
@@ -133,4 +182,5 @@ def index():
|
||||
trend_labels = trend_labels,
|
||||
trend_data = trend_data,
|
||||
facility_perf = facility_perf,
|
||||
customer_facilities = customer_facilities,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user