Skip to content

Startup

First-run seeding, startup variables and job cleanup served by the /erudi/startup routes.

src.domains.startup

Startup domain - Application startup state management.

This domain manages persistent UI state flags that survive across application restarts, such as welcome popup display status. Uses singleton pattern for the StartupVariables entity.

Modules:

Name Description
- endpoints

FastAPI routes for startup state management.

- repository

Data access layer for StartupVariables entity.

- schemas

Pydantic models for request/response validation.

Example

from src.domains.startup.repository import Startup_Variables_Repository from src.domains.startup.schemas import WelcomePopupResponse

In FastAPI endpoint

@router.get("/welcome-popup", response_model=WelcomePopupResponse) def check_popup(repo: Startup_Variables_Repository = Depends(get_startup_repository)): vars = repo.get_or_create() return WelcomePopupResponse(has_already_displayed=vars.welcome_popup_has_already_displayed)

src.domains.startup.endpoints

FastAPI endpoints for startup state management.

This module provides REST API endpoints for managing application startup state, including UI flags that persist across app restarts (e.g., welcome popup status).

Architecture
  • Endpoints use repository pattern via dependency injection.
  • Pydantic schemas validate all request/response data.
  • Structured logging for traceability.
  • Proper error handling with domain-specific exceptions.
Endpoints

GET /startup/welcome-popup - Check and update welcome popup display status.

Example

curl http://127.0.0.1:27182/erudi/startup/welcome-popup {"has_already_displayed": false}

get_startup_repository(db=Depends(get_db))

FastAPI dependency injection factory for Startup_Variables_Repository.

Parameters:

Name Type Description Default
db Session

SQLAlchemy session (injected by FastAPI via Depends(get_db)).

Depends(get_db)

Returns:

Name Type Description
Startup_Variables_Repository Startup_Variables_Repository

Configured repository instance.

Source code in backend/src/domains/startup/endpoints.py
def get_startup_repository(db: Session = Depends(get_db)) -> Startup_Variables_Repository:
    """FastAPI dependency injection factory for Startup_Variables_Repository.

    Args:
        db: SQLAlchemy session (injected by FastAPI via Depends(get_db)).

    Returns:
        Startup_Variables_Repository: Configured repository instance.
    """
    return Startup_Variables_Repository(db)

get_welcome_popup_status(startup_repo=Depends(get_startup_repository), db=Depends(get_db)) async

Check welcome popup display status and mark as shown if first time.

Implements "check-and-set" logic: returns False on first call (popup should show), then sets flag to True. Subsequent calls return True (popup already shown).

Parameters:

Name Type Description Default
startup_repo Startup_Variables_Repository

Injected startup variables repository.

Depends(get_startup_repository)
db Session

Database session for transaction control.

Depends(get_db)

Returns:

Name Type Description
WelcomePopupResponse

{"has_already_displayed": bool}

Raises:

Type Description
DatabaseException

If database operation fails.

Example

First call: GET /startup/welcome-popup → {"has_already_displayed": false} Second call: GET /startup/welcome-popup → {"has_already_displayed": true}

Source code in backend/src/domains/startup/endpoints.py
@router.get("/welcome-popup", response_model=WelcomePopupResponse)
async def get_welcome_popup_status(
    startup_repo: Startup_Variables_Repository = Depends(get_startup_repository),
    db: Session = Depends(get_db),
):
    """Check welcome popup display status and mark as shown if first time.

    Implements "check-and-set" logic: returns False on first call (popup should show),
    then sets flag to True. Subsequent calls return True (popup already shown).

    Args:
        startup_repo: Injected startup variables repository.
        db: Database session for transaction control.

    Returns:
        WelcomePopupResponse: {"has_already_displayed": bool}

    Raises:
        DatabaseException: If database operation fails.

    Example:
        First call:  GET /startup/welcome-popup → {"has_already_displayed": false}
        Second call: GET /startup/welcome-popup → {"has_already_displayed": true}
    """
    try:
        # Get or create singleton startup variables
        vars = startup_repo.get_or_create()

        # Check current status
        already_displayed = vars.welcome_popup_has_already_displayed

        # If not displayed yet, mark as displayed now
        if not already_displayed:
            startup_repo.mark_welcome_popup_displayed(vars)
            db.commit()
            logger.info("Welcome popup marked as displayed (first time)")
            return WelcomePopupResponse(has_already_displayed=False)

        # Already displayed before
        logger.debug("Welcome popup status: already displayed")
        return WelcomePopupResponse(has_already_displayed=True)

    except Exception as e:
        db.rollback()
        raise DatabaseException("Failed to get welcome popup status", trace=str(e))

src.domains.startup.repository

Data access layer for StartupVariables entity.

This module provides the repository class following the Repository pattern for database operations on startup state variables. The repository encapsulates all SQLAlchemy queries and database interactions for the singleton StartupVariables entity.

Architecture
  • Startup_Variables_Repository: Singleton operations for startup state management.
Repository Pattern Benefits
  • Single source of truth for data access logic.
  • Easy to mock for testing.
  • Clear separation between business logic (services) and data access.
  • Consistent error handling and logging.
Example

from src.domains.startup.repository import Startup_Variables_Repository

In endpoint or service

startup_repo = Startup_Variables_Repository(db) vars = startup_repo.get_or_create()

Startup_Variables_Repository(db)

Repository for StartupVariables entity database operations.

Handles all operations for the singleton StartupVariables entity, which stores persistent UI state flags across app restarts (e.g., welcome popup display status).

Attributes:

Name Type Description
db

SQLAlchemy database session (injected by FastAPI).

Example

startup_repo = Startup_Variables_Repository(db) vars = startup_repo.get_or_create() startup_repo.mark_welcome_popup_displayed(vars)

