# Add or ensure these imports are present from fastapi import BackgroundTasks from .service import PortfolioService # Ensure service is imported from App.routers.tasks.models import ImportTask from tortoise.expressions import Q # For querying JSON fields from datetime import date async def trigger_regeneration_if_needed( background_tasks: BackgroundTasks, portfolio_id: int, transaction_date: date, reason: str, ): """Checks if a transaction is back-dated and queues the regeneration task.""" if transaction_date < date.today(): task = await ImportTask.create( task_type="portfolio_regeneration", status="pending", details={ "portfolio_id": portfolio_id, "reason": reason, "start_date": transaction_date.isoformat(), }, ) background_tasks.add_task( PortfolioService.regenerate_snapshots_task, task.id, portfolio_id, transaction_date, ) return "Holding saved. Historical performance data is being updated in the background." return "Holding saved successfully."