Skip to content

A modern platform for visual, flexible, and extensible graph-based investigations. For cybersecurity analysts and investigators.

feat(api): use dedicated services for business logic

dextmorgn2372678bda61
diff --git a/flowsint-api/app/api/routes/analysis.py b/flowsint-api/app/api/routes/analysis.py
index d66bf651..f311ec39 100644
--- a/flowsint-api/app/api/routes/analysis.py
+++ b/flowsint-api/app/api/routes/analysis.py
@@ -1,42 +1,30 @@
-from uuid import UUID, uuid4
-from app.security.permissions import check_investigation_permission
+from uuid import UUID
from fastapi import APIRouter, HTTPException, Depends, status
from typing import List
-from datetime import datetime
-from sqlalchemy import or_
from sqlalchemy.orm import Session
+
from flowsint_core.core.postgre_db import get_db
-from flowsint_core.core.models import Analysis, Profile, InvestigationUserRole
-from flowsint_core.core.types import Role
+from flowsint_core.core.models import Profile
+from flowsint_core.core.services import (
+ create_analysis_service,
+ NotFoundError,
+ PermissionDeniedError,
+)
from app.api.deps import get_current_user
from app.api.schemas.analysis import AnalysisRead, AnalysisCreate, AnalysisUpdate
router = APIRouter()
-# Get the list of all analyses for the current user
@router.get("", response_model=List[AnalysisRead])
def get_analyses(
db: Session = Depends(get_db), current_user: Profile = Depends(get_current_user)
):
- # Get all analyses from investigations where user has at least VIEWER role
- allowed_roles_for_read = [Role.OWNER, Role.EDITOR, Role.VIEWER]
-
- query = db.query(Analysis).join(
- InvestigationUserRole,
- InvestigationUserRole.investigation_id == Analysis.investigation_id,
- )
-
- query = query.filter(InvestigationUserRole.user_id == current_user.id)
-
- # Filter by allowed roles
- conditions = [InvestigationUserRole.roles.any(role) for role in allowed_roles_for_read]
- query = query.filter(or_(*conditions))
+ """Get all analyses accessible to the current user."""
+ service = create_analysis_service(db)
+ return service.get_accessible_analyses(current_user.id)
- return query.distinct().all()
-
-# Create a New analysis
@router.post(
"/create", response_model=AnalysisRead, status_code=status.HTTP_201_CREATED
)
@@ -45,64 +33,47 @@ def create_analysis(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- check_investigation_permission(
- current_user.id, payload.investigation_id, actions=["create"], db=db
- )
- new_analysis = Analysis(
- id=uuid4(),
- title=payload.title,
- description=payload.description,
- content=payload.content,
- owner_id=current_user.id,
- investigation_id=payload.investigation_id,
- created_at=datetime.utcnow(),
- last_updated_at=datetime.utcnow(),
- )
- db.add(new_analysis)
- db.commit()
- db.refresh(new_analysis)
- return new_analysis
+ service = create_analysis_service(db)
+ try:
+ return service.create(
+ title=payload.title,
+ description=payload.description,
+ content=payload.content,
+ investigation_id=payload.investigation_id,
+ owner_id=current_user.id,
+ )
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
-# Get an analysis by ID
@router.get("/{analysis_id}", response_model=AnalysisRead)
def get_analysis_by_id(
analysis_id: UUID,
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- analysis = (
- db.query(Analysis)
- .filter(Analysis.id == analysis_id)
- .first()
- )
- if not analysis:
+ service = create_analysis_service(db)
+ try:
+ return service.get_by_id(analysis_id, current_user.id)
+ except NotFoundError:
raise HTTPException(status_code=404, detail="Analysis not found")
- check_investigation_permission(
- current_user.id, analysis.investigation_id, actions=["read"], db=db
- )
- return analysis
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
-# Get analyses by investigation ID
@router.get("/investigation/{investigation_id}", response_model=List[AnalysisRead])
def get_analyses_by_investigation(
investigation_id: UUID,
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- check_investigation_permission(
- current_user.id, investigation_id, actions=["read"], db=db
- )
- analyses = (
- db.query(Analysis)
- .filter(Analysis.investigation_id == investigation_id)
- .all()
- )
- return analyses
+ service = create_analysis_service(db)
+ try:
+ return service.get_by_investigation(investigation_id, current_user.id)
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
-# Update an analysis by ID
@router.put("/{analysis_id}", response_model=AnalysisRead)
def update_analysis(
analysis_id: UUID,
@@ -110,51 +81,33 @@ def update_analysis(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- analysis = (
- db.query(Analysis)
- .filter(Analysis.id == analysis_id)
- .first()
- )
- if not analysis:
- raise HTTPException(status_code=404, detail="Analysis not found")
- check_investigation_permission(
- current_user.id, analysis.investigation_id, actions=["update"], db=db
- )
- if payload.title is not None:
- analysis.title = payload.title
- if payload.description is not None:
- analysis.description = payload.description
- if payload.content is not None:
- analysis.content = payload.content
- if payload.investigation_id is not None:
- # Check permission for the new investigation as well
- check_investigation_permission(
- current_user.id, payload.investigation_id, actions=["update"], db=db
+ service = create_analysis_service(db)
+ try:
+ return service.update(
+ analysis_id=analysis_id,
+ user_id=current_user.id,
+ title=payload.title,
+ description=payload.description,
+ content=payload.content,
+ investigation_id=payload.investigation_id,
)
- analysis.investigation_id = payload.investigation_id
- analysis.last_updated_at = datetime.utcnow()
- db.commit()
- db.refresh(analysis)
- return analysis
+ except NotFoundError:
+ raise HTTPException(status_code=404, detail="Analysis not found")
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
-# Delete an analysis by ID
@router.delete("/{analysis_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_analysis(
analysis_id: UUID,
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- analysis = (
- db.query(Analysis)
- .filter(Analysis.id == analysis_id)
- .first()
- )
- if not analysis:
+ service = create_analysis_service(db)
+ try:
+ service.delete(analysis_id, current_user.id)
+ return None
+ except NotFoundError:
raise HTTPException(status_code=404, detail="Analysis not found")
- check_investigation_permission(
- current_user.id, analysis.investigation_id, actions=["delete"], db=db
- )
- db.delete(analysis)
- db.commit()
- return None
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
diff --git a/flowsint-api/app/api/routes/auth.py b/flowsint-api/app/api/routes/auth.py
index c36b8697..52066fa9 100644
--- a/flowsint-api/app/api/routes/auth.py
+++ b/flowsint-api/app/api/routes/auth.py
@@ -1,14 +1,15 @@
from fastapi import APIRouter, Depends, HTTPException
from fastapi.security import OAuth2PasswordRequestForm
from sqlalchemy.orm import Session
-from sqlalchemy.exc import SQLAlchemyError, IntegrityError
-from flowsint_core.core.auth import (
- verify_password,
- create_access_token,
- get_password_hash,
+from sqlalchemy.exc import SQLAlchemyError
+
+from flowsint_core.core.services import (
+ create_auth_service,
+ AuthenticationError,
+ ConflictError,
+ DatabaseError,
)
from app.api.schemas.profile import ProfileCreate
-from flowsint_core.core.models import Profile
from flowsint_core.core.postgre_db import get_db
router = APIRouter()
@@ -18,50 +19,23 @@ router = APIRouter()
def login_for_access_token(
form_data: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(get_db)
):
+ service = create_auth_service(db)
try:
- user = db.query(Profile).filter(Profile.email == form_data.username).first()
-
- if not user or not verify_password(form_data.password, user.hashed_password):
- raise HTTPException(status_code=400, detail="Incorrect email or password")
-
- access_token = create_access_token(data={"sub": user.email})
- return {
- "access_token": access_token,
- "user_id": user.id,
- "token_type": "bearer",
- }
-
- except SQLAlchemyError as e:
- # Log optionnel
+ return service.authenticate(form_data.username, form_data.password)
+ except AuthenticationError:
+ raise HTTPException(status_code=400, detail="Incorrect email or password")
+ except (DatabaseError, SQLAlchemyError) as e:
print(f"[ERROR] DB error during login: {e}")
raise HTTPException(status_code=500, detail="Internal server error")
@router.post("/register", status_code=201)
def register(user: ProfileCreate, db: Session = Depends(get_db)):
+ service = create_auth_service(db)
try:
- existing_user = db.query(Profile).filter(Profile.email == user.email).first()
-
- if existing_user:
- raise HTTPException(status_code=400, detail="Email already registered")
-
- hashed_password = get_password_hash(user.password)
- new_user = Profile(email=user.email, hashed_password=hashed_password)
-
- db.add(new_user)
- db.commit()
- db.refresh(new_user)
-
- return {
- "message": "User registered successfully",
- "email": new_user.email,
- }
-
- except IntegrityError:
- db.rollback()
+ return service.register(user.email, user.password)
+ except ConflictError:
raise HTTPException(status_code=400, detail="Email already registered")
-
- except SQLAlchemyError as e:
- db.rollback()
+ except (DatabaseError, SQLAlchemyError) as e:
print(f"[ERROR] DB error during registration: {e}")
raise HTTPException(status_code=500, detail="Internal server error")
diff --git a/flowsint-api/app/api/routes/chat.py b/flowsint-api/app/api/routes/chat.py
index 1878c670..5975937f 100644
--- a/flowsint-api/app/api/routes/chat.py
+++ b/flowsint-api/app/api/routes/chat.py
@@ -1,4 +1,4 @@
-from fastapi import APIRouter, HTTPException
+from fastapi import APIRouter, HTTPException, Depends, status
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
import os
@@ -6,90 +6,43 @@ from mistralai import Mistral
from mistralai.models import UserMessage, AssistantMessage, SystemMessage
import json
from uuid import UUID, uuid4
-from fastapi import APIRouter, HTTPException, Depends, status
from typing import Dict, List, Optional
from datetime import datetime
-from sqlalchemy import or_
from sqlalchemy.orm import Session
+
from flowsint_core.core.postgre_db import get_db
-from flowsint_core.core.models import Chat, ChatMessage, Profile, InvestigationUserRole
-from flowsint_core.core.types import Role
+from flowsint_core.core.models import ChatMessage, Profile
+from flowsint_core.core.services import (
+ create_chat_service,
+ NotFoundError,
+)
from app.api.deps import get_current_user
from app.api.schemas.chat import ChatCreate, ChatRead
-from app.security.permissions import check_investigation_permission
router = APIRouter()
-def clean_context(context: List[Dict]) -> List[Dict]:
- print(context)
- """Remove unnecessary keys from context data."""
- cleaned = []
- for item in context:
- if isinstance(item, dict):
- # Create a copy and remove unwanted keys
- cleaned_item = item["data"].copy()
- # Remove top-level keys
- cleaned_item.pop("id", None)
- cleaned_item.pop("sketch_id", None)
- # Remove from data if it exists
- if "data" in cleaned_item and isinstance(cleaned_item["data"], dict):
- cleaned_item["data"].pop("sketch_id", None)
- # Remove measured/dimensions
- cleaned_item.pop("measured", None)
- cleaned.append(cleaned_item)
- return cleaned
-
-
class ChatRequest(BaseModel):
prompt: str
context: Optional[List[Dict]] = None
-# Get all chats
@router.get("/", response_model=List[ChatRead])
def get_chats(
db: Session = Depends(get_db), current_user: Profile = Depends(get_current_user)
):
- chats = db.query(Chat).filter(Chat.owner_id == current_user.id).all()
-
- # Sort messages for each chat by created_at in ascending order
- for chat in chats:
- chat.messages.sort(key=lambda x: x.created_at)
-
- return chats
-
+ service = create_chat_service(db)
+ return service.get_chats_for_user(current_user.id)
-# @router.get("/delete-all", status_code=status.HTTP_204_NO_CONTENT)
-# def delete_all_chat(db: Session = Depends(get_db)):
-# chats = db.query(Chat).all()
-# for chat in chats:
-# db.delete(chat)
-# db.commit()
-# return {"result": "done"}
-
-# Get analyses by investigation ID
@router.get("/investigation/{investigation_id}", response_model=List[ChatRead])
def get_chats_by_investigation(
investigation_id: UUID,
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- chats = (
- db.query(Chat)
- .filter(
- Chat.investigation_id == investigation_id, Chat.owner_id == current_user.id
- )
- .order_by(Chat.created_at.asc())
- .all()
- )
-
- # Sort messages for each chat by created_at in ascending order
- for chat in chats:
- chat.messages.sort(key=lambda x: x.created_at)
-
- return chats
+ service = create_chat_service(db)
+ return service.get_by_investigation(investigation_id, current_user.id)
@router.post("/stream/{chat_id}")
@@ -99,30 +52,18 @@ async def stream_chat(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- # Check if Chat exists
- chat = (
- db.query(Chat)
- .filter(Chat.id == chat_id, Chat.owner_id == current_user.id)
- .first()
- )
- if not chat:
+ service = create_chat_service(db)
+
+ try:
+ chat = service.get_by_id(chat_id, current_user.id)
+ except NotFoundError:
raise HTTPException(status_code=404, detail="Chat not found")
- # Update chat's last_updated_at
- chat.last_updated_at = datetime.utcnow()
- db.commit()
- # A new message is created
- user_message = ChatMessage(
- id=uuid4(),
- content=payload.prompt,
- context=payload.context,
- chat_id=chat_id,
- is_bot=False,
- created_at=datetime.utcnow(),
- )
- db.add(user_message)
- db.commit()
- db.refresh(user_message)
+ # Add user message
+ service.add_user_message(chat_id, current_user.id, payload.prompt, payload.context)
+
+ # Prepare AI context
+ ai_context = service.prepare_ai_context(chat, payload.prompt, payload.context)
try:
api_key = os.environ.get("MISTRAL_API_KEY")
@@ -134,35 +75,15 @@ async def stream_chat(
client = Mistral(api_key=api_key)
model = "mistral-small-latest"
accumulated_content = []
- context_message = None
- # Convert database messages to Mistral format
+
+ # Build messages for Mistral
messages = [
SystemMessage(
content="You are a CTI/OSINT investigator and you are trying to investigate on a variety of real life cases. Use your knowledge and analytics capabilities to analyse the context and answer the question the best you can. If you need to reference some items (an IP, a domain or something particular) please use the code brackets, like : `12.23.34.54` to reference it."
)
]
- # Add context as a single system message if provided
- if payload.context:
- try:
- # Clean context by removing unnecessary keys
- cleaned_context = clean_context(payload.context)
- if cleaned_context:
- context_str = json.dumps(cleaned_context, indent=2, default=str)
- context_message = f"Context: {context_str}"
- # Limit context message length to avoid token limits
- if len(context_message) > 2000:
- context_message = context_message[:2000] + "..."
- except Exception as e:
- # If context processing fails, skip it
- print(f"Context processing error: {e}")
-
- # Sort messages by created_at in ascending order and get recent messages
- sorted_messages = sorted(chat.messages, key=lambda x: x.created_at)
- recent_messages = (
- sorted_messages[-5:] if len(sorted_messages) > 5 else sorted_messages
- )
- for message in recent_messages:
+ for message in ai_context["recent_messages"]:
if message.is_bot:
messages.append(
AssistantMessage(content=json.dumps(message.content, default=str))
@@ -172,11 +93,10 @@ async def stream_chat(
UserMessage(content=json.dumps(message.content, default=str))
)
- # Add the current context
- if context_message:
- messages.append(SystemMessage(content=context_message))
- # Add the current user message
- messages.append(UserMessage(content=payload.prompt))
+ if ai_context["context_message"]:
+ messages.append(SystemMessage(content=ai_context["context_message"]))
+
+ messages.append(UserMessage(content=ai_context["user_prompt"]))
async def generate():
response = await client.chat.stream_async(model=model, messages=messages)
@@ -187,18 +107,8 @@ async def stream_chat(
accumulated_content.append(content_chunk)
yield f"data: {json.dumps({'content': content_chunk})}\n\n"
- # Create the bot message after all chunks have been processed
- chat_message = ChatMessage(
- id=uuid4(),
- content="".join(accumulated_content),
- chat_id=chat_id,
- is_bot=True,
- created_at=datetime.utcnow(),
- )
-
- db.add(chat_message)
- db.commit()
- db.refresh(chat_message)
+ # Save bot message after streaming completes
+ service.add_bot_message(chat_id, "".join(accumulated_content))
yield "data: [DONE]\n\n"
@@ -207,63 +117,43 @@ async def stream_chat(
raise HTTPException(status_code=500, detail=str(e))
-# Create a new chat
@router.post("/create", response_model=ChatRead, status_code=status.HTTP_201_CREATED)
def create_chat(
payload: ChatCreate,
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- new_chat = Chat(
- id=uuid4(),
+ service = create_chat_service(db)
+ return service.create(
title=payload.title,
description=payload.description,
- owner_id=current_user.id,
investigation_id=payload.investigation_id,
- created_at=datetime.utcnow(),
- last_updated_at=datetime.utcnow(),
+ owner_id=current_user.id,
)
- db.add(new_chat)
- db.commit()
- db.refresh(new_chat)
- return new_chat
-# Get a chat by ID
@router.get("/{chat_id}", response_model=ChatRead)
def get_chat_by_id(
chat_id: UUID,
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- chat = (
- db.query(Chat)
- .filter(Chat.id == chat_id, Chat.owner_id == current_user.id)
- .first()
- )
- if not chat:
+ service = create_chat_service(db)
+ try:
+ return service.get_by_id(chat_id, current_user.id)
+ except NotFoundError:
raise HTTPException(status_code=404, detail="Chat not found")
- # Sort messages by created_at in ascending order
- chat.messages.sort(key=lambda x: x.created_at)
-
- return chat
-
-# Delete an chat by ID
@router.delete("/{chat_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_chat(
chat_id: UUID,
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- chat = (
- db.query(Chat)
- .filter(Chat.id == chat_id, Chat.owner_id == current_user.id)
- .first()
- )
- if not chat:
+ service = create_chat_service(db)
+ try:
+ service.delete(chat_id, current_user.id)
+ return None
+ except NotFoundError:
raise HTTPException(status_code=404, detail="Chat not found")
- db.delete(chat)
- db.commit()
- return None
diff --git a/flowsint-api/app/api/routes/custom_types.py b/flowsint-api/app/api/routes/custom_types.py
index 612920f0..ed9a053c 100644
--- a/flowsint-api/app/api/routes/custom_types.py
+++ b/flowsint-api/app/api/routes/custom_types.py
@@ -3,8 +3,15 @@ from uuid import UUID
from typing import List
from fastapi import APIRouter, HTTPException, Depends, status
from sqlalchemy.orm import Session
+
from flowsint_core.core.postgre_db import get_db
-from flowsint_core.core.models import CustomType, Profile
+from flowsint_core.core.models import Profile
+from flowsint_core.core.services import (
+ create_custom_type_service,
+ NotFoundError,
+ ValidationError,
+ ConflictError,
+)
from app.api.deps import get_current_user
from app.api.schemas.custom_type import (
CustomTypeCreate,
@@ -29,47 +36,22 @@ def create_custom_type(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- """
- Create a new custom type.
-
- Validates the JSON Schema and stores it in the database.
- """
- # Validate the JSON Schema
- validate_json_schema(custom_type.json_schema)
-
- # Calculate checksum
- checksum = calculate_schema_checksum(custom_type.json_schema)
-
- # Check for duplicate name for this user
- existing = (
- db.query(CustomType)
- .filter(
- CustomType.owner_id == current_user.id,
- CustomType.name == custom_type.name
+ """Create a new custom type."""
+ service = create_custom_type_service(db)
+ try:
+ return service.create(
+ name=custom_type.name,
+ json_schema=custom_type.json_schema,
+ user_id=current_user.id,
+ description=custom_type.description,
+ status=custom_type.status,
+ validate_schema_func=validate_json_schema,
+ calculate_checksum_func=calculate_schema_checksum,
)
- .first()
- )
- if existing:
- raise HTTPException(
- status_code=400,
- detail=f"Custom type with name '{custom_type.name}' already exists"
- )
-
- # Create the custom type
- db_custom_type = CustomType(
- name=custom_type.name,
- owner_id=current_user.id,
- schema=custom_type.json_schema,
- description=custom_type.description,
- status=custom_type.status,
- checksum=checksum,
- )
-
- db.add(db_custom_type)
- db.commit()
- db.refresh(db_custom_type)
-
- return db_custom_type
+ except ValidationError as e:
+ raise HTTPException(status_code=400, detail=str(e))
+ except ConflictError as e:
+ raise HTTPException(status_code=400, detail=str(e))
@router.get("", response_model=List[CustomTypeRead])
@@ -78,23 +60,12 @@ def list_custom_types(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- """
- List all custom types for the current user.
-
- Can be filtered by status (draft, published, archived).
- """
- query = db.query(CustomType).filter(CustomType.owner_id == current_user.id)
-
- if status:
- if status not in ["draft", "published", "archived"]:
- raise HTTPException(
- status_code=400,
- detail="Status must be one of: draft, published, archived"
- )
- query = query.filter(CustomType.status == status)
-
- custom_types = query.order_by(CustomType.created_at.desc()).all()
- return custom_types
+ """List all custom types for the current user."""
+ service = create_custom_type_service(db)
+ try:
+ return service.list_custom_types(current_user.id, status)
+ except ValidationError as e:
+ raise HTTPException(status_code=400, detail=str(e))
@router.get("/{id}", response_model=CustomTypeRead)
@@ -104,19 +75,11 @@ def get_custom_type(
current_user: Profile = Depends(get_current_user),
):
"""Get a specific custom type by ID."""
- custom_type = (
- db.query(CustomType)
- .filter(CustomType.id == id, CustomType.owner_id == current_user.id)
- .first()
- )
-
- if not custom_type:
- raise HTTPException(
- status_code=404,
- detail="Custom type not found"
- )
-
- return custom_type
+ service = create_custom_type_service(db)
+ try:
+ return service.get_by_id(id, current_user.id)
+ except NotFoundError:
+ raise HTTPException(status_code=404, detail="Custom type not found")
@router.get("/{id}/schema")
@@ -126,19 +89,11 @@ def get_custom_type_schema(
current_user: Profile = Depends(get_current_user),
):
"""Get the raw JSON Schema for a custom type."""
- custom_type = (
- db.query(CustomType)
- .filter(CustomType.id == id, CustomType.owner_id == current_user.id)
- .first()
- )
-
- if not custom_type:
- raise HTTPException(
- status_code=404,
- detail="Custom type not found"
- )
-
- return custom_type.schema
+ service = create_custom_type_service(db)
+ try:
+ return service.get_schema(id, current_user.id)
+ except NotFoundError:
+ raise HTTPException(status_code=404, detail="Custom type not found")
@router.put("/{id}", response_model=CustomTypeRead)
@@ -148,58 +103,25 @@ def update_custom_type(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- """
- Update a custom type.
-
- If the schema is changed, a new checksum is calculated.
- """
- custom_type = (
- db.query(CustomType)
- .filter(CustomType.id == id, CustomType.owner_id == current_user.id)
- .first()
- )
-
- if not custom_type:
- raise HTTPException(
- status_code=404,
- detail="Custom type not found"
+ """Update a custom type."""
+ service = create_custom_type_service(db)
+ try:
+ return service.update(
+ custom_type_id=id,
+ user_id=current_user.id,
+ name=update_data.name,
+ json_schema=update_data.json_schema,
+ description=update_data.description,
+ status=update_data.status,
+ validate_schema_func=validate_json_schema,
+ calculate_checksum_func=calculate_schema_checksum,
)
-
- # Update fields
- if update_data.name is not None:
- # Check for duplicate name
- existing = (
- db.query(CustomType)
- .filter(
- CustomType.owner_id == current_user.id,
- CustomType.name == update_data.name,
- CustomType.id != id
- )
- .first()
- )
- if existing:
- raise HTTPException(
- status_code=400,
- detail=f"Custom type with name '{update_data.name}' already exists"
- )
- custom_type.name = update_data.name
-
- if update_data.json_schema is not None:
- # Validate the new schema
- validate_json_schema(update_data.json_schema)
- custom_type.schema = update_data.json_schema
- custom_type.checksum = calculate_schema_checksum(update_data.json_schema)
-
- if update_data.description is not None:
- custom_type.description = update_data.description
-
- if update_data.status is not None:
- custom_type.status = update_data.status
-
- db.commit()
- db.refresh(custom_type)
-
- return custom_type
+ except NotFoundError:
+ raise HTTPException(status_code=404, detail="Custom type not found")
+ except ValidationError as e:
+ raise HTTPException(status_code=400, detail=str(e))
+ except ConflictError as e:
+ raise HTTPException(status_code=400, detail=str(e))
@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT)
@@ -209,22 +131,12 @@ def delete_custom_type(
current_user: Profile = Depends(get_current_user),
):
"""Delete a custom type."""
- custom_type = (
- db.query(CustomType)
- .filter(CustomType.id == id, CustomType.owner_id == current_user.id)
- .first()
- )
-
- if not custom_type:
- raise HTTPException(
- status_code=404,
- detail="Custom type not found"
- )
-
- db.delete(custom_type)
- db.commit()
-
- return None
+ service = create_custom_type_service(db)
+ try:
+ service.delete(id, current_user.id)
+ return None
+ except NotFoundError:
+ raise HTTPException(status_code=404, detail="Custom type not found")
@router.post("/{id}/validate", response_model=CustomTypeValidateResponse)
@@ -234,30 +146,18 @@ def validate_payload(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- """
- Validate a payload against a custom type's schema.
-
- This is useful for testing before publishing a type.
- """
- custom_type = (
- db.query(CustomType)
- .filter(CustomType.id == id, CustomType.owner_id == current_user.id)
- .first()
- )
-
- if not custom_type:
- raise HTTPException(
- status_code=404,
- detail="Custom type not found"
+ """Validate a payload against a custom type's schema."""
+ service = create_custom_type_service(db)
+ try:
+ is_valid, errors = service.validate_payload(
+ id,
+ current_user.id,
+ payload_data.payload,
+ validate_payload_func=validate_payload_against_schema,
)
-
- # Validate the payload against the schema
- is_valid, errors = validate_payload_against_schema(
- payload_data.payload,
- custom_type.schema
- )
-
- return CustomTypeValidateResponse(
- valid=is_valid,
- errors=errors if not is_valid else None
- )
+ return CustomTypeValidateResponse(
+ valid=is_valid,
+ errors=errors if not is_valid else None,
+ )
+ except NotFoundError:
+ raise HTTPException(status_code=404, detail="Custom type not found")
diff --git a/flowsint-api/app/api/routes/enrichers.py b/flowsint-api/app/api/routes/enrichers.py
index f96b4615..03739d2f 100644
--- a/flowsint-api/app/api/routes/enrichers.py
+++ b/flowsint-api/app/api/routes/enrichers.py
@@ -1,38 +1,20 @@
-from typing import Any, List, Optional
+from typing import List, Optional
from fastapi import APIRouter, Depends, HTTPException, Query
-from flowsint_core.core.celery import celery
-from flowsint_core.core.graph import create_graph_service, GraphEdge, GraphNode
-from flowsint_core.core.models import CustomType, Profile
-from flowsint_core.core.postgre_db import get_db
-from flowsint_core.core.types import FlowBranch
-from flowsint_enrichers import ENRICHER_REGISTRY, load_all_enrichers
from pydantic import BaseModel
-from sqlalchemy import func
from sqlalchemy.orm import Session
+from flowsint_core.core.celery import celery
+from flowsint_core.core.graph import create_graph_service
+from flowsint_core.core.models import Profile
+from flowsint_core.core.postgre_db import get_db
+from flowsint_core.core.services import create_enricher_service
+from flowsint_enrichers import ENRICHER_REGISTRY, load_all_enrichers
from app.api.deps import get_current_user
-# Auto-discover and register all enrichers
load_all_enrichers()
-class FlowComputationRequest(BaseModel):
- nodes: List[GraphNode]
- edges: List[GraphEdge]
- inputType: Optional[str] = None
-
-
-class FlowComputationResponse(BaseModel):
- flowBranches: List[FlowBranch]
- initialData: Any
-
-
-class StepSimulationRequest(BaseModel):
- flowBranches: List[FlowBranch]
- currentStepIndex: int
-
-
class launchEnricherPayload(BaseModel):
node_ids: List[str]
sketch_id: str
@@ -41,30 +23,15 @@ class launchEnricherPayload(BaseModel):
router = APIRouter()
-# Get the list of all enrichers
@router.get("/")
def get_enrichers(
category: Optional[str] = Query(None),
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- if not category or category.lower() == "undefined":
- return ENRICHER_REGISTRY.list(exclude=["n8n_connector"])
- # Si catégorie custom
- custom_type = (
- db.query(CustomType)
- .filter(
- CustomType.owner_id == current_user.id,
- CustomType.status == "published",
- func.lower(CustomType.name) == category.lower(),
- )
- .first()
- )
-
- if custom_type:
- return ENRICHER_REGISTRY.list(exclude=["n8n_connector"], wobbly_type=True)
-
- return ENRICHER_REGISTRY.list_by_input_type(category, exclude=["n8n_connector"])
+ """Get all enrichers, optionally filtered by category."""
+ service = create_enricher_service(db)
+ return service.get_enrichers(category, current_user.id, ENRICHER_REGISTRY)
@router.post("/{enricher_name}/launch")
@@ -78,12 +45,13 @@ async def launch_enricher(
graph_service = create_graph_service(sketch_id=payload.sketch_id)
entities = graph_service.get_nodes_by_ids_for_task(payload.node_ids)
- # send deserialized nodes
+ # Send deserialized nodes
entities = [entity.model_dump(mode="json", serialize_as_any=True) for entity in entities]
if not entities:
raise HTTPException(
status_code=404, detail="No entities found with provided IDs"
)
+
task = celery.send_task(
"run_enricher",
args=[
diff --git a/flowsint-api/app/api/routes/events.py b/flowsint-api/app/api/routes/events.py
index d189e4d3..6161a0b3 100644
--- a/flowsint-api/app/api/routes/events.py
+++ b/flowsint-api/app/api/routes/events.py
@@ -1,16 +1,20 @@
from fastapi import APIRouter, Depends, HTTPException, Request
from sqlalchemy.orm import Session
-from flowsint_core.core.postgre_db import get_db
-from flowsint_core.core.models import Log, Sketch, Scan
-from flowsint_core.core.events import event_emitter
from sse_starlette.sse import EventSourceResponse
-from flowsint_core.core.types import Event
-from app.api.deps import get_current_user, get_current_user_sse
-from flowsint_core.core.models import Profile, Sketch
-from app.security.permissions import check_investigation_permission
import json
import asyncio
-from datetime import datetime, timedelta
+from datetime import datetime
+
+from flowsint_core.core.postgre_db import get_db
+from flowsint_core.core.events import event_emitter
+from flowsint_core.core.models import Profile
+from flowsint_core.core.services import (
+ create_log_service,
+ NotFoundError,
+ PermissionDeniedError,
+ DatabaseError,
+)
+from app.api.deps import get_current_user, get_current_user_sse
router = APIRouter()
@@ -21,62 +25,16 @@ def get_logs_by_sketch(
limit: int = 100,
since: datetime | None = None,
db: Session = Depends(get_db),
- current_user: Profile = Depends(get_current_user)
+ current_user: Profile = Depends(get_current_user),
):
- """Get historical logs for a specific sketch with optional filtering"""
- # Check if sketch exists
- sketch = db.query(Sketch).filter(Sketch.id == sketch_id).first()
- if not sketch:
- raise HTTPException(
- status_code=404, detail=f"Sketch with id {sketch_id} not found"
- )
-
- check_investigation_permission(
- current_user.id, sketch.investigation_id, actions=["read"], db=db
- )
-
- print(
- f"[EventEmitter] Fetching logs for sketch {sketch_id} (limit: {limit}, since: {since})"
- )
- query = (
- db.query(Log).filter(Log.sketch_id == sketch_id).order_by(Log.created_at.desc())
- )
-
- if since:
- query = query.filter(Log.created_at > since)
- else:
- # Default to last 24 hours if no since parameter
- query = query.filter(Log.created_at > datetime.utcnow() - timedelta(days=1))
-
- logs = query.limit(limit).all()
-
- # Reverse to show chronologically (oldest to newest)
- logs = list(reversed(logs))
-
- results = []
- for log in logs:
- # Ensure payload is always a dictionary
- if isinstance(log.content, dict):
- payload = log.content
- elif isinstance(log.content, str):
- payload = {"message": log.content}
- elif log.content is None:
- payload = {}
- else:
- # Handle other types by converting to string and wrapping
- payload = {"content": str(log.content)}
-
- results.append(
- Event(
- id=str(log.id),
- sketch_id=str(log.sketch_id) if log.sketch_id else None,
- type=log.type,
- payload=payload,
- created_at=log.created_at
- )
- )
-
- return results
+ """Get historical logs for a specific sketch with optional filtering."""
+ service = create_log_service(db)
+ try:
+ return service.get_logs_by_sketch(sketch_id, current_user.id, limit, since)
+ except NotFoundError as e:
+ raise HTTPException(status_code=404, detail=str(e))
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
@router.get("/sketch/{sketch_id}/stream")
@@ -84,26 +42,22 @@ async def stream_events(
request: Request,
sketch_id: str,
db: Session = Depends(get_db),
- current_user: Profile = Depends(get_current_user_sse)
+ current_user: Profile = Depends(get_current_user_sse),
):
- """Stream events for a specific scan in real-time"""
-
- # Check if sketch exists
- sketch = db.query(Sketch).filter(Sketch.id == sketch_id).first()
- if not sketch:
- raise HTTPException(
- status_code=404, detail=f"Sketch with id {sketch_id} not found"
- )
-
- check_investigation_permission(
- current_user.id, sketch.investigation_id, actions=["read"], db=db
- )
+ """Stream events for a specific sketch in real-time."""
+ service = create_log_service(db)
+ try:
+ # Verify permission
+ service._get_sketch_with_permission(sketch_id, current_user.id, ["read"])
+ except NotFoundError as e:
+ raise HTTPException(status_code=404, detail=str(e))
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
async def event_generator():
channel = sketch_id
await event_emitter.subscribe(channel)
try:
- # Initial connection message
yield 'data: {"event": "connected", "data": "Connected to log stream"}\n\n'
while True:
if await request.is_disconnected():
@@ -111,15 +65,12 @@ async def stream_events(
data = await event_emitter.get_message(channel)
if data is None:
- await asyncio.sleep(0.1) # avoid tight loop on None
+ await asyncio.sleep(0.1)
continue
- # Handle different types of events
if isinstance(data, dict) and data.get("type") == "enricher_complete":
- # Send enricher completion event
yield json.dumps({"event": "enricher_complete", "data": data})
else:
- # Send regular log event
yield json.dumps({"event": "log", "data": data})
await asyncio.sleep(0.1)
@@ -147,25 +98,16 @@ def delete_scan_logs(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- """Delete all logs for a specific scan"""
- # Check if sketch exists and user has permission
- sketch = db.query(Sketch).filter(Sketch.id == sketch_id).first()
- if not sketch:
- raise HTTPException(
- status_code=404, detail=f"Sketch with id {sketch_id} not found"
- )
-
- check_investigation_permission(
- current_user.id, sketch.investigation_id, actions=["delete"], db=db
- )
-
+ """Delete all logs for a specific sketch."""
+ service = create_log_service(db)
try:
- db.query(Log).filter(Log.sketch_id == sketch_id).delete()
- db.commit()
- return {"message": f"All logs have been deleted successfully"}
- except Exception as e:
- db.rollback()
- raise HTTPException(status_code=500, detail=f"Failed to delete logs: {str(e)}")
+ return service.delete_logs_by_sketch(sketch_id, current_user.id)
+ except NotFoundError as e:
+ raise HTTPException(status_code=404, detail=str(e))
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
+ except DatabaseError as e:
+ raise HTTPException(status_code=500, detail=str(e))
@router.get("/sketch/{sketch_id}/status/stream")
@@ -173,26 +115,21 @@ async def stream_sketch_status(
request: Request,
sketch_id: str,
db: Session = Depends(get_db),
- current_user: Profile = Depends(get_current_user_sse)
+ current_user: Profile = Depends(get_current_user_sse),
):
- """Stream COMPLETED events for a specific sketch (for graph refresh)"""
-
- # Check if sketch exists
- sketch = db.query(Sketch).filter(Sketch.id == sketch_id).first()
- if not sketch:
- raise HTTPException(
- status_code=404, detail=f"Sketch with id {sketch_id} not found"
- )
-
- check_investigation_permission(
- current_user.id, sketch.investigation_id, actions=["read"], db=db
- )
+ """Stream COMPLETED events for a specific sketch (for graph refresh)."""
+ service = create_log_service(db)
+ try:
+ service._get_sketch_with_permission(sketch_id, current_user.id, ["read"])
+ except NotFoundError as e:
+ raise HTTPException(status_code=404, detail=str(e))
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
async def status_generator():
channel = f"{sketch_id}_status"
await event_emitter.subscribe(channel)
try:
- # Initial connection message
yield json.dumps({"event": "connected", "data": "Connected to status stream"})
while True:
@@ -204,7 +141,6 @@ async def stream_sketch_status(
await asyncio.sleep(0.1)
continue
- # Send status event
yield json.dumps({"event": "status", "data": data})
await asyncio.sleep(0.1)
@@ -231,29 +167,21 @@ async def stream_status(
request: Request,
scan_id: str,
db: Session = Depends(get_db),
- current_user: Profile = Depends(get_current_user_sse)
+ current_user: Profile = Depends(get_current_user_sse),
):
- """Stream status updates for a specific scan in real-time"""
-
- # Check if scan exists and user has permission
- scan = db.query(Scan).filter(Scan.id == scan_id).first()
- if not scan:
- raise HTTPException(
- status_code=404, detail=f"Scan with id {scan_id} not found"
- )
-
- # Check investigation permission via sketch
- sketch = db.query(Sketch).filter(Sketch.id == scan.sketch_id).first()
- if sketch:
- check_investigation_permission(
- current_user.id, sketch.investigation_id, actions=["read"], db=db
- )
+ """Stream status updates for a specific scan in real-time."""
+ service = create_log_service(db)
+ try:
+ service.get_scan_with_permission(scan_id, current_user.id)
+ except NotFoundError as e:
+ raise HTTPException(status_code=404, detail=str(e))
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
async def status_generator():
print("[EventEmitter] Start status generator")
await event_emitter.subscribe(f"scan_{scan_id}_status")
try:
- # Initial connection message
yield 'data: {"event": "connected", "data": "Connected to status stream"}\n\n'
while True:
@@ -265,9 +193,7 @@ async def stream_status(
yield f"data: {data}\n\n"
except asyncio.CancelledError:
- print(
- f"[EventEmitter] Client disconnected from status stream for scan_id: {scan_id}"
- )
+ print(f"[EventEmitter] Client disconnected from status stream for scan_id: {scan_id}")
finally:
await event_emitter.unsubscribe(f"scan_{scan_id}_status")
diff --git a/flowsint-api/app/api/routes/flows.py b/flowsint-api/app/api/routes/flows.py
index f2d5c042..e15b06c0 100644
--- a/flowsint-api/app/api/routes/flows.py
+++ b/flowsint-api/app/api/routes/flows.py
@@ -1,13 +1,16 @@
-from datetime import datetime
from typing import Any, Dict, List, Optional
-from uuid import UUID, uuid4
-from fastapi import APIRouter, Depends, HTTPException, Query, status
+from uuid import UUID
-# Auto-discover and register all enrichers
+from fastapi import APIRouter, Depends, HTTPException, Query, status
from flowsint_core.core.celery import celery
from flowsint_core.core.graph import create_graph_service
-from flowsint_core.core.models import CustomType, Flow, Profile, Sketch
+from flowsint_core.core.models import Profile
from flowsint_core.core.postgre_db import get_db
+from flowsint_core.core.services import (
+ NotFoundError,
+ PermissionDeniedError,
+ create_flow_service,
+)
from flowsint_core.core.types import FlowBranch, FlowEdge, FlowNode, FlowStep
from flowsint_core.utils import extract_input_schema_flow
from flowsint_enrichers import ENRICHER_REGISTRY, load_all_enrichers
@@ -31,12 +34,10 @@ from flowsint_types import (
Website,
)
from pydantic import BaseModel
-from sqlalchemy import func
from sqlalchemy.orm import Session
from app.api.deps import get_current_user
from app.api.schemas.flow import FlowCreate, FlowRead, FlowUpdate
-from app.security.permissions import check_investigation_permission
load_all_enrichers()
@@ -71,37 +72,10 @@ def get_flows(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- if not category or category.lower() == "undefined":
- return db.query(Flow).order_by(Flow.last_updated_at.desc()).all()
-
- custom_type = (
- db.query(CustomType)
- .filter(
- CustomType.owner_id == current_user.id,
- CustomType.status == "published",
- func.lower(CustomType.name) == category.lower(),
- )
- .first()
- )
- if custom_type:
- flows = db.query(Flow).order_by(Flow.last_updated_at.desc()).all()
- return [
- {
- **(flow.to_dict() if hasattr(flow, "to_dict") else flow.__dict__),
- "wobblyType": True,
- }
- for flow in flows
- ]
-
- flows = db.query(Flow).order_by(Flow.last_updated_at.desc()).all()
- return [
- flow
- for flow in flows
- if any(cat.lower() == category.lower() for cat in flow.category)
- ]
+ service = create_flow_service(db)
+ return service.get_all_flows(category, current_user.id)
-# Returns the "raw_materials" for the flow editor
@router.get("/raw_materials")
async def get_material_list():
enrichers = ENRICHER_REGISTRY.list_by_categories()
@@ -147,56 +121,46 @@ async def get_material_list():
extract_input_schema_flow(CryptoNFT),
]
- # Put types first, then add all enricher categories
flattened_enrichers = {"types": object_inputs}
flattened_enrichers.update(enricher_categories)
return {"items": flattened_enrichers}
-# Returns the "raw_materials" for the flow editor
@router.get("/input_type/{input_type}")
-async def get_material_list(input_type: str):
+async def get_material_by_input_type(input_type: str):
enrichers = ENRICHER_REGISTRY.list_by_input_type(input_type)
return {"items": enrichers}
-# Create a new flow
@router.post("/create", response_model=FlowRead, status_code=status.HTTP_201_CREATED)
def create_flow(
payload: FlowCreate,
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- new_flow = Flow(
- id=uuid4(),
+ service = create_flow_service(db)
+ return service.create(
name=payload.name,
description=payload.description,
category=payload.category,
flow_schema=payload.flow_schema,
- created_at=datetime.utcnow(),
- last_updated_at=datetime.utcnow(),
)
- db.add(new_flow)
- db.commit()
- db.refresh(new_flow)
- return new_flow
-# Get a flow by ID
@router.get("/{flow_id}", response_model=FlowRead)
def get_flow_by_id(
flow_id: UUID,
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- flow = db.query(Flow).filter(Flow.id == flow_id).first()
- if not flow:
- raise HTTPException(status_code=404, detail="flow not found")
- return flow
+ service = create_flow_service(db)
+ try:
+ return service.get_by_id(flow_id)
+ except NotFoundError:
+ raise HTTPException(status_code=404, detail="Flow not found")
-# Update a flow by ID
@router.put("/{flow_id}", response_model=FlowRead)
def update_flow(
flow_id: UUID,
@@ -204,37 +168,25 @@ def update_flow(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- flow = db.query(Flow).filter(Flow.id == flow_id).first()
- if not flow:
- raise HTTPException(status_code=404, detail="flow not found")
- update_data = payload.model_dump(exclude_unset=True)
- for key, value in update_data.items():
- print(f"only update {key}")
- if key == "category":
- if "SocialAccount" in value:
- value.append("Username")
- setattr(flow, key, value)
-
- flow.last_updated_at = datetime.utcnow()
-
- db.commit()
- db.refresh(flow)
- return flow
+ service = create_flow_service(db)
+ try:
+ return service.update(flow_id, payload.model_dump(exclude_unset=True))
+ except NotFoundError:
+ raise HTTPException(status_code=404, detail="Flow not found")
-# Delete a flow by ID
@router.delete("/{flow_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_flow(
flow_id: UUID,
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- flow = db.query(Flow).filter(Flow.id == flow_id).first()
- if not flow:
- raise HTTPException(status_code=404, detail="flow not found")
- db.delete(flow)
- db.commit()
- return None
+ service = create_flow_service(db)
+ try:
+ service.delete(flow_id)
+ return None
+ except NotFoundError:
+ raise HTTPException(status_code=404, detail="Flow not found")
@router.post("/{flow_id}/launch")
@@ -244,19 +196,10 @@ async def launch_flow(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
+ service = create_flow_service(db)
try:
- flow = db.query(Flow).filter(Flow.id == flow_id).first()
- if flow is None:
- raise HTTPException(status_code=404, detail="flow not found")
-
- # Check investigation permission via sketch
- sketch = db.query(Sketch).filter(Sketch.id == payload.sketch_id).first()
- if not sketch:
- raise HTTPException(status_code=404, detail="Sketch not found")
-
- check_investigation_permission(
- current_user.id, sketch.investigation_id, actions=["update"], db=db
- )
+ flow = service.get_by_id(UUID(flow_id))
+ service.get_sketch_for_launch(payload.sketch_id, current_user.id)
# Retrieve entities from Neo4J by their element IDs
graph_service = create_graph_service(sketch_id=payload.sketch_id)
@@ -266,13 +209,14 @@ async def launch_flow(
nodes = [FlowNode(**node) for node in flow.flow_schema["nodes"]]
edges = [FlowEdge(**edge) for edge in flow.flow_schema["edges"]]
- entities = [entity.model_dump(mode="json", serialize_as_any=True) for entity in entities]
-
+ entities = [
+ entity.model_dump(mode="json", serialize_as_any=True) for entity in entities
+ ]
- # For flow computation, we still need a sample value
- # Use the label from the first node data
sample_value = (
- entities[0].get("nodeLabel", "sample_value") if len(entities) else "sample_value"
+ entities[0].get("nodeLabel", "sample_value")
+ if len(entities)
+ else "sample_value"
)
flow_branches = compute_flow_branches(sample_value, nodes, edges)
serializable_branches = [branch.model_dump() for branch in flow_branches]
@@ -288,8 +232,10 @@ async def launch_flow(
)
return {"id": task.id}
- except HTTPException:
- raise
+ except NotFoundError as e:
+ raise HTTPException(status_code=404, detail=str(e))
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
except Exception as e:
print(e)
raise HTTPException(status_code=500, detail=f"Error launching flow: {str(e)}")
@@ -332,7 +278,6 @@ def compute_flow_branches(
initial_value: Any, nodes: List[FlowNode], edges: List[FlowEdge]
) -> List[FlowBranch]:
"""Computes flow branches based on nodes and edges with proper DFS traversal"""
- # Find input nodes (starting points)
input_nodes = [node for node in nodes if node.data.get("type") == "type"]
if not input_nodes:
@@ -357,34 +302,25 @@ def compute_flow_branches(
node_map = {node.id: node for node in nodes}
branches = []
branch_counter = 0
- # Track enricher outputs across all branches
enricher_outputs = {}
def calculate_path_length(start_node: str, visited: set = None) -> int:
- """Calculate the shortest possible path length from a node to any leaf"""
if visited is None:
visited = set()
-
if start_node in visited:
return float("inf")
-
visited.add(start_node)
out_edges = [edge for edge in edges if edge.source == start_node]
-
if not out_edges:
return 1
-
min_length = float("inf")
for edge in out_edges:
length = calculate_path_length(edge.target, visited.copy())
min_length = min(min_length, length)
-
return 1 + min_length
def get_outgoing_edges(node_id: str) -> List[FlowEdge]:
- """Get outgoing edges sorted by the shortest possible path length"""
out_edges = [edge for edge in edges if edge.source == node_id]
- # Sort edges by the length of the shortest possible path from their target
return sorted(out_edges, key=lambda e: calculate_path_length(e.target))
def create_step(
@@ -420,7 +356,6 @@ def compute_flow_branches(
) -> None:
nonlocal branch_counter
- # Skip if node is already in current path (cycle detection)
if current_node_id in path:
return
@@ -428,7 +363,6 @@ def compute_flow_branches(
if not current_node:
return
- # Process node outputs
is_input_node = current_node.data.get("type") == "type"
if is_input_node:
outputs_array = current_node.data["outputs"].get("properties", [])
@@ -437,18 +371,14 @@ def compute_flow_branches(
)
current_outputs = {first_output_name: initial_value}
else:
- # Check if we already have outputs for this enricher
if current_node_id in enricher_outputs:
current_outputs = enricher_outputs[current_node_id]
else:
current_outputs = process_node_data(current_node, input_data)
- # Store the outputs for future use
enricher_outputs[current_node_id] = current_outputs
- # Extract node parameters
node_params = current_node.data.get("params", {})
- # Create and add current step
current_step = create_step(
current_node_id,
branch_id,
@@ -462,19 +392,15 @@ def compute_flow_branches(
path.append(current_node_id)
branch_visited.add(current_node_id)
- # Get all outgoing edges sorted by path length
out_edges = get_outgoing_edges(current_node_id)
if not out_edges:
- # Leaf node reached, save the branch
branches.append(FlowBranch(id=branch_id, name=branch_name, steps=steps[:]))
else:
- # Process each outgoing edge in order of shortest path
for i, edge in enumerate(out_edges):
- if edge.target in path: # Skip if would create cycle
+ if edge.target in path:
continue
- # Prepare next node's input
output_key = edge.sourceHandle
if not output_key and current_outputs:
output_key = list(current_outputs.keys())[0]
@@ -488,7 +414,6 @@ def compute_flow_branches(
next_input = {edge.targetHandle or "input": output_value}
if i == 0:
- # Continue in same branch (will be shortest path)
explore_branch(
edge.target,
branch_id,
@@ -501,31 +426,26 @@ def compute_flow_branches(
current_outputs,
)
else:
- # Create new branch starting from current node
branch_counter += 1
new_branch_id = f"{branch_id}-{branch_counter}"
new_branch_name = f"{branch_name} (Branch {branch_counter})"
- new_steps = steps[: len(steps)] # Copy steps up to current node
- new_branch_visited = (
- branch_visited.copy()
- ) # Create new visited set for the branch
+ new_steps = steps[: len(steps)]
+ new_branch_visited = branch_visited.copy()
explore_branch(
edge.target,
new_branch_id,
new_branch_name,
depth + 1,
next_input,
- path[:], # Create new path copy for branch
+ path[:],
new_branch_visited,
new_steps,
current_outputs,
)
- # Backtrack: remove current node from path and remove its step
path.pop()
steps.pop()
- # Start exploration from each input node
for index, input_node in enumerate(input_nodes):
branch_id = f"branch-{index}"
branch_name = f"Flow {index + 1}" if len(input_nodes) > 1 else "Main Flow"
@@ -535,76 +455,61 @@ def compute_flow_branches(
branch_name,
0,
{},
- [], # Use list for path to maintain order
- set(), # Use set for visited to check membership
+ [],
+ set(),
[],
None,
)
- # Sort branches by length (number of steps)
branches.sort(key=lambda branch: len(branch.steps))
return branches
def process_node_data(node: FlowNode, inputs: Dict[str, Any]) -> Dict[str, Any]:
- """Traite les données de nœud en fonction du type de nœud et des entrées"""
+ """Process node data based on node type and inputs"""
outputs = {}
output_types = node.data["outputs"].get("properties", [])
for output in output_types:
output_name = output.get("name", "output")
class_name = node.data.get("class_name", "")
- # For simulation purposes, we'll return a placeholder value based on the enricher type
+
if class_name in ["ReverseResolveEnricher", "ResolveEnricher"]:
- # IP/Domain resolution enrichers
outputs[output_name] = (
"192.168.1.1" if "ip" in output_name.lower() else "example.com"
)
elif class_name == "SubdomainEnricher":
- # Subdomain enricher
outputs[output_name] = f"sub.{inputs.get('input', 'example.com')}"
-
elif class_name == "WhoisEnricher":
- # WHOIS enricher
outputs[output_name] = {
"domain": inputs.get("input", "example.com"),
"registrar": "Example Registrar",
"creation_date": "2020-01-01",
}
-
elif class_name == "IpToInfosEnricher":
- # Geolocation enricher
outputs[output_name] = {
"country": "France",
"city": "Paris",
"coordinates": {"lat": 48.8566, "lon": 2.3522},
}
-
elif class_name == "MaigretEnricher":
- # Social media enricher
outputs[output_name] = {
"username": inputs.get("input", "user123"),
"platforms": ["twitter", "github", "linkedin"],
}
-
elif class_name == "HoleheEnricher":
- # Email verification enricher
outputs[output_name] = {
"email": inputs.get("input", "[email protected]"),
"exists": True,
"platforms": ["gmail", "github"],
}
-
elif class_name == "SireneEnricher":
- # Organization enricher
outputs[output_name] = {
"name": inputs.get("input", "Example Corp"),
"siret": "12345678901234",
"address": "1 Example Street",
}
-
else:
- # For unknown enrichers, pass through the input
outputs[output_name] = inputs.get("input") or f"flowed_{output_name}"
return outputs
diff --git a/flowsint-api/app/api/routes/investigations.py b/flowsint-api/app/api/routes/investigations.py
index a01a95e8..36f79e62 100644
--- a/flowsint-api/app/api/routes/investigations.py
+++ b/flowsint-api/app/api/routes/investigations.py
@@ -1,18 +1,16 @@
-from uuid import UUID, uuid4
-from app.security.permissions import check_investigation_permission
+from uuid import UUID
from fastapi import APIRouter, HTTPException, Depends, status
from typing import List
-from datetime import datetime
+from sqlalchemy.orm import Session
+
from flowsint_core.core.types import Role
-from sqlalchemy import or_
-from sqlalchemy.orm import Session, selectinload
from flowsint_core.core.postgre_db import get_db
-from flowsint_core.core.models import (
- Analysis,
- Investigation,
- InvestigationUserRole,
- Profile,
- Sketch,
+from flowsint_core.core.models import Profile
+from flowsint_core.core.services import (
+ create_investigation_service,
+ NotFoundError,
+ PermissionDeniedError,
+ DatabaseError,
)
from app.api.deps import get_current_user
from app.api.schemas.investigation import (
@@ -21,61 +19,23 @@ from app.api.schemas.investigation import (
InvestigationUpdate,
)
from app.api.schemas.sketch import SketchRead
-from flowsint_core.core.graph import create_graph_service
router = APIRouter()
-def get_user_accessible_investigations(
- user_id: str, db: Session, allowed_roles: list[Role] = None
-) -> list[Investigation]:
- """
- Returns all investigations accessible to user depending on its roles
- """
- query = db.query(Investigation).join(
- InvestigationUserRole,
- InvestigationUserRole.investigation_id == Investigation.id,
- )
-
- query = query.filter(InvestigationUserRole.user_id == user_id)
-
- if allowed_roles:
- # ARRAY(Role) contains any of allowed_roles
- conditions = [InvestigationUserRole.roles.any(role) for role in allowed_roles]
- # Inclut également le propriétaire de l’investigation
- query = query.filter(or_(*conditions, Investigation.owner_id == user_id))
-
- return (
- query.options(
- selectinload(Investigation.sketches),
- selectinload(Investigation.analyses),
- selectinload(Investigation.owner),
- )
- .distinct()
- .all()
- )
-
-
-# Get the list of all investigations
@router.get("", response_model=List[InvestigationRead])
def get_investigations(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- """
- Récupère toutes les investigations accessibles à l'utilisateur
- selon ses rôles (OWNER, EDITOR, VIEWER).
- """
- allowed_roles_for_read = [Role.OWNER, Role.EDITOR, Role.VIEWER]
-
- investigations = get_user_accessible_investigations(
- user_id=current_user.id, db=db, allowed_roles=allowed_roles_for_read
+ """Get all investigations accessible to the user based on their roles."""
+ service = create_investigation_service(db)
+ allowed_roles = [Role.OWNER, Role.EDITOR, Role.VIEWER]
+ return service.get_accessible_investigations(
+ user_id=current_user.id, allowed_roles=allowed_roles
)
- return investigations
-
-# Create a new investigation
@router.post(
"/create", response_model=InvestigationRead, status_code=status.HTTP_201_CREATED
)
@@ -84,73 +44,46 @@ def create_investigation(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- new_investigation = Investigation(
- id=uuid4(),
+ service = create_investigation_service(db)
+ return service.create(
name=payload.name,
- description=payload.description or payload.name,
+ description=payload.description,
owner_id=current_user.id,
- status="active",
)
- db.add(new_investigation)
-
- new_roles = InvestigationUserRole(
- id=uuid4(),
- user_id=current_user.id,
- investigation_id=new_investigation.id,
- roles=[Role.OWNER],
- )
- db.add(new_roles)
-
- db.commit()
- db.refresh(new_investigation)
- db.refresh(new_roles)
-
- return new_investigation
-# Get a investigation by ID
@router.get("/{investigation_id}", response_model=InvestigationRead)
def get_investigation_by_id(
investigation_id: UUID,
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- check_investigation_permission(current_user.id, investigation_id, actions=["read"], db=db)
- investigation = (
- db.query(Investigation)
- .options(
- selectinload(Investigation.sketches),
- selectinload(Investigation.analyses),
- selectinload(Investigation.owner),
- )
- .filter(Investigation.id == investigation_id)
- .filter(Investigation.owner_id == current_user.id)
- .first()
- )
- if not investigation:
+ service = create_investigation_service(db)
+ try:
+ return service.get_by_id(investigation_id, current_user.id)
+ except NotFoundError:
raise HTTPException(status_code=404, detail="Investigation not found")
- return investigation
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
-# Get a investigation by ID
@router.get("/{investigation_id}/sketches", response_model=List[SketchRead])
def get_sketches_by_investigation(
investigation_id: UUID,
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- check_investigation_permission(current_user.id, investigation_id, actions=["read"], db=db)
- sketches = (
- db.query(Sketch).filter(Sketch.investigation_id == investigation_id).all()
- )
- if not sketches:
+ service = create_investigation_service(db)
+ try:
+ return service.get_sketches(investigation_id, current_user.id)
+ except NotFoundError:
raise HTTPException(
status_code=404, detail="No sketches found for this investigation"
)
- return sketches
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
-# Update a investigation by ID
@router.put("/{investigation_id}", response_model=InvestigationRead)
def update_investigation(
investigation_id: UUID,
@@ -158,69 +91,34 @@ def update_investigation(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- check_investigation_permission(current_user.id, investigation_id, actions=["write"], db=db)
- investigation = (
- db.query(Investigation).filter(Investigation.id == investigation_id).first()
- )
- if not investigation:
+ service = create_investigation_service(db)
+ try:
+ return service.update(
+ investigation_id=investigation_id,
+ user_id=current_user.id,
+ name=payload.name,
+ description=payload.description,
+ status=payload.status,
+ )
+ except NotFoundError:
raise HTTPException(status_code=404, detail="Investigation not found")
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
- investigation.name = payload.name
- investigation.description = payload.description
- investigation.status = payload.status
- investigation.last_updated_at = datetime.utcnow()
-
- db.commit()
- db.refresh(investigation)
- return investigation
-
-# Delete a investigation by ID
@router.delete("/{investigation_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_investigation(
investigation_id: UUID,
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- check_investigation_permission(current_user.id, investigation_id, actions=["delete"], db=db)
- investigation = (
- db.query(Investigation)
- .filter(
- Investigation.id == investigation_id,
- Investigation.owner_id == current_user.id,
- )
- .first()
- )
- if not investigation:
+ service = create_investigation_service(db)
+ try:
+ service.delete(investigation_id, current_user.id)
+ return None
+ except NotFoundError:
raise HTTPException(status_code=404, detail="Investigation not found")
-
- # Get all sketches related to this investigation
- sketches = (
- db.query(Sketch).filter(Sketch.investigation_id == investigation_id).all()
- )
- analyses = (
- db.query(Analysis).filter(Sketch.investigation_id == investigation_id).all()
- )
-
- # Delete all nodes and relationships for each sketch in Neo4j using GraphService
- for sketch in sketches:
- try:
- graph_service = create_graph_service(
- sketch_id=str(sketch.id),
- enable_batching=False,
- )
- graph_service.delete_all_sketch_nodes()
- except Exception as e:
- print(f"Neo4j cleanup error for sketch {sketch.id}: {e}")
- raise HTTPException(status_code=500, detail="Failed to clean up graph data")
-
- # Delete all sketches from PostgreSQL
- for sketch in sketches:
- db.delete(sketch)
- for analysis in analyses:
- db.delete(analysis)
-
- # Finally delete the investigation
- db.delete(investigation)
- db.commit()
- return None
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
+ except DatabaseError:
+ raise HTTPException(status_code=500, detail="Failed to clean up graph data")
diff --git a/flowsint-api/app/api/routes/keys.py b/flowsint-api/app/api/routes/keys.py
index 861dea08..256a8989 100644
--- a/flowsint-api/app/api/routes/keys.py
+++ b/flowsint-api/app/api/routes/keys.py
@@ -1,23 +1,28 @@
-from uuid import UUID, uuid4
+from uuid import UUID
from fastapi import APIRouter, HTTPException, Depends, status
from typing import List
-from flowsint_core.core.vault import Vault
from sqlalchemy.orm import Session
+
+from flowsint_core.core.services import (
+ create_key_service,
+ NotFoundError,
+ DatabaseError,
+)
from flowsint_core.core.postgre_db import get_db
-from flowsint_core.core.models import Profile, Key
+from flowsint_core.core.models import Profile
from app.api.deps import get_current_user
from app.api.schemas.key import KeyRead, KeyCreate
router = APIRouter()
-# Get the list of all keys for a user, just the public method for viewing
@router.get("", response_model=List[KeyRead])
def get_keys(
db: Session = Depends(get_db), current_user: Profile = Depends(get_current_user)
):
- keys = db.query(Key).filter(Key.owner_id == current_user.id).all()
- response_data = [
+ service = create_key_service(db)
+ keys = service.get_keys_for_user(current_user.id)
+ return [
KeyRead(
id=key.id,
owner_id=key.owner_id,
@@ -26,61 +31,57 @@ def get_keys(
)
for key in keys
]
- return response_data
-# Get a key by ID, just the public method for viewing
@router.get("/{id}", response_model=KeyRead)
def get_key_by_id(
id: UUID,
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- key = db.query(Key).filter(Key.id == id, Key.owner_id == current_user.id).first()
- if not key:
+ service = create_key_service(db)
+ try:
+ key = service.get_key_by_id(id, current_user.id)
+ return KeyRead(
+ id=key.id,
+ owner_id=key.owner_id,
+ name=key.name,
+ created_at=key.created_at,
+ )
+ except NotFoundError:
raise HTTPException(status_code=404, detail="Key not found")
- # Create a response with obfuscated key
- response_data = KeyRead(
- id=key.id,
- owner_id=key.owner_id,
- name=key.name,
- created_at=key.created_at,
- )
- return response_data
-
-# Create a new key
@router.post("/create", response_model=KeyRead, status_code=status.HTTP_201_CREATED)
def create_key(
payload: KeyCreate,
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
+ service = create_key_service(db)
try:
- vault = Vault(db=db, owner_id=current_user.id)
- key = vault.set_secret(vault_ref=payload.name, plain_key=payload.key)
- if not key:
- raise HTTPException(
- status_code=500, detail="An error occured creating the key."
- )
- return key
- except Exception as e:
+ key = service.create_key(payload.name, payload.key, current_user.id)
+ return KeyRead(
+ id=key.id,
+ owner_id=key.owner_id,
+ name=key.name,
+ created_at=key.created_at,
+ )
+ except DatabaseError:
raise HTTPException(
- status_code=500, detail="An error occured creating the key."
+ status_code=500, detail="An error occurred creating the key."
)
-# Delete a key by ID
@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_key(
id: UUID,
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- key = db.query(Key).filter(Key.id == id, Key.owner_id == current_user.id).first()
- if not key:
+ service = create_key_service(db)
+ try:
+ service.delete_key(id, current_user.id)
+ return None
+ except NotFoundError:
raise HTTPException(status_code=404, detail="Key not found")
- db.delete(key)
- db.commit()
- return None
diff --git a/flowsint-api/app/api/routes/scan.py b/flowsint-api/app/api/routes/scan.py
index ee331ecb..43df0d25 100644
--- a/flowsint-api/app/api/routes/scan.py
+++ b/flowsint-api/app/api/routes/scan.py
@@ -1,84 +1,56 @@
from uuid import UUID
from fastapi import APIRouter, HTTPException, Depends, status
from typing import List
-from sqlalchemy import or_
from sqlalchemy.orm import Session
+
from flowsint_core.core.postgre_db import get_db
-from flowsint_core.core.models import Scan, Profile, Sketch, InvestigationUserRole
-from flowsint_core.core.types import Role
+from flowsint_core.core.models import Profile
+from flowsint_core.core.services import (
+ create_scan_service,
+ NotFoundError,
+ PermissionDeniedError,
+)
from app.api.deps import get_current_user
from app.api.schemas.scan import ScanRead
-from app.security.permissions import check_investigation_permission
router = APIRouter()
-# Get the list of all scans
- "",
- response_model=List[ScanRead],
-)
[email protected]("", response_model=List[ScanRead])
def get_scans(
db: Session = Depends(get_db), current_user: Profile = Depends(get_current_user)
):
- # Get all scans from sketches in investigations where user has at least VIEWER role
- allowed_roles_for_read = [Role.OWNER, Role.EDITOR, Role.VIEWER]
-
- query = db.query(Scan).join(
- Sketch, Sketch.id == Scan.sketch_id
- ).join(
- InvestigationUserRole,
- InvestigationUserRole.investigation_id == Sketch.investigation_id,
- )
-
- query = query.filter(InvestigationUserRole.user_id == current_user.id)
-
- # Filter by allowed roles
- conditions = [InvestigationUserRole.roles.any(role) for role in allowed_roles_for_read]
- query = query.filter(or_(*conditions))
+ """Get all scans accessible to the current user."""
+ service = create_scan_service(db)
+ return service.get_accessible_scans(current_user.id)
- return query.distinct().all()
-
-# Get a scan by ID
@router.get("/{id}", response_model=ScanRead)
def get_scan_by_id(
id: UUID,
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- scan = db.query(Scan).filter(Scan.id == id).first()
- if not scan:
+ service = create_scan_service(db)
+ try:
+ return service.get_by_id(id, current_user.id)
+ except NotFoundError:
raise HTTPException(status_code=404, detail="Scan not found")
-
- # Check investigation permission via sketch
- sketch = db.query(Sketch).filter(Sketch.id == scan.sketch_id).first()
- if sketch:
- check_investigation_permission(
- current_user.id, sketch.investigation_id, actions=["read"], db=db
- )
-
- return scan
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
-# Delete a scan by ID
@router.delete("/{id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_scan_by_id(
id: UUID,
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- scan = db.query(Scan).filter(Scan.id == id).first()
- if not scan:
+ service = create_scan_service(db)
+ try:
+ service.delete(id, current_user.id)
+ return None
+ except NotFoundError:
raise HTTPException(status_code=404, detail="Scan not found")
-
- # Check investigation permission via sketch
- sketch = db.query(Sketch).filter(Sketch.id == scan.sketch_id).first()
- if sketch:
- check_investigation_permission(
- current_user.id, sketch.investigation_id, actions=["delete"], db=db
- )
-
- db.delete(scan)
- db.commit()
- return None
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
diff --git a/flowsint-api/app/api/routes/sketches.py b/flowsint-api/app/api/routes/sketches.py
index a987304d..8d88f449 100644
--- a/flowsint-api/app/api/routes/sketches.py
+++ b/flowsint-api/app/api/routes/sketches.py
@@ -11,23 +11,29 @@ from fastapi import (
UploadFile,
status,
)
-from flowsint_core.core.graph import create_graph_service, GraphNode
-from flowsint_core.core.models import Profile, Sketch
+from flowsint_core.core.graph import GraphNode
+from flowsint_core.core.models import Profile
from flowsint_core.core.postgre_db import get_db
+from flowsint_core.core.services import (
+ create_sketch_service,
+ NotFoundError,
+ PermissionDeniedError,
+ ValidationError,
+ DatabaseError,
+)
from flowsint_core.imports import (
EntityMapping,
ImportService,
create_import_service,
FileParseResult,
)
-from flowsint_core.utils import flatten
+from flowsint_core.core.graph import create_graph_service
from pydantic import BaseModel, Field
from sqlalchemy.orm import Session
from app.api.deps import get_current_user
from app.api.schemas.sketch import SketchCreate, SketchRead, SketchUpdate
from app.api.sketch_utils import update_sketch_timestamp
-from app.security.permissions import check_investigation_permission
router = APIRouter()
@@ -48,7 +54,6 @@ class RelationshipDeleteInput(BaseModel):
relationshipIds: List[str]
-
class NodeEditInput(BaseModel):
nodeId: str
updates: Dict[str, Any]
@@ -68,32 +73,68 @@ class NodeMergeInput(BaseModel):
)
+class RelationInput(BaseModel):
+ source: str
+ target: str
+ type: Literal["one-way", "two-way"]
+ label: str = "RELATED_TO"
+
+
+class NodePosition(BaseModel):
+ nodeId: str
+ x: float
+ y: float
+
+
+class UpdatePositionsInput(BaseModel):
+ positions: List[NodePosition]
+
+
+class EntityMappingInput(BaseModel):
+ """Pydantic model for parsing entity mapping input from frontend."""
+ id: str
+ entity_type: str
+ include: bool = True
+ nodeLabel: str
+ node_id: Optional[str] = None
+ data: Dict[str, Any]
+
+
+class ImportExecuteResponse(BaseModel):
+ """Response model for import execution."""
+ status: str
+ nodes_created: int
+ nodes_skipped: int
+ errors: List[str]
+
+
@router.post("/create", response_model=SketchRead, status_code=status.HTTP_201_CREATED)
def create_sketch(
data: SketchCreate,
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- sketch_data = data.model_dump()
- investigation_id = sketch_data.get("investigation_id")
- if not investigation_id:
- raise HTTPException(status_code=404, detail="Investigation not found")
- check_investigation_permission(
- current_user.id, investigation_id, actions=["create"], db=db
- )
- sketch_data["owner_id"] = current_user.id
- sketch = Sketch(**sketch_data)
- db.add(sketch)
- db.commit()
- db.refresh(sketch)
- return sketch
+ service = create_sketch_service(db)
+ try:
+ sketch_data = data.model_dump()
+ return service.create(
+ title=sketch_data.get("title"),
+ description=sketch_data.get("description"),
+ investigation_id=sketch_data.get("investigation_id"),
+ owner_id=current_user.id,
+ )
+ except ValidationError as e:
+ raise HTTPException(status_code=404, detail=str(e))
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
@router.get("", response_model=List[SketchRead])
def list_sketches(
db: Session = Depends(get_db), current_user: Profile = Depends(get_current_user)
):
- return db.query(Sketch).filter(Sketch.owner_id == current_user.id).all()
+ service = create_sketch_service(db)
+ return service.list_sketches(current_user.id)
@router.get("/{sketch_id}")
@@ -102,13 +143,13 @@ def get_sketch_by_id(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- sketch = db.query(Sketch).filter(Sketch.id == sketch_id).first()
- if not sketch:
+ service = create_sketch_service(db)
+ try:
+ return service.get_by_id(sketch_id, current_user.id)
+ except NotFoundError:
raise HTTPException(status_code=404, detail="Sketch not found")
- check_investigation_permission(
- current_user.id, sketch.investigation_id, actions=["read"], db=db
- )
- return sketch
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
@router.put("/{id}", response_model=SketchRead)
@@ -118,17 +159,13 @@ def update_sketch(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- sketch = db.query(Sketch).filter(Sketch.id == id).first()
- if not sketch:
+ service = create_sketch_service(db)
+ try:
+ return service.update(id, current_user.id, payload.model_dump(exclude_unset=True))
+ except NotFoundError:
raise HTTPException(status_code=404, detail="Sketch not found")
- check_investigation_permission(
- current_user.id, sketch.investigation_id, actions=["update"], db=db
- )
- for key, value in payload.model_dump(exclude_unset=True).items():
- setattr(sketch, key, value)
- db.commit()
- db.refresh(sketch)
- return sketch
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
@router.delete("/{id}", status_code=204)
@@ -137,27 +174,16 @@ def delete_sketch(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- sketch = db.query(Sketch).filter(Sketch.id == id).first()
- if not sketch:
- raise HTTPException(status_code=404, detail="Sketch not found")
- check_investigation_permission(
- current_user.id, sketch.investigation_id, actions=["delete"], db=db
- )
-
- # Delete all nodes and relationships in Neo4j first using GraphService
+ service = create_sketch_service(db)
try:
- graph_service = create_graph_service(
- sketch_id=str(id), enable_batching=False
- )
- graph_service.delete_all_sketch_nodes()
- except Exception as e:
- print(f"Neo4j cleanup error: {e}")
+ service.delete(id, current_user.id)
+ except NotFoundError:
+ raise HTTPException(status_code=404, detail="Sketch not found")
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
+ except DatabaseError:
raise HTTPException(status_code=500, detail="Failed to clean up graph data")
- # Then delete the sketch from PostgreSQL
- db.delete(sketch)
- db.commit()
-
@router.get("/{sketch_id}/graph")
async def get_sketch_nodes(
@@ -166,38 +192,14 @@ async def get_sketch_nodes(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- """
- Get the nodes and edges for a sketch.
- Args:
- id: The ID of the sketch
- format: Optional format parameter. If "inline", returns inline relationships
- db: The database session
- current_user: The current user
- Returns:
- A dictionary containing the nodes and relationships for the sketch
- nds: []
- rls: []
- Or if format=inline: List of inline relationship strings
- """
- sketch = db.query(Sketch).filter(Sketch.id == sketch_id).first()
- if not sketch:
+ """Get the nodes and edges for a sketch."""
+ service = create_sketch_service(db)
+ try:
+ return service.get_graph(UUID(sketch_id), current_user.id, format)
+ except NotFoundError:
raise HTTPException(status_code=404, detail="Graph not found")
- check_investigation_permission(
- current_user.id, sketch.investigation_id, actions=["read"], db=db
- )
- # Get all nodes and relationships using GraphService
- graph_service = create_graph_service(
- sketch_id=sketch_id, enable_batching=False
- )
- graph_data = graph_service.get_sketch_graph()
- if format == "inline":
- from flowsint_core.utils import get_inline_relationships
-
- return get_inline_relationships(graph_data.nodes, graph_data.edges)
-
- graph = graph_data.model_dump(mode="json", serialize_as_any=True)
-
- return {"nds": graph["nodes"], "rls": graph["edges"]}
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
@router.post("/{sketch_id}/nodes/add")
@@ -209,38 +211,17 @@ def add_node(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- sketch = db.query(Sketch).filter(Sketch.id == sketch_id).first()
- if not sketch:
- raise HTTPException(status_code=404, detail="Sketch not found")
- check_investigation_permission(
- current_user.id, sketch.investigation_id, actions=["update"], db=db
- )
+ service = create_sketch_service(db)
try:
- graph_service = create_graph_service(
- sketch_id=sketch_id,
- enable_batching=False,
- )
- node_id = graph_service.create_node(node)
- except Exception as e:
- print(e)
- raise HTTPException(status_code=500, detail=f"Database error: {str(e)}")
-
- if not node_id:
+ return service.add_node(UUID(sketch_id), current_user.id, node)
+ except NotFoundError:
+ raise HTTPException(status_code=404, detail="Sketch not found")
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
+ except ValidationError:
raise HTTPException(status_code=400, detail="Node creation failed")
-
- node.id = node_id
-
- return {
- "status": "node added",
- "node": node,
- }
-
-
-class RelationInput(BaseModel):
- source: str
- target: str
- type: Literal["one-way", "two-way"]
- label: str = "RELATED_TO"
+ except DatabaseError as e:
+ raise HTTPException(status_code=500, detail=str(e))
@router.post("/{sketch_id}/relations/add")
@@ -252,35 +233,19 @@ def add_edge(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- sketch = db.query(Sketch).filter(Sketch.id == sketch_id).first()
- if not sketch:
- raise HTTPException(status_code=404, detail="Sketch not found")
- check_investigation_permission(
- current_user.id, sketch.investigation_id, actions=["update"], db=db
- )
-
- # Create relationship using GraphService
+ service = create_sketch_service(db)
try:
- graph_service = create_graph_service(
- sketch_id=sketch_id,
- enable_batching=False,
+ return service.add_relationship(
+ UUID(sketch_id), current_user.id, relation.source, relation.target, relation.label
)
- result = graph_service.create_relationship_by_element_id(
- from_element_id=relation.source,
- to_element_id=relation.target,
- rel_label=relation.label,
- )
- except Exception as e:
- print(f"Edge creation error: {e}")
- raise HTTPException(status_code=500, detail="Failed to create edge")
-
- if not result:
+ except NotFoundError:
+ raise HTTPException(status_code=404, detail="Sketch not found")
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
+ except ValidationError:
raise HTTPException(status_code=400, detail="Edge creation failed")
-
- return {
- "status": "edge added",
- "edge": result,
- }
+ except DatabaseError:
+ raise HTTPException(status_code=500, detail="Failed to create edge")
@router.put("/{sketch_id}/nodes/edit")
@@ -292,48 +257,18 @@ def edit_node(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- # First verify the sketch exists and belongs to the user
- sketch = db.query(Sketch).filter(Sketch.id == sketch_id).first()
- if not sketch:
- raise HTTPException(status_code=404, detail="Sketch not found")
- check_investigation_permission(
- current_user.id, sketch.investigation_id, actions=["update"], db=db
- )
-
- updates = node_edit.updates
+ service = create_sketch_service(db)
try:
- graph_service = create_graph_service(
- sketch_id=sketch_id,
- enable_batching=False,
- )
- updated_element_id = graph_service.update_node(
- element_id=node_edit.nodeId,
- updates=updates,
+ return service.update_node(
+ UUID(sketch_id), current_user.id, node_edit.nodeId, node_edit.updates
)
- except Exception as e:
- print(f"Node update error: {e}")
+ except NotFoundError as e:
+ raise HTTPException(status_code=404, detail=str(e))
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
+ except DatabaseError:
raise HTTPException(status_code=500, detail="Failed to update node")
- if not updated_element_id:
- raise HTTPException(status_code=404, detail="Node not found or not accessible")
-
- return {
- "status": "node updated",
- "node": {
- "id": updated_element_id,
- },
- }
-
-
-class NodePosition(BaseModel):
- nodeId: str
- x: float
- y: float
-
-
-class UpdatePositionsInput(BaseModel):
- positions: List[NodePosition]
-
@router.put("/{sketch_id}/nodes/positions")
@update_sketch_timestamp
@@ -344,40 +279,18 @@ def update_node_positions(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- """
- Update positions (x, y) for multiple nodes in batch.
- This is used to persist node positions after drag operations in the graph viewer.
- """
- # Verify the sketch exists and user has access
- sketch = db.query(Sketch).filter(Sketch.id == sketch_id).first()
- if not sketch:
- raise HTTPException(status_code=404, detail="Sketch not found")
- check_investigation_permission(
- current_user.id, sketch.investigation_id, actions=["update"], db=db
- )
-
- if not data.positions:
- return {"status": "no positions to update", "count": 0}
-
- # Convert Pydantic models to dicts for GraphService
- positions = [pos.model_dump() for pos in data.positions]
-
- # Update positions using GraphService
+ """Update positions (x, y) for multiple nodes in batch."""
+ service = create_sketch_service(db)
try:
- graph_service = create_graph_service(
- sketch_id=sketch_id,
- enable_batching=False,
- )
- updated_count = graph_service.update_nodes_positions(positions=positions)
- except Exception as e:
- print(f"Position update error: {e}")
+ positions = [pos.model_dump() for pos in data.positions]
+ return service.update_node_positions(UUID(sketch_id), current_user.id, positions)
+ except NotFoundError:
+ raise HTTPException(status_code=404, detail="Sketch not found")
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
+ except DatabaseError:
raise HTTPException(status_code=500, detail="Failed to update node positions")
- return {
- "status": "positions updated",
- "count": updated_count,
- }
-
@router.delete("/{sketch_id}/nodes")
@update_sketch_timestamp
@@ -388,27 +301,16 @@ def delete_nodes(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- # First verify the sketch exists and belongs to the user
- sketch = db.query(Sketch).filter(Sketch.id == sketch_id).first()
- if not sketch:
- raise HTTPException(status_code=404, detail="Sketch not found")
- check_investigation_permission(
- current_user.id, sketch.investigation_id, actions=["update"], db=db
- )
-
- # Delete nodes and their relationships using GraphService
+ service = create_sketch_service(db)
try:
- graph_service = create_graph_service(
- sketch_id=sketch_id,
- enable_batching=False,
- )
- deleted_count = graph_service.delete_nodes(nodes.nodeIds)
- except Exception as e:
- print(f"Node deletion error: {e}")
+ return service.delete_nodes(UUID(sketch_id), current_user.id, nodes.nodeIds)
+ except NotFoundError:
+ raise HTTPException(status_code=404, detail="Sketch not found")
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
+ except DatabaseError:
raise HTTPException(status_code=500, detail="Failed to delete nodes")
- return {"status": "nodes deleted", "count": deleted_count}
-
@router.delete("/{sketch_id}/relationships")
@update_sketch_timestamp
@@ -419,29 +321,18 @@ def delete_relationships(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- # First verify the sketch exists and belongs to the user
- sketch = db.query(Sketch).filter(Sketch.id == sketch_id).first()
- if not sketch:
- raise HTTPException(status_code=404, detail="Sketch not found")
- check_investigation_permission(
- current_user.id, sketch.investigation_id, actions=["update"], db=db
- )
-
- # Delete relationships using GraphService
+ service = create_sketch_service(db)
try:
- graph_service = create_graph_service(
- sketch_id=sketch_id,
- enable_batching=False,
- )
- deleted_count = graph_service.delete_relationships(
- relationships.relationshipIds
+ return service.delete_relationships(
+ UUID(sketch_id), current_user.id, relationships.relationshipIds
)
- except Exception as e:
- print(f"Relationship deletion error: {e}")
+ except NotFoundError:
+ raise HTTPException(status_code=404, detail="Sketch not found")
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
+ except DatabaseError:
raise HTTPException(status_code=500, detail="Failed to delete relationships")
- return {"status": "relationships deleted", "count": deleted_count}
-
@router.put("/{sketch_id}/relationships/edit")
@update_sketch_timestamp
@@ -452,42 +343,21 @@ def edit_relationship(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- # First verify the sketch exists and belongs to the user
- sketch = db.query(Sketch).filter(Sketch.id == sketch_id).first()
- if not sketch:
- raise HTTPException(status_code=404, detail="Sketch not found")
- check_investigation_permission(
- current_user.id, sketch.investigation_id, actions=["update"], db=db
- )
-
- # Update edge using GraphService
+ service = create_sketch_service(db)
try:
- graph_service = create_graph_service(
- sketch_id=sketch_id,
- enable_batching=False,
+ return service.update_relationship(
+ UUID(sketch_id),
+ current_user.id,
+ relationship_edit.relationshipId,
+ relationship_edit.data,
)
- result = graph_service.update_relationship(
- element_id=relationship_edit.relationshipId,
- properties=relationship_edit.data,
- )
- except Exception as e:
- print(f"Relationship update error: {e}")
+ except NotFoundError as e:
+ raise HTTPException(status_code=404, detail=str(e))
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
+ except DatabaseError:
raise HTTPException(status_code=500, detail="Failed to update relationship")
- if not result:
- raise HTTPException(
- status_code=404, detail="Relationship not found or not accessible"
- )
-
- return {
- "status": "relationship updated",
- "relationship": {
- "id": result["id"],
- "label": result["type"],
- "data": result["data"],
- },
- }
-
@router.post("/{sketch_id}/nodes/merge")
@update_sketch_timestamp
@@ -499,49 +369,20 @@ def merge_nodes(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- sketch = db.query(Sketch).filter(Sketch.id == sketch_id).first()
- if not sketch:
- raise HTTPException(status_code=404, detail="Sketch not found")
- check_investigation_permission(
- current_user.id, sketch.investigation_id, actions=["update"], db=db
- )
-
- if not oldNodes or len(oldNodes) == 0:
- raise HTTPException(status_code=400, detail="oldNodes cannot be empty")
-
- node_data = newNode.data.model_dump() if newNode.data else {}
- node_type = node_data.get("type", "Node")
-
- properties = {
- "type": node_type.lower(),
- "label": node_data.get("label", "Merged Node"),
- }
-
- flattened_data = flatten(node_data)
- properties.update(flattened_data)
-
+ service = create_sketch_service(db)
try:
- graph_service = create_graph_service(
- sketch_id=sketch_id,
- enable_batching=False,
- )
- new_node_element_id = graph_service.merge_nodes(
- old_node_ids=oldNodes,
- new_node_data=properties,
- new_node_id=newNode.id,
+ node_data = newNode.data.model_dump() if newNode.data else {}
+ return service.merge_nodes(
+ UUID(sketch_id), current_user.id, oldNodes, newNode.id, node_data
)
- except Exception as e:
- print(f"Node merge error: {e}")
- raise HTTPException(status_code=500, detail=f"Failed to merge nodes: {str(e)}")
-
- if not new_node_element_id:
- raise HTTPException(status_code=500, detail="Failed to merge nodes")
-
- return {
- "status": "nodes merged",
- "count": len(oldNodes),
- "new_node_id": new_node_element_id,
- }
+ except NotFoundError:
+ raise HTTPException(status_code=404, detail="Sketch not found")
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
+ except ValidationError as e:
+ raise HTTPException(status_code=400, detail=str(e))
+ except DatabaseError as e:
+ raise HTTPException(status_code=500, detail=str(e))
@router.get("/{sketch_id}/nodes/{node_id}")
@@ -551,26 +392,16 @@ def get_related_nodes(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- sketch = db.query(Sketch).filter(Sketch.id == sketch_id).first()
- if not sketch:
- raise HTTPException(status_code=404, detail="Sketch not found")
- check_investigation_permission(
- current_user.id, sketch.investigation_id, actions=["read"], db=db
- )
-
+ service = create_sketch_service(db)
try:
- graph_service = create_graph_service(sketch_id=sketch_id)
- result = graph_service.get_neighbors(node_id)
-
- except Exception as e:
- print(e)
+ return service.get_neighbors(UUID(sketch_id), current_user.id, node_id)
+ except NotFoundError as e:
+ raise HTTPException(status_code=404, detail=str(e))
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
+ except DatabaseError:
raise HTTPException(status_code=500, detail="Failed to retrieve related nodes")
- if not result.nodes:
- raise HTTPException(status_code=404, detail="Node not found")
-
- return {"nds": result.nodes, "rls": result.edges}
-
@router.post("/{sketch_id}/import/analyze", response_model=FileParseResult)
async def analyze_import_file(
@@ -579,32 +410,26 @@ async def analyze_import_file(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- """
- Analyze an uploaded TXT or JSON file for import.
- Each line represents one entity. Detects entity types and provides preview.
- """
- # Verify sketch exists and user has access
- sketch = db.query(Sketch).filter(Sketch.id == sketch_id).first()
- if not sketch:
+ """Analyze an uploaded TXT or JSON file for import."""
+ service = create_sketch_service(db)
+ try:
+ service.get_by_id(UUID(sketch_id), current_user.id)
+ except NotFoundError:
raise HTTPException(status_code=404, detail="Sketch not found")
- check_investigation_permission(
- current_user.id, sketch.investigation_id, actions=["read"], db=db
- )
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
- # Validate file extension
if not file.filename or not file.filename.lower().endswith((".txt", ".json")):
raise HTTPException(
status_code=400,
detail="Only .txt and .json files are supported. Please upload a correct format.",
)
- # Read file content
try:
content = await file.read()
except Exception as e:
raise HTTPException(status_code=400, detail=f"Failed to read file: {str(e)}")
- # Analyze file using ImportService
try:
result = ImportService.analyze_file(
file_content=content,
@@ -618,26 +443,6 @@ async def analyze_import_file(
return result
-class EntityMappingInput(BaseModel):
- """Pydantic model for parsing entity mapping input from frontend."""
-
- id: str
- entity_type: str
- include: bool = True
- nodeLabel: str
- node_id: Optional[str] = None
- data: Dict[str, Any]
-
-
-class ImportExecuteResponse(BaseModel):
- """Response model for import execution."""
-
- status: str
- nodes_created: int
- nodes_skipped: int
- errors: List[str]
-
-
@router.post("/{sketch_id}/import/execute", response_model=ImportExecuteResponse)
@update_sketch_timestamp
async def execute_import(
@@ -647,26 +452,21 @@ async def execute_import(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- """
- Execute the import of entities into the sketch.
- Uses the entity mappings provided by the frontend (no file re-parsing needed).
- """
+ """Execute the import of entities into the sketch."""
import json
- # Verify sketch exists and user has access
- sketch = db.query(Sketch).filter(Sketch.id == sketch_id).first()
- if not sketch:
+ service = create_sketch_service(db)
+ try:
+ service.get_by_id(UUID(sketch_id), current_user.id)
+ except NotFoundError:
raise HTTPException(status_code=404, detail="Sketch not found")
- check_investigation_permission(
- current_user.id, sketch.investigation_id, actions=["update"], db=db
- )
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
- # Parse entity mappings JSON
try:
mappings = json.loads(entity_mappings_json)
nodes = mappings.get("nodes", [])
edges = mappings.get("edges", [])
- print(nodes)
entity_mapping_inputs = [EntityMappingInput(**m) for m in nodes]
except json.JSONDecodeError:
raise HTTPException(status_code=400, detail="Invalid entity_mappings JSON")
@@ -675,7 +475,6 @@ async def execute_import(
status_code=400, detail=f"Failed to parse entity_mappings: {str(e)}"
)
- # Convert Pydantic inputs to service dataclasses
entity_mappings = [
EntityMapping(
id=m.id,
@@ -688,10 +487,7 @@ async def execute_import(
for m in entity_mapping_inputs
]
- # Execute import using ImportService
- graph_service = create_graph_service(
- sketch_id=sketch_id, enable_batching=False
- )
+ graph_service = create_graph_service(sketch_id=sketch_id, enable_batching=False)
import_service = create_import_service(graph_service)
try:
@@ -717,39 +513,13 @@ async def export_sketch(
db: Session = Depends(get_db),
current_user: Profile = Depends(get_current_user),
):
- """
- Export the sketch in the specified format.
- Args:
- id: The ID of the sketch
- format: Export format - "json" (only format for now)
- db: The database session
- current_user: The current user
- Returns:
- The sketch data in the requested format
- """
- sketch = db.query(Sketch).filter(Sketch.id == id).first()
- if not sketch:
+ """Export the sketch in the specified format."""
+ service = create_sketch_service(db)
+ try:
+ return service.export_sketch(UUID(id), current_user.id, format)
+ except NotFoundError:
raise HTTPException(status_code=404, detail="Sketch not found")
- check_investigation_permission(
- current_user.id, sketch.investigation_id, actions=["read"], db=db
- )
-
- # Get all nodes and relationships using GraphService
- graph_service = create_graph_service(
- sketch_id=id, enable_batching=False
- )
- graph_data = graph_service.get_sketch_graph()
-
- if format == "json":
- return {
- "sketch": {
- "id": str(sketch.id),
- "title": sketch.title,
- "description": sketch.description,
- },
- "nodes": [node.model_dump(mode="json") for node in graph_data.nodes],
- "edges": [edge.model_dump(mode="json") for edge in graph_data.edges],
- }
-
- else:
- raise HTTPException(status_code=400, detail=f"Unsupported format: {format}")
+ except PermissionDeniedError:
+ raise HTTPException(status_code=403, detail="Forbidden")
+ except ValidationError as e:
+ raise HTTPException(status_code=400, detail=str(e))
diff --git a/flowsint-api/app/api/routes/types.py b/flowsint-api/app/api/routes/types.py
index 931bd4b1..558668e2 100644
--- a/flowsint-api/app/api/routes/types.py
+++ b/flowsint-api/app/api/routes/types.py
@@ -1,318 +1,18 @@
-from typing import Any, Dict, Optional, Type
-from uuid import uuid4
-
from fastapi import APIRouter, Depends
-from flowsint_core.core.models import CustomType, Profile
-from flowsint_core.core.postgre_db import get_db
-from flowsint_types.registry import get_type
-from pydantic import BaseModel, TypeAdapter
from sqlalchemy.orm import Session
+from flowsint_core.core.models import Profile
+from flowsint_core.core.postgre_db import get_db
+from flowsint_core.core.services import create_type_registry_service
from app.api.deps import get_current_user
router = APIRouter()
-# Helper function to get a type by name from the registry
-def get_type_from_registry(type_name: str) -> Optional[Type[BaseModel]]:
- """Get a type from the TYPE_REGISTRY by name."""
- return get_type(type_name, case_sensitive=True)
-
-
-# Returns the "types" for the sketches
@router.get("/")
async def get_types_list(
db: Session = Depends(get_db), current_user: Profile = Depends(get_current_user)
):
- # Define categories with type names to look up in TYPE_REGISTRY
- # Format: (type_name, label_key, optional_icon)
- category_definitions = [
- {
- "id": uuid4(),
- "type": "global",
- "key": "global_category",
- "icon": "phrase",
- "label": "Global",
- "fields": [],
- "children": [
- ("Phrase", "text", None),
- ("Location", "address", None),
- ],
- },
- {
- "id": uuid4(),
- "type": "person",
- "key": "person_category",
- "icon": "individual",
- "label": "Identities & Entities",
- "fields": [],
- "children": [
- ("Individual", "full_name", None),
- ("Username", "value", "username"),
- ("Organization", "name", None),
- ],
- },
- {
- "id": uuid4(),
- "type": "organization",
- "key": "organization_category",
- "icon": "organization",
- "label": "Organization",
- "fields": [],
- "children": [
- ("Organization", "name", None),
- ],
- },
- {
- "id": uuid4(),
- "type": "contact_category",
- "key": "contact",
- "icon": "phone",
- "label": "Communication & Contact",
- "fields": [],
- "children": [
- ("Phone", "number", None),
- ("Email", "email", None),
- ("Username", "value", None),
- ("SocialAccount", "username", "socialaccount"),
- ("Message", "content", "message"),
- ],
- },
- {
- "id": uuid4(),
- "type": "network_category",
- "key": "network",
- "icon": "domain",
- "label": "Network",
- "fields": [],
- "children": [
- ("ASN", "number", None),
- ("CIDR", "network", None),
- ("Domain", "domain", None),
- ("Website", "url", None),
- ("Ip", "address", None),
- ("Port", "number", None),
- ("DNSRecord", "name", "dns"),
- ("SSLCertificate", "subject", "ssl"),
- ("WebTracker", "name", "webtracker"),
- ],
- },
- {
- "id": uuid4(),
- "type": "security_category",
- "key": "security",
- "icon": "credential",
- "label": "Security & Access",
- "fields": [],
- "children": [
- ("Credential", "username", "credential"),
- ("Session", "session_id", "session"),
- ("Device", "device_id", "device"),
- ("Malware", "name", "malware"),
- ("Weapon", "name", "weapon"),
- ],
- },
- {
- "id": uuid4(),
- "type": "files_category",
- "key": "files",
- "icon": "file",
- "label": "Files & Documents",
- "fields": [],
- "children": [
- ("Document", "title", "document"),
- ("File", "filename", "file"),
- ],
- },
- {
- "id": uuid4(),
- "type": "financial_category",
- "key": "financial",
- "icon": "creditcard",
- "label": "Financial Data",
- "fields": [],
- "children": [
- ("BankAccount", "account_number", "creditcard"),
- ("CreditCard", "card_number", "creditcard"),
- ],
- },
- {
- "id": uuid4(),
- "type": "leak_category",
- "key": "leaks",
- "icon": "breach",
- "label": "Leaks",
- "fields": [],
- "children": [
- ("Leak", "name", "breach"),
- ],
- },
- {
- "id": uuid4(),
- "type": "crypto_category",
- "key": "crypto",
- "icon": "cryptowallet",
- "label": "Crypto",
- "fields": [],
- "children": [
- ("CryptoWallet", "address", "cryptowallet"),
- ("CryptoWalletTransaction", "hash", "cryptowallet"),
- ("CryptoNFT", "name", "cryptowallet"),
- ],
- },
- ]
-
- # Build the types list by looking up each type in TYPE_REGISTRY
- types = []
- for category in category_definitions:
- category_copy = category.copy()
- children_schemas = []
-
- for child_def in category["children"]:
- type_name, label_key, icon = child_def
- model = get_type_from_registry(type_name)
-
- if model:
- children_schemas.append(
- extract_input_schema(model, label_key=label_key, icon=icon)
- )
- else:
- # Log warning but continue - type might not be available
- print(f"Warning: Type {type_name} not found in TYPE_REGISTRY")
-
- category_copy["children"] = children_schemas
- types.append(category_copy)
-
- # Add custom types
- custom_types = (
- db.query(CustomType)
- .filter(
- CustomType.owner_id == current_user.id,
- CustomType.status == "published", # Only show published custom types
- )
- .all()
- )
-
- if custom_types:
- custom_types_children = []
- for custom_type in custom_types:
- # Extract the label_key from the schema (use first required field or first property)
- schema = custom_type.schema
- properties = schema.get("properties", {})
- required = schema.get("required", [])
-
- # Try to use the first required field, or the first property
- label_key = (
- required[0]
- if required
- else list(properties.keys())[0]
- if properties
- else "value"
- )
-
- custom_types_children.append(
- {
- "id": custom_type.id,
- "type": custom_type.name,
- "key": custom_type.name.lower(),
- "label_key": label_key,
- "icon": "custom",
- "label": custom_type.name,
- "description": custom_type.description or "",
- "fields": [
- {
- "name": prop,
- "label": info.get("title", prop),
- "description": info.get("description", ""),
- "type": "text",
- "required": prop in required,
- }
- for prop, info in properties.items()
- ],
- "custom": True, # Mark as custom type
- }
- )
-
- types.append(
- {
- "id": uuid4(),
- "type": "custom_types_category",
- "key": "custom_types",
- "icon": "custom",
- "label": "Custom types",
- "fields": [],
- "children": custom_types_children,
- }
- )
-
- return types
-
-
-def extract_input_schema(
- model: Type[BaseModel], label_key: str, icon: Optional[str] = None
-) -> Dict[str, Any]:
- adapter = TypeAdapter(model)
- schema = adapter.json_schema()
- # Use the main schema properties, not the $defs
- type_name = model.__name__
- details = schema
- return {
- "id": uuid4(),
- "type": type_name,
- "key": type_name.lower(),
- "label_key": label_key,
- "icon": icon or type_name.lower(),
- "label": type_name,
- "description": details.get("description", ""),
- "fields": [
- resolve_field(prop, details=info, schema=schema)
- for prop, info in details.get("properties", {}).items()
- # exclude nodeLabel from properties to fill
- if prop != "nodeLabel"
- ],
- }
-
-
-def resolve_field(prop: str, details: dict, schema: dict = None) -> Dict:
- """_summary_
- The fields can sometimes contain nested complex objects, like:
- - Organization having Individual[] as dirigeants, so we want to skip those.
- Args:
- details (dict): _description_
- schema_context (dict, optional): _description_. Defaults to None.
-
- Returns:
- str: _description_
- """
- field = {
- "name": prop,
- "label": details.get("title", prop),
- "description": details.get("description", ""),
- "type": "text",
- }
- if has_enum(details):
- field["type"] = "select"
- field["options"] = [
- {"label": label, "value": label} for label in get_enum_values(details)
- ]
- field["required"] = is_required(details)
-
- return field
-
-
-def has_enum(schema: dict) -> bool:
- any_of = schema.get("anyOf", [])
- return any(isinstance(entry, dict) and "enum" in entry for entry in any_of)
-
-
-def is_required(schema: dict) -> bool:
- any_of = schema.get("anyOf", [])
- return not any(entry == {"type": "null"} for entry in any_of)
-
-
-def get_enum_values(schema: dict) -> list:
- enum_values = []
- for entry in schema.get("anyOf", []):
- if isinstance(entry, dict) and "enum" in entry:
- enum_values.extend(entry["enum"])
- return enum_values
+ """Get the complete types list for sketches."""
+ service = create_type_registry_service(db)
+ return service.get_types_list(current_user.id)