Initialize repository with database session.

Parameters:

Name Type Description Default
db Session

SQLAlchemy session for database operations.

required
Source code in backend/src/domains/startup/repository.py
def __init__(self, db: Session):
    """Initialize repository with database session.

    Args:
        db: SQLAlchemy session for database operations.
    """
    self.db = db
    logger.debug("Initializing Startup_Variables_Repository")

get_or_create()

Retrieve the singleton StartupVariables record, creating if absent.

Returns:

Name Type Description
StartupVariables StartupVariables

The singleton startup state entity.

Note

This is a singleton entity - only one row should exist in the table. Creates with default values (welcome_popup_has_already_displayed=False) if missing.

Source code in backend/src/domains/startup/repository.py
def get_or_create(self) -> StartupVariables:
    """Retrieve the singleton StartupVariables record, creating if absent.

    Returns:
        StartupVariables: The singleton startup state entity.

    Note:
        This is a singleton entity - only one row should exist in the table.
        Creates with default values (welcome_popup_has_already_displayed=False) if missing.
    """
    logger.debug("Retrieving or creating StartupVariables singleton")
    vars = self.db.query(StartupVariables).first()

    if not vars:
        logger.info("StartupVariables not found, creating new record")
        vars = StartupVariables(welcome_popup_has_already_displayed=False)
        self.db.add(vars)
        self.db.flush()
        self.db.refresh(vars)
        logger.info(f"Created StartupVariables {vars.id}")

    return vars

get_welcome_popup_status()

Check if welcome popup has already been displayed to user.

Returns:

Name Type Description
bool bool

True if popup was already shown, False if first time.

Source code in backend/src/domains/startup/repository.py
def get_welcome_popup_status(self) -> bool:
    """Check if welcome popup has already been displayed to user.

    Returns:
        bool: True if popup was already shown, False if first time.
    """
    logger.debug("Checking welcome popup display status")
    vars = self.get_or_create()
    return vars.welcome_popup_has_already_displayed

mark_welcome_popup_displayed(vars)

Mark welcome popup as displayed (set flag to True).

Parameters:

Name Type Description Default
vars StartupVariables

StartupVariables entity to update.

required

Returns:

Name Type Description
StartupVariables StartupVariables

Updated entity (not yet committed, use flush()).

Source code in backend/src/domains/startup/repository.py
def mark_welcome_popup_displayed(self, vars: StartupVariables) -> StartupVariables:
    """Mark welcome popup as displayed (set flag to True).

    Args:
        vars: StartupVariables entity to update.

    Returns:
        StartupVariables: Updated entity (not yet committed, use flush()).
    """
    logger.info("Marking welcome popup as displayed")
    vars.welcome_popup_has_already_displayed = True
    self.db.flush()
    self.db.refresh(vars)
    return vars

reset(vars)

Reset all startup variables to default values.

Parameters:

Name Type Description Default
vars StartupVariables

StartupVariables entity to reset.

required

Returns:

Name Type Description
StartupVariables StartupVariables

Reset entity (not yet committed, use flush()).

Source code in backend/src/domains/startup/repository.py
def reset(self, vars: StartupVariables) -> StartupVariables:
    """Reset all startup variables to default values.

    Args:
        vars: StartupVariables entity to reset.

    Returns:
        StartupVariables: Reset entity (not yet committed, use flush()).
    """
    logger.info("Resetting StartupVariables to defaults")
    vars.welcome_popup_has_already_displayed = False
    self.db.flush()
    self.db.refresh(vars)
    return vars

update_field(vars, field, value)

Update a specific field on the StartupVariables entity.

Parameters:

Name Type Description Default
vars StartupVariables

StartupVariables entity to update.

required
field str

Name of the field to update.

required
value

New value for the field.

required

Returns:

Name Type Description
StartupVariables StartupVariables

Updated entity (not yet committed, use flush()).

Raises:

Type Description
AttributeError

If field does not exist on entity.

Source code in backend/src/domains/startup/repository.py
def update_field(self, vars: StartupVariables, field: str, value) -> StartupVariables:
    """Update a specific field on the StartupVariables entity.

    Args:
        vars: StartupVariables entity to update.
        field: Name of the field to update.
        value: New value for the field.

    Returns:
        StartupVariables: Updated entity (not yet committed, use flush()).

    Raises:
        AttributeError: If field does not exist on entity.
    """
    logger.info(f"Updating StartupVariables field: {field} = {value}")
    if not hasattr(vars, field):
        raise AttributeError(f"StartupVariables has no field '{field}'")

    setattr(vars, field, value)
    self.db.flush()
    self.db.refresh(vars)
    return vars

src.domains.startup.schemas

Pydantic validation schemas for startup state management.

This module defines data transfer objects (DTOs) for the startup domain, handling UI state persistence flags across application restarts.

Schema Purpose: - WelcomePopupResponse: Response indicating if welcome popup should be shown.

Example

from src.domains.startup.schemas import WelcomePopupResponse from fastapi import FastAPI

app = FastAPI()

@app.get("/startup/welcome-popup", response_model=WelcomePopupResponse) def check_welcome_popup(): return {"has_already_displayed": True}

WelcomePopupResponse

Bases: BaseModel

Response schema for welcome popup display status.

Indicates whether the welcome popup has already been shown to the user. Used by frontend to determine if popup should be displayed.

Attributes:

Name Type Description
has_already_displayed bool

True if popup was already shown, False if first time.

Example

response = WelcomePopupResponse(has_already_displayed=False) print(response.has_already_displayed) False

Config

Pydantic configuration for WelcomePopupResponse model.

Enables JSON schema generation for OpenAPI documentation.