SurfSense/surfsense_backend/app/notifications/service/messages/insufficient_credits.py
CREDO23 7fb0707933 refactor(backend): rename search_space -> workspace across app bulk (Phase 2 Wave D)
Scoped codemod over surfsense_backend/app (excluding routes/, Wave E): renames
search_space_id -> workspace_id, search_space -> workspace, SearchSpace -> Workspace
across services, utils, tasks, agents, gateway, event_bus, notifications, podcasts,
automations, observability params, and prompt .md files. Also flips the camelCase
payload key searchSpaceId -> workspaceId (no backend reader; hard cutover).

Preserved carve-outs (verbatim): Celery task names "delete_search_space_background"
and "ai_sort_search_space" (wire names), and the OTel/metric key "search_space.id"
(dashboards depend on it). Enum values 'SEARCH_SPACE' and SearchSourceConnector
untouched.
2026-06-26 18:30:47 +02:00

30 lines
1.1 KiB
Python

"""Pure presentation logic for insufficient-credit notifications."""
from __future__ import annotations
import hashlib
from datetime import UTC, datetime
from app.notifications.service.messages.text import truncate
def operation_id(document_name: str, workspace_id: int) -> str:
"""Build a unique id for an insufficient-credits notification."""
timestamp = datetime.now(UTC).strftime("%Y%m%d_%H%M%S_%f")
doc_hash = hashlib.md5(document_name.encode()).hexdigest()[:8]
return f"insufficient_credits_{workspace_id}_{timestamp}_{doc_hash}"
def summary(
document_name: str, balance_micros: int, required_micros: int
) -> tuple[str, str]:
"""Compute the title and message for a blocked-by-insufficient-credits document."""
display_name = truncate(document_name, 40)
title = f"Insufficient credits: {display_name}"
balance_usd = max(0, balance_micros) / 1_000_000
required_usd = max(0, required_micros) / 1_000_000
message = (
f"This document costs about ${required_usd:.2f} to process but you have "
f"${balance_usd:.2f} of credit left. Add more credits to continue."
)
return title, message