Skip to main content

Current User

info

current_user is a Flask-JWT-Extended proxy that holds the fully resolved industry and user context for the authenticated request. It is populated once per request by @CachedData.jwt.user_lookup_loader in app/__init__.py and is available anywhere via flask_jwt_extended.current_user.

How It Is Populated

On every protected request:

  1. JWT token is decoded — sub (industryId) and userId are extracted from the payload
  2. If the token carries targetIndustryId and the caller is an internal or company industry, the target industry is loaded instead
  3. DatabaseSupporter.get_industry_details_by_id() fetches the raw industry document from Cosmos DB
  4. IndustryDataFormatter().formatIndustryData() transforms the raw document into the current_user shape
  5. userId, permissions, alertsConfig, and leakageEnabled are merged in
  6. The result is returned and bound to current_user for the duration of the request

Structure

current_user = {
# Identity
'industryId': 'IND001',
'industryName': 'Acme Water Authority',
'userId': 'IND001_john',
'permissions': ['report', 'alerts'],

# Units — only deployed units (isDeployed: true), keyed by unitId
# Alert thresholds from alertsConfig are merged into each unit entry
'unitsMapping': {
'UNIT001': {
'unitId': 'UNIT001',
'unitName': 'Main Meter',
'standardCategoryId': 'flow',
'isDeployed': True,
'unitThreshold': 5000,
'monthlyThreshold': 150000,
'alertEnabled': True,
# ... other unit fields
}
},

# Categories — only enabled categories, keyed by categoryId
'categories': { ... },
'categoriesReadOnly': { ... }, # same as categories, unmodified copy
'categoriesV2': { ... }, # from industry.categoriesV2
'standardCategories': { ... }, # from industry.standardCategoriesView

# Full raw industry document from Cosmos DB
'industryData': { ... },

# Shift-aware date helpers (calculated at request time)
'todayDate': '09-11-2024', # "today" per industry shift, DD-MM-YYYY
'daysOffset': 0, # 1 if shift hasn't started yet today, else 0
'nowUTCWithShiftDateTime': datetime(...), # shift start time in UTC for today
'utcShiftTime': datetime(...),
'localShiftTime': datetime(...),

# Alerts
'alertsConfig': { ... }, # raw alerts config doc for this industry
'leakageEnabled': False, # True if any unit has stable_flow alert enabled
}

Key Fields Explained

unitsMapping

A dict of all deployed units (isDeployed: True) keyed by unitId. Non-deployed units are excluded. Alert thresholds (unitThreshold, monthlyThreshold, maxFlowRate, minFlowRate, hourlyThreshold, maxCapacity, lowThreshold, highThreshold) from alertsConfig are merged directly into each unit entry, so services can read thresholds from current_user['unitsMapping']['UNIT001']['unitThreshold'] without an extra DB call.

categories / categoriesV2 / standardCategories

  • categories — industry's primary category configuration (disabled categories are filtered out)
  • categoriesV2 — from industry.categoriesV2, same filter applied
  • standardCategories — from industry.standardCategoriesView, used for water neutrality and similar cross-category features

daysOffset and todayDate

info

Industries configure a shift start time (e.g. 06:00:00). If the current UTC time is before today's shift start, daysOffset is 1 (meaning "today" for this industry is still yesterday). todayDate reflects this — it is the date of the most recently completed shift, not necessarily the calendar date.

Services use daysOffset to adjust date range queries so they align with the industry's shift boundary rather than midnight UTC.

targetIndustryId override

note

If the request includes a targetIndustryId header and the JWT's sub is the internal industry ID (Config.INTERNAL_INDUSTRY_ID) or the company industry ID (Config.COMPANY_INDUSTRY_ID), current_user is populated with the target industry's data instead. This is how admin routes act on behalf of other industries without requiring a separate token.

permissions

List of permission strings from the JWT payload. Services check this to gate access to specific features.

leakageEnabled

True if any unit in the industry's alertsConfig has alertEnabled.stable_flow: true. Used to conditionally show leakage detection UI.

Usage in Services

danger

The industryId from current_user is always used as the authoritative source — never from a request parameter — to prevent cross-industry data access.

Services never query the database for industry or user data that is already in current_user:

from flask_jwt_extended import current_user

industry_id = current_user['industryId']
units = current_user['unitsMapping']
today = current_user['todayDate']
days_offset = current_user['daysOffset']
alerts_config = current_user['alertsConfig']