Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/api/routers/breeze_buddy/templates/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
DeleteTemplateResponse,
TemplateListResponse,
)
from app.services.redis.client import get_redis_service

from .rbac import apply_hierarchical_template_filters, validate_template_access

Expand Down Expand Up @@ -83,7 +84,7 @@
raise ValueError("nodes must be specified in flow structure")

# Check if template already exists
existing = await get_template_by_merchant(

Check warning on line 87 in app/api/routers/breeze_buddy/templates/handlers.py

View workflow job for this annotation

GitHub Actions / build

Pyrefly deprecated

`app.database.accessor.breeze_buddy.template.get_template_by_merchant` is deprecated Name-based template lookup. Pass template_id and use get_template_by_id_with_fallback() instead.
template_data.reseller_id,
template_data.merchant_id,
template_data.name,
Expand Down Expand Up @@ -198,7 +199,7 @@
)

try:
template = await get_template_by_merchant(

Check warning on line 202 in app/api/routers/breeze_buddy/templates/handlers.py

View workflow job for this annotation

GitHub Actions / build

Pyrefly deprecated

`app.database.accessor.breeze_buddy.template.get_template_by_merchant` is deprecated Name-based template lookup. Pass template_id and use get_template_by_id_with_fallback() instead.
reseller_id=reseller_id,
merchant_id=merchant_id,
name=name,
Expand Down Expand Up @@ -491,6 +492,9 @@
# self-correct on TTL expiry.
try:
await invalidate_template(template_id)
redis = await get_redis_service()
await redis.delete(f"greeting:template:{template_id}")
logger.info(f"Greeting cache busted for template: {template_id}")
Comment on lines +495 to +497
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win

Extract duplicate cache invalidation logic into a helper function.

The same 3-line greeting cache invalidation block appears in both replace_template_handler and delete_template_handler. This violates the DRY principle and makes the code harder to maintain.

♻️ Proposed refactor to eliminate duplication

Add a helper function at the module level:

async def invalidate_greeting_cache(template_id: str) -> None:
    """
    Invalidate greeting audio cache for a template.
    
    Args:
        template_id: Template UUID
        
    Raises:
        Exception: Any Redis-related errors (caller should handle)
    """
    redis = await get_redis_service()
    await redis.delete(f"greeting:template:{template_id}")
    logger.info(f"Greeting cache busted for template: {template_id}")

Then in both handlers, replace the duplicate blocks with:

         try:
             await invalidate_template(template_id)
-            redis = await get_redis_service()
-            await redis.delete(f"greeting:template:{template_id}")
-            logger.info(f"Greeting cache busted for template: {template_id}")
+            await invalidate_greeting_cache(template_id)
         except Exception as cache_exc:

Also applies to: 589-591

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@app/api/routers/breeze_buddy/templates/handlers.py` around lines 495 - 497,
The three-line Redis cache invalidation repeated in replace_template_handler and
delete_template_handler should be extracted into a module-level async helper
named e.g. invalidate_greeting_cache(template_id: str) that calls
get_redis_service(), performs redis.delete(f"greeting:template:{template_id}")
and logs via logger.info; replace the duplicated blocks in both
replace_template_handler and delete_template_handler with a call to
invalidate_greeting_cache(template_id) so all Redis cache logic is centralized
and maintainable.

Comment on lines +495 to +497
except Exception as cache_exc:
logger.warning(
f"Template cache invalidation failed for {template_id}: {cache_exc}"
Expand Down Expand Up @@ -582,6 +586,9 @@

try:
await invalidate_template(template_id)
redis = await get_redis_service()
await redis.delete(f"greeting:template:{template_id}")
logger.info(f"Greeting cache busted for template: {template_id}")
Comment on lines +589 to +591
except Exception as cache_exc:
logger.warning(
f"Template cache invalidation failed for {template_id}: {cache_exc}"
Expand Down
Loading