Add user delete process

This commit is contained in:
Urtzi Alfaro
2025-10-31 11:54:19 +01:00
parent 63f5c6d512
commit 269d3b5032
74 changed files with 16783 additions and 213 deletions

View File

@@ -3,7 +3,7 @@
Recipe Operations API - Business operations and complex workflows
"""
from fastapi import APIRouter, Depends, HTTPException, Header, Query
from fastapi import APIRouter, Depends, HTTPException, Header, Query, Path
from sqlalchemy.ext.asyncio import AsyncSession
from uuid import UUID
import logging
@@ -219,3 +219,84 @@ async def get_recipe_count(
except Exception as e:
logger.error(f"Error getting recipe count: {e}")
raise HTTPException(status_code=500, detail="Internal server error")
# ============================================================================
# Tenant Data Deletion Operations (Internal Service Only)
# ============================================================================
from shared.auth.access_control import service_only_access
from app.services.tenant_deletion_service import RecipesTenantDeletionService
@router.delete(
route_builder.build_base_route("tenant/{tenant_id}", include_tenant_prefix=False),
response_model=dict
)
@service_only_access
async def delete_tenant_data(
tenant_id: str = Path(..., description="Tenant ID to delete data for"),
current_user: dict = Depends(get_current_user_dep),
db: AsyncSession = Depends(get_db)
):
"""
Delete all recipes data for a tenant (Internal service only)
"""
try:
logger.info("recipes.tenant_deletion.api_called", tenant_id=tenant_id)
deletion_service = RecipesTenantDeletionService(db)
result = await deletion_service.safe_delete_tenant_data(tenant_id)
if not result.success:
raise HTTPException(
status_code=500,
detail=f"Tenant data deletion failed: {', '.join(result.errors)}"
)
return {
"message": "Tenant data deletion completed successfully",
"summary": result.to_dict()
}
except HTTPException:
raise
except Exception as e:
logger.error("recipes.tenant_deletion.api_error", tenant_id=tenant_id, error=str(e), exc_info=True)
raise HTTPException(status_code=500, detail=f"Failed to delete tenant data: {str(e)}")
@router.get(
route_builder.build_base_route("tenant/{tenant_id}/deletion-preview", include_tenant_prefix=False),
response_model=dict
)
@service_only_access
async def preview_tenant_data_deletion(
tenant_id: str = Path(..., description="Tenant ID to preview deletion for"),
current_user: dict = Depends(get_current_user_dep),
db: AsyncSession = Depends(get_db)
):
"""
Preview what data would be deleted for a tenant (dry-run)
"""
try:
logger.info("recipes.tenant_deletion.preview_called", tenant_id=tenant_id)
deletion_service = RecipesTenantDeletionService(db)
result = await deletion_service.preview_deletion(tenant_id)
if not result.success:
raise HTTPException(
status_code=500,
detail=f"Tenant deletion preview failed: {', '.join(result.errors)}"
)
return {
"tenant_id": tenant_id,
"service": "recipes-service",
"data_counts": result.deleted_counts,
"total_items": sum(result.deleted_counts.values())
}
except HTTPException:
raise
except Exception as e:
logger.error("recipes.tenant_deletion.preview_error", tenant_id=tenant_id, error=str(e), exc_info=True)
raise HTTPException(status_code=500, detail=f"Failed to preview tenant data deletion: {str(e)}")

View File

@@ -390,3 +390,86 @@ async def get_recipe_deletion_summary(
except Exception as e:
logger.error(f"Error getting deletion summary: {e}")
raise HTTPException(status_code=500, detail="Internal server error")
# ===== Tenant Data Deletion Endpoints =====
@router.delete("/tenant/{tenant_id}")
async def delete_tenant_data(
tenant_id: str,
current_user: dict = Depends(get_current_user_dep),
db: AsyncSession = Depends(get_db)
):
"""
Delete all recipe-related data for a tenant
Only accessible by internal services (called during tenant deletion)
"""
logger.info(f"Tenant data deletion request received for tenant: {tenant_id}")
# Only allow internal service calls
if current_user.get("type") != "service":
raise HTTPException(
status_code=403,
detail="This endpoint is only accessible to internal services"
)
try:
from app.services.tenant_deletion_service import RecipesTenantDeletionService
deletion_service = RecipesTenantDeletionService(db)
result = await deletion_service.safe_delete_tenant_data(tenant_id)
return {
"message": "Tenant data deletion completed in recipes-service",
"summary": result.to_dict()
}
except Exception as e:
logger.error(f"Tenant data deletion failed for {tenant_id}: {e}")
raise HTTPException(
status_code=500,
detail=f"Failed to delete tenant data: {str(e)}"
)
@router.get("/tenant/{tenant_id}/deletion-preview")
async def preview_tenant_data_deletion(
tenant_id: str,
current_user: dict = Depends(get_current_user_dep),
db: AsyncSession = Depends(get_db)
):
"""
Preview what data would be deleted for a tenant (dry-run)
Accessible by internal services and tenant admins
"""
# Allow internal services and admins
is_service = current_user.get("type") == "service"
is_admin = current_user.get("role") in ["owner", "admin"]
if not (is_service or is_admin):
raise HTTPException(
status_code=403,
detail="Insufficient permissions"
)
try:
from app.services.tenant_deletion_service import RecipesTenantDeletionService
deletion_service = RecipesTenantDeletionService(db)
preview = await deletion_service.get_tenant_data_preview(tenant_id)
return {
"tenant_id": tenant_id,
"service": "recipes-service",
"data_counts": preview,
"total_items": sum(preview.values())
}
except Exception as e:
logger.error(f"Deletion preview failed for {tenant_id}: {e}")
raise HTTPException(
status_code=500,
detail=f"Failed to get deletion preview: {str(e)}"
)

View File

@@ -0,0 +1,134 @@
"""
Recipes Service - Tenant Data Deletion
Handles deletion of all recipe-related data for a tenant
"""
from typing import Dict
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, delete, func
import structlog
from shared.services.tenant_deletion import BaseTenantDataDeletionService, TenantDataDeletionResult
from app.models.recipes import Recipe, RecipeIngredient, ProductionBatch
logger = structlog.get_logger()
class RecipesTenantDeletionService(BaseTenantDataDeletionService):
"""Service for deleting all recipe-related data for a tenant"""
def __init__(self, db_session: AsyncSession):
super().__init__("recipes-service")
self.db = db_session
async def get_tenant_data_preview(self, tenant_id: str) -> Dict[str, int]:
"""Get counts of what would be deleted"""
try:
preview = {}
# Count recipes
recipe_count = await self.db.scalar(
select(func.count(Recipe.id)).where(Recipe.tenant_id == tenant_id)
)
preview["recipes"] = recipe_count or 0
# Count recipe ingredients (will be deleted via CASCADE)
ingredient_count = await self.db.scalar(
select(func.count(RecipeIngredient.id))
.where(RecipeIngredient.tenant_id == tenant_id)
)
preview["recipe_ingredients"] = ingredient_count or 0
# Count production batches (will be deleted via CASCADE)
batch_count = await self.db.scalar(
select(func.count(ProductionBatch.id))
.where(ProductionBatch.tenant_id == tenant_id)
)
preview["production_batches"] = batch_count or 0
return preview
except Exception as e:
logger.error("Error getting deletion preview",
tenant_id=tenant_id,
error=str(e))
return {}
async def delete_tenant_data(self, tenant_id: str) -> TenantDataDeletionResult:
"""Delete all data for a tenant"""
result = TenantDataDeletionResult(tenant_id, self.service_name)
try:
# Get preview before deletion for reporting
preview = await self.get_tenant_data_preview(tenant_id)
# Delete production batches first (foreign key to recipes)
try:
batch_delete = await self.db.execute(
delete(ProductionBatch).where(ProductionBatch.tenant_id == tenant_id)
)
deleted_batches = batch_delete.rowcount
result.add_deleted_items("production_batches", deleted_batches)
logger.info("Deleted production batches for tenant",
tenant_id=tenant_id,
count=deleted_batches)
except Exception as e:
logger.error("Error deleting production batches",
tenant_id=tenant_id,
error=str(e))
result.add_error(f"Production batch deletion: {str(e)}")
# Delete recipe ingredients (foreign key to recipes)
try:
ingredient_delete = await self.db.execute(
delete(RecipeIngredient).where(RecipeIngredient.tenant_id == tenant_id)
)
deleted_ingredients = ingredient_delete.rowcount
result.add_deleted_items("recipe_ingredients", deleted_ingredients)
logger.info("Deleted recipe ingredients for tenant",
tenant_id=tenant_id,
count=deleted_ingredients)
except Exception as e:
logger.error("Error deleting recipe ingredients",
tenant_id=tenant_id,
error=str(e))
result.add_error(f"Recipe ingredient deletion: {str(e)}")
# Delete recipes (parent table)
try:
recipe_delete = await self.db.execute(
delete(Recipe).where(Recipe.tenant_id == tenant_id)
)
deleted_recipes = recipe_delete.rowcount
result.add_deleted_items("recipes", deleted_recipes)
logger.info("Deleted recipes for tenant",
tenant_id=tenant_id,
count=deleted_recipes)
except Exception as e:
logger.error("Error deleting recipes",
tenant_id=tenant_id,
error=str(e))
result.add_error(f"Recipe deletion: {str(e)}")
# Commit all deletions
await self.db.commit()
logger.info("Tenant data deletion completed",
tenant_id=tenant_id,
deleted_counts=result.deleted_counts)
except Exception as e:
logger.error("Fatal error during tenant data deletion",
tenant_id=tenant_id,
error=str(e))
await self.db.rollback()
result.add_error(f"Fatal error: {str(e)}")
return result