File size: 1,179 Bytes
9d4bd7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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."