added get_organization_dashboard

This commit is contained in:
Beverly Nguyen
2025-10-24 12:47:43 -07:00
parent 6115e0f778
commit bc08521d83

View File

@@ -1,4 +1,5 @@
import json
import uuid
from datetime import datetime
from zoneinfo import ZoneInfo
@@ -10,7 +11,11 @@ from app.config import QueueNames
from app.dao.annual_billing_dao import set_default_free_allowance_for_service
from app.dao.dao_utils import transaction
from app.dao.fact_billing_dao import fetch_usage_year_for_organization
from app.dao.notifications_dao import dao_get_notification_counts_for_organization
from app.dao.notifications_dao import (
dao_get_notification_counts_per_service,
dao_get_recent_sms_template_per_service,
)
from app.dao.services_dao import dao_get_service_primary_contacts
from app.dao.organization_dao import (
dao_add_service_to_organization,
dao_add_user_to_organization,
@@ -151,11 +156,8 @@ def get_organization_services_usage(organization_id):
year = int(request.args.get("year", "none"))
except ValueError:
return jsonify(result="error", message="No valid year provided"), 400
include_all = request.args.get("include_all_services", "false").lower() == "true"
services = fetch_usage_year_for_organization(
organization_id, year, include_all_services=include_all
)
services = fetch_usage_year_for_organization(organization_id, year)
list_services = services.values()
sorted_services = sorted(
list_services, key=lambda s: (-s["active"], s["service_name"].lower())
@@ -163,6 +165,41 @@ def get_organization_services_usage(organization_id):
return jsonify(services=sorted_services)
@organization_blueprint.route("/<uuid:organization_id>/dashboard", methods=["GET"])
def get_organization_dashboard(organization_id):
check_suspicious_id(organization_id)
try:
year = int(request.args.get("year", "none"))
except ValueError:
return jsonify(result="error", message="No valid year provided"), 400
services_with_usage = fetch_usage_year_for_organization(
organization_id, year, include_all_services=True
)
service_ids = [uuid.UUID(service_id) for service_id in services_with_usage.keys()]
if not service_ids:
return jsonify(services=[]), 200
recent_templates = dao_get_recent_sms_template_per_service(service_ids)
primary_contacts = dao_get_service_primary_contacts(service_ids)
for service_id_str, service_data in services_with_usage.items():
service_uuid = uuid.UUID(service_id_str)
service_data["recent_sms_template_name"] = recent_templates.get(service_uuid)
service_data["primary_contact"] = primary_contacts.get(service_uuid)
services_list = list(services_with_usage.values())
sorted_services = sorted(
services_list, key=lambda s: (-s["active"], s["service_name"].lower())
)
return jsonify(services=sorted_services)
@organization_blueprint.route(
"/<uuid:organization_id>/users/<uuid:user_id>", methods=["POST"]
)
@@ -294,7 +331,7 @@ def get_organization_message_allowance(organization_id):
current_year = datetime.now(tz=ZoneInfo("UTC")).year
service_ids = [service.id for service in services]
messages_by_service = dao_get_notification_counts_for_organization(
messages_by_service = dao_get_notification_counts_per_service(
service_ids, current_year
